diff options
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 160 |
1 files changed, 95 insertions, 65 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 0b4e74a0..c09fdaeb 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -4,17 +4,17 @@ namespace Wallabag\ApiBundle\Controller; | |||
4 | 4 | ||
5 | use Hateoas\Configuration\Route; | 5 | use Hateoas\Configuration\Route; |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | 6 | use Hateoas\Representation\Factory\PagerfantaFactory; |
7 | use JMS\Serializer\SerializationContext; | ||
8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | 7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
9 | use Symfony\Component\HttpFoundation\JsonResponse; | 8 | use Symfony\Component\HttpFoundation\JsonResponse; |
10 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
11 | use Symfony\Component\HttpFoundation\Response; | 10 | use Symfony\Component\HttpFoundation\Response; |
11 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
12 | use Symfony\Component\HttpKernel\Exception\HttpException; | 12 | use Symfony\Component\HttpKernel\Exception\HttpException; |
13 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
14 | use Wallabag\CoreBundle\Entity\Entry; | 13 | use Wallabag\CoreBundle\Entity\Entry; |
15 | use Wallabag\CoreBundle\Entity\Tag; | 14 | use Wallabag\CoreBundle\Entity\Tag; |
16 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | 15 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; |
17 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | 16 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
17 | use Wallabag\CoreBundle\Helper\UrlHasher; | ||
18 | 18 | ||
19 | class EntryRestController extends WallabagRestController | 19 | class EntryRestController extends WallabagRestController |
20 | { | 20 | { |
@@ -28,8 +28,10 @@ class EntryRestController extends WallabagRestController | |||
28 | * @ApiDoc( | 28 | * @ApiDoc( |
29 | * parameters={ | 29 | * parameters={ |
30 | * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, | 30 | * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, |
31 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, | 31 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="DEPRECATED, use hashed_url instead"}, |
32 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} | 32 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="DEPRECATED, use hashed_urls instead"}, |
33 | * {"name"="hashed_url", "dataType"="string", "required"=false, "format"="A hashed url", "description"="Hashed url using SHA1 to check if it exists"}, | ||
34 | * {"name"="hashed_urls", "dataType"="string", "required"=false, "format"="An array of hashed urls (?hashed_urls[]=xxx...&hashed_urls[]=xxx...)", "description"="An array of hashed urls using SHA1 to check if they exist"} | ||
33 | * } | 35 | * } |
34 | * ) | 36 | * ) |
35 | * | 37 | * |
@@ -38,38 +40,49 @@ class EntryRestController extends WallabagRestController | |||
38 | public function getEntriesExistsAction(Request $request) | 40 | public function getEntriesExistsAction(Request $request) |
39 | { | 41 | { |
40 | $this->validateAuthentication(); | 42 | $this->validateAuthentication(); |
43 | $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); | ||
41 | 44 | ||
42 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | 45 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); |
43 | $urls = $request->query->get('urls', []); | ||
44 | |||
45 | // handle multiple urls first | ||
46 | if (!empty($urls)) { | ||
47 | $results = []; | ||
48 | foreach ($urls as $url) { | ||
49 | $res = $this->getDoctrine() | ||
50 | ->getRepository('WallabagCoreBundle:Entry') | ||
51 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | ||
52 | 46 | ||
53 | $results[$url] = $this->returnExistInformation($res, $returnId); | 47 | $hashedUrls = $request->query->get('hashed_urls', []); |
54 | } | 48 | $hashedUrl = $request->query->get('hashed_url', ''); |
55 | 49 | if (!empty($hashedUrl)) { | |
56 | return $this->sendResponse($results); | 50 | $hashedUrls[] = $hashedUrl; |
57 | } | 51 | } |
58 | 52 | ||
59 | // let's see if it is a simple url? | 53 | $urls = $request->query->get('urls', []); |
60 | $url = $request->query->get('url', ''); | 54 | $url = $request->query->get('url', ''); |
55 | if (!empty($url)) { | ||
56 | $urls[] = $url; | ||
57 | } | ||
58 | |||
59 | $urlHashMap = []; | ||
60 | foreach ($urls as $urlToHash) { | ||
61 | $urlHash = UrlHasher::hashUrl($urlToHash); | ||
62 | $hashedUrls[] = $urlHash; | ||
63 | $urlHashMap[$urlHash] = $urlToHash; | ||
64 | } | ||
61 | 65 | ||
62 | if (empty($url)) { | 66 | if (empty($hashedUrls)) { |
63 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | 67 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); |
64 | } | 68 | } |
65 | 69 | ||
66 | $res = $this->getDoctrine() | 70 | $results = []; |
67 | ->getRepository('WallabagCoreBundle:Entry') | 71 | foreach ($hashedUrls as $hashedUrlToSearch) { |
68 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | 72 | $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId()); |
73 | |||
74 | $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId); | ||
75 | } | ||
76 | |||
77 | $results = $this->replaceUrlHashes($results, $urlHashMap); | ||
69 | 78 | ||
70 | $exists = $this->returnExistInformation($res, $returnId); | 79 | if (!empty($url) || !empty($hashedUrl)) { |
80 | $hu = array_keys($results)[0]; | ||
71 | 81 | ||
72 | return $this->sendResponse(['exists' => $exists]); | 82 | return $this->sendResponse(['exists' => $results[$hu]]); |
83 | } | ||
84 | |||
85 | return $this->sendResponse($results); | ||
73 | } | 86 | } |
74 | 87 | ||
75 | /** | 88 | /** |
@@ -79,13 +92,14 @@ class EntryRestController extends WallabagRestController | |||
79 | * parameters={ | 92 | * parameters={ |
80 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, | 93 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, |
81 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, | 94 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, |
82 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, | 95 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated' or 'archived', default 'created'", "description"="sort entries by date."}, |
83 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | 96 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, |
84 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | 97 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, |
85 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | 98 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, |
86 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | 99 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, |
87 | * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, | 100 | * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, |
88 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"}, | 101 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"}, |
102 | * {"name"="detail", "dataType"="string", "required"=false, "format"="metadata or full, metadata by default", "description"="include content field if 'full'. 'full' by default for backward compatibility."}, | ||
89 | * } | 103 | * } |
90 | * ) | 104 | * ) |
91 | * | 105 | * |
@@ -98,24 +112,30 @@ class EntryRestController extends WallabagRestController | |||
98 | $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); | 112 | $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); |
99 | $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); | 113 | $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); |
100 | $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public'); | 114 | $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public'); |
101 | $sort = $request->query->get('sort', 'created'); | 115 | $sort = strtolower($request->query->get('sort', 'created')); |
102 | $order = $request->query->get('order', 'desc'); | 116 | $order = strtolower($request->query->get('order', 'desc')); |
103 | $page = (int) $request->query->get('page', 1); | 117 | $page = (int) $request->query->get('page', 1); |
104 | $perPage = (int) $request->query->get('perPage', 30); | 118 | $perPage = (int) $request->query->get('perPage', 30); |
105 | $tags = \is_array($request->query->get('tags')) ? '' : (string) $request->query->get('tags', ''); | 119 | $tags = \is_array($request->query->get('tags')) ? '' : (string) $request->query->get('tags', ''); |
106 | $since = $request->query->get('since', 0); | 120 | $since = $request->query->get('since', 0); |
121 | $detail = strtolower($request->query->get('detail', 'full')); | ||
107 | 122 | ||
108 | /** @var \Pagerfanta\Pagerfanta $pager */ | 123 | try { |
109 | $pager = $this->get('wallabag_core.entry_repository')->findEntries( | 124 | /** @var \Pagerfanta\Pagerfanta $pager */ |
110 | $this->getUser()->getId(), | 125 | $pager = $this->get('wallabag_core.entry_repository')->findEntries( |
111 | $isArchived, | 126 | $this->getUser()->getId(), |
112 | $isStarred, | 127 | $isArchived, |
113 | $isPublic, | 128 | $isStarred, |
114 | $sort, | 129 | $isPublic, |
115 | $order, | 130 | $sort, |
116 | $since, | 131 | $order, |
117 | $tags | 132 | $since, |
118 | ); | 133 | $tags, |
134 | $detail | ||
135 | ); | ||
136 | } catch (\Exception $e) { | ||
137 | throw new BadRequestHttpException($e->getMessage()); | ||
138 | } | ||
119 | 139 | ||
120 | $pager->setMaxPerPage($perPage); | 140 | $pager->setMaxPerPage($perPage); |
121 | $pager->setCurrentPage($page); | 141 | $pager->setCurrentPage($page); |
@@ -135,8 +155,9 @@ class EntryRestController extends WallabagRestController | |||
135 | 'perPage' => $perPage, | 155 | 'perPage' => $perPage, |
136 | 'tags' => $tags, | 156 | 'tags' => $tags, |
137 | 'since' => $since, | 157 | 'since' => $since, |
158 | 'detail' => $detail, | ||
138 | ], | 159 | ], |
139 | UrlGeneratorInterface::ABSOLUTE_URL | 160 | true |
140 | ) | 161 | ) |
141 | ); | 162 | ); |
142 | 163 | ||
@@ -344,9 +365,7 @@ class EntryRestController extends WallabagRestController | |||
344 | 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), | 365 | 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), |
345 | 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), | 366 | 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), |
346 | // faking the open graph preview picture | 367 | // faking the open graph preview picture |
347 | 'open_graph' => [ | 368 | 'image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), |
348 | 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), | ||
349 | ], | ||
350 | 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), | 369 | 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), |
351 | ] | 370 | ] |
352 | ); | 371 | ); |
@@ -358,7 +377,7 @@ class EntryRestController extends WallabagRestController | |||
358 | } | 377 | } |
359 | 378 | ||
360 | if (null !== $data['isArchived']) { | 379 | if (null !== $data['isArchived']) { |
361 | $entry->setArchived((bool) $data['isArchived']); | 380 | $entry->updateArchived((bool) $data['isArchived']); |
362 | } | 381 | } |
363 | 382 | ||
364 | if (null !== $data['isStarred']) { | 383 | if (null !== $data['isStarred']) { |
@@ -474,7 +493,7 @@ class EntryRestController extends WallabagRestController | |||
474 | } | 493 | } |
475 | 494 | ||
476 | if (null !== $data['isArchived']) { | 495 | if (null !== $data['isArchived']) { |
477 | $entry->setArchived((bool) $data['isArchived']); | 496 | $entry->updateArchived((bool) $data['isArchived']); |
478 | } | 497 | } |
479 | 498 | ||
480 | if (null !== $data['isStarred']) { | 499 | if (null !== $data['isStarred']) { |
@@ -545,7 +564,7 @@ class EntryRestController extends WallabagRestController | |||
545 | } | 564 | } |
546 | 565 | ||
547 | // if refreshing entry failed, don't save it | 566 | // if refreshing entry failed, don't save it |
548 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | 567 | if ($this->container->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { |
549 | return new JsonResponse([], 304); | 568 | return new JsonResponse([], 304); |
550 | } | 569 | } |
551 | 570 | ||
@@ -565,18 +584,31 @@ class EntryRestController extends WallabagRestController | |||
565 | * @ApiDoc( | 584 | * @ApiDoc( |
566 | * requirements={ | 585 | * requirements={ |
567 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | 586 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} |
587 | * }, | ||
588 | * parameters={ | ||
589 | * {"name"="expect", "dataType"="string", "required"=false, "format"="id or entry", "description"="Only returns the id instead of the deleted entry's full entity if 'id' is specified. Default to entry"}, | ||
568 | * } | 590 | * } |
569 | * ) | 591 | * ) |
570 | * | 592 | * |
571 | * @return JsonResponse | 593 | * @return JsonResponse |
572 | */ | 594 | */ |
573 | public function deleteEntriesAction(Entry $entry) | 595 | public function deleteEntriesAction(Entry $entry, Request $request) |
574 | { | 596 | { |
597 | $expect = $request->query->get('expect', 'entry'); | ||
598 | if (!\in_array($expect, ['id', 'entry'], true)) { | ||
599 | throw new BadRequestHttpException(sprintf("expect: 'id' or 'entry' expected, %s given", $expect)); | ||
600 | } | ||
575 | $this->validateAuthentication(); | 601 | $this->validateAuthentication(); |
576 | $this->validateUserAccess($entry->getUser()->getId()); | 602 | $this->validateUserAccess($entry->getUser()->getId()); |
577 | 603 | ||
578 | // We copy $entry to keep id in returned object | 604 | $response = $this->sendResponse([ |
579 | $e = $entry; | 605 | 'id' => $entry->getId(), |
606 | ]); | ||
607 | // We clone $entry to keep id in returned object | ||
608 | if ('entry' === $expect) { | ||
609 | $e = clone $entry; | ||
610 | $response = $this->sendResponse($e); | ||
611 | } | ||
580 | 612 | ||
581 | $em = $this->getDoctrine()->getManager(); | 613 | $em = $this->getDoctrine()->getManager(); |
582 | $em->remove($entry); | 614 | $em->remove($entry); |
@@ -585,7 +617,7 @@ class EntryRestController extends WallabagRestController | |||
585 | // entry deleted, dispatch event about it! | 617 | // entry deleted, dispatch event about it! |
586 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | 618 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); |
587 | 619 | ||
588 | return $this->sendResponse($e); | 620 | return $response; |
589 | } | 621 | } |
590 | 622 | ||
591 | /** | 623 | /** |
@@ -769,29 +801,27 @@ class EntryRestController extends WallabagRestController | |||
769 | } | 801 | } |
770 | 802 | ||
771 | /** | 803 | /** |
772 | * Shortcut to send data serialized in json. | 804 | * Replace the hashedUrl keys in $results with the unhashed URL from the |
773 | * | 805 | * request, as recorded in $urlHashMap. |
774 | * @param mixed $data | ||
775 | * | ||
776 | * @return JsonResponse | ||
777 | */ | 806 | */ |
778 | private function sendResponse($data) | 807 | private function replaceUrlHashes(array $results, array $urlHashMap) |
779 | { | 808 | { |
780 | // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 | 809 | $newResults = []; |
781 | $context = new SerializationContext(); | 810 | foreach ($results as $hash => $res) { |
782 | $context->setSerializeNull(true); | 811 | if (isset($urlHashMap[$hash])) { |
783 | 812 | $newResults[$urlHashMap[$hash]] = $res; | |
784 | $json = $this->get('jms_serializer')->serialize($data, 'json', $context); | 813 | } else { |
814 | $newResults[$hash] = $res; | ||
815 | } | ||
816 | } | ||
785 | 817 | ||
786 | return (new JsonResponse())->setJson($json); | 818 | return $newResults; |
787 | } | 819 | } |
788 | 820 | ||
789 | /** | 821 | /** |
790 | * Retrieve value from the request. | 822 | * Retrieve value from the request. |
791 | * Used for POST & PATCH on a an entry. | 823 | * Used for POST & PATCH on a an entry. |
792 | * | 824 | * |
793 | * @param Request $request | ||
794 | * | ||
795 | * @return array | 825 | * @return array |
796 | */ | 826 | */ |
797 | private function retrieveValueFromRequest(Request $request) | 827 | private function retrieveValueFromRequest(Request $request) |
@@ -814,8 +844,8 @@ class EntryRestController extends WallabagRestController | |||
814 | /** | 844 | /** |
815 | * Return information about the entry if it exist and depending on the id or not. | 845 | * Return information about the entry if it exist and depending on the id or not. |
816 | * | 846 | * |
817 | * @param Entry|null $entry | 847 | * @param Entry|bool|null $entry |
818 | * @param bool $returnId | 848 | * @param bool $returnId |
819 | * | 849 | * |
820 | * @return bool|int | 850 | * @return bool|int |
821 | */ | 851 | */ |