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 $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'));
180 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
181 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
182 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
183 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
184 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
188 * @return JsonResponse
190 public function postEntriesAction(Request
$request)
192 $this->validateAuthentication();
194 $url = $request->request
->get('url');
195 $title = $request->request
->get('title');
196 $isArchived = $request->request
->get('archive');
197 $isStarred = $request->request
->get('starred');
199 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
201 if (false === $entry) {
202 $entry = new Entry($this->getUser());
204 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
208 } catch (\Exception
$e) {
209 $this->get('logger')->error('Error while saving an entry', [
213 $entry->setUrl($url);
217 if (!is_null($title)) {
218 $entry->setTitle($title);
221 $tags = $request->request
->get('tags', '');
223 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
226 if (!is_null($isStarred)) {
227 $entry->setStarred((bool) $isStarred);
230 if (!is_null($isArchived)) {
231 $entry->setArchived((bool) $isArchived);
234 $em = $this->getDoctrine()->getManager();
235 $em->persist($entry);
238 // entry saved, dispatch event about it!
239 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
241 $json = $this->get('serializer')->serialize($entry, 'json');
243 return (new JsonResponse())->setJson($json);
247 * Change several properties of an entry.
251 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
254 * {"name"="title", "dataType"="string", "required"=false},
255 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
256 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
257 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
261 * @return JsonResponse
263 public function patchEntriesAction(Entry
$entry, Request
$request)
265 $this->validateAuthentication();
266 $this->validateUserAccess($entry->getUser()->getId());
268 $title = $request->request
->get('title');
269 $isArchived = $request->request
->get('archive');
270 $isStarred = $request->request
->get('starred');
272 if (!is_null($title)) {
273 $entry->setTitle($title);
276 if (!is_null($isArchived)) {
277 $entry->setArchived((bool) $isArchived);
280 if (!is_null($isStarred)) {
281 $entry->setStarred((bool) $isStarred);
284 $tags = $request->request
->get('tags', '');
286 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
289 $em = $this->getDoctrine()->getManager();
292 $json = $this->get('serializer')->serialize($entry, 'json');
294 return (new JsonResponse())->setJson($json);
299 * 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).
303 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
307 * @return JsonResponse
309 public function patchEntriesReloadAction(Entry
$entry)
311 $this->validateAuthentication();
312 $this->validateUserAccess($entry->getUser()->getId());
315 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
316 } catch (\Exception
$e) {
317 $this->get('logger')->error('Error while saving an entry', [
322 return new JsonResponse([], 304);
325 // if refreshing entry failed, don't save it
326 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
327 return new JsonResponse([], 304);
330 $em = $this->getDoctrine()->getManager();
331 $em->persist($entry);
334 // entry saved, dispatch event about it!
335 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
337 $json = $this->get('serializer')->serialize($entry, 'json');
339 return (new JsonResponse())->setJson($json);
343 * Delete **permanently** an entry.
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
351 * @return JsonResponse
353 public function deleteEntriesAction(Entry
$entry)
355 $this->validateAuthentication();
356 $this->validateUserAccess($entry->getUser()->getId());
358 $em = $this->getDoctrine()->getManager();
362 // entry deleted, dispatch event about it!
363 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
365 $json = $this->get('serializer')->serialize($entry, 'json');
367 return (new JsonResponse())->setJson($json);
371 * Retrieve all tags for an entry.
375 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
379 * @return JsonResponse
381 public function getEntriesTagsAction(Entry
$entry)
383 $this->validateAuthentication();
384 $this->validateUserAccess($entry->getUser()->getId());
386 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
388 return (new JsonResponse())->setJson($json);
392 * Add one or more tags to an entry.
396 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
399 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
403 * @return JsonResponse
405 public function postEntriesTagsAction(Request
$request, Entry
$entry)
407 $this->validateAuthentication();
408 $this->validateUserAccess($entry->getUser()->getId());
410 $tags = $request->request
->get('tags', '');
412 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
415 $em = $this->getDoctrine()->getManager();
416 $em->persist($entry);
419 $json = $this->get('serializer')->serialize($entry, 'json');
421 return (new JsonResponse())->setJson($json);
425 * Permanently remove one tag for an entry.
429 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
430 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
434 * @return JsonResponse
436 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
438 $this->validateAuthentication();
439 $this->validateUserAccess($entry->getUser()->getId());
441 $entry->removeTag($tag);
442 $em = $this->getDoctrine()->getManager();
443 $em->persist($entry);
446 $json = $this->get('serializer')->serialize($entry, 'json');
448 return (new JsonResponse())->setJson($json);