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
243 foreach ($urls as $key => $url) {
244 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
246 $this->getUser()->getId()
249 $results[$key]['url'] = $url;
251 if (false === $entry) {
252 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
253 new Entry($this->getUser()),
258 $em = $this->getDoctrine()->getManager();
259 $em->persist($entry);
262 $results[$key]['entry'] = $entry instanceof Entry
? $entry->getId() : false;
264 // entry saved, dispatch event about it!
265 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
269 $json = $this->get('serializer')->serialize($results, 'json');
271 return (new JsonResponse())->setJson($json);
279 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
280 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
281 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
282 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
283 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
287 * @return JsonResponse
289 public function postEntriesAction(Request
$request)
291 $this->validateAuthentication();
293 $url = $request->request
->get('url');
294 $title = $request->request
->get('title');
295 $isArchived = $request->request
->get('archive');
296 $isStarred = $request->request
->get('starred');
298 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
300 if (false === $entry) {
301 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
302 new Entry($this->getUser()),
307 if (!is_null($title)) {
308 $entry->setTitle($title);
311 $tags = $request->request
->get('tags', '');
313 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
316 if (!is_null($isStarred)) {
317 $entry->setStarred((bool) $isStarred);
320 if (!is_null($isArchived)) {
321 $entry->setArchived((bool) $isArchived);
324 $em = $this->getDoctrine()->getManager();
325 $em->persist($entry);
328 // entry saved, dispatch event about it!
329 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
331 $json = $this->get('serializer')->serialize($entry, 'json');
333 return (new JsonResponse())->setJson($json);
337 * Change several properties of an entry.
341 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
344 * {"name"="title", "dataType"="string", "required"=false},
345 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
346 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
347 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
351 * @return JsonResponse
353 public function patchEntriesAction(Entry
$entry, Request
$request)
355 $this->validateAuthentication();
356 $this->validateUserAccess($entry->getUser()->getId());
358 $title = $request->request
->get('title');
359 $isArchived = $request->request
->get('archive');
360 $isStarred = $request->request
->get('starred');
362 if (!is_null($title)) {
363 $entry->setTitle($title);
366 if (!is_null($isArchived)) {
367 $entry->setArchived((bool) $isArchived);
370 if (!is_null($isStarred)) {
371 $entry->setStarred((bool) $isStarred);
374 $tags = $request->request
->get('tags', '');
376 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
379 $em = $this->getDoctrine()->getManager();
382 $json = $this->get('serializer')->serialize($entry, 'json');
384 return (new JsonResponse())->setJson($json);
389 * 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).
393 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
397 * @return JsonResponse
399 public function patchEntriesReloadAction(Entry
$entry)
401 $this->validateAuthentication();
402 $this->validateUserAccess($entry->getUser()->getId());
405 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
406 } catch (\Exception
$e) {
407 $this->get('logger')->error('Error while saving an entry', [
412 return new JsonResponse([], 304);
415 // if refreshing entry failed, don't save it
416 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
417 return new JsonResponse([], 304);
420 $em = $this->getDoctrine()->getManager();
421 $em->persist($entry);
424 // entry saved, dispatch event about it!
425 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
427 $json = $this->get('serializer')->serialize($entry, 'json');
429 return (new JsonResponse())->setJson($json);
433 * Delete **permanently** an entry.
437 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
441 * @return JsonResponse
443 public function deleteEntriesAction(Entry
$entry)
445 $this->validateAuthentication();
446 $this->validateUserAccess($entry->getUser()->getId());
448 $em = $this->getDoctrine()->getManager();
452 // entry deleted, dispatch event about it!
453 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
455 $json = $this->get('serializer')->serialize($entry, 'json');
457 return (new JsonResponse())->setJson($json);
461 * Retrieve all tags for an entry.
465 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
469 * @return JsonResponse
471 public function getEntriesTagsAction(Entry
$entry)
473 $this->validateAuthentication();
474 $this->validateUserAccess($entry->getUser()->getId());
476 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
478 return (new JsonResponse())->setJson($json);
482 * Add one or more tags to an entry.
486 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
489 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
493 * @return JsonResponse
495 public function postEntriesTagsAction(Request
$request, Entry
$entry)
497 $this->validateAuthentication();
498 $this->validateUserAccess($entry->getUser()->getId());
500 $tags = $request->request
->get('tags', '');
502 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
505 $em = $this->getDoctrine()->getManager();
506 $em->persist($entry);
509 $json = $this->get('serializer')->serialize($entry, 'json');
511 return (new JsonResponse())->setJson($json);
515 * Permanently remove one tag for an entry.
519 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
520 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
524 * @return JsonResponse
526 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
528 $this->validateAuthentication();
529 $this->validateUserAccess($entry->getUser()->getId());
531 $entry->removeTag($tag);
532 $em = $this->getDoctrine()->getManager();
533 $em->persist($entry);
536 $json = $this->get('serializer')->serialize($entry, 'json');
538 return (new JsonResponse())->setJson($json);
542 * Handles an entries list delete tags from them.
546 * {"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."}
550 * @return JsonResponse
552 public function deleteEntriesTagsListAction(Request
$request)
554 $this->validateAuthentication();
556 $list = json_decode($request->query
->get('list', []));
559 // handle multiple urls
561 foreach ($list as $key => $element) {
562 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
564 $this->getUser()->getId()
567 $results[$key]['url'] = $element->url
;
568 $results[$key]['entry'] = $entry instanceof Entry
? $entry->getId() : false;
570 $tags = $element->tags
;
572 if (false !== $entry && !(empty($tags))) {
573 $tags = explode(',', $tags);
574 foreach ($tags as $label) {
575 $label = trim($label);
577 $tag = $this->getDoctrine()
578 ->getRepository('WallabagCoreBundle:Tag')
579 ->findOneByLabel($label);
581 if (false !== $tag) {
582 $entry->removeTag($tag);
586 $em = $this->getDoctrine()->getManager();
587 $em->persist($entry);
593 $json = $this->get('serializer')->serialize($results, 'json');
595 return (new JsonResponse())->setJson($json);
599 * Handles an entries list and add tags to them.
603 * {"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."}
607 * @return JsonResponse
609 public function postEntriesTagsListAction(Request
$request)
611 $this->validateAuthentication();
613 $list = json_decode($request->query
->get('list', []));
616 // handle multiple urls
618 foreach ($list as $key => $element) {
619 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
621 $this->getUser()->getId()
624 $results[$key]['url'] = $element->url
;
625 $results[$key]['entry'] = $entry instanceof Entry
? $entry->getId() : false;
627 $tags = $element->tags
;
629 if (false !== $entry && !(empty($tags))) {
630 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
632 $em = $this->getDoctrine()->getManager();
633 $em->persist($entry);
639 $json = $this->get('serializer')->serialize($results, 'json');
641 return (new JsonResponse())->setJson($json);