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