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.php92
1 files changed, 54 insertions, 38 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 06520af9..9f933adb 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -14,6 +14,7 @@ use Wallabag\CoreBundle\Entity\Entry;
14use Wallabag\CoreBundle\Entity\Tag; 14use Wallabag\CoreBundle\Entity\Tag;
15use Wallabag\CoreBundle\Event\EntryDeletedEvent; 15use Wallabag\CoreBundle\Event\EntryDeletedEvent;
16use Wallabag\CoreBundle\Event\EntrySavedEvent; 16use Wallabag\CoreBundle\Event\EntrySavedEvent;
17use Wallabag\CoreBundle\Helper\UrlHasher;
17 18
18class EntryRestController extends WallabagRestController 19class EntryRestController extends WallabagRestController
19{ 20{
@@ -43,50 +44,45 @@ class EntryRestController extends WallabagRestController
43 44
44 $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');
45 46
46 $urls = $request->query->get('urls', []);
47 $hashedUrls = $request->query->get('hashed_urls', []); 47 $hashedUrls = $request->query->get('hashed_urls', []);
48 $hashedUrl = $request->query->get('hashed_url', '');
49 if (!empty($hashedUrl)) {
50 $hashedUrls[] = $hashedUrl;
51 }
48 52
49 // handle multiple urls first 53 $urls = $request->query->get('urls', []);
50 if (!empty($hashedUrls)) { 54 $url = $request->query->get('url', '');
51 $results = []; 55 if (!empty($url)) {
52 foreach ($hashedUrls as $hashedUrl) { 56 $urls[] = $url;
53 $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); 57 }
54
55 $results[$hashedUrl] = $this->returnExistInformation($res, $returnId);
56 }
57 58
58 return $this->sendResponse($results); 59 $urlHashMap = [];
60 foreach ($urls as $urlToHash) {
61 $urlHash = UrlHasher::hashUrl($urlToHash);
62 $hashedUrls[] = $urlHash;
63 $urlHashMap[$urlHash] = $urlToHash;
59 } 64 }
60 65
61 // @deprecated, to be remove in 3.0 66 if (empty($hashedUrls)) {
62 if (!empty($urls)) { 67 throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId());
63 $results = []; 68 }
64 foreach ($urls as $url) {
65 $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId());
66 69
67 $results[$url] = $this->returnExistInformation($res, $returnId); 70 $results = [];
68 } 71 foreach ($hashedUrls as $hashedUrlToSearch) {
72 $res = $repo->findByHashedUrlAndUserId($hashedUrlToSearch, $this->getUser()->getId());
69 73
70 return $this->sendResponse($results); 74 $results[$hashedUrlToSearch] = $this->returnExistInformation($res, $returnId);
71 } 75 }
72 76
73 // let's see if it is a simple url? 77 $results = $this->replaceUrlHashes($results, $urlHashMap);
74 $url = $request->query->get('url', '');
75 $hashedUrl = $request->query->get('hashed_url', '');
76 78
77 if (empty($url) && empty($hashedUrl)) { 79 if (!empty($url) || !empty($hashedUrl)) {
78 throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); 80 $hu = array_keys($results)[0];
79 }
80 81
81 $method = 'findByUrlAndUserId'; 82 return $this->sendResponse(['exists' => $results[$hu]]);
82 if (!empty($hashedUrl)) {
83 $method = 'findByHashedUrlAndUserId';
84 $url = $hashedUrl;
85 } 83 }
86 84
87 $res = $repo->$method($url, $this->getUser()->getId()); 85 return $this->sendResponse($results);
88
89 return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]);
90 } 86 }
91 87
92 /** 88 /**
@@ -103,6 +99,7 @@ class EntryRestController extends WallabagRestController
103 * {"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."},
104 * {"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."},
105 * {"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."},
106 * } 103 * }
107 * ) 104 * )
108 * 105 *
@@ -121,6 +118,7 @@ class EntryRestController extends WallabagRestController
121 $perPage = (int) $request->query->get('perPage', 30); 118 $perPage = (int) $request->query->get('perPage', 30);
122 $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', '');
123 $since = $request->query->get('since', 0); 120 $since = $request->query->get('since', 0);
121 $detail = strtolower($request->query->get('detail', 'full'));
124 122
125 try { 123 try {
126 /** @var \Pagerfanta\Pagerfanta $pager */ 124 /** @var \Pagerfanta\Pagerfanta $pager */
@@ -132,7 +130,8 @@ class EntryRestController extends WallabagRestController
132 $sort, 130 $sort,
133 $order, 131 $order,
134 $since, 132 $since,
135 $tags 133 $tags,
134 $detail
136 ); 135 );
137 } catch (\Exception $e) { 136 } catch (\Exception $e) {
138 throw new BadRequestHttpException($e->getMessage()); 137 throw new BadRequestHttpException($e->getMessage());
@@ -156,6 +155,7 @@ class EntryRestController extends WallabagRestController
156 'perPage' => $perPage, 155 'perPage' => $perPage,
157 'tags' => $tags, 156 'tags' => $tags,
158 'since' => $since, 157 'since' => $since,
158 'detail' => $detail,
159 ], 159 ],
160 true 160 true
161 ) 161 )
@@ -365,9 +365,7 @@ class EntryRestController extends WallabagRestController
365 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), 365 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(),
366 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), 366 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(),
367 // faking the open graph preview picture 367 // faking the open graph preview picture
368 'open_graph' => [ 368 'image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(),
369 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(),
370 ],
371 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), 369 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(),
372 ] 370 ]
373 ); 371 );
@@ -566,7 +564,7 @@ class EntryRestController extends WallabagRestController
566 } 564 }
567 565
568 // if refreshing entry failed, don't save it 566 // if refreshing entry failed, don't save it
569 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { 567 if ($this->container->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
570 return new JsonResponse([], 304); 568 return new JsonResponse([], 304);
571 } 569 }
572 570
@@ -803,6 +801,24 @@ class EntryRestController extends WallabagRestController
803 } 801 }
804 802
805 /** 803 /**
804 * Replace the hashedUrl keys in $results with the unhashed URL from the
805 * request, as recorded in $urlHashMap.
806 */
807 private function replaceUrlHashes(array $results, array $urlHashMap)
808 {
809 $newResults = [];
810 foreach ($results as $hash => $res) {
811 if (isset($urlHashMap[$hash])) {
812 $newResults[$urlHashMap[$hash]] = $res;
813 } else {
814 $newResults[$hash] = $res;
815 }
816 }
817
818 return $newResults;
819 }
820
821 /**
806 * Retrieve value from the request. 822 * Retrieve value from the request.
807 * Used for POST & PATCH on a an entry. 823 * Used for POST & PATCH on a an entry.
808 * 824 *
@@ -830,8 +846,8 @@ class EntryRestController extends WallabagRestController
830 /** 846 /**
831 * 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.
832 * 848 *
833 * @param Entry|null $entry 849 * @param Entry|bool|null $entry
834 * @param bool $returnId 850 * @param bool $returnId
835 * 851 *
836 * @return bool|int 852 * @return bool|int
837 */ 853 */