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