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