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
;
14 class EntryRestController
extends WallabagRestController
17 * Check if an entry exist by url.
21 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"},
22 * {"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"}
26 * @return JsonResponse
28 public function getEntriesExistsAction(Request
$request)
30 $this->validateAuthentication();
32 $urls = $request->query
->get('urls', []);
34 // handle multiple urls first
37 foreach ($urls as $url) {
38 $res = $this->getDoctrine()
39 ->getRepository('WallabagCoreBundle:Entry')
40 ->findByUrlAndUserId($url, $this->getUser()->getId());
42 $results[$url] = false === $res ? false : true;
45 $json = $this->get('serializer')->serialize($results, 'json');
47 return (new JsonResponse())->setJson($json);
50 // let's see if it is a simple url?
51 $url = $request->query
->get('url', '');
54 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId());
57 $res = $this->getDoctrine()
58 ->getRepository('WallabagCoreBundle:Entry')
59 ->findByUrlAndUserId($url, $this->getUser()->getId());
61 $exists = false === $res ? false : true;
63 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
65 return (new JsonResponse())->setJson($json);
69 * Retrieve all entries. It could be filtered by many options.
73 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
74 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
75 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
76 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
77 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
78 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
79 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
80 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
84 * @return JsonResponse
86 public function getEntriesAction(Request
$request)
88 $this->validateAuthentication();
90 $isArchived = (null === $request->query
->get('archive')) ? null : (bool) $request->query
->get('archive');
91 $isStarred = (null === $request->query
->get('starred')) ? null : (bool) $request->query
->get('starred');
92 $sort = $request->query
->get('sort', 'created');
93 $order = $request->query
->get('order', 'desc');
94 $page = (int) $request->query
->get('page', 1);
95 $perPage = (int) $request->query
->get('perPage', 30);
96 $tags = $request->query
->get('tags', '');
97 $since = $request->query
->get('since', 0);
99 $pager = $this->getDoctrine()
100 ->getRepository('WallabagCoreBundle:Entry')
101 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
103 $pager->setCurrentPage($page);
104 $pager->setMaxPerPage($perPage);
106 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
107 $paginatedCollection = $pagerfantaFactory->createRepresentation(
112 'archive' => $isArchived,
113 'starred' => $isStarred,
117 'perPage' => $perPage,
121 UrlGeneratorInterface
::ABSOLUTE_URL
125 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
127 return (new JsonResponse())->setJson($json);
131 * Retrieve a single entry.
135 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
139 * @return JsonResponse
141 public function getEntryAction(Entry
$entry)
143 $this->validateAuthentication();
144 $this->validateUserAccess($entry->getUser()->getId());
146 $json = $this->get('serializer')->serialize($entry, 'json');
148 return (new JsonResponse())->setJson($json);
156 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
157 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
158 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
159 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
160 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
164 * @return JsonResponse
166 public function postEntriesAction(Request
$request)
168 $this->validateAuthentication();
170 $url = $request->request
->get('url');
171 $title = $request->request
->get('title');
172 $isArchived = $request->request
->get('archive');
173 $isStarred = $request->request
->get('starred');
175 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
177 if (false === $entry) {
178 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
179 new Entry($this->getUser()),
184 if (!is_null($title)) {
185 $entry->setTitle($title);
188 $tags = $request->request
->get('tags', '');
190 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
193 if (!is_null($isStarred)) {
194 $entry->setStarred((bool) $isStarred);
197 if (!is_null($isArchived)) {
198 $entry->setArchived((bool) $isArchived);
201 $em = $this->getDoctrine()->getManager();
202 $em->persist($entry);
206 $json = $this->get('serializer')->serialize($entry, 'json');
208 return (new JsonResponse())->setJson($json);
212 * Change several properties of an entry.
216 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
219 * {"name"="title", "dataType"="string", "required"=false},
220 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
221 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
222 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
226 * @return JsonResponse
228 public function patchEntriesAction(Entry
$entry, Request
$request)
230 $this->validateAuthentication();
231 $this->validateUserAccess($entry->getUser()->getId());
233 $title = $request->request
->get('title');
234 $isArchived = $request->request
->get('archive');
235 $isStarred = $request->request
->get('starred');
237 if (!is_null($title)) {
238 $entry->setTitle($title);
241 if (!is_null($isArchived)) {
242 $entry->setArchived((bool) $isArchived);
245 if (!is_null($isStarred)) {
246 $entry->setStarred((bool) $isStarred);
249 $tags = $request->request
->get('tags', '');
251 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
254 $em = $this->getDoctrine()->getManager();
257 $json = $this->get('serializer')->serialize($entry, 'json');
259 return (new JsonResponse())->setJson($json);
263 * Delete **permanently** an entry.
267 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
271 * @return JsonResponse
273 public function deleteEntriesAction(Entry
$entry)
275 $this->validateAuthentication();
276 $this->validateUserAccess($entry->getUser()->getId());
278 $em = $this->getDoctrine()->getManager();
282 $json = $this->get('serializer')->serialize($entry, 'json');
284 return (new JsonResponse())->setJson($json);
288 * Retrieve all tags for an entry.
292 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
296 * @return JsonResponse
298 public function getEntriesTagsAction(Entry
$entry)
300 $this->validateAuthentication();
301 $this->validateUserAccess($entry->getUser()->getId());
303 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
305 return (new JsonResponse())->setJson($json);
309 * Add one or more tags to an entry.
313 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
316 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
320 * @return JsonResponse
322 public function postEntriesTagsAction(Request
$request, Entry
$entry)
324 $this->validateAuthentication();
325 $this->validateUserAccess($entry->getUser()->getId());
327 $tags = $request->request
->get('tags', '');
329 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
332 $em = $this->getDoctrine()->getManager();
333 $em->persist($entry);
336 $json = $this->get('serializer')->serialize($entry, 'json');
338 return (new JsonResponse())->setJson($json);
342 * Permanently remove one tag for an entry.
346 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
347 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
351 * @return JsonResponse
353 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
355 $this->validateAuthentication();
356 $this->validateUserAccess($entry->getUser()->getId());
358 $entry->removeTag($tag);
359 $em = $this->getDoctrine()->getManager();
360 $em->persist($entry);
363 $json = $this->get('serializer')->serialize($entry, 'json');
365 return (new JsonResponse())->setJson($json);