aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/EntryRestController.php
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2017-06-07 15:40:59 +0200
committerGitHub <noreply@github.com>2017-06-07 15:40:59 +0200
commitc0d756f67d1cc0fc6832d404a09729f9219f0595 (patch)
tree0a6ca6138d6de94529202dccebd0bcd9c2fdb21b /src/Wallabag/ApiBundle/Controller/EntryRestController.php
parent4e4a5b534ff241f25c35fad24c9c79eb12f4adde (diff)
parentdb0c48af361eb20fbfcac625869de6f26112d3f7 (diff)
downloadwallabag-c0d756f67d1cc0fc6832d404a09729f9219f0595.tar.gz
wallabag-c0d756f67d1cc0fc6832d404a09729f9219f0595.tar.zst
wallabag-c0d756f67d1cc0fc6832d404a09729f9219f0595.zip
Merge pull request #3181 from wallabag/api-content-patch
Add ability to patch an entry with more fields
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php150
1 files changed, 75 insertions, 75 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 93c8157e..09b73ccb 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -299,65 +299,18 @@ class EntryRestController extends WallabagRestController
299 $this->validateAuthentication(); 299 $this->validateAuthentication();
300 300
301 $url = $request->request->get('url'); 301 $url = $request->request->get('url');
302 $title = $request->request->get('title');
303 $tags = $request->request->get('tags', []);
304 $isArchived = $request->request->get('archive');
305 $isStarred = $request->request->get('starred');
306 $content = $request->request->get('content');
307 $language = $request->request->get('language');
308 $picture = $request->request->get('preview_picture');
309 $publishedAt = $request->request->get('published_at');
310 $authors = $request->request->get('authors', '');
311 302
312 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); 303 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
304 $url,
305 $this->getUser()->getId()
306 );
313 307
314 if (false === $entry) { 308 if (false === $entry) {
315 $entry = new Entry($this->getUser()); 309 $entry = new Entry($this->getUser());
316 }
317
318 try {
319 $this->get('wallabag_core.content_proxy')->updateEntry(
320 $entry,
321 $url,
322 [
323 'title' => $title,
324 'html' => $content,
325 'url' => $url,
326 'language' => $language,
327 'date' => $publishedAt,
328 // faking the preview picture
329 'open_graph' => [
330 'og_image' => $picture,
331 ],
332 'authors' => explode(',', $authors),
333 ]
334 );
335 } catch (\Exception $e) {
336 $this->get('logger')->error('Error while saving an entry', [
337 'exception' => $e,
338 'entry' => $entry,
339 ]);
340 $entry->setUrl($url); 310 $entry->setUrl($url);
341 } 311 }
342 312
343 if (!empty($tags)) { 313 $this->upsertEntry($entry, $request);
344 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
345 }
346
347 if (!is_null($isStarred)) {
348 $entry->setStarred((bool) $isStarred);
349 }
350
351 if (!is_null($isArchived)) {
352 $entry->setArchived((bool) $isArchived);
353 }
354
355 $em = $this->getDoctrine()->getManager();
356 $em->persist($entry);
357 $em->flush();
358
359 // entry saved, dispatch event about it!
360 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
361 314
362 return $this->sendResponse($entry); 315 return $this->sendResponse($entry);
363 } 316 }
@@ -374,6 +327,11 @@ class EntryRestController extends WallabagRestController
374 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, 327 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
375 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, 328 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
376 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, 329 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
330 * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"},
331 * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"},
332 * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"},
333 * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"},
334 * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"},
377 * } 335 * }
378 * ) 336 * )
379 * 337 *
@@ -384,29 +342,7 @@ class EntryRestController extends WallabagRestController
384 $this->validateAuthentication(); 342 $this->validateAuthentication();
385 $this->validateUserAccess($entry->getUser()->getId()); 343 $this->validateUserAccess($entry->getUser()->getId());
386 344
387 $title = $request->request->get('title'); 345 $this->upsertEntry($entry, $request, true);
388 $isArchived = $request->request->get('archive');
389 $isStarred = $request->request->get('starred');
390
391 if (!is_null($title)) {
392 $entry->setTitle($title);
393 }
394
395 if (!is_null($isArchived)) {
396 $entry->setArchived((bool) $isArchived);
397 }
398
399 if (!is_null($isStarred)) {
400 $entry->setStarred((bool) $isStarred);
401 }
402
403 $tags = $request->request->get('tags', '');
404 if (!empty($tags)) {
405 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
406 }
407
408 $em = $this->getDoctrine()->getManager();
409 $em->flush();
410 346
411 return $this->sendResponse($entry); 347 return $this->sendResponse($entry);
412 } 348 }
@@ -673,4 +609,68 @@ class EntryRestController extends WallabagRestController
673 609
674 return (new JsonResponse())->setJson($json); 610 return (new JsonResponse())->setJson($json);
675 } 611 }
612
613 /**
614 * Update or Insert a new entry.
615 *
616 * @param Entry $entry
617 * @param Request $request
618 * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting)
619 */
620 private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false)
621 {
622 $title = $request->request->get('title');
623 $tags = $request->request->get('tags', []);
624 $isArchived = $request->request->get('archive');
625 $isStarred = $request->request->get('starred');
626 $content = $request->request->get('content');
627 $language = $request->request->get('language');
628 $picture = $request->request->get('preview_picture');
629 $publishedAt = $request->request->get('published_at');
630 $authors = $request->request->get('authors', '');
631
632 try {
633 $this->get('wallabag_core.content_proxy')->updateEntry(
634 $entry,
635 $entry->getUrl(),
636 [
637 'title' => !empty($title) ? $title : $entry->getTitle(),
638 'html' => !empty($content) ? $content : $entry->getContent(),
639 'url' => $entry->getUrl(),
640 'language' => !empty($language) ? $language : $entry->getLanguage(),
641 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(),
642 // faking the open graph preview picture
643 'open_graph' => [
644 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(),
645 ],
646 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(),
647 ],
648 $disableContentUpdate
649 );
650 } catch (\Exception $e) {
651 $this->get('logger')->error('Error while saving an entry', [
652 'exception' => $e,
653 'entry' => $entry,
654 ]);
655 }
656
657 if (!is_null($isArchived)) {
658 $entry->setArchived((bool) $isArchived);
659 }
660
661 if (!is_null($isStarred)) {
662 $entry->setStarred((bool) $isStarred);
663 }
664
665 if (!empty($tags)) {
666 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
667 }
668
669 $em = $this->getDoctrine()->getManager();
670 $em->persist($entry);
671 $em->flush();
672
673 // entry saved, dispatch event about it!
674 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
675 }
676} 676}