aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ApiBundle
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r--src/Wallabag/ApiBundle/Controller/EntryRestController.php268
-rw-r--r--src/Wallabag/ApiBundle/Controller/TagRestController.php8
2 files changed, 239 insertions, 37 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index 54c1747c..4801811d 100644
--- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
@@ -5,6 +5,7 @@ namespace Wallabag\ApiBundle\Controller;
5use Hateoas\Configuration\Route; 5use Hateoas\Configuration\Route;
6use Hateoas\Representation\Factory\PagerfantaFactory; 6use Hateoas\Representation\Factory\PagerfantaFactory;
7use Nelmio\ApiDocBundle\Annotation\ApiDoc; 7use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8use Symfony\Component\HttpKernel\Exception\HttpException;
8use Symfony\Component\HttpFoundation\Request; 9use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpFoundation\JsonResponse; 10use Symfony\Component\HttpFoundation\JsonResponse;
10use Symfony\Component\Routing\Generator\UrlGeneratorInterface; 11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -41,12 +42,10 @@ class EntryRestController extends WallabagRestController
41 ->getRepository('WallabagCoreBundle:Entry') 42 ->getRepository('WallabagCoreBundle:Entry')
42 ->findByUrlAndUserId($url, $this->getUser()->getId()); 43 ->findByUrlAndUserId($url, $this->getUser()->getId());
43 44
44 $results[$url] = false === $res ? false : true; 45 $results[$url] = $res instanceof Entry ? $res->getId() : false;
45 } 46 }
46 47
47 $json = $this->get('serializer')->serialize($results, 'json'); 48 return $this->sendResponse($results);
48
49 return (new JsonResponse())->setJson($json);
50 } 49 }
51 50
52 // let's see if it is a simple url? 51 // let's see if it is a simple url?
@@ -60,11 +59,9 @@ class EntryRestController extends WallabagRestController
60 ->getRepository('WallabagCoreBundle:Entry') 59 ->getRepository('WallabagCoreBundle:Entry')
61 ->findByUrlAndUserId($url, $this->getUser()->getId()); 60 ->findByUrlAndUserId($url, $this->getUser()->getId());
62 61
63 $exists = false === $res ? false : true; 62 $exists = $res instanceof Entry ? $res->getId() : false;
64
65 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
66 63
67 return (new JsonResponse())->setJson($json); 64 return $this->sendResponse(['exists' => $exists]);
68 } 65 }
69 66
70 /** 67 /**
@@ -125,9 +122,7 @@ class EntryRestController extends WallabagRestController
125 ) 122 )
126 ); 123 );
127 124
128 $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); 125 return $this->sendResponse($paginatedCollection);
129
130 return (new JsonResponse())->setJson($json);
131 } 126 }
132 127
133 /** 128 /**
@@ -146,9 +141,7 @@ class EntryRestController extends WallabagRestController
146 $this->validateAuthentication(); 141 $this->validateAuthentication();
147 $this->validateUserAccess($entry->getUser()->getId()); 142 $this->validateUserAccess($entry->getUser()->getId());
148 143
149 $json = $this->get('serializer')->serialize($entry, 'json'); 144 return $this->sendResponse($entry);
150
151 return (new JsonResponse())->setJson($json);
152 } 145 }
153 146
154 /** 147 /**
@@ -174,6 +167,110 @@ class EntryRestController extends WallabagRestController
174 } 167 }
175 168
176 /** 169 /**
170 * Handles an entries list and delete URL.
171 *
172 * @ApiDoc(
173 * parameters={
174 * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."}
175 * }
176 * )
177 *
178 * @return JsonResponse
179 */
180 public function deleteEntriesListAction(Request $request)
181 {
182 $this->validateAuthentication();
183
184 $urls = json_decode($request->query->get('urls', []));
185
186 if (empty($urls)) {
187 return $this->sendResponse([]);
188 }
189
190 $results = [];
191
192 // handle multiple urls
193 foreach ($urls as $key => $url) {
194 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
195 $url,
196 $this->getUser()->getId()
197 );
198
199 $results[$key]['url'] = $url;
200
201 if (false !== $entry) {
202 $em = $this->getDoctrine()->getManager();
203 $em->remove($entry);
204 $em->flush();
205
206 // entry deleted, dispatch event about it!
207 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
208 }
209
210 $results[$key]['entry'] = $entry instanceof Entry ? true : false;
211 }
212
213 return $this->sendResponse($results);
214 }
215
216 /**
217 * Handles an entries list and create URL.
218 *
219 * @ApiDoc(
220 * parameters={
221 * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."}
222 * }
223 * )
224 *
225 * @return JsonResponse
226 *
227 * @throws HttpException When limit is reached
228 */
229 public function postEntriesListAction(Request $request)
230 {
231 $this->validateAuthentication();
232
233 $urls = json_decode($request->query->get('urls', []));
234 $results = [];
235
236 $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions');
237
238 if (count($urls) > $limit) {
239 throw new HttpException(400, 'API limit reached');
240 }
241
242 // handle multiple urls
243 if (!empty($urls)) {
244 foreach ($urls as $key => $url) {
245 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
246 $url,
247 $this->getUser()->getId()
248 );
249
250 $results[$key]['url'] = $url;
251
252 if (false === $entry) {
253 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
254 new Entry($this->getUser()),
255 $url
256 );
257 }
258
259 $em = $this->getDoctrine()->getManager();
260 $em->persist($entry);
261 $em->flush();
262
263 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
264
265 // entry saved, dispatch event about it!
266 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
267 }
268 }
269
270 return $this->sendResponse($results);
271 }
272
273 /**
177 * Create an entry. 274 * Create an entry.
178 * 275 *
179 * @ApiDoc( 276 * @ApiDoc(
@@ -239,9 +336,7 @@ class EntryRestController extends WallabagRestController
239 // entry saved, dispatch event about it! 336 // entry saved, dispatch event about it!
240 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 337 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
241 338
242 $json = $this->get('serializer')->serialize($entry, 'json'); 339 return $this->sendResponse($entry);
243
244 return (new JsonResponse())->setJson($json);
245 } 340 }
246 341
247 /** 342 /**
@@ -290,9 +385,7 @@ class EntryRestController extends WallabagRestController
290 $em = $this->getDoctrine()->getManager(); 385 $em = $this->getDoctrine()->getManager();
291 $em->flush(); 386 $em->flush();
292 387
293 $json = $this->get('serializer')->serialize($entry, 'json'); 388 return $this->sendResponse($entry);
294
295 return (new JsonResponse())->setJson($json);
296 } 389 }
297 390
298 /** 391 /**
@@ -335,9 +428,7 @@ class EntryRestController extends WallabagRestController
335 // entry saved, dispatch event about it! 428 // entry saved, dispatch event about it!
336 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 429 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
337 430
338 $json = $this->get('serializer')->serialize($entry, 'json'); 431 return $this->sendResponse($entry);
339
340 return (new JsonResponse())->setJson($json);
341 } 432 }
342 433
343 /** 434 /**
@@ -363,9 +454,7 @@ class EntryRestController extends WallabagRestController
363 // entry deleted, dispatch event about it! 454 // entry deleted, dispatch event about it!
364 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); 455 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
365 456
366 $json = $this->get('serializer')->serialize($entry, 'json'); 457 return $this->sendResponse($entry);
367
368 return (new JsonResponse())->setJson($json);
369 } 458 }
370 459
371 /** 460 /**
@@ -384,9 +473,7 @@ class EntryRestController extends WallabagRestController
384 $this->validateAuthentication(); 473 $this->validateAuthentication();
385 $this->validateUserAccess($entry->getUser()->getId()); 474 $this->validateUserAccess($entry->getUser()->getId());
386 475
387 $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); 476 return $this->sendResponse($entry->getTags());
388
389 return (new JsonResponse())->setJson($json);
390 } 477 }
391 478
392 /** 479 /**
@@ -417,9 +504,7 @@ class EntryRestController extends WallabagRestController
417 $em->persist($entry); 504 $em->persist($entry);
418 $em->flush(); 505 $em->flush();
419 506
420 $json = $this->get('serializer')->serialize($entry, 'json'); 507 return $this->sendResponse($entry);
421
422 return (new JsonResponse())->setJson($json);
423 } 508 }
424 509
425 /** 510 /**
@@ -444,7 +529,124 @@ class EntryRestController extends WallabagRestController
444 $em->persist($entry); 529 $em->persist($entry);
445 $em->flush(); 530 $em->flush();
446 531
447 $json = $this->get('serializer')->serialize($entry, 'json'); 532 return $this->sendResponse($entry);
533 }
534
535 /**
536 * Handles an entries list delete tags from them.
537 *
538 * @ApiDoc(
539 * parameters={
540 * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."}
541 * }
542 * )
543 *
544 * @return JsonResponse
545 */
546 public function deleteEntriesTagsListAction(Request $request)
547 {
548 $this->validateAuthentication();
549
550 $list = json_decode($request->query->get('list', []));
551
552 if (empty($list)) {
553 return $this->sendResponse([]);
554 }
555
556 // handle multiple urls
557 $results = [];
558
559 foreach ($list as $key => $element) {
560 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
561 $element->url,
562 $this->getUser()->getId()
563 );
564
565 $results[$key]['url'] = $element->url;
566 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
567
568 $tags = $element->tags;
569
570 if (false !== $entry && !(empty($tags))) {
571 $tags = explode(',', $tags);
572 foreach ($tags as $label) {
573 $label = trim($label);
574
575 $tag = $this->getDoctrine()
576 ->getRepository('WallabagCoreBundle:Tag')
577 ->findOneByLabel($label);
578
579 if (false !== $tag) {
580 $entry->removeTag($tag);
581 }
582 }
583
584 $em = $this->getDoctrine()->getManager();
585 $em->persist($entry);
586 $em->flush();
587 }
588 }
589
590 return $this->sendResponse($results);
591 }
592
593 /**
594 * Handles an entries list and add tags to them.
595 *
596 * @ApiDoc(
597 * parameters={
598 * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."}
599 * }
600 * )
601 *
602 * @return JsonResponse
603 */
604 public function postEntriesTagsListAction(Request $request)
605 {
606 $this->validateAuthentication();
607
608 $list = json_decode($request->query->get('list', []));
609
610 if (empty($list)) {
611 return $this->sendResponse([]);
612 }
613
614 $results = [];
615
616 // handle multiple urls
617 foreach ($list as $key => $element) {
618 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
619 $element->url,
620 $this->getUser()->getId()
621 );
622
623 $results[$key]['url'] = $element->url;
624 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
625
626 $tags = $element->tags;
627
628 if (false !== $entry && !(empty($tags))) {
629 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
630
631 $em = $this->getDoctrine()->getManager();
632 $em->persist($entry);
633 $em->flush();
634 }
635 }
636
637 return $this->sendResponse($results);
638 }
639
640 /**
641 * Shortcut to send data serialized in json.
642 *
643 * @param mixed $data
644 *
645 * @return JsonResponse
646 */
647 private function sendResponse($data)
648 {
649 $json = $this->get('serializer')->serialize($data, 'json');
448 650
449 return (new JsonResponse())->setJson($json); 651 return (new JsonResponse())->setJson($json);
450 } 652 }
diff --git a/src/Wallabag/ApiBundle/Controller/TagRestController.php b/src/Wallabag/ApiBundle/Controller/TagRestController.php
index bc6d4e64..354187a0 100644
--- a/src/Wallabag/ApiBundle/Controller/TagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/TagRestController.php
@@ -31,7 +31,7 @@ class TagRestController extends WallabagRestController
31 } 31 }
32 32
33 /** 33 /**
34 * Permanently remove one tag from **every** entry. 34 * Permanently remove one tag from **every** entry by passing the Tag label.
35 * 35 *
36 * @ApiDoc( 36 * @ApiDoc(
37 * requirements={ 37 * requirements={
@@ -44,7 +44,7 @@ class TagRestController extends WallabagRestController
44 public function deleteTagLabelAction(Request $request) 44 public function deleteTagLabelAction(Request $request)
45 { 45 {
46 $this->validateAuthentication(); 46 $this->validateAuthentication();
47 $label = $request->request->get('tag', ''); 47 $label = $request->get('tag', '');
48 48
49 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label); 49 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
50 50
@@ -78,7 +78,7 @@ class TagRestController extends WallabagRestController
78 { 78 {
79 $this->validateAuthentication(); 79 $this->validateAuthentication();
80 80
81 $tagsLabels = $request->request->get('tags', ''); 81 $tagsLabels = $request->get('tags', '');
82 82
83 $tags = []; 83 $tags = [];
84 84
@@ -106,7 +106,7 @@ class TagRestController extends WallabagRestController
106 } 106 }
107 107
108 /** 108 /**
109 * Permanently remove one tag from **every** entry. 109 * Permanently remove one tag from **every** entry by passing the Tag ID.
110 * 110 *
111 * @ApiDoc( 111 * @ApiDoc(
112 * requirements={ 112 * requirements={