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 = $this->get('wallabag_core.content_proxy')->updateEntry(
204 new Entry($this->getUser()),
209 if (!is_null($title)) {
210 $entry->setTitle($title);
213 $tags = $request->request
->get('tags', '');
215 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
218 if (!is_null($isStarred)) {
219 $entry->setStarred((bool) $isStarred);
222 if (!is_null($isArchived)) {
223 $entry->setArchived((bool) $isArchived);
226 $em = $this->getDoctrine()->getManager();
227 $em->persist($entry);
230 // entry saved, dispatch event about it!
231 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
233 $json = $this->get('serializer')->serialize($entry, 'json');
235 return (new JsonResponse())->setJson($json);
239 * Change several properties of an entry.
243 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
246 * {"name"="title", "dataType"="string", "required"=false},
247 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
248 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
249 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
253 * @return JsonResponse
255 public function patchEntriesAction(Entry
$entry, Request
$request)
257 $this->validateAuthentication();
258 $this->validateUserAccess($entry->getUser()->getId());
260 $title = $request->request
->get('title');
261 $isArchived = $request->request
->get('archive');
262 $isStarred = $request->request
->get('starred');
264 if (!is_null($title)) {
265 $entry->setTitle($title);
268 if (!is_null($isArchived)) {
269 $entry->setArchived((bool) $isArchived);
272 if (!is_null($isStarred)) {
273 $entry->setStarred((bool) $isStarred);
276 $tags = $request->request
->get('tags', '');
278 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
281 $em = $this->getDoctrine()->getManager();
284 $json = $this->get('serializer')->serialize($entry, 'json');
286 return (new JsonResponse())->setJson($json);
291 * 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).
295 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
299 * @return JsonResponse
301 public function patchEntriesReloadAction(Entry
$entry)
303 $this->validateAuthentication();
304 $this->validateUserAccess($entry->getUser()->getId());
307 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
308 } catch (\Exception
$e) {
309 $this->get('logger')->error('Error while saving an entry', [
314 return new JsonResponse([], 304);
317 // if refreshing entry failed, don't save it
318 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
319 return new JsonResponse([], 304);
322 $em = $this->getDoctrine()->getManager();
323 $em->persist($entry);
326 // entry saved, dispatch event about it!
327 $this->get('event_dispatcher')->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
329 $json = $this->get('serializer')->serialize($entry, 'json');
331 return (new JsonResponse())->setJson($json);
335 * Delete **permanently** an entry.
339 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
343 * @return JsonResponse
345 public function deleteEntriesAction(Entry
$entry)
347 $this->validateAuthentication();
348 $this->validateUserAccess($entry->getUser()->getId());
350 $em = $this->getDoctrine()->getManager();
354 // entry deleted, dispatch event about it!
355 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent
::NAME
, new EntryDeletedEvent($entry));
357 $json = $this->get('serializer')->serialize($entry, 'json');
359 return (new JsonResponse())->setJson($json);
363 * Retrieve all tags for an entry.
367 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
371 * @return JsonResponse
373 public function getEntriesTagsAction(Entry
$entry)
375 $this->validateAuthentication();
376 $this->validateUserAccess($entry->getUser()->getId());
378 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
380 return (new JsonResponse())->setJson($json);
384 * Add one or more tags to an entry.
388 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
391 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
395 * @return JsonResponse
397 public function postEntriesTagsAction(Request
$request, Entry
$entry)
399 $this->validateAuthentication();
400 $this->validateUserAccess($entry->getUser()->getId());
402 $tags = $request->request
->get('tags', '');
404 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
407 $em = $this->getDoctrine()->getManager();
408 $em->persist($entry);
411 $json = $this->get('serializer')->serialize($entry, 'json');
413 return (new JsonResponse())->setJson($json);
417 * Permanently remove one tag for an entry.
421 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
422 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
426 * @return JsonResponse
428 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
430 $this->validateAuthentication();
431 $this->validateUserAccess($entry->getUser()->getId());
433 $entry->removeTag($tag);
434 $em = $this->getDoctrine()->getManager();
435 $em->persist($entry);
438 $json = $this->get('serializer')->serialize($entry, 'json');
440 return (new JsonResponse())->setJson($json);