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.php4
2 files changed, 237 insertions, 35 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php
index c544815e..632b16d9 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(
@@ -230,9 +327,7 @@ class EntryRestController extends WallabagRestController
230 // entry saved, dispatch event about it! 327 // entry saved, dispatch event about it!
231 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 328 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
232 329
233 $json = $this->get('serializer')->serialize($entry, 'json'); 330 return $this->sendResponse($entry);
234
235 return (new JsonResponse())->setJson($json);
236 } 331 }
237 332
238 /** 333 /**
@@ -281,9 +376,7 @@ class EntryRestController extends WallabagRestController
281 $em = $this->getDoctrine()->getManager(); 376 $em = $this->getDoctrine()->getManager();
282 $em->flush(); 377 $em->flush();
283 378
284 $json = $this->get('serializer')->serialize($entry, 'json'); 379 return $this->sendResponse($entry);
285
286 return (new JsonResponse())->setJson($json);
287 } 380 }
288 381
289 /** 382 /**
@@ -326,9 +419,7 @@ class EntryRestController extends WallabagRestController
326 // entry saved, dispatch event about it! 419 // entry saved, dispatch event about it!
327 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); 420 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
328 421
329 $json = $this->get('serializer')->serialize($entry, 'json'); 422 return $this->sendResponse($entry);
330
331 return (new JsonResponse())->setJson($json);
332 } 423 }
333 424
334 /** 425 /**
@@ -354,9 +445,7 @@ class EntryRestController extends WallabagRestController
354 // entry deleted, dispatch event about it! 445 // entry deleted, dispatch event about it!
355 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); 446 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
356 447
357 $json = $this->get('serializer')->serialize($entry, 'json'); 448 return $this->sendResponse($entry);
358
359 return (new JsonResponse())->setJson($json);
360 } 449 }
361 450
362 /** 451 /**
@@ -375,9 +464,7 @@ class EntryRestController extends WallabagRestController
375 $this->validateAuthentication(); 464 $this->validateAuthentication();
376 $this->validateUserAccess($entry->getUser()->getId()); 465 $this->validateUserAccess($entry->getUser()->getId());
377 466
378 $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); 467 return $this->sendResponse($entry->getTags());
379
380 return (new JsonResponse())->setJson($json);
381 } 468 }
382 469
383 /** 470 /**
@@ -408,9 +495,7 @@ class EntryRestController extends WallabagRestController
408 $em->persist($entry); 495 $em->persist($entry);
409 $em->flush(); 496 $em->flush();
410 497
411 $json = $this->get('serializer')->serialize($entry, 'json'); 498 return $this->sendResponse($entry);
412
413 return (new JsonResponse())->setJson($json);
414 } 499 }
415 500
416 /** 501 /**
@@ -435,7 +520,124 @@ class EntryRestController extends WallabagRestController
435 $em->persist($entry); 520 $em->persist($entry);
436 $em->flush(); 521 $em->flush();
437 522
438 $json = $this->get('serializer')->serialize($entry, 'json'); 523 return $this->sendResponse($entry);
524 }
525
526 /**
527 * Handles an entries list delete tags from them.
528 *
529 * @ApiDoc(
530 * parameters={
531 * {"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."}
532 * }
533 * )
534 *
535 * @return JsonResponse
536 */
537 public function deleteEntriesTagsListAction(Request $request)
538 {
539 $this->validateAuthentication();
540
541 $list = json_decode($request->query->get('list', []));
542
543 if (empty($list)) {
544 return $this->sendResponse([]);
545 }
546
547 // handle multiple urls
548 $results = [];
549
550 foreach ($list as $key => $element) {
551 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
552 $element->url,
553 $this->getUser()->getId()
554 );
555
556 $results[$key]['url'] = $element->url;
557 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
558
559 $tags = $element->tags;
560
561 if (false !== $entry && !(empty($tags))) {
562 $tags = explode(',', $tags);
563 foreach ($tags as $label) {
564 $label = trim($label);
565
566 $tag = $this->getDoctrine()
567 ->getRepository('WallabagCoreBundle:Tag')
568 ->findOneByLabel($label);
569
570 if (false !== $tag) {
571 $entry->removeTag($tag);
572 }
573 }
574
575 $em = $this->getDoctrine()->getManager();
576 $em->persist($entry);
577 $em->flush();
578 }
579 }
580
581 return $this->sendResponse($results);
582 }
583
584 /**
585 * Handles an entries list and add tags to them.
586 *
587 * @ApiDoc(
588 * parameters={
589 * {"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."}
590 * }
591 * )
592 *
593 * @return JsonResponse
594 */
595 public function postEntriesTagsListAction(Request $request)
596 {
597 $this->validateAuthentication();
598
599 $list = json_decode($request->query->get('list', []));
600
601 if (empty($list)) {
602 return $this->sendResponse([]);
603 }
604
605 $results = [];
606
607 // handle multiple urls
608 foreach ($list as $key => $element) {
609 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
610 $element->url,
611 $this->getUser()->getId()
612 );
613
614 $results[$key]['url'] = $element->url;
615 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
616
617 $tags = $element->tags;
618
619 if (false !== $entry && !(empty($tags))) {
620 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
621
622 $em = $this->getDoctrine()->getManager();
623 $em->persist($entry);
624 $em->flush();
625 }
626 }
627
628 return $this->sendResponse($results);
629 }
630
631 /**
632 * Shortcut to send data serialized in json.
633 *
634 * @param mixed $data
635 *
636 * @return JsonResponse
637 */
638 private function sendResponse($data)
639 {
640 $json = $this->get('serializer')->serialize($data, 'json');
439 641
440 return (new JsonResponse())->setJson($json); 642 return (new JsonResponse())->setJson($json);
441 } 643 }
diff --git a/src/Wallabag/ApiBundle/Controller/TagRestController.php b/src/Wallabag/ApiBundle/Controller/TagRestController.php
index bc6d4e64..47298d7e 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={
@@ -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={