]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
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; | |
5a619812 JB |
13 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
14 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
900c8448 NL |
15 | |
16 | class EntryRestController extends WallabagRestController | |
17 | { | |
18 | /** | |
19 | * Check if an entry exist by url. | |
20 | * | |
21 | * @ApiDoc( | |
22 | * parameters={ | |
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"} | |
25 | * } | |
26 | * ) | |
27 | * | |
28 | * @return JsonResponse | |
29 | */ | |
30 | public function getEntriesExistsAction(Request $request) | |
31 | { | |
32 | $this->validateAuthentication(); | |
33 | ||
34 | $urls = $request->query->get('urls', []); | |
35 | ||
36 | // handle multiple urls first | |
37 | if (!empty($urls)) { | |
38 | $results = []; | |
39 | foreach ($urls as $url) { | |
40 | $res = $this->getDoctrine() | |
41 | ->getRepository('WallabagCoreBundle:Entry') | |
42 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
43 | ||
44 | $results[$url] = false === $res ? false : true; | |
45 | } | |
46 | ||
47 | $json = $this->get('serializer')->serialize($results, 'json'); | |
48 | ||
49 | return (new JsonResponse())->setJson($json); | |
50 | } | |
51 | ||
52 | // let's see if it is a simple url? | |
53 | $url = $request->query->get('url', ''); | |
54 | ||
55 | if (empty($url)) { | |
56 | throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId()); | |
57 | } | |
58 | ||
59 | $res = $this->getDoctrine() | |
60 | ->getRepository('WallabagCoreBundle:Entry') | |
61 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
62 | ||
63 | $exists = false === $res ? false : true; | |
64 | ||
65 | $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); | |
66 | ||
67 | return (new JsonResponse())->setJson($json); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Retrieve all entries. It could be filtered by many options. | |
72 | * | |
73 | * @ApiDoc( | |
74 | * parameters={ | |
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."}, | |
83 | * } | |
84 | * ) | |
85 | * | |
86 | * @return JsonResponse | |
87 | */ | |
88 | public function getEntriesAction(Request $request) | |
89 | { | |
90 | $this->validateAuthentication(); | |
91 | ||
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); | |
100 | ||
b60a666d | 101 | /** @var \Pagerfanta\Pagerfanta $pager */ |
900c8448 NL |
102 | $pager = $this->getDoctrine() |
103 | ->getRepository('WallabagCoreBundle:Entry') | |
104 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); | |
105 | ||
900c8448 | 106 | $pager->setMaxPerPage($perPage); |
b60a666d | 107 | $pager->setCurrentPage($page); |
900c8448 NL |
108 | |
109 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | |
110 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | |
111 | $pager, | |
112 | new Route( | |
113 | 'api_get_entries', | |
114 | [ | |
115 | 'archive' => $isArchived, | |
116 | 'starred' => $isStarred, | |
117 | 'sort' => $sort, | |
118 | 'order' => $order, | |
119 | 'page' => $page, | |
120 | 'perPage' => $perPage, | |
121 | 'tags' => $tags, | |
122 | 'since' => $since, | |
123 | ], | |
124 | UrlGeneratorInterface::ABSOLUTE_URL | |
125 | ) | |
126 | ); | |
127 | ||
128 | $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); | |
129 | ||
130 | return (new JsonResponse())->setJson($json); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Retrieve a single entry. | |
135 | * | |
136 | * @ApiDoc( | |
137 | * requirements={ | |
138 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
139 | * } | |
140 | * ) | |
141 | * | |
142 | * @return JsonResponse | |
143 | */ | |
144 | public function getEntryAction(Entry $entry) | |
145 | { | |
146 | $this->validateAuthentication(); | |
147 | $this->validateUserAccess($entry->getUser()->getId()); | |
148 | ||
149 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
150 | ||
151 | return (new JsonResponse())->setJson($json); | |
152 | } | |
153 | ||
864c1dd2 JB |
154 | /** |
155 | * Retrieve a single entry as a predefined format. | |
156 | * | |
157 | * @ApiDoc( | |
158 | * requirements={ | |
159 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
160 | * } | |
161 | * ) | |
162 | * | |
163 | * @return Response | |
164 | */ | |
165 | public function getEntryExportAction(Entry $entry, Request $request) | |
166 | { | |
167 | $this->validateAuthentication(); | |
168 | $this->validateUserAccess($entry->getUser()->getId()); | |
169 | ||
170 | return $this->get('wallabag_core.helper.entries_export') | |
171 | ->setEntries($entry) | |
172 | ->updateTitle('entry') | |
173 | ->exportAs($request->attributes->get('_format')); | |
174 | } | |
175 | ||
900c8448 NL |
176 | /** |
177 | * Create an entry. | |
178 | * | |
179 | * @ApiDoc( | |
180 | * parameters={ | |
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"}, | |
186 | * } | |
187 | * ) | |
188 | * | |
189 | * @return JsonResponse | |
190 | */ | |
191 | public function postEntriesAction(Request $request) | |
192 | { | |
193 | $this->validateAuthentication(); | |
194 | ||
195 | $url = $request->request->get('url'); | |
196 | $title = $request->request->get('title'); | |
197 | $isArchived = $request->request->get('archive'); | |
198 | $isStarred = $request->request->get('starred'); | |
199 | ||
200 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); | |
201 | ||
202 | if (false === $entry) { | |
203 | $entry = $this->get('wallabag_core.content_proxy')->updateEntry( | |
204 | new Entry($this->getUser()), | |
205 | $url | |
206 | ); | |
207 | } | |
208 | ||
209 | if (!is_null($title)) { | |
210 | $entry->setTitle($title); | |
211 | } | |
212 | ||
213 | $tags = $request->request->get('tags', ''); | |
214 | if (!empty($tags)) { | |
215 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
216 | } | |
217 | ||
218 | if (!is_null($isStarred)) { | |
219 | $entry->setStarred((bool) $isStarred); | |
220 | } | |
221 | ||
222 | if (!is_null($isArchived)) { | |
223 | $entry->setArchived((bool) $isArchived); | |
224 | } | |
225 | ||
226 | $em = $this->getDoctrine()->getManager(); | |
227 | $em->persist($entry); | |
900c8448 NL |
228 | $em->flush(); |
229 | ||
5a619812 JB |
230 | // entry saved, dispatch event about it! |
231 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
232 | ||
900c8448 NL |
233 | $json = $this->get('serializer')->serialize($entry, 'json'); |
234 | ||
235 | return (new JsonResponse())->setJson($json); | |
236 | } | |
237 | ||
238 | /** | |
239 | * Change several properties of an entry. | |
240 | * | |
241 | * @ApiDoc( | |
242 | * requirements={ | |
243 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
244 | * }, | |
245 | * parameters={ | |
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."}, | |
250 | * } | |
251 | * ) | |
252 | * | |
253 | * @return JsonResponse | |
254 | */ | |
255 | public function patchEntriesAction(Entry $entry, Request $request) | |
256 | { | |
257 | $this->validateAuthentication(); | |
258 | $this->validateUserAccess($entry->getUser()->getId()); | |
259 | ||
260 | $title = $request->request->get('title'); | |
261 | $isArchived = $request->request->get('archive'); | |
262 | $isStarred = $request->request->get('starred'); | |
263 | ||
264 | if (!is_null($title)) { | |
265 | $entry->setTitle($title); | |
266 | } | |
267 | ||
268 | if (!is_null($isArchived)) { | |
269 | $entry->setArchived((bool) $isArchived); | |
270 | } | |
271 | ||
272 | if (!is_null($isStarred)) { | |
273 | $entry->setStarred((bool) $isStarred); | |
274 | } | |
275 | ||
276 | $tags = $request->request->get('tags', ''); | |
277 | if (!empty($tags)) { | |
278 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
279 | } | |
280 | ||
281 | $em = $this->getDoctrine()->getManager(); | |
282 | $em->flush(); | |
283 | ||
284 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
285 | ||
286 | return (new JsonResponse())->setJson($json); | |
287 | } | |
288 | ||
0a6f4568 JB |
289 | /** |
290 | * Reload an entry. | |
5cd0857e | 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). |
0a6f4568 JB |
292 | * |
293 | * @ApiDoc( | |
294 | * requirements={ | |
295 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
296 | * } | |
297 | * ) | |
298 | * | |
299 | * @return JsonResponse | |
300 | */ | |
301 | public function patchEntriesReloadAction(Entry $entry) | |
302 | { | |
303 | $this->validateAuthentication(); | |
304 | $this->validateUserAccess($entry->getUser()->getId()); | |
305 | ||
0a6f4568 JB |
306 | try { |
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', [ | |
310 | 'exception' => $e, | |
311 | 'entry' => $entry, | |
312 | ]); | |
313 | ||
5cd0857e | 314 | return new JsonResponse([], 304); |
0a6f4568 JB |
315 | } |
316 | ||
317 | // if refreshing entry failed, don't save it | |
318 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
5cd0857e | 319 | return new JsonResponse([], 304); |
0a6f4568 JB |
320 | } |
321 | ||
322 | $em = $this->getDoctrine()->getManager(); | |
323 | $em->persist($entry); | |
324 | $em->flush(); | |
325 | ||
326 | // entry saved, dispatch event about it! | |
327 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
328 | ||
329 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
330 | ||
331 | return (new JsonResponse())->setJson($json); | |
332 | } | |
333 | ||
900c8448 NL |
334 | /** |
335 | * Delete **permanently** an entry. | |
336 | * | |
337 | * @ApiDoc( | |
338 | * requirements={ | |
339 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
340 | * } | |
341 | * ) | |
342 | * | |
343 | * @return JsonResponse | |
344 | */ | |
345 | public function deleteEntriesAction(Entry $entry) | |
346 | { | |
347 | $this->validateAuthentication(); | |
348 | $this->validateUserAccess($entry->getUser()->getId()); | |
349 | ||
350 | $em = $this->getDoctrine()->getManager(); | |
351 | $em->remove($entry); | |
352 | $em->flush(); | |
353 | ||
5a619812 JB |
354 | // entry deleted, dispatch event about it! |
355 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
356 | ||
900c8448 NL |
357 | $json = $this->get('serializer')->serialize($entry, 'json'); |
358 | ||
359 | return (new JsonResponse())->setJson($json); | |
360 | } | |
361 | ||
362 | /** | |
363 | * Retrieve all tags for an entry. | |
364 | * | |
365 | * @ApiDoc( | |
366 | * requirements={ | |
367 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
368 | * } | |
369 | * ) | |
370 | * | |
371 | * @return JsonResponse | |
372 | */ | |
373 | public function getEntriesTagsAction(Entry $entry) | |
374 | { | |
375 | $this->validateAuthentication(); | |
376 | $this->validateUserAccess($entry->getUser()->getId()); | |
377 | ||
378 | $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); | |
379 | ||
380 | return (new JsonResponse())->setJson($json); | |
381 | } | |
382 | ||
383 | /** | |
384 | * Add one or more tags to an entry. | |
385 | * | |
386 | * @ApiDoc( | |
387 | * requirements={ | |
388 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
389 | * }, | |
390 | * parameters={ | |
391 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
392 | * } | |
393 | * ) | |
394 | * | |
395 | * @return JsonResponse | |
396 | */ | |
397 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
398 | { | |
399 | $this->validateAuthentication(); | |
400 | $this->validateUserAccess($entry->getUser()->getId()); | |
401 | ||
402 | $tags = $request->request->get('tags', ''); | |
403 | if (!empty($tags)) { | |
404 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | |
405 | } | |
406 | ||
407 | $em = $this->getDoctrine()->getManager(); | |
408 | $em->persist($entry); | |
409 | $em->flush(); | |
410 | ||
411 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
412 | ||
413 | return (new JsonResponse())->setJson($json); | |
414 | } | |
415 | ||
416 | /** | |
417 | * Permanently remove one tag for an entry. | |
418 | * | |
419 | * @ApiDoc( | |
420 | * requirements={ | |
421 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"}, | |
422 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
423 | * } | |
424 | * ) | |
425 | * | |
426 | * @return JsonResponse | |
427 | */ | |
428 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
429 | { | |
430 | $this->validateAuthentication(); | |
431 | $this->validateUserAccess($entry->getUser()->getId()); | |
432 | ||
433 | $entry->removeTag($tag); | |
434 | $em = $this->getDoctrine()->getManager(); | |
435 | $em->persist($entry); | |
436 | $em->flush(); | |
437 | ||
438 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
439 | ||
440 | return (new JsonResponse())->setJson($json); | |
441 | } | |
442 | } |