aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle/Controller/EntryRestController.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php106
1 files changed, 60 insertions, 46 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 33b75665..aaacdcdc 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -4,18 +4,17 @@ namespace Wallabag\ApiBundle\Controller;
4 4
5use Hateoas\Configuration\Route; 5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory; 6use Hateoas\Representation\Factory\PagerfantaFactory;
7use JMS\Serializer\SerializationContext;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc; 7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Symfony\Component\HttpFoundation\JsonResponse; 8use Symfony\Component\HttpFoundation\JsonResponse;
10use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
11use Symfony\Component\HttpFoundation\Response; 10use Symfony\Component\HttpFoundation\Response;
12use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; 11use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
13use Symfony\Component\HttpKernel\Exception\HttpException; 12use Symfony\Component\HttpKernel\Exception\HttpException;
14use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15use Wallabag\CoreBundle\Entity\Entry; 13use Wallabag\CoreBundle\Entity\Entry;
16use Wallabag\CoreBundle\Entity\Tag; 14use Wallabag\CoreBundle\Entity\Tag;
17use Wallabag\CoreBundle\Event\EntryDeletedEvent; 15use Wallabag\CoreBundle\Event\EntryDeletedEvent;
18use Wallabag\CoreBundle\Event\EntrySavedEvent; 16use Wallabag\CoreBundle\Event\EntrySavedEvent;
17use Wallabag\CoreBundle\Helper\UrlHasher;
19 18
20class EntryRestController extends WallabagRestController 19class EntryRestController extends WallabagRestController
21{ 20{
@@ -29,8 +28,10 @@ class EntryRestController extends WallabagRestController
29 * @ApiDoc( 28 * @ApiDoc(
30 * parameters={ 29 * parameters={
31 * {"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"},
32 * {"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"},
33 * {"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"}
34 * } 35 * }
35 * ) 36 * )
36 * 37 *
@@ -39,38 +40,49 @@ class EntryRestController extends WallabagRestController
39 public function getEntriesExistsAction(Request $request) 40 public function getEntriesExistsAction(Request $request)
40 { 41 {
41 $this->validateAuthentication(); 42 $this->validateAuthentication();
43 $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry');
42 44
43 $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');
44 $urls = $request->query->get('urls', []);
45
46 // handle multiple urls first
47 if (!empty($urls)) {
48 $results = [];
49 foreach ($urls as $url) {
50 $res = $this->getDoctrine()
51 ->getRepository('WallabagCoreBundle:Entry')
52 ->findByUrlAndUserId($url, $this->getUser()->getId());
53
54 $results[$url] = $this->returnExistInformation($res, $returnId);
55 }
56 46
57 return $this->sendResponse($results); 47 $hashedUrls = $request->query->get('hashed_urls', []);
48 $hashedUrl = $request->query->get('hashed_url', '');
49 if (!empty($hashedUrl)) {
50 $hashedUrls[] = $hashedUrl;
58 } 51 }
59 52
60 // let's see if it is a simple url? 53 $urls = $request->query->get('urls', []);
61 $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 }
62 65
63 if (empty($url)) { 66 if (empty($hashedUrls)) {
64 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());
65 } 68 }
66 69
67 $res = $this->getDoctrine() 70 $results = [];
68 ->getRepository('WallabagCoreBundle:Entry') 71 foreach ($hashedUrls as $hashedUrlToSearch) {
69 ->findByUrlAndUserId($url, $this->getUser()->getId()); 72 $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId());
73
74 $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId);
75 }
70 76
71 $exists = $this->returnExistInformation($res, $returnId); 77 $results = $this->replaceUrlHashes($results, $urlHashMap);
72 78
73 return $this->sendResponse(['exists' => $exists]); 79 if (!empty($url) || !empty($hashedUrl)) {
80 $hu = array_keys($results)[0];
81
82 return $this->sendResponse(['exists' => $results[$hu]]);
83 }
84
85 return $this->sendResponse($results);
74 } 86 }
75 87
76 /** 88 /**
@@ -80,13 +92,14 @@ class EntryRestController extends WallabagRestController
80 * parameters={ 92 * parameters={
81 * {"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."},
82 * {"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."},
83 * {"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."},
84 * {"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."},
85 * {"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."},
86 * {"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."},
87 * {"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."},
88 * {"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."},
89 * {"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."},
90 * } 103 * }
91 * ) 104 * )
92 * 105 *
@@ -105,6 +118,7 @@ class EntryRestController extends WallabagRestController
105 $perPage = (int) $request->query->get('perPage', 30); 118 $perPage = (int) $request->query->get('perPage', 30);
106 $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', '');
107 $since = $request->query->get('since', 0); 120 $since = $request->query->get('since', 0);
121 $detail = strtolower($request->query->get('detail', 'full'));
108 122
109 try { 123 try {
110 /** @var \Pagerfanta\Pagerfanta $pager */ 124 /** @var \Pagerfanta\Pagerfanta $pager */
@@ -116,7 +130,8 @@ class EntryRestController extends WallabagRestController
116 $sort, 130 $sort,
117 $order, 131 $order,
118 $since, 132 $since,
119 $tags 133 $tags,
134 $detail
120 ); 135 );
121 } catch (\Exception $e) { 136 } catch (\Exception $e) {
122 throw new BadRequestHttpException($e->getMessage()); 137 throw new BadRequestHttpException($e->getMessage());
@@ -140,8 +155,9 @@ class EntryRestController extends WallabagRestController
140 'perPage' => $perPage, 155 'perPage' => $perPage,
141 'tags' => $tags, 156 'tags' => $tags,
142 'since' => $since, 157 'since' => $since,
158 'detail' => $detail,
143 ], 159 ],
144 UrlGeneratorInterface::ABSOLUTE_URL 160 true
145 ) 161 )
146 ); 162 );
147 163
@@ -349,9 +365,7 @@ class EntryRestController extends WallabagRestController
349 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), 365 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(),
350 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), 366 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(),
351 // faking the open graph preview picture 367 // faking the open graph preview picture
352 'open_graph' => [ 368 'image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(),
353 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(),
354 ],
355 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), 369 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(),
356 ] 370 ]
357 ); 371 );
@@ -363,7 +377,7 @@ class EntryRestController extends WallabagRestController
363 } 377 }
364 378
365 if (null !== $data['isArchived']) { 379 if (null !== $data['isArchived']) {
366 $entry->setArchived((bool) $data['isArchived']); 380 $entry->updateArchived((bool) $data['isArchived']);
367 } 381 }
368 382
369 if (null !== $data['isStarred']) { 383 if (null !== $data['isStarred']) {
@@ -479,7 +493,7 @@ class EntryRestController extends WallabagRestController
479 } 493 }
480 494
481 if (null !== $data['isArchived']) { 495 if (null !== $data['isArchived']) {
482 $entry->setArchived((bool) $data['isArchived']); 496 $entry->updateArchived((bool) $data['isArchived']);
483 } 497 }
484 498
485 if (null !== $data['isStarred']) { 499 if (null !== $data['isStarred']) {
@@ -787,21 +801,21 @@ class EntryRestController extends WallabagRestController
787 } 801 }
788 802
789 /** 803 /**
790 * Shortcut to send data serialized in json. 804 * Replace the hashedUrl keys in $results with the unhashed URL from the
791 * 805 * request, as recorded in $urlHashMap.
792 * @param mixed $data
793 *
794 * @return JsonResponse
795 */ 806 */
796 private function sendResponse($data) 807 private function replaceUrlHashes(array $results, array $urlHashMap)
797 { 808 {
798 // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 809 $newResults = [];
799 $context = new SerializationContext(); 810 foreach ($results as $hash => $res) {
800 $context->setSerializeNull(true); 811 if (isset($urlHashMap[$hash])) {
801 812 $newResults[$urlHashMap[$hash]] = $res;
802 $json = $this->get('jms_serializer')->serialize($data, 'json', $context); 813 } else {
814 $newResults[$hash] = $res;
815 }
816 }
803 817
804 return (new JsonResponse())->setJson($json); 818 return $newResults;
805 } 819 }
806 820
807 /** 821 /**
@@ -832,8 +846,8 @@ class EntryRestController extends WallabagRestController
832 /** 846 /**
833 * Return information about the entry if it exist and depending on the id or not. 847 * Return information about the entry if it exist and depending on the id or not.
834 * 848 *
835 * @param Entry|null $entry 849 * @param Entry|bool|null $entry
836 * @param bool $returnId 850 * @param bool $returnId
837 * 851 *
838 * @return bool|int 852 * @return bool|int
839 */ 853 */