3 namespace Wallabag\ApiBundle\Controller
;
5 use Hateoas\Configuration\Route
;
6 use Hateoas\Representation\Factory\PagerfantaFactory
;
7 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
8 use Symfony\Component\HttpFoundation\Request
;
9 use Symfony\Component\HttpFoundation\JsonResponse
;
10 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
11 use Wallabag\CoreBundle\Entity\Entry
;
12 use Wallabag\CoreBundle\Entity\Tag
;
13 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
14 use Wallabag\CoreBundle\Event\EntryDeletedEvent
;
16 class EntryRestController
extends WallabagRestController
19 * Check if an entry exist by url.
23 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
24 * {"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"}
28 * @return JsonResponse
30 public function getEntriesExistsAction(Request
$request)
32 $this->validateAuthentication();
34 $urls = $request->query
->get('urls', []);
36 // handle multiple urls first
39 foreach ($urls as $url) {
40 $res = $this->getDoctrine()
41 ->getRepository('WallabagCoreBundle:Entry')
42 ->findByUrlAndUserId($url, $this->getUser()->getId());
44 $results[$url] = $res instanceof Entry
? $res->getId() : false;
47 $json = $this->get('serializer')->serialize($results, 'json');
49 return (new JsonResponse())->setJson($json);
52 // let's see if it is a simple url?
53 $url = $request->query
->get('url', '');
56 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
59 $res = $this->getDoctrine()
60 ->getRepository('WallabagCoreBundle:Entry')
61 ->findByUrlAndUserId($url, $this->getUser()->getId());
63 $exists = $res instanceof Entry
? $res->getId() : false;
65 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
67 return (new JsonResponse())->setJson($json);
71 * Retrieve all entries. It could be filtered by many options.
75 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
76 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
77 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
78 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
79 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
80 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
81 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
82 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
86 * @return JsonResponse
88 public function getEntriesAction(Request
$request)
90 $this->validateAuthentication();
92 $isArchived = (null === $request->query
->get('archive')) ? null : (bool) $request->query
->get('archive');
93 $isStarred = (null === $request->query
->get('starred')) ? null : (bool) $request->query
->get('starred');
94 $sort = $request->query
->get('sort', 'created');
95 $order = $request->query
->get('order', 'desc');
96 $page = (int) $request->query
->get('page', 1);
97 $perPage = (int) $request->query
->get('perPage', 30);
98 $tags = $request->query
->get('tags', '');
99 $since = $request->query
->get('since', 0);
101 $pager = $this->getDoctrine()
102 ->getRepository('WallabagCoreBundle:Entry')
103 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
105 $pager->setCurrentPage($page);
106 $pager->setMaxPerPage($perPage);
108 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
109 $paginatedCollection = $pagerfantaFactory->createRepresentation(
114 'archive' => $isArchived,
115 'starred' => $isStarred,
119 'perPage' => $perPage,
123 UrlGeneratorInterface
::ABSOLUTE_URL
127 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
129 return (new JsonResponse())->setJson($json);
133 * Retrieve a single entry.
137 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
141 * @return JsonResponse
143 public function getEntryAction(Entry
$entry)
145 $this->validateAuthentication();
146 $this->validateUserAccess($entry->getUser()->getId());
148 $json = $this->get('serializer')->serialize($entry, 'json');
150 return (new JsonResponse())->setJson($json);
154 * Retrieve a single entry as a predefined format.
158 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
164 public function getEntryExportAction(Entry
$entry, Request
$request)
166 $this->validateAuthentication();
167 $this->validateUserAccess($entry->getUser()->getId());
169 return $this->get('wallabag_core.helper.entries_export')
171 ->updateTitle('entry')
172 ->exportAs($request->attributes
->get('_format'));
176 * Handles an entries list and delete URL.
180 * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."}
184 * @return JsonResponse
186 public function deleteEntriesListAction(Request
$request)
188 $this->validateAuthentication();
190 $urls = json_decode($request->query
->get('urls', []));
193 // handle multiple urls
196 foreach ($urls as $key => $url) {
197 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
199 $this->getUser()->getId()
202 $results[$key]['url'] = $url;
204 if (false !== $entry) {
205 $em = $this->getDoctrine()->getManager();
209 // entry deleted, dispatch event about it!
210 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
213 $results[$key]['entry'] = $entry instanceof Entry
? true : false;
217 $json = $this->get('serializer')->serialize($results, 'json');
219 return (new JsonResponse())->setJson($json);
223 * Handles an entries list and create URL.
227 * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."}
231 * @return JsonResponse
233 public function postEntriesListAction(Request
$request)
235 $this->validateAuthentication();
237 $urls = json_decode($request->query
->get('urls', []));
240 // handle multiple urls
242 foreach ($urls as $key => $url) {
243 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
245 $this->getUser()->getId()
248 $results[$key]['url'] = $url;
250 if (false === $entry) {
251 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
252 new Entry($this->getUser()),
257 $em = $this->getDoctrine()->getManager();
258 $em->persist($entry);
261 $results[$key]['entry'] = $entry instanceof Entry
? $entry->getId() : false;
263 // entry saved, dispatch event about it!
264 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
268 $json = $this->get('serializer')->serialize($results, 'json');
270 return (new JsonResponse())->setJson($json);
278 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
279 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
280 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
281 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
282 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
286 * @return JsonResponse
288 public function postEntriesAction(Request
$request)
290 $this->validateAuthentication();
292 $url = $request->request
->get('url');
293 $title = $request->request
->get('title');
294 $isArchived = $request->request
->get('archive');
295 $isStarred = $request->request
->get('starred');
297 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
299 if (false === $entry) {
300 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
301 new Entry($this->getUser()),
306 if (!is_null($title)) {
307 $entry->setTitle($title);
310 $tags = $request->request
->get('tags', '');
312 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
315 if (!is_null($isStarred)) {
316 $entry->setStarred((bool) $isStarred);
319 if (!is_null($isArchived)) {
320 $entry->setArchived((bool) $isArchived);
323 $em = $this->getDoctrine()->getManager();
324 $em->persist($entry);
327 // entry saved, dispatch event about it!
328 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
330 $json = $this->get('serializer')->serialize($entry, 'json');
332 return (new JsonResponse())->setJson($json);
336 * Change several properties of an entry.
340 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
343 * {"name"="title", "dataType"="string", "required"=false},
344 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
345 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
346 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
350 * @return JsonResponse
352 public function patchEntriesAction(Entry
$entry, Request
$request)
354 $this->validateAuthentication();
355 $this->validateUserAccess($entry->getUser()->getId());
357 $title = $request->request
->get('title');
358 $isArchived = $request->request
->get('archive');
359 $isStarred = $request->request
->get('starred');
361 if (!is_null($title)) {
362 $entry->setTitle($title);
365 if (!is_null($isArchived)) {
366 $entry->setArchived((bool) $isArchived);
369 if (!is_null($isStarred)) {
370 $entry->setStarred((bool) $isStarred);
373 $tags = $request->request
->get('tags', '');
375 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
378 $em = $this->getDoctrine()->getManager();
381 $json = $this->get('serializer')->serialize($entry, 'json');
383 return (new JsonResponse())->setJson($json);
388 * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error).
392 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
396 * @return JsonResponse
398 public function patchEntriesReloadAction(Entry
$entry)
400 $this->validateAuthentication();
401 $this->validateUserAccess($entry->getUser()->getId());
404 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
405 } catch (\Exception
$e) {
406 $this->get('logger')->error('Error while saving an entry', [
411 return new JsonResponse([], 304);
414 // if refreshing entry failed, don't save it
415 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
416 return new JsonResponse([], 304);
419 $em = $this->getDoctrine()->getManager();
420 $em->persist($entry);
423 // entry saved, dispatch event about it!
424 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
426 $json = $this->get('serializer')->serialize($entry, 'json');
428 return (new JsonResponse())->setJson($json);
432 * Delete **permanently** an entry.
436 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
440 * @return JsonResponse
442 public function deleteEntriesAction(Entry
$entry)
444 $this->validateAuthentication();
445 $this->validateUserAccess($entry->getUser()->getId());
447 $em = $this->getDoctrine()->getManager();
451 // entry deleted, dispatch event about it!
452 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
454 $json = $this->get('serializer')->serialize($entry, 'json');
456 return (new JsonResponse())->setJson($json);
460 * Retrieve all tags for an entry.
464 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
468 * @return JsonResponse
470 public function getEntriesTagsAction(Entry
$entry)
472 $this->validateAuthentication();
473 $this->validateUserAccess($entry->getUser()->getId());
475 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
477 return (new JsonResponse())->setJson($json);
481 * Add one or more tags to an entry.
485 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
488 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
492 * @return JsonResponse
494 public function postEntriesTagsAction(Request
$request, Entry
$entry)
496 $this->validateAuthentication();
497 $this->validateUserAccess($entry->getUser()->getId());
499 $tags = $request->request
->get('tags', '');
501 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
504 $em = $this->getDoctrine()->getManager();
505 $em->persist($entry);
508 $json = $this->get('serializer')->serialize($entry, 'json');
510 return (new JsonResponse())->setJson($json);
514 * Permanently remove one tag for an entry.
518 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
519 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
523 * @return JsonResponse
525 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
527 $this->validateAuthentication();
528 $this->validateUserAccess($entry->getUser()->getId());
530 $entry->removeTag($tag);
531 $em = $this->getDoctrine()->getManager();
532 $em->persist($entry);
535 $json = $this->get('serializer')->serialize($entry, 'json');
537 return (new JsonResponse())->setJson($json);
541 * Handles an entries list delete tags from them.
545 * {"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."}
549 * @return JsonResponse
551 public function deleteEntriesTagsListAction(Request
$request)
553 $this->validateAuthentication();
555 $list = json_decode($request->query
->get('list', []));
558 // handle multiple urls
560 foreach ($list as $key => $element) {
561 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
563 $this->getUser()->getId()
566 $results[$key]['url'] = $element->url
;
567 $results[$key]['entry'] = $entry instanceof Entry
? $entry->getId() : false;
569 $tags = $element->tags
;
571 if (false !== $entry && !(empty($tags))) {
572 $tags = explode(',', $tags);
573 foreach ($tags as $label) {
574 $label = trim($label);
576 $tag = $this->getDoctrine()
577 ->getRepository('WallabagCoreBundle:Tag')
578 ->findOneByLabel($label);
580 if (false !== $tag) {
581 $entry->removeTag($tag);
585 $em = $this->getDoctrine()->getManager();
586 $em->persist($entry);
592 $json = $this->get('serializer')->serialize($results, 'json');
594 return (new JsonResponse())->setJson($json);
598 * Handles an entries list and add tags to them.
602 * {"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."}
606 * @return JsonResponse
608 public function postEntriesTagsListAction(Request
$request)
610 $this->validateAuthentication();
612 $list = json_decode($request->query
->get('list', []));
615 // handle multiple urls
617 foreach ($list as $key => $element) {
618 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
620 $this->getUser()->getId()
623 $results[$key]['url'] = $element->url
;
624 $results[$key]['entry'] = $entry instanceof Entry
? $entry->getId() : false;
626 $tags = $element->tags
;
628 if (false !== $entry && !(empty($tags))) {
629 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
631 $em = $this->getDoctrine()->getManager();
632 $em->persist($entry);
638 $json = $this->get('serializer')->serialize($results, 'json');
640 return (new JsonResponse())->setJson($json);