diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/WallabagRestController.php | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php index 791bf80b..ed31c536 100644 --- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php +++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php | |||
@@ -27,7 +27,8 @@ class WallabagRestController extends FOSRestController | |||
27 | * | 27 | * |
28 | * @ApiDoc( | 28 | * @ApiDoc( |
29 | * parameters={ | 29 | * parameters={ |
30 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"} | 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"} | ||
31 | * } | 32 | * } |
32 | * ) | 33 | * ) |
33 | * | 34 | * |
@@ -37,10 +38,29 @@ class WallabagRestController extends FOSRestController | |||
37 | { | 38 | { |
38 | $this->validateAuthentication(); | 39 | $this->validateAuthentication(); |
39 | 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? | ||
40 | $url = $request->query->get('url', ''); | 60 | $url = $request->query->get('url', ''); |
41 | 61 | ||
42 | if (empty($url)) { | 62 | if (empty($url)) { |
43 | throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId()); | 63 | throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId()); |
44 | } | 64 | } |
45 | 65 | ||
46 | $res = $this->getDoctrine() | 66 | $res = $this->getDoctrine() |
@@ -400,6 +420,8 @@ class WallabagRestController extends FOSRestController | |||
400 | ->getRepository('WallabagCoreBundle:Entry') | 420 | ->getRepository('WallabagCoreBundle:Entry') |
401 | ->removeTag($this->getUser()->getId(), $tag); | 421 | ->removeTag($this->getUser()->getId(), $tag); |
402 | 422 | ||
423 | $this->cleanOrphanTag($tag); | ||
424 | |||
403 | $json = $this->get('serializer')->serialize($tag, 'json'); | 425 | $json = $this->get('serializer')->serialize($tag, 'json'); |
404 | 426 | ||
405 | return (new JsonResponse())->setJson($json); | 427 | return (new JsonResponse())->setJson($json); |
@@ -440,6 +462,8 @@ class WallabagRestController extends FOSRestController | |||
440 | ->getRepository('WallabagCoreBundle:Entry') | 462 | ->getRepository('WallabagCoreBundle:Entry') |
441 | ->removeTags($this->getUser()->getId(), $tags); | 463 | ->removeTags($this->getUser()->getId(), $tags); |
442 | 464 | ||
465 | $this->cleanOrphanTag($tags); | ||
466 | |||
443 | $json = $this->get('serializer')->serialize($tags, 'json'); | 467 | $json = $this->get('serializer')->serialize($tags, 'json'); |
444 | 468 | ||
445 | return (new JsonResponse())->setJson($json); | 469 | return (new JsonResponse())->setJson($json); |
@@ -464,6 +488,8 @@ class WallabagRestController extends FOSRestController | |||
464 | ->getRepository('WallabagCoreBundle:Entry') | 488 | ->getRepository('WallabagCoreBundle:Entry') |
465 | ->removeTag($this->getUser()->getId(), $tag); | 489 | ->removeTag($this->getUser()->getId(), $tag); |
466 | 490 | ||
491 | $this->cleanOrphanTag($tag); | ||
492 | |||
467 | $json = $this->get('serializer')->serialize($tag, 'json'); | 493 | $json = $this->get('serializer')->serialize($tag, 'json'); |
468 | 494 | ||
469 | return (new JsonResponse())->setJson($json); | 495 | return (new JsonResponse())->setJson($json); |
@@ -486,6 +512,28 @@ class WallabagRestController extends FOSRestController | |||
486 | } | 512 | } |
487 | 513 | ||
488 | /** | 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 | /** | ||
489 | * Validate that the first id is equal to the second one. | 537 | * Validate that the first id is equal to the second one. |
490 | * If not, throw exception. It means a user try to access information from an other user. | 538 | * If not, throw exception. It means a user try to access information from an other user. |
491 | * | 539 | * |