3 namespace Wallabag\ApiBundle\Controller
;
5 use FOS\RestBundle\Controller\FOSRestController
;
6 use Hateoas\Configuration\Route
as HateoasRoute
;
7 use Hateoas\Representation\Factory\PagerfantaFactory
;
8 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Symfony\Component\HttpFoundation\JsonResponse
;
12 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
13 use Symfony\Component\Security\Core\Exception\AccessDeniedException
;
14 use Wallabag\CoreBundle\Entity\Entry
;
15 use Wallabag\CoreBundle\Entity\Tag
;
16 use Wallabag\AnnotationBundle\Entity\Annotation
;
17 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
19 class WallabagRestController
extends FOSRestController
21 private function validateAuthentication()
23 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
24 throw new AccessDeniedException();
29 * Check if an entry exist by url.
33 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
34 * {"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"}
38 * @return JsonResponse
40 public function getEntriesExistsAction(Request
$request)
42 $this->validateAuthentication();
44 $urls = $request->query
->get('urls', []);
46 // handle multiple urls first
49 foreach ($urls as $url) {
50 $res = $this->getDoctrine()
51 ->getRepository('WallabagCoreBundle:Entry')
52 ->findByUrlAndUserId($url, $this->getUser()->getId());
54 $results[$url] = false === $res ? false : true;
57 $json = $this->get('serializer')->serialize($results, 'json');
59 return (new JsonResponse())->setJson($json);
62 // let's see if it is a simple url?
63 $url = $request->query
->get('url', '');
66 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
69 $res = $this->getDoctrine()
70 ->getRepository('WallabagCoreBundle:Entry')
71 ->findByUrlAndUserId($url, $this->getUser()->getId());
73 $exists = false === $res ? false : true;
75 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
77 return (new JsonResponse())->setJson($json);
81 * Retrieve all entries. It could be filtered by many options.
85 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
86 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
87 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
88 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
89 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
90 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
91 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
92 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
96 * @return JsonResponse
98 public function getEntriesAction(Request
$request)
100 $this->validateAuthentication();
102 $isArchived = (null === $request->query
->get('archive')) ? null : (bool) $request->query
->get('archive');
103 $isStarred = (null === $request->query
->get('starred')) ? null : (bool) $request->query
->get('starred');
104 $sort = $request->query
->get('sort', 'created');
105 $order = $request->query
->get('order', 'desc');
106 $page = (int) $request->query
->get('page', 1);
107 $perPage = (int) $request->query
->get('perPage', 30);
108 $tags = $request->query
->get('tags', '');
109 $since = $request->query
->get('since', 0);
111 $pager = $this->getDoctrine()
112 ->getRepository('WallabagCoreBundle:Entry')
113 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
115 $pager->setCurrentPage($page);
116 $pager->setMaxPerPage($perPage);
118 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
119 $paginatedCollection = $pagerfantaFactory->createRepresentation(
124 'archive' => $isArchived,
125 'starred' => $isStarred,
129 'perPage' => $perPage,
133 UrlGeneratorInterface
::ABSOLUTE_URL
137 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
139 return (new JsonResponse())->setJson($json);
143 * Retrieve a single entry.
147 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
151 * @return JsonResponse
153 public function getEntryAction(Entry
$entry)
155 $this->validateAuthentication();
156 $this->validateUserAccess($entry->getUser()->getId());
158 $json = $this->get('serializer')->serialize($entry, 'json');
160 return (new JsonResponse())->setJson($json);
164 * Retrieve a single entry as a predefined format.
168 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
174 public function getEntryExportAction(Entry
$entry, Request
$request)
176 $this->validateAuthentication();
177 $this->validateUserAccess($entry->getUser()->getId());
179 return $this->get('wallabag_core.helper.entries_export')
181 ->updateTitle('entry')
182 ->exportAs($request->attributes
->get('_format'));
190 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
191 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
192 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
193 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
194 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
198 * @return JsonResponse
200 public function postEntriesAction(Request
$request)
202 $this->validateAuthentication();
204 $url = $request->request
->get('url');
205 $title = $request->request
->get('title');
206 $isArchived = $request->request
->get('archive');
207 $isStarred = $request->request
->get('starred');
209 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
211 if (false === $entry) {
212 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
213 new Entry($this->getUser()),
218 if (!is_null($title)) {
219 $entry->setTitle($title);
222 $tags = $request->request
->get('tags', '');
224 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
227 if (!is_null($isStarred)) {
228 $entry->setStarred((bool) $isStarred);
231 if (!is_null($isArchived)) {
232 $entry->setArchived((bool) $isArchived);
235 $em = $this->getDoctrine()->getManager();
236 $em->persist($entry);
240 $json = $this->get('serializer')->serialize($entry, 'json');
242 return (new JsonResponse())->setJson($json);
246 * Change several properties of an entry.
250 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
253 * {"name"="title", "dataType"="string", "required"=false},
254 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
255 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
256 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
260 * @return JsonResponse
262 public function patchEntriesAction(Entry
$entry, Request
$request)
264 $this->validateAuthentication();
265 $this->validateUserAccess($entry->getUser()->getId());
267 $title = $request->request
->get('title');
268 $isArchived = $request->request
->get('archive');
269 $isStarred = $request->request
->get('starred');
271 if (!is_null($title)) {
272 $entry->setTitle($title);
275 if (!is_null($isArchived)) {
276 $entry->setArchived((bool) $isArchived);
279 if (!is_null($isStarred)) {
280 $entry->setStarred((bool) $isStarred);
283 $tags = $request->request
->get('tags', '');
285 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
288 $em = $this->getDoctrine()->getManager();
291 $json = $this->get('serializer')->serialize($entry, 'json');
293 return (new JsonResponse())->setJson($json);
297 * Delete **permanently** an entry.
301 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
305 * @return JsonResponse
307 public function deleteEntriesAction(Entry
$entry)
309 $this->validateAuthentication();
310 $this->validateUserAccess($entry->getUser()->getId());
312 $em = $this->getDoctrine()->getManager();
316 $json = $this->get('serializer')->serialize($entry, 'json');
318 return (new JsonResponse())->setJson($json);
322 * Retrieve all tags for an entry.
326 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
330 * @return JsonResponse
332 public function getEntriesTagsAction(Entry
$entry)
334 $this->validateAuthentication();
335 $this->validateUserAccess($entry->getUser()->getId());
337 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
339 return (new JsonResponse())->setJson($json);
343 * Add one or more tags to an entry.
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
350 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
354 * @return JsonResponse
356 public function postEntriesTagsAction(Request
$request, Entry
$entry)
358 $this->validateAuthentication();
359 $this->validateUserAccess($entry->getUser()->getId());
361 $tags = $request->request
->get('tags', '');
363 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
366 $em = $this->getDoctrine()->getManager();
367 $em->persist($entry);
370 $json = $this->get('serializer')->serialize($entry, 'json');
372 return (new JsonResponse())->setJson($json);
376 * Permanently remove one tag for an entry.
380 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
381 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
385 * @return JsonResponse
387 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
389 $this->validateAuthentication();
390 $this->validateUserAccess($entry->getUser()->getId());
392 $entry->removeTag($tag);
393 $em = $this->getDoctrine()->getManager();
394 $em->persist($entry);
397 $json = $this->get('serializer')->serialize($entry, 'json');
399 return (new JsonResponse())->setJson($json);
407 * @return JsonResponse
409 public function getTagsAction()
411 $this->validateAuthentication();
413 $tags = $this->getDoctrine()
414 ->getRepository('WallabagCoreBundle:Tag')
415 ->findAllTags($this->getUser()->getId());
417 $json = $this->get('serializer')->serialize($tags, 'json');
419 return (new JsonResponse())->setJson($json);
423 * Permanently remove one tag from **every** entry.
427 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
431 * @return JsonResponse
433 public function deleteTagLabelAction(Request
$request)
435 $this->validateAuthentication();
436 $label = $request->request
->get('tag', '');
438 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
441 throw $this->createNotFoundException('Tag not found');
445 ->getRepository('WallabagCoreBundle:Entry')
446 ->removeTag($this->getUser()->getId(), $tag);
448 $this->cleanOrphanTag($tag);
450 $json = $this->get('serializer')->serialize($tag, 'json');
452 return (new JsonResponse())->setJson($json);
456 * Permanently remove some tags from **every** entry.
460 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
464 * @return JsonResponse
466 public function deleteTagsLabelAction(Request
$request)
468 $this->validateAuthentication();
470 $tagsLabels = $request->request
->get('tags', '');
474 foreach (explode(',', $tagsLabels) as $tagLabel) {
475 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
477 if (!empty($tagEntity)) {
478 $tags[] = $tagEntity;
483 throw $this->createNotFoundException('Tags not found');
487 ->getRepository('WallabagCoreBundle:Entry')
488 ->removeTags($this->getUser()->getId(), $tags);
490 $this->cleanOrphanTag($tags);
492 $json = $this->get('serializer')->serialize($tags, 'json');
494 return (new JsonResponse())->setJson($json);
498 * Permanently remove one tag from **every** entry.
502 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
506 * @return JsonResponse
508 public function deleteTagAction(Tag
$tag)
510 $this->validateAuthentication();
513 ->getRepository('WallabagCoreBundle:Entry')
514 ->removeTag($this->getUser()->getId(), $tag);
516 $this->cleanOrphanTag($tag);
518 $json = $this->get('serializer')->serialize($tag, 'json');
520 return (new JsonResponse())->setJson($json);
524 * Retrieve annotations for an entry.
528 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
532 * @param Entry $entry
534 * @return JsonResponse
536 public function getAnnotationsAction(Entry
$entry)
538 $this->validateAuthentication();
540 return $this->forward('WallabagApiBundle:WallabagRest:getAnnotations', [
546 * Creates a new annotation.
548 * @param Request $request
549 * @param Entry $entry
551 * @return JsonResponse
554 * {"name"="ranges", "dataType"="array", "requirement"="\w+", "description"="The range array for the annotation"},
555 * {"name"="quote", "dataType"="string", "required"=false, "description"="Optional, quote for the annotation"},
556 * {"name"="text", "dataType"="string", "required"=true, "description"=""},
560 public function postAnnotationAction(Request
$request, Entry
$entry)
562 $this->validateAuthentication();
564 return $this->forward('WallabagApiBundle:WallabagRest:postAnnotation', [
565 'request' => $request,
571 * Updates an annotation.
575 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
579 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
581 * @param Annotation $annotation
582 * @param Request $request
584 * @return JsonResponse
586 public function putAnnotationAction(Annotation
$annotation, Request
$request)
588 $this->validateAuthentication();
590 return $this->forward('WallabagApiBundle:WallabagRest:putAnnotation', [
591 'annotation' => $annotation,
592 'request' => $request,
597 * Removes an annotation.
601 * {"name"="annotation", "dataType"="string", "requirement"="\w+", "description"="The annotation ID"}
605 * @ParamConverter("annotation", class="WallabagAnnotationBundle:Annotation")
607 * @param Annotation $annotation
609 * @return JsonResponse
611 public function deleteAnnotationAction(Annotation
$annotation)
613 $this->validateAuthentication();
615 return $this->forward('WallabagApiBundle:WallabagRest:deleteAnnotation', [
616 'annotation' => $annotation,
621 * Retrieve version number.
625 * @return JsonResponse
627 public function getVersionAction()
629 $version = $this->container
->getParameter('wallabag_core.version');
631 $json = $this->get('serializer')->serialize($version, 'json');
633 return (new JsonResponse())->setJson($json);
637 * Remove orphan tag in case no entries are associated to it.
639 * @param Tag|array $tags
641 private function cleanOrphanTag($tags)
643 if (!is_array($tags)) {
647 $em = $this->getDoctrine()->getManager();
649 foreach ($tags as $tag) {
650 if (count($tag->getEntries()) === 0) {
659 * Validate that the first id is equal to the second one.
660 * If not, throw exception. It means a user try to access information from an other user.
662 * @param int $requestUserId User id from the requested source
664 private function validateUserAccess($requestUserId)
666 $user = $this->get('security.token_storage')->getToken()->getUser();
667 if ($requestUserId != $user->getId()) {
668 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());