aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-10-29 13:20:01 +0200
committerGitHub <noreply@github.com>2016-10-29 13:20:01 +0200
commit267087d969e68e2f5abcb18f526120d835d9a686 (patch)
tree49d4f5b01f168591bc19180506289da5f076fa87 /src
parenteca4d030bf90cdf6ca0b6a86b18bd8f66a36db08 (diff)
parentbc4564a709ccae949999af77aa456287a72f1591 (diff)
downloadwallabag-267087d969e68e2f5abcb18f526120d835d9a686.tar.gz
wallabag-267087d969e68e2f5abcb18f526120d835d9a686.tar.zst
wallabag-267087d969e68e2f5abcb18f526120d835d9a686.zip
Merge pull request #2509 from wallabag/explode-api-controller
Exploded WallabagRestController into many controllers
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php367
-rw-r--r--src/Wallabag/ApiBundle/Controller/TagRestController.php171
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php522
-rw-r--r--src/Wallabag/ApiBundle/Resources/config/routing_rest.yml7
4 files changed, 546 insertions, 521 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
new file mode 100644
index 00000000..24fa7b3b
--- /dev/null
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -0,0 +1,367 @@
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpFoundation\JsonResponse;
10use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Entity\Tag;
13
14class EntryRestController extends WallabagRestController
15{
16 /**
17 * Check if an entry exist by url.
18 *
19 * @ApiDoc(
20 * parameters={
21 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
22 * {"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"}
23 * }
24 * )
25 *
26 * @return JsonResponse
27 */
28 public function getEntriesExistsAction(Request $request)
29 {
30 $this->validateAuthentication();
31
32 $urls = $request->query->get('urls', []);
33
34 // handle multiple urls first
35 if (!empty($urls)) {
36 $results = [];
37 foreach ($urls as $url) {
38 $res = $this->getDoctrine()
39 ->getRepository('WallabagCoreBundle:Entry')
40 ->findByUrlAndUserId($url, $this->getUser()->getId());
41
42 $results[$url] = false === $res ? false : true;
43 }
44
45 $json = $this->get('serializer')->serialize($results, 'json');
46
47 return (new JsonResponse())->setJson($json);
48 }
49
50 // let's see if it is a simple url?
51 $url = $request->query->get('url', '');
52
53 if (empty($url)) {
54 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
55 }
56
57 $res = $this->getDoctrine()
58 ->getRepository('WallabagCoreBundle:Entry')
59 ->findByUrlAndUserId($url, $this->getUser()->getId());
60
61 $exists = false === $res ? false : true;
62
63 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
64
65 return (new JsonResponse())->setJson($json);
66 }
67
68 /**
69 * Retrieve all entries. It could be filtered by many options.
70 *
71 * @ApiDoc(
72 * parameters={
73 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
74 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
75 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
76 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
77 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
78 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
79 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
80 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
81 * }
82 * )
83 *
84 * @return JsonResponse
85 */
86 public function getEntriesAction(Request $request)
87 {
88 $this->validateAuthentication();
89
90 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
91 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
92 $sort = $request->query->get('sort', 'created');
93 $order = $request->query->get('order', 'desc');
94 $page = (int) $request->query->get('page', 1);
95 $perPage = (int) $request->query->get('perPage', 30);
96 $tags = $request->query->get('tags', '');
97 $since = $request->query->get('since', 0);
98
99 $pager = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
102
103 $pager->setCurrentPage($page);
104 $pager->setMaxPerPage($perPage);
105
106 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
107 $paginatedCollection = $pagerfantaFactory->createRepresentation(
108 $pager,
109 new Route(
110 'api_get_entries',
111 [
112 'archive' => $isArchived,
113 'starred' => $isStarred,
114 'sort' => $sort,
115 'order' => $order,
116 'page' => $page,
117 'perPage' => $perPage,
118 'tags' => $tags,
119 'since' => $since,
120 ],
121 UrlGeneratorInterface::ABSOLUTE_URL
122 )
123 );
124
125 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
126
127 return (new JsonResponse())->setJson($json);
128 }
129
130 /**
131 * Retrieve a single entry.
132 *
133 * @ApiDoc(
134 * requirements={
135 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
136 * }
137 * )
138 *
139 * @return JsonResponse
140 */
141 public function getEntryAction(Entry $entry)
142 {
143 $this->validateAuthentication();
144 $this->validateUserAccess($entry->getUser()->getId());
145
146 $json = $this->get('serializer')->serialize($entry, 'json');
147
148 return (new JsonResponse())->setJson($json);
149 }
150
151 /**
152 * Create an entry.
153 *
154 * @ApiDoc(
155 * parameters={
156 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
157 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
158 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
159 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
160 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
161 * }
162 * )
163 *
164 * @return JsonResponse
165 */
166 public function postEntriesAction(Request $request)
167 {
168 $this->validateAuthentication();
169
170 $url = $request->request->get('url');
171 $title = $request->request->get('title');
172 $isArchived = $request->request->get('archive');
173 $isStarred = $request->request->get('starred');
174
175 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
176
177 if (false === $entry) {
178 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
179 new Entry($this->getUser()),
180 $url
181 );
182 }
183
184 if (!is_null($title)) {
185 $entry->setTitle($title);
186 }
187
188 $tags = $request->request->get('tags', '');
189 if (!empty($tags)) {
190 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
191 }
192
193 if (!is_null($isStarred)) {
194 $entry->setStarred((bool) $isStarred);
195 }
196
197 if (!is_null($isArchived)) {
198 $entry->setArchived((bool) $isArchived);
199 }
200
201 $em = $this->getDoctrine()->getManager();
202 $em->persist($entry);
203
204 $em->flush();
205
206 $json = $this->get('serializer')->serialize($entry, 'json');
207
208 return (new JsonResponse())->setJson($json);
209 }
210
211 /**
212 * Change several properties of an entry.
213 *
214 * @ApiDoc(
215 * requirements={
216 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
217 * },
218 * parameters={
219 * {"name"="title", "dataType"="string", "required"=false},
220 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
221 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
222 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
223 * }
224 * )
225 *
226 * @return JsonResponse
227 */
228 public function patchEntriesAction(Entry $entry, Request $request)
229 {
230 $this->validateAuthentication();
231 $this->validateUserAccess($entry->getUser()->getId());
232
233 $title = $request->request->get('title');
234 $isArchived = $request->request->get('archive');
235 $isStarred = $request->request->get('starred');
236
237 if (!is_null($title)) {
238 $entry->setTitle($title);
239 }
240
241 if (!is_null($isArchived)) {
242 $entry->setArchived((bool) $isArchived);
243 }
244
245 if (!is_null($isStarred)) {
246 $entry->setStarred((bool) $isStarred);
247 }
248
249 $tags = $request->request->get('tags', '');
250 if (!empty($tags)) {
251 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
252 }
253
254 $em = $this->getDoctrine()->getManager();
255 $em->flush();
256
257 $json = $this->get('serializer')->serialize($entry, 'json');
258
259 return (new JsonResponse())->setJson($json);
260 }
261
262 /**
263 * Delete **permanently** an entry.
264 *
265 * @ApiDoc(
266 * requirements={
267 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
268 * }
269 * )
270 *
271 * @return JsonResponse
272 */
273 public function deleteEntriesAction(Entry $entry)
274 {
275 $this->validateAuthentication();
276 $this->validateUserAccess($entry->getUser()->getId());
277
278 $em = $this->getDoctrine()->getManager();
279 $em->remove($entry);
280 $em->flush();
281
282 $json = $this->get('serializer')->serialize($entry, 'json');
283
284 return (new JsonResponse())->setJson($json);
285 }
286
287 /**
288 * Retrieve all tags for an entry.
289 *
290 * @ApiDoc(
291 * requirements={
292 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
293 * }
294 * )
295 *
296 * @return JsonResponse
297 */
298 public function getEntriesTagsAction(Entry $entry)
299 {
300 $this->validateAuthentication();
301 $this->validateUserAccess($entry->getUser()->getId());
302
303 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
304
305 return (new JsonResponse())->setJson($json);
306 }
307
308 /**
309 * Add one or more tags to an entry.
310 *
311 * @ApiDoc(
312 * requirements={
313 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
314 * },
315 * parameters={
316 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
317 * }
318 * )
319 *
320 * @return JsonResponse
321 */
322 public function postEntriesTagsAction(Request $request, Entry $entry)
323 {
324 $this->validateAuthentication();
325 $this->validateUserAccess($entry->getUser()->getId());
326
327 $tags = $request->request->get('tags', '');
328 if (!empty($tags)) {
329 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
330 }
331
332 $em = $this->getDoctrine()->getManager();
333 $em->persist($entry);
334 $em->flush();
335
336 $json = $this->get('serializer')->serialize($entry, 'json');
337
338 return (new JsonResponse())->setJson($json);
339 }
340
341 /**
342 * Permanently remove one tag for an entry.
343 *
344 * @ApiDoc(
345 * requirements={
346 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
348 * }
349 * )
350 *
351 * @return JsonResponse
352 */
353 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
354 {
355 $this->validateAuthentication();
356 $this->validateUserAccess($entry->getUser()->getId());
357
358 $entry->removeTag($tag);
359 $em = $this->getDoctrine()->getManager();
360 $em->persist($entry);
361 $em->flush();
362
363 $json = $this->get('serializer')->serialize($entry, 'json');
364
365 return (new JsonResponse())->setJson($json);
366 }
367}
diff --git a/src/Wallabag/ApiBundle/Controller/TagRestController.php b/src/Wallabag/ApiBundle/Controller/TagRestController.php
new file mode 100644
index 00000000..4e7ddc66
--- /dev/null
+++ b/src/Wallabag/ApiBundle/Controller/TagRestController.php
@@ -0,0 +1,171 @@
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6use Symfony\Component\HttpFoundation\Request;
7use Symfony\Component\HttpFoundation\JsonResponse;
8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag;
10
11class TagRestController extends WallabagRestController
12{
13 /**
14 * Retrieve all tags.
15 *
16 * @ApiDoc()
17 *
18 * @return JsonResponse
19 */
20 public function getTagsAction()
21 {
22 $this->validateAuthentication();
23
24 $tags = $this->getDoctrine()
25 ->getRepository('WallabagCoreBundle:Tag')
26 ->findAllTags($this->getUser()->getId());
27
28 $json = $this->get('serializer')->serialize($tags, 'json');
29
30 return (new JsonResponse())->setJson($json);
31 }
32
33 /**
34 * Permanently remove one tag from **every** entry.
35 *
36 * @ApiDoc(
37 * requirements={
38 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
39 * }
40 * )
41 *
42 * @return JsonResponse
43 */
44 public function deleteTagLabelAction(Request $request)
45 {
46 $this->validateAuthentication();
47 $label = $request->request->get('tag', '');
48
49 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
50
51 if (empty($tag)) {
52 throw $this->createNotFoundException('Tag not found');
53 }
54
55 $this->getDoctrine()
56 ->getRepository('WallabagCoreBundle:Entry')
57 ->removeTag($this->getUser()->getId(), $tag);
58
59 $this->cleanOrphanTag($tag);
60
61 $json = $this->get('serializer')->serialize($tag, 'json');
62
63 return (new JsonResponse())->setJson($json);
64 }
65
66 /**
67 * Permanently remove some tags from **every** entry.
68 *
69 * @ApiDoc(
70 * requirements={
71 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
72 * }
73 * )
74 *
75 * @return JsonResponse
76 */
77 public function deleteTagsLabelAction(Request $request)
78 {
79 $this->validateAuthentication();
80
81 $tagsLabels = $request->request->get('tags', '');
82
83 $tags = [];
84
85 foreach (explode(',', $tagsLabels) as $tagLabel) {
86 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
87
88 if (!empty($tagEntity)) {
89 $tags[] = $tagEntity;
90 }
91 }
92
93 if (empty($tags)) {
94 throw $this->createNotFoundException('Tags not found');
95 }
96
97 $this->getDoctrine()
98 ->getRepository('WallabagCoreBundle:Entry')
99 ->removeTags($this->getUser()->getId(), $tags);
100
101 $this->cleanOrphanTag($tags);
102
103 $json = $this->get('serializer')->serialize($tags, 'json');
104
105 return (new JsonResponse())->setJson($json);
106 }
107
108 /**
109 * Permanently remove one tag from **every** entry.
110 *
111 * @ApiDoc(
112 * requirements={
113 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
114 * }
115 * )
116 *
117 * @return JsonResponse
118 */
119 public function deleteTagAction(Tag $tag)
120 {
121 $this->validateAuthentication();
122
123 $this->getDoctrine()
124 ->getRepository('WallabagCoreBundle:Entry')
125 ->removeTag($this->getUser()->getId(), $tag);
126
127 $this->cleanOrphanTag($tag);
128
129 $json = $this->get('serializer')->serialize($tag, 'json');
130
131 return (new JsonResponse())->setJson($json);
132 }
133
134 /**
135 * Retrieve version number.
136 *
137 * @ApiDoc()
138 *
139 * @return JsonResponse
140 */
141 public function getVersionAction()
142 {
143 $version = $this->container->getParameter('wallabag_core.version');
144
145 $json = $this->get('serializer')->serialize($version, 'json');
146
147 return (new JsonResponse())->setJson($json);
148 }
149
150 /**
151 * Remove orphan tag in case no entries are associated to it.
152 *
153 * @param Tag|array $tags
154 */
155 private function cleanOrphanTag($tags)
156 {
157 if (!is_array($tags)) {
158 $tags = [$tags];
159 }
160
161 $em = $this->getDoctrine()->getManager();
162
163 foreach ($tags as $tag) {
164 if (count($tag->getEntries()) === 0) {
165 $em->remove($tag);
166 }
167 }
168
169 $em->flush();
170 }
171}
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 9997913d..e927a890 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -3,19 +3,12 @@
3namespace Wallabag\ApiBundle\Controller; 3namespace Wallabag\ApiBundle\Controller;
4 4
5use FOS\RestBundle\Controller\FOSRestController; 5use FOS\RestBundle\Controller\FOSRestController;
6use Hateoas\Configuration\Route;
7use Hateoas\Representation\Factory\PagerfantaFactory;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
9use Symfony\Component\HttpFoundation\Request;
10use Symfony\Component\HttpFoundation\JsonResponse;
11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
12use Symfony\Component\Security\Core\Exception\AccessDeniedException; 6use Symfony\Component\Security\Core\Exception\AccessDeniedException;
13use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
14use Wallabag\CoreBundle\Entity\Tag;
15 8
16class WallabagRestController extends FOSRestController 9class WallabagRestController extends FOSRestController
17{ 10{
18 private function validateAuthentication() 11 protected function validateAuthentication()
19 { 12 {
20 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 13 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
21 throw new AccessDeniedException(); 14 throw new AccessDeniedException();
@@ -23,523 +16,12 @@ class WallabagRestController extends FOSRestController
23 } 16 }
24 17
25 /** 18 /**
26 * Check if an entry exist by url.
27 *
28 * @ApiDoc(
29 * parameters={
30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
31 * {"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 * }
33 * )
34 *
35 * @return JsonResponse
36 */
37 public function getEntriesExistsAction(Request $request)
38 {
39 $this->validateAuthentication();
40
41 $urls = $request->query->get('urls', []);
42
43 // handle multiple urls first
44 if (!empty($urls)) {
45 $results = [];
46 foreach ($urls as $url) {
47 $res = $this->getDoctrine()
48 ->getRepository('WallabagCoreBundle:Entry')
49 ->findByUrlAndUserId($url, $this->getUser()->getId());
50
51 $results[$url] = false === $res ? false : true;
52 }
53
54 $json = $this->get('serializer')->serialize($results, 'json');
55
56 return (new JsonResponse())->setJson($json);
57 }
58
59 // let's see if it is a simple url?
60 $url = $request->query->get('url', '');
61
62 if (empty($url)) {
63 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
64 }
65
66 $res = $this->getDoctrine()
67 ->getRepository('WallabagCoreBundle:Entry')
68 ->findByUrlAndUserId($url, $this->getUser()->getId());
69
70 $exists = false === $res ? false : true;
71
72 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
73
74 return (new JsonResponse())->setJson($json);
75 }
76
77 /**
78 * Retrieve all entries. It could be filtered by many options.
79 *
80 * @ApiDoc(
81 * parameters={
82 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
83 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
84 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
85 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
86 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
87 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
88 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
89 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
90 * }
91 * )
92 *
93 * @return JsonResponse
94 */
95 public function getEntriesAction(Request $request)
96 {
97 $this->validateAuthentication();
98
99 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
100 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
101 $sort = $request->query->get('sort', 'created');
102 $order = $request->query->get('order', 'desc');
103 $page = (int) $request->query->get('page', 1);
104 $perPage = (int) $request->query->get('perPage', 30);
105 $tags = $request->query->get('tags', '');
106 $since = $request->query->get('since', 0);
107
108 $pager = $this->getDoctrine()
109 ->getRepository('WallabagCoreBundle:Entry')
110 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
111
112 $pager->setCurrentPage($page);
113 $pager->setMaxPerPage($perPage);
114
115 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
116 $paginatedCollection = $pagerfantaFactory->createRepresentation(
117 $pager,
118 new Route(
119 'api_get_entries',
120 [
121 'archive' => $isArchived,
122 'starred' => $isStarred,
123 'sort' => $sort,
124 'order' => $order,
125 'page' => $page,
126 'perPage' => $perPage,
127 'tags' => $tags,
128 'since' => $since,
129 ],
130 UrlGeneratorInterface::ABSOLUTE_URL
131 )
132 );
133
134 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
135
136 return (new JsonResponse())->setJson($json);
137 }
138
139 /**
140 * Retrieve a single entry.
141 *
142 * @ApiDoc(
143 * requirements={
144 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
145 * }
146 * )
147 *
148 * @return JsonResponse
149 */
150 public function getEntryAction(Entry $entry)
151 {
152 $this->validateAuthentication();
153 $this->validateUserAccess($entry->getUser()->getId());
154
155 $json = $this->get('serializer')->serialize($entry, 'json');
156
157 return (new JsonResponse())->setJson($json);
158 }
159
160 /**
161 * Create an entry.
162 *
163 * @ApiDoc(
164 * parameters={
165 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
166 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
167 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
168 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
169 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
170 * }
171 * )
172 *
173 * @return JsonResponse
174 */
175 public function postEntriesAction(Request $request)
176 {
177 $this->validateAuthentication();
178
179 $url = $request->request->get('url');
180 $title = $request->request->get('title');
181 $isArchived = $request->request->get('archive');
182 $isStarred = $request->request->get('starred');
183
184 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
185
186 if (false === $entry) {
187 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
188 new Entry($this->getUser()),
189 $url
190 );
191 }
192
193 if (!is_null($title)) {
194 $entry->setTitle($title);
195 }
196
197 $tags = $request->request->get('tags', '');
198 if (!empty($tags)) {
199 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
200 }
201
202 if (!is_null($isStarred)) {
203 $entry->setStarred((bool) $isStarred);
204 }
205
206 if (!is_null($isArchived)) {
207 $entry->setArchived((bool) $isArchived);
208 }
209
210 $em = $this->getDoctrine()->getManager();
211 $em->persist($entry);
212
213 $em->flush();
214
215 $json = $this->get('serializer')->serialize($entry, 'json');
216
217 return (new JsonResponse())->setJson($json);
218 }
219
220 /**
221 * Change several properties of an entry.
222 *
223 * @ApiDoc(
224 * requirements={
225 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
226 * },
227 * parameters={
228 * {"name"="title", "dataType"="string", "required"=false},
229 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
230 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
231 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
232 * }
233 * )
234 *
235 * @return JsonResponse
236 */
237 public function patchEntriesAction(Entry $entry, Request $request)
238 {
239 $this->validateAuthentication();
240 $this->validateUserAccess($entry->getUser()->getId());
241
242 $title = $request->request->get('title');
243 $isArchived = $request->request->get('archive');
244 $isStarred = $request->request->get('starred');
245
246 if (!is_null($title)) {
247 $entry->setTitle($title);
248 }
249
250 if (!is_null($isArchived)) {
251 $entry->setArchived((bool) $isArchived);
252 }
253
254 if (!is_null($isStarred)) {
255 $entry->setStarred((bool) $isStarred);
256 }
257
258 $tags = $request->request->get('tags', '');
259 if (!empty($tags)) {
260 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
261 }
262
263 $em = $this->getDoctrine()->getManager();
264 $em->flush();
265
266 $json = $this->get('serializer')->serialize($entry, 'json');
267
268 return (new JsonResponse())->setJson($json);
269 }
270
271 /**
272 * Delete **permanently** an entry.
273 *
274 * @ApiDoc(
275 * requirements={
276 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
277 * }
278 * )
279 *
280 * @return JsonResponse
281 */
282 public function deleteEntriesAction(Entry $entry)
283 {
284 $this->validateAuthentication();
285 $this->validateUserAccess($entry->getUser()->getId());
286
287 $em = $this->getDoctrine()->getManager();
288 $em->remove($entry);
289 $em->flush();
290
291 $json = $this->get('serializer')->serialize($entry, 'json');
292
293 return (new JsonResponse())->setJson($json);
294 }
295
296 /**
297 * Retrieve all tags for an entry.
298 *
299 * @ApiDoc(
300 * requirements={
301 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
302 * }
303 * )
304 *
305 * @return JsonResponse
306 */
307 public function getEntriesTagsAction(Entry $entry)
308 {
309 $this->validateAuthentication();
310 $this->validateUserAccess($entry->getUser()->getId());
311
312 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
313
314 return (new JsonResponse())->setJson($json);
315 }
316
317 /**
318 * Add one or more tags to an entry.
319 *
320 * @ApiDoc(
321 * requirements={
322 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
323 * },
324 * parameters={
325 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
326 * }
327 * )
328 *
329 * @return JsonResponse
330 */
331 public function postEntriesTagsAction(Request $request, Entry $entry)
332 {
333 $this->validateAuthentication();
334 $this->validateUserAccess($entry->getUser()->getId());
335
336 $tags = $request->request->get('tags', '');
337 if (!empty($tags)) {
338 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
339 }
340
341 $em = $this->getDoctrine()->getManager();
342 $em->persist($entry);
343 $em->flush();
344
345 $json = $this->get('serializer')->serialize($entry, 'json');
346
347 return (new JsonResponse())->setJson($json);
348 }
349
350 /**
351 * Permanently remove one tag for an entry.
352 *
353 * @ApiDoc(
354 * requirements={
355 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
356 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
357 * }
358 * )
359 *
360 * @return JsonResponse
361 */
362 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
363 {
364 $this->validateAuthentication();
365 $this->validateUserAccess($entry->getUser()->getId());
366
367 $entry->removeTag($tag);
368 $em = $this->getDoctrine()->getManager();
369 $em->persist($entry);
370 $em->flush();
371
372 $json = $this->get('serializer')->serialize($entry, 'json');
373
374 return (new JsonResponse())->setJson($json);
375 }
376
377 /**
378 * Retrieve all tags.
379 *
380 * @ApiDoc()
381 *
382 * @return JsonResponse
383 */
384 public function getTagsAction()
385 {
386 $this->validateAuthentication();
387
388 $tags = $this->getDoctrine()
389 ->getRepository('WallabagCoreBundle:Tag')
390 ->findAllTags($this->getUser()->getId());
391
392 $json = $this->get('serializer')->serialize($tags, 'json');
393
394 return (new JsonResponse())->setJson($json);
395 }
396
397 /**
398 * Permanently remove one tag from **every** entry.
399 *
400 * @ApiDoc(
401 * requirements={
402 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
403 * }
404 * )
405 *
406 * @return JsonResponse
407 */
408 public function deleteTagLabelAction(Request $request)
409 {
410 $this->validateAuthentication();
411 $label = $request->request->get('tag', '');
412
413 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
414
415 if (empty($tag)) {
416 throw $this->createNotFoundException('Tag not found');
417 }
418
419 $this->getDoctrine()
420 ->getRepository('WallabagCoreBundle:Entry')
421 ->removeTag($this->getUser()->getId(), $tag);
422
423 $this->cleanOrphanTag($tag);
424
425 $json = $this->get('serializer')->serialize($tag, 'json');
426
427 return (new JsonResponse())->setJson($json);
428 }
429
430 /**
431 * Permanently remove some tags from **every** entry.
432 *
433 * @ApiDoc(
434 * requirements={
435 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
436 * }
437 * )
438 *
439 * @return JsonResponse
440 */
441 public function deleteTagsLabelAction(Request $request)
442 {
443 $this->validateAuthentication();
444
445 $tagsLabels = $request->request->get('tags', '');
446
447 $tags = [];
448
449 foreach (explode(',', $tagsLabels) as $tagLabel) {
450 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
451
452 if (!empty($tagEntity)) {
453 $tags[] = $tagEntity;
454 }
455 }
456
457 if (empty($tags)) {
458 throw $this->createNotFoundException('Tags not found');
459 }
460
461 $this->getDoctrine()
462 ->getRepository('WallabagCoreBundle:Entry')
463 ->removeTags($this->getUser()->getId(), $tags);
464
465 $this->cleanOrphanTag($tags);
466
467 $json = $this->get('serializer')->serialize($tags, 'json');
468
469 return (new JsonResponse())->setJson($json);
470 }
471
472 /**
473 * Permanently remove one tag from **every** entry.
474 *
475 * @ApiDoc(
476 * requirements={
477 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
478 * }
479 * )
480 *
481 * @return JsonResponse
482 */
483 public function deleteTagAction(Tag $tag)
484 {
485 $this->validateAuthentication();
486
487 $this->getDoctrine()
488 ->getRepository('WallabagCoreBundle:Entry')
489 ->removeTag($this->getUser()->getId(), $tag);
490
491 $this->cleanOrphanTag($tag);
492
493 $json = $this->get('serializer')->serialize($tag, 'json');
494
495 return (new JsonResponse())->setJson($json);
496 }
497
498 /**
499 * Retrieve version number.
500 *
501 * @ApiDoc()
502 *
503 * @return JsonResponse
504 */
505 public function getVersionAction()
506 {
507 $version = $this->container->getParameter('wallabag_core.version');
508
509 $json = $this->get('serializer')->serialize($version, 'json');
510
511 return (new JsonResponse())->setJson($json);
512 }
513
514 /**
515 * Remove orphan tag in case no entries are associated to it.
516 *
517 * @param Tag|array $tags
518 */
519 private function cleanOrphanTag($tags)
520 {
521 if (!is_array($tags)) {
522 $tags = [$tags];
523 }
524
525 $em = $this->getDoctrine()->getManager();
526
527 foreach ($tags as $tag) {
528 if (count($tag->getEntries()) === 0) {
529 $em->remove($tag);
530 }
531 }
532
533 $em->flush();
534 }
535
536 /**
537 * Validate that the first id is equal to the second one. 19 * Validate that the first id is equal to the second one.
538 * If not, throw exception. It means a user try to access information from an other user. 20 * If not, throw exception. It means a user try to access information from an other user.
539 * 21 *
540 * @param int $requestUserId User id from the requested source 22 * @param int $requestUserId User id from the requested source
541 */ 23 */
542 private function validateUserAccess($requestUserId) 24 protected function validateUserAccess($requestUserId)
543 { 25 {
544 $user = $this->get('security.token_storage')->getToken()->getUser(); 26 $user = $this->get('security.token_storage')->getToken()->getUser();
545 if ($requestUserId != $user->getId()) { 27 if ($requestUserId != $user->getId()) {
diff --git a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
index 5f43f971..c1af9e02 100644
--- a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
+++ b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml
@@ -1,4 +1,9 @@
1entries: 1entries:
2 type: rest 2 type: rest
3 resource: "WallabagApiBundle:WallabagRest" 3 resource: "WallabagApiBundle:EntryRest"
4 name_prefix: api_
5
6tags:
7 type: rest
8 resource: "WallabagApiBundle:TagRest"
4 name_prefix: api_ 9 name_prefix: api_