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] = false === $res ? false : true;
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 = false === $res ? false : true;
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 /** @var \Pagerfanta\Pagerfanta $pager */
102 $pager = $this->getDoctrine()
103 ->getRepository('WallabagCoreBundle:Entry')
104 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
106 $pager->setMaxPerPage($perPage);
107 $pager->setCurrentPage($page);
109 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
110 $paginatedCollection = $pagerfantaFactory->createRepresentation(
115 'archive' => $isArchived,
116 'starred' => $isStarred,
120 'perPage' => $perPage,
124 UrlGeneratorInterface
::ABSOLUTE_URL
128 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
130 return (new JsonResponse())->setJson($json);
134 * Retrieve a single entry.
138 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
142 * @return JsonResponse
144 public function getEntryAction(Entry
$entry)
146 $this->validateAuthentication();
147 $this->validateUserAccess($entry->getUser()->getId());
149 $json = $this->get('serializer')->serialize($entry, 'json');
151 return (new JsonResponse())->setJson($json);
155 * Retrieve a single entry as a predefined format.
159 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
165 public function getEntryExportAction(Entry
$entry, Request
$request)
167 $this->validateAuthentication();
168 $this->validateUserAccess($entry->getUser()->getId());
170 return $this->get('wallabag_core.helper.entries_export')
172 ->updateTitle('entry')
173 ->exportAs($request->attributes
->get('_format'));
181 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
182 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
183 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
184 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
185 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
189 * @return JsonResponse
191 public function postEntriesAction(Request
$request)
193 $this->validateAuthentication();
195 $url = $request->request
->get('url');
196 $title = $request->request
->get('title');
197 $isArchived = $request->request
->get('archive');
198 $isStarred = $request->request
->get('starred');
200 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
202 if (false === $entry) {
203 $entry = new Entry($this->getUser());
205 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
209 } catch (\Exception
$e) {
210 $this->get('logger')->error('Error while saving an entry', [
214 $entry->setUrl($url);
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);
239 // entry saved, dispatch event about it!
240 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
242 $json = $this->get('serializer')->serialize($entry, 'json');
244 return (new JsonResponse())->setJson($json);
248 * Change several properties of an entry.
252 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
255 * {"name"="title", "dataType"="string", "required"=false},
256 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
257 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
258 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
262 * @return JsonResponse
264 public function patchEntriesAction(Entry
$entry, Request
$request)
266 $this->validateAuthentication();
267 $this->validateUserAccess($entry->getUser()->getId());
269 $title = $request->request
->get('title');
270 $isArchived = $request->request
->get('archive');
271 $isStarred = $request->request
->get('starred');
273 if (!is_null($title)) {
274 $entry->setTitle($title);
277 if (!is_null($isArchived)) {
278 $entry->setArchived((bool) $isArchived);
281 if (!is_null($isStarred)) {
282 $entry->setStarred((bool) $isStarred);
285 $tags = $request->request
->get('tags', '');
287 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
290 $em = $this->getDoctrine()->getManager();
293 $json = $this->get('serializer')->serialize($entry, 'json');
295 return (new JsonResponse())->setJson($json);
300 * 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).
304 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
308 * @return JsonResponse
310 public function patchEntriesReloadAction(Entry
$entry)
312 $this->validateAuthentication();
313 $this->validateUserAccess($entry->getUser()->getId());
316 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
317 } catch (\Exception
$e) {
318 $this->get('logger')->error('Error while saving an entry', [
323 return new JsonResponse([], 304);
326 // if refreshing entry failed, don't save it
327 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
328 return new JsonResponse([], 304);
331 $em = $this->getDoctrine()->getManager();
332 $em->persist($entry);
335 // entry saved, dispatch event about it!
336 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
338 $json = $this->get('serializer')->serialize($entry, 'json');
340 return (new JsonResponse())->setJson($json);
344 * Delete **permanently** an entry.
348 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
352 * @return JsonResponse
354 public function deleteEntriesAction(Entry
$entry)
356 $this->validateAuthentication();
357 $this->validateUserAccess($entry->getUser()->getId());
359 $em = $this->getDoctrine()->getManager();
363 // entry deleted, dispatch event about it!
364 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
366 $json = $this->get('serializer')->serialize($entry, 'json');
368 return (new JsonResponse())->setJson($json);
372 * Retrieve all tags for an entry.
376 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
380 * @return JsonResponse
382 public function getEntriesTagsAction(Entry
$entry)
384 $this->validateAuthentication();
385 $this->validateUserAccess($entry->getUser()->getId());
387 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
389 return (new JsonResponse())->setJson($json);
393 * Add one or more tags to an entry.
397 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
400 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
404 * @return JsonResponse
406 public function postEntriesTagsAction(Request
$request, Entry
$entry)
408 $this->validateAuthentication();
409 $this->validateUserAccess($entry->getUser()->getId());
411 $tags = $request->request
->get('tags', '');
413 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
416 $em = $this->getDoctrine()->getManager();
417 $em->persist($entry);
420 $json = $this->get('serializer')->serialize($entry, 'json');
422 return (new JsonResponse())->setJson($json);
426 * Permanently remove one tag for an entry.
430 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
431 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
435 * @return JsonResponse
437 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
439 $this->validateAuthentication();
440 $this->validateUserAccess($entry->getUser()->getId());
442 $entry->removeTag($tag);
443 $em = $this->getDoctrine()->getManager();
444 $em->persist($entry);
447 $json = $this->get('serializer')->serialize($entry, 'json');
449 return (new JsonResponse())->setJson($json);