]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Hateoas\Configuration\Route; | |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | |
7 | use JMS\Serializer\SerializationContext; | |
8 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
9 | use Symfony\Component\HttpFoundation\JsonResponse; | |
10 | use Symfony\Component\HttpFoundation\Request; | |
11 | use Symfony\Component\HttpKernel\Exception\HttpException; | |
12 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
13 | use Wallabag\CoreBundle\Entity\Entry; | |
14 | use Wallabag\CoreBundle\Entity\Tag; | |
15 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
16 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | |
17 | ||
18 | class EntryRestController extends WallabagRestController | |
19 | { | |
20 | /** | |
21 | * Check if an entry exist by url. | |
22 | * Return ID if entry(ies) exist (and if you give the return_id parameter). | |
23 | * Otherwise it returns false. | |
24 | * | |
25 | * @todo Remove that `return_id` in the next major release | |
26 | * | |
27 | * @ApiDoc( | |
28 | * parameters={ | |
29 | * {"name"="return_id", "dataType"="string", "required"=false, "format"="1 or 0", "description"="Set 1 if you want to retrieve ID in case entry(ies) exists, 0 by default"}, | |
30 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, | |
31 | * {"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"} | |
32 | * } | |
33 | * ) | |
34 | * | |
35 | * @return JsonResponse | |
36 | */ | |
37 | public function getEntriesExistsAction(Request $request) | |
38 | { | |
39 | $this->validateAuthentication(); | |
40 | ||
41 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); | |
42 | $urls = $request->query->get('urls', []); | |
43 | ||
44 | // handle multiple urls first | |
45 | if (!empty($urls)) { | |
46 | $results = []; | |
47 | foreach ($urls as $url) { | |
48 | $res = $this->getDoctrine() | |
49 | ->getRepository('WallabagCoreBundle:Entry') | |
50 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
51 | ||
52 | $results[$url] = $this->returnExistInformation($res, $returnId); | |
53 | } | |
54 | ||
55 | return $this->sendResponse($results); | |
56 | } | |
57 | ||
58 | // let's see if it is a simple url? | |
59 | $url = $request->query->get('url', ''); | |
60 | ||
61 | if (empty($url)) { | |
62 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | |
63 | } | |
64 | ||
65 | $res = $this->getDoctrine() | |
66 | ->getRepository('WallabagCoreBundle:Entry') | |
67 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
68 | ||
69 | $exists = $this->returnExistInformation($res, $returnId); | |
70 | ||
71 | return $this->sendResponse(['exists' => $exists]); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Retrieve all entries. It could be filtered by many options. | |
76 | * | |
77 | * @ApiDoc( | |
78 | * parameters={ | |
79 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, | |
80 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, | |
81 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, | |
82 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | |
83 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | |
84 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | |
85 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | |
86 | * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, | |
87 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"}, | |
88 | * } | |
89 | * ) | |
90 | * | |
91 | * @return JsonResponse | |
92 | */ | |
93 | public function getEntriesAction(Request $request) | |
94 | { | |
95 | $this->validateAuthentication(); | |
96 | ||
97 | $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); | |
98 | $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); | |
99 | $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public'); | |
100 | $sort = $request->query->get('sort', 'created'); | |
101 | $order = $request->query->get('order', 'desc'); | |
102 | $page = (int) $request->query->get('page', 1); | |
103 | $perPage = (int) $request->query->get('perPage', 30); | |
104 | $tags = $request->query->get('tags', ''); | |
105 | $since = $request->query->get('since', 0); | |
106 | ||
107 | /** @var \Pagerfanta\Pagerfanta $pager */ | |
108 | $pager = $this->get('wallabag_core.entry_repository')->findEntries( | |
109 | $this->getUser()->getId(), | |
110 | $isArchived, | |
111 | $isStarred, | |
112 | $isPublic, | |
113 | $sort, | |
114 | $order, | |
115 | $since, | |
116 | $tags | |
117 | ); | |
118 | ||
119 | $pager->setMaxPerPage($perPage); | |
120 | $pager->setCurrentPage($page); | |
121 | ||
122 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | |
123 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | |
124 | $pager, | |
125 | new Route( | |
126 | 'api_get_entries', | |
127 | [ | |
128 | 'archive' => $isArchived, | |
129 | 'starred' => $isStarred, | |
130 | 'public' => $isPublic, | |
131 | 'sort' => $sort, | |
132 | 'order' => $order, | |
133 | 'page' => $page, | |
134 | 'perPage' => $perPage, | |
135 | 'tags' => $tags, | |
136 | 'since' => $since, | |
137 | ], | |
138 | UrlGeneratorInterface::ABSOLUTE_URL | |
139 | ) | |
140 | ); | |
141 | ||
142 | return $this->sendResponse($paginatedCollection); | |
143 | } | |
144 | ||
145 | /** | |
146 | * Retrieve a single entry. | |
147 | * | |
148 | * @ApiDoc( | |
149 | * requirements={ | |
150 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
151 | * } | |
152 | * ) | |
153 | * | |
154 | * @return JsonResponse | |
155 | */ | |
156 | public function getEntryAction(Entry $entry) | |
157 | { | |
158 | $this->validateAuthentication(); | |
159 | $this->validateUserAccess($entry->getUser()->getId()); | |
160 | ||
161 | return $this->sendResponse($entry); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Retrieve a single entry as a predefined format. | |
166 | * | |
167 | * @ApiDoc( | |
168 | * requirements={ | |
169 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
170 | * } | |
171 | * ) | |
172 | * | |
173 | * @return Response | |
174 | */ | |
175 | public function getEntryExportAction(Entry $entry, Request $request) | |
176 | { | |
177 | $this->validateAuthentication(); | |
178 | $this->validateUserAccess($entry->getUser()->getId()); | |
179 | ||
180 | return $this->get('wallabag_core.helper.entries_export') | |
181 | ->setEntries($entry) | |
182 | ->updateTitle('entry') | |
183 | ->exportAs($request->attributes->get('_format')); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Handles an entries list and delete URL. | |
188 | * | |
189 | * @ApiDoc( | |
190 | * parameters={ | |
191 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} | |
192 | * } | |
193 | * ) | |
194 | * | |
195 | * @return JsonResponse | |
196 | */ | |
197 | public function deleteEntriesListAction(Request $request) | |
198 | { | |
199 | $this->validateAuthentication(); | |
200 | ||
201 | $urls = json_decode($request->query->get('urls', [])); | |
202 | ||
203 | if (empty($urls)) { | |
204 | return $this->sendResponse([]); | |
205 | } | |
206 | ||
207 | $results = []; | |
208 | ||
209 | // handle multiple urls | |
210 | foreach ($urls as $key => $url) { | |
211 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
212 | $url, | |
213 | $this->getUser()->getId() | |
214 | ); | |
215 | ||
216 | $results[$key]['url'] = $url; | |
217 | ||
218 | if (false !== $entry) { | |
219 | $em = $this->getDoctrine()->getManager(); | |
220 | $em->remove($entry); | |
221 | $em->flush(); | |
222 | ||
223 | // entry deleted, dispatch event about it! | |
224 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
225 | } | |
226 | ||
227 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; | |
228 | } | |
229 | ||
230 | return $this->sendResponse($results); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Handles an entries list and create URL. | |
235 | * | |
236 | * @ApiDoc( | |
237 | * parameters={ | |
238 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} | |
239 | * } | |
240 | * ) | |
241 | * | |
242 | * @throws HttpException When limit is reached | |
243 | * | |
244 | * @return JsonResponse | |
245 | */ | |
246 | public function postEntriesListAction(Request $request) | |
247 | { | |
248 | $this->validateAuthentication(); | |
249 | ||
250 | $urls = json_decode($request->query->get('urls', [])); | |
251 | ||
252 | $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); | |
253 | ||
254 | if (count($urls) > $limit) { | |
255 | throw new HttpException(400, 'API limit reached'); | |
256 | } | |
257 | ||
258 | $results = []; | |
259 | if (empty($urls)) { | |
260 | return $this->sendResponse($results); | |
261 | } | |
262 | ||
263 | // handle multiple urls | |
264 | foreach ($urls as $key => $url) { | |
265 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
266 | $url, | |
267 | $this->getUser()->getId() | |
268 | ); | |
269 | ||
270 | $results[$key]['url'] = $url; | |
271 | ||
272 | if (false === $entry) { | |
273 | $entry = new Entry($this->getUser()); | |
274 | ||
275 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $url); | |
276 | } | |
277 | ||
278 | $em = $this->getDoctrine()->getManager(); | |
279 | $em->persist($entry); | |
280 | $em->flush(); | |
281 | ||
282 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
283 | ||
284 | // entry saved, dispatch event about it! | |
285 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
286 | } | |
287 | ||
288 | return $this->sendResponse($results); | |
289 | } | |
290 | ||
291 | /** | |
292 | * Create an entry. | |
293 | * | |
294 | * If you want to provide the HTML content (which means wallabag won't fetch it from the url), you must provide `content`, `title` & `url` fields **non-empty**. | |
295 | * Otherwise, content will be fetched as normal from the url and values will be overwritten. | |
296 | * | |
297 | * @ApiDoc( | |
298 | * parameters={ | |
299 | * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, | |
300 | * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, | |
301 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
302 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, | |
303 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, | |
304 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, | |
305 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
306 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
307 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | |
308 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | |
309 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, | |
310 | * } | |
311 | * ) | |
312 | * | |
313 | * @return JsonResponse | |
314 | */ | |
315 | public function postEntriesAction(Request $request) | |
316 | { | |
317 | $this->validateAuthentication(); | |
318 | ||
319 | $url = $request->request->get('url'); | |
320 | ||
321 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
322 | $url, | |
323 | $this->getUser()->getId() | |
324 | ); | |
325 | ||
326 | if (false === $entry) { | |
327 | $entry = new Entry($this->getUser()); | |
328 | $entry->setUrl($url); | |
329 | } | |
330 | ||
331 | $data = $this->retrieveValueFromRequest($request); | |
332 | ||
333 | try { | |
334 | $this->get('wallabag_core.content_proxy')->updateEntry( | |
335 | $entry, | |
336 | $entry->getUrl(), | |
337 | [ | |
338 | 'title' => !empty($data['title']) ? $data['title'] : $entry->getTitle(), | |
339 | 'html' => !empty($data['content']) ? $data['content'] : $entry->getContent(), | |
340 | 'url' => $entry->getUrl(), | |
341 | 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), | |
342 | 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), | |
343 | // faking the open graph preview picture | |
344 | 'open_graph' => [ | |
345 | 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), | |
346 | ], | |
347 | 'authors' => is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), | |
348 | ] | |
349 | ); | |
350 | } catch (\Exception $e) { | |
351 | $this->get('logger')->error('Error while saving an entry', [ | |
352 | 'exception' => $e, | |
353 | 'entry' => $entry, | |
354 | ]); | |
355 | } | |
356 | ||
357 | if (!is_null($data['isArchived'])) { | |
358 | $entry->setArchived((bool) $data['isArchived']); | |
359 | } | |
360 | ||
361 | if (!is_null($data['isStarred'])) { | |
362 | $entry->setStarred((bool) $data['isStarred']); | |
363 | } | |
364 | ||
365 | if (!empty($data['tags'])) { | |
366 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); | |
367 | } | |
368 | ||
369 | if (!is_null($data['isPublic'])) { | |
370 | if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { | |
371 | $entry->generateUid(); | |
372 | } elseif (false === (bool) $data['isPublic']) { | |
373 | $entry->cleanUid(); | |
374 | } | |
375 | } | |
376 | ||
377 | $em = $this->getDoctrine()->getManager(); | |
378 | $em->persist($entry); | |
379 | $em->flush(); | |
380 | ||
381 | // entry saved, dispatch event about it! | |
382 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
383 | ||
384 | return $this->sendResponse($entry); | |
385 | } | |
386 | ||
387 | /** | |
388 | * Change several properties of an entry. | |
389 | * | |
390 | * @ApiDoc( | |
391 | * requirements={ | |
392 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
393 | * }, | |
394 | * parameters={ | |
395 | * {"name"="title", "dataType"="string", "required"=false}, | |
396 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
397 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | |
398 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | |
399 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, | |
400 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
401 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
402 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | |
403 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | |
404 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, | |
405 | * } | |
406 | * ) | |
407 | * | |
408 | * @return JsonResponse | |
409 | */ | |
410 | public function patchEntriesAction(Entry $entry, Request $request) | |
411 | { | |
412 | $this->validateAuthentication(); | |
413 | $this->validateUserAccess($entry->getUser()->getId()); | |
414 | ||
415 | $contentProxy = $this->get('wallabag_core.content_proxy'); | |
416 | ||
417 | $data = $this->retrieveValueFromRequest($request); | |
418 | ||
419 | // this is a special case where user want to manually update the entry content | |
420 | // the ContentProxy will only cleanup the html | |
421 | // and also we force to not re-fetch the content in case of error | |
422 | if (!empty($data['content'])) { | |
423 | try { | |
424 | $contentProxy->updateEntry( | |
425 | $entry, | |
426 | $entry->getUrl(), | |
427 | [ | |
428 | 'html' => $data['content'], | |
429 | ], | |
430 | true | |
431 | ); | |
432 | } catch (\Exception $e) { | |
433 | $this->get('logger')->error('Error while saving an entry', [ | |
434 | 'exception' => $e, | |
435 | 'entry' => $entry, | |
436 | ]); | |
437 | } | |
438 | } | |
439 | ||
440 | if (!empty($data['title'])) { | |
441 | $entry->setTitle($data['title']); | |
442 | } | |
443 | ||
444 | if (!empty($data['language'])) { | |
445 | $contentProxy->updateLanguage($entry, $data['language']); | |
446 | } | |
447 | ||
448 | if (!empty($data['authors']) && is_string($data['authors'])) { | |
449 | $entry->setPublishedBy(explode(',', $data['authors'])); | |
450 | } | |
451 | ||
452 | if (!empty($data['picture'])) { | |
453 | $contentProxy->updatePreviewPicture($entry, $data['picture']); | |
454 | } | |
455 | ||
456 | if (!empty($data['publishedAt'])) { | |
457 | $contentProxy->updatePublishedAt($entry, $data['publishedAt']); | |
458 | } | |
459 | ||
460 | if (!is_null($data['isArchived'])) { | |
461 | $entry->setArchived((bool) $data['isArchived']); | |
462 | } | |
463 | ||
464 | if (!is_null($data['isStarred'])) { | |
465 | $entry->setStarred((bool) $data['isStarred']); | |
466 | } | |
467 | ||
468 | if (!empty($data['tags'])) { | |
469 | $entry->removeAllTags(); | |
470 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); | |
471 | } | |
472 | ||
473 | if (!is_null($data['isPublic'])) { | |
474 | if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { | |
475 | $entry->generateUid(); | |
476 | } elseif (false === (bool) $data['isPublic']) { | |
477 | $entry->cleanUid(); | |
478 | } | |
479 | } | |
480 | ||
481 | $em = $this->getDoctrine()->getManager(); | |
482 | $em->persist($entry); | |
483 | $em->flush(); | |
484 | ||
485 | // entry saved, dispatch event about it! | |
486 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
487 | ||
488 | return $this->sendResponse($entry); | |
489 | } | |
490 | ||
491 | /** | |
492 | * Reload an entry. | |
493 | * 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). | |
494 | * | |
495 | * @ApiDoc( | |
496 | * requirements={ | |
497 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
498 | * } | |
499 | * ) | |
500 | * | |
501 | * @return JsonResponse | |
502 | */ | |
503 | public function patchEntriesReloadAction(Entry $entry) | |
504 | { | |
505 | $this->validateAuthentication(); | |
506 | $this->validateUserAccess($entry->getUser()->getId()); | |
507 | ||
508 | try { | |
509 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
510 | } catch (\Exception $e) { | |
511 | $this->get('logger')->error('Error while saving an entry', [ | |
512 | 'exception' => $e, | |
513 | 'entry' => $entry, | |
514 | ]); | |
515 | ||
516 | return new JsonResponse([], 304); | |
517 | } | |
518 | ||
519 | // if refreshing entry failed, don't save it | |
520 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
521 | return new JsonResponse([], 304); | |
522 | } | |
523 | ||
524 | $em = $this->getDoctrine()->getManager(); | |
525 | $em->persist($entry); | |
526 | $em->flush(); | |
527 | ||
528 | // entry saved, dispatch event about it! | |
529 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
530 | ||
531 | return $this->sendResponse($entry); | |
532 | } | |
533 | ||
534 | /** | |
535 | * Delete **permanently** an entry. | |
536 | * | |
537 | * @ApiDoc( | |
538 | * requirements={ | |
539 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
540 | * } | |
541 | * ) | |
542 | * | |
543 | * @return JsonResponse | |
544 | */ | |
545 | public function deleteEntriesAction(Entry $entry) | |
546 | { | |
547 | $this->validateAuthentication(); | |
548 | $this->validateUserAccess($entry->getUser()->getId()); | |
549 | ||
550 | $em = $this->getDoctrine()->getManager(); | |
551 | $em->remove($entry); | |
552 | $em->flush(); | |
553 | ||
554 | // entry deleted, dispatch event about it! | |
555 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
556 | ||
557 | return $this->sendResponse($entry); | |
558 | } | |
559 | ||
560 | /** | |
561 | * Retrieve all tags for an entry. | |
562 | * | |
563 | * @ApiDoc( | |
564 | * requirements={ | |
565 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
566 | * } | |
567 | * ) | |
568 | * | |
569 | * @return JsonResponse | |
570 | */ | |
571 | public function getEntriesTagsAction(Entry $entry) | |
572 | { | |
573 | $this->validateAuthentication(); | |
574 | $this->validateUserAccess($entry->getUser()->getId()); | |
575 | ||
576 | return $this->sendResponse($entry->getTags()); | |
577 | } | |
578 | ||
579 | /** | |
580 | * Add one or more tags to an entry. | |
581 | * | |
582 | * @ApiDoc( | |
583 | * requirements={ | |
584 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
585 | * }, | |
586 | * parameters={ | |
587 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
588 | * } | |
589 | * ) | |
590 | * | |
591 | * @return JsonResponse | |
592 | */ | |
593 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
594 | { | |
595 | $this->validateAuthentication(); | |
596 | $this->validateUserAccess($entry->getUser()->getId()); | |
597 | ||
598 | $tags = $request->request->get('tags', ''); | |
599 | if (!empty($tags)) { | |
600 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | |
601 | } | |
602 | ||
603 | $em = $this->getDoctrine()->getManager(); | |
604 | $em->persist($entry); | |
605 | $em->flush(); | |
606 | ||
607 | return $this->sendResponse($entry); | |
608 | } | |
609 | ||
610 | /** | |
611 | * Permanently remove one tag for an entry. | |
612 | * | |
613 | * @ApiDoc( | |
614 | * requirements={ | |
615 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"}, | |
616 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
617 | * } | |
618 | * ) | |
619 | * | |
620 | * @return JsonResponse | |
621 | */ | |
622 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
623 | { | |
624 | $this->validateAuthentication(); | |
625 | $this->validateUserAccess($entry->getUser()->getId()); | |
626 | ||
627 | $entry->removeTag($tag); | |
628 | $em = $this->getDoctrine()->getManager(); | |
629 | $em->persist($entry); | |
630 | $em->flush(); | |
631 | ||
632 | return $this->sendResponse($entry); | |
633 | } | |
634 | ||
635 | /** | |
636 | * Handles an entries list delete tags from them. | |
637 | * | |
638 | * @ApiDoc( | |
639 | * parameters={ | |
640 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."} | |
641 | * } | |
642 | * ) | |
643 | * | |
644 | * @return JsonResponse | |
645 | */ | |
646 | public function deleteEntriesTagsListAction(Request $request) | |
647 | { | |
648 | $this->validateAuthentication(); | |
649 | ||
650 | $list = json_decode($request->query->get('list', [])); | |
651 | ||
652 | if (empty($list)) { | |
653 | return $this->sendResponse([]); | |
654 | } | |
655 | ||
656 | // handle multiple urls | |
657 | $results = []; | |
658 | ||
659 | foreach ($list as $key => $element) { | |
660 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
661 | $element->url, | |
662 | $this->getUser()->getId() | |
663 | ); | |
664 | ||
665 | $results[$key]['url'] = $element->url; | |
666 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
667 | ||
668 | $tags = $element->tags; | |
669 | ||
670 | if (false !== $entry && !(empty($tags))) { | |
671 | $tags = explode(',', $tags); | |
672 | foreach ($tags as $label) { | |
673 | $label = trim($label); | |
674 | ||
675 | $tag = $this->getDoctrine() | |
676 | ->getRepository('WallabagCoreBundle:Tag') | |
677 | ->findOneByLabel($label); | |
678 | ||
679 | if (false !== $tag) { | |
680 | $entry->removeTag($tag); | |
681 | } | |
682 | } | |
683 | ||
684 | $em = $this->getDoctrine()->getManager(); | |
685 | $em->persist($entry); | |
686 | $em->flush(); | |
687 | } | |
688 | } | |
689 | ||
690 | return $this->sendResponse($results); | |
691 | } | |
692 | ||
693 | /** | |
694 | * Handles an entries list and add tags to them. | |
695 | * | |
696 | * @ApiDoc( | |
697 | * parameters={ | |
698 | * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2'}, {'url': 'http://...','tags': 'tag1, tag2'}]", "description"="Urls (as an array) to handle."} | |
699 | * } | |
700 | * ) | |
701 | * | |
702 | * @return JsonResponse | |
703 | */ | |
704 | public function postEntriesTagsListAction(Request $request) | |
705 | { | |
706 | $this->validateAuthentication(); | |
707 | ||
708 | $list = json_decode($request->query->get('list', [])); | |
709 | ||
710 | if (empty($list)) { | |
711 | return $this->sendResponse([]); | |
712 | } | |
713 | ||
714 | $results = []; | |
715 | ||
716 | // handle multiple urls | |
717 | foreach ($list as $key => $element) { | |
718 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
719 | $element->url, | |
720 | $this->getUser()->getId() | |
721 | ); | |
722 | ||
723 | $results[$key]['url'] = $element->url; | |
724 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
725 | ||
726 | $tags = $element->tags; | |
727 | ||
728 | if (false !== $entry && !(empty($tags))) { | |
729 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | |
730 | ||
731 | $em = $this->getDoctrine()->getManager(); | |
732 | $em->persist($entry); | |
733 | $em->flush(); | |
734 | } | |
735 | } | |
736 | ||
737 | return $this->sendResponse($results); | |
738 | } | |
739 | ||
740 | /** | |
741 | * Shortcut to send data serialized in json. | |
742 | * | |
743 | * @param mixed $data | |
744 | * | |
745 | * @return JsonResponse | |
746 | */ | |
747 | private function sendResponse($data) | |
748 | { | |
749 | // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 | |
750 | $context = new SerializationContext(); | |
751 | $context->setSerializeNull(true); | |
752 | ||
753 | $json = $this->get('serializer')->serialize($data, 'json', $context); | |
754 | ||
755 | return (new JsonResponse())->setJson($json); | |
756 | } | |
757 | ||
758 | /** | |
759 | * Retrieve value from the request. | |
760 | * Used for POST & PATCH on a an entry. | |
761 | * | |
762 | * @param Request $request | |
763 | * | |
764 | * @return array | |
765 | */ | |
766 | private function retrieveValueFromRequest(Request $request) | |
767 | { | |
768 | return [ | |
769 | 'title' => $request->request->get('title'), | |
770 | 'tags' => $request->request->get('tags', []), | |
771 | 'isArchived' => $request->request->get('archive'), | |
772 | 'isStarred' => $request->request->get('starred'), | |
773 | 'isPublic' => $request->request->get('public'), | |
774 | 'content' => $request->request->get('content'), | |
775 | 'language' => $request->request->get('language'), | |
776 | 'picture' => $request->request->get('preview_picture'), | |
777 | 'publishedAt' => $request->request->get('published_at'), | |
778 | 'authors' => $request->request->get('authors', ''), | |
779 | ]; | |
780 | } | |
781 | ||
782 | /** | |
783 | * Return information about the entry if it exist and depending on the id or not. | |
784 | * | |
785 | * @param Entry|null $entry | |
786 | * @param bool $returnId | |
787 | * | |
788 | * @return bool|int | |
789 | */ | |
790 | private function returnExistInformation($entry, $returnId) | |
791 | { | |
792 | if ($returnId) { | |
793 | return $entry instanceof Entry ? $entry->getId() : null; | |
794 | } | |
795 | ||
796 | return $entry instanceof Entry; | |
797 | } | |
798 | } |