]>
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"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, | |
303 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, | |
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 | $this->upsertEntry($entry, $request); | |
332 | ||
333 | return $this->sendResponse($entry); | |
334 | } | |
335 | ||
336 | /** | |
337 | * Change several properties of an entry. | |
338 | * | |
339 | * @ApiDoc( | |
340 | * requirements={ | |
341 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
342 | * }, | |
343 | * parameters={ | |
344 | * {"name"="title", "dataType"="string", "required"=false}, | |
345 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
346 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | |
347 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | |
348 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, | |
349 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
350 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
351 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | |
352 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | |
353 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, | |
354 | * } | |
355 | * ) | |
356 | * | |
357 | * @return JsonResponse | |
358 | */ | |
359 | public function patchEntriesAction(Entry $entry, Request $request) | |
360 | { | |
361 | $this->validateAuthentication(); | |
362 | $this->validateUserAccess($entry->getUser()->getId()); | |
363 | ||
364 | $this->upsertEntry($entry, $request, true); | |
365 | ||
366 | return $this->sendResponse($entry); | |
367 | } | |
368 | ||
369 | /** | |
370 | * Reload an entry. | |
371 | * 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). | |
372 | * | |
373 | * @ApiDoc( | |
374 | * requirements={ | |
375 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
376 | * } | |
377 | * ) | |
378 | * | |
379 | * @return JsonResponse | |
380 | */ | |
381 | public function patchEntriesReloadAction(Entry $entry) | |
382 | { | |
383 | $this->validateAuthentication(); | |
384 | $this->validateUserAccess($entry->getUser()->getId()); | |
385 | ||
386 | try { | |
387 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); | |
388 | } catch (\Exception $e) { | |
389 | $this->get('logger')->error('Error while saving an entry', [ | |
390 | 'exception' => $e, | |
391 | 'entry' => $entry, | |
392 | ]); | |
393 | ||
394 | return new JsonResponse([], 304); | |
395 | } | |
396 | ||
397 | // if refreshing entry failed, don't save it | |
398 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
399 | return new JsonResponse([], 304); | |
400 | } | |
401 | ||
402 | $em = $this->getDoctrine()->getManager(); | |
403 | $em->persist($entry); | |
404 | $em->flush(); | |
405 | ||
406 | // entry saved, dispatch event about it! | |
407 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
408 | ||
409 | return $this->sendResponse($entry); | |
410 | } | |
411 | ||
412 | /** | |
413 | * Delete **permanently** an entry. | |
414 | * | |
415 | * @ApiDoc( | |
416 | * requirements={ | |
417 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
418 | * } | |
419 | * ) | |
420 | * | |
421 | * @return JsonResponse | |
422 | */ | |
423 | public function deleteEntriesAction(Entry $entry) | |
424 | { | |
425 | $this->validateAuthentication(); | |
426 | $this->validateUserAccess($entry->getUser()->getId()); | |
427 | ||
428 | $em = $this->getDoctrine()->getManager(); | |
429 | $em->remove($entry); | |
430 | $em->flush(); | |
431 | ||
432 | // entry deleted, dispatch event about it! | |
433 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
434 | ||
435 | return $this->sendResponse($entry); | |
436 | } | |
437 | ||
438 | /** | |
439 | * Retrieve all tags for an entry. | |
440 | * | |
441 | * @ApiDoc( | |
442 | * requirements={ | |
443 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
444 | * } | |
445 | * ) | |
446 | * | |
447 | * @return JsonResponse | |
448 | */ | |
449 | public function getEntriesTagsAction(Entry $entry) | |
450 | { | |
451 | $this->validateAuthentication(); | |
452 | $this->validateUserAccess($entry->getUser()->getId()); | |
453 | ||
454 | return $this->sendResponse($entry->getTags()); | |
455 | } | |
456 | ||
457 | /** | |
458 | * Add one or more tags to an entry. | |
459 | * | |
460 | * @ApiDoc( | |
461 | * requirements={ | |
462 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
463 | * }, | |
464 | * parameters={ | |
465 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
466 | * } | |
467 | * ) | |
468 | * | |
469 | * @return JsonResponse | |
470 | */ | |
471 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
472 | { | |
473 | $this->validateAuthentication(); | |
474 | $this->validateUserAccess($entry->getUser()->getId()); | |
475 | ||
476 | $tags = $request->request->get('tags', ''); | |
477 | if (!empty($tags)) { | |
478 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | |
479 | } | |
480 | ||
481 | $em = $this->getDoctrine()->getManager(); | |
482 | $em->persist($entry); | |
483 | $em->flush(); | |
484 | ||
485 | return $this->sendResponse($entry); | |
486 | } | |
487 | ||
488 | /** | |
489 | * Permanently remove one tag for an entry. | |
490 | * | |
491 | * @ApiDoc( | |
492 | * requirements={ | |
493 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"}, | |
494 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
495 | * } | |
496 | * ) | |
497 | * | |
498 | * @return JsonResponse | |
499 | */ | |
500 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
501 | { | |
502 | $this->validateAuthentication(); | |
503 | $this->validateUserAccess($entry->getUser()->getId()); | |
504 | ||
505 | $entry->removeTag($tag); | |
506 | $em = $this->getDoctrine()->getManager(); | |
507 | $em->persist($entry); | |
508 | $em->flush(); | |
509 | ||
510 | return $this->sendResponse($entry); | |
511 | } | |
512 | ||
513 | /** | |
514 | * Handles an entries list delete tags from them. | |
515 | * | |
516 | * @ApiDoc( | |
517 | * parameters={ | |
518 | * {"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."} | |
519 | * } | |
520 | * ) | |
521 | * | |
522 | * @return JsonResponse | |
523 | */ | |
524 | public function deleteEntriesTagsListAction(Request $request) | |
525 | { | |
526 | $this->validateAuthentication(); | |
527 | ||
528 | $list = json_decode($request->query->get('list', [])); | |
529 | ||
530 | if (empty($list)) { | |
531 | return $this->sendResponse([]); | |
532 | } | |
533 | ||
534 | // handle multiple urls | |
535 | $results = []; | |
536 | ||
537 | foreach ($list as $key => $element) { | |
538 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
539 | $element->url, | |
540 | $this->getUser()->getId() | |
541 | ); | |
542 | ||
543 | $results[$key]['url'] = $element->url; | |
544 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
545 | ||
546 | $tags = $element->tags; | |
547 | ||
548 | if (false !== $entry && !(empty($tags))) { | |
549 | $tags = explode(',', $tags); | |
550 | foreach ($tags as $label) { | |
551 | $label = trim($label); | |
552 | ||
553 | $tag = $this->getDoctrine() | |
554 | ->getRepository('WallabagCoreBundle:Tag') | |
555 | ->findOneByLabel($label); | |
556 | ||
557 | if (false !== $tag) { | |
558 | $entry->removeTag($tag); | |
559 | } | |
560 | } | |
561 | ||
562 | $em = $this->getDoctrine()->getManager(); | |
563 | $em->persist($entry); | |
564 | $em->flush(); | |
565 | } | |
566 | } | |
567 | ||
568 | return $this->sendResponse($results); | |
569 | } | |
570 | ||
571 | /** | |
572 | * Handles an entries list and add tags to them. | |
573 | * | |
574 | * @ApiDoc( | |
575 | * parameters={ | |
576 | * {"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."} | |
577 | * } | |
578 | * ) | |
579 | * | |
580 | * @return JsonResponse | |
581 | */ | |
582 | public function postEntriesTagsListAction(Request $request) | |
583 | { | |
584 | $this->validateAuthentication(); | |
585 | ||
586 | $list = json_decode($request->query->get('list', [])); | |
587 | ||
588 | if (empty($list)) { | |
589 | return $this->sendResponse([]); | |
590 | } | |
591 | ||
592 | $results = []; | |
593 | ||
594 | // handle multiple urls | |
595 | foreach ($list as $key => $element) { | |
596 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
597 | $element->url, | |
598 | $this->getUser()->getId() | |
599 | ); | |
600 | ||
601 | $results[$key]['url'] = $element->url; | |
602 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
603 | ||
604 | $tags = $element->tags; | |
605 | ||
606 | if (false !== $entry && !(empty($tags))) { | |
607 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | |
608 | ||
609 | $em = $this->getDoctrine()->getManager(); | |
610 | $em->persist($entry); | |
611 | $em->flush(); | |
612 | } | |
613 | } | |
614 | ||
615 | return $this->sendResponse($results); | |
616 | } | |
617 | ||
618 | /** | |
619 | * Shortcut to send data serialized in json. | |
620 | * | |
621 | * @param mixed $data | |
622 | * | |
623 | * @return JsonResponse | |
624 | */ | |
625 | private function sendResponse($data) | |
626 | { | |
627 | // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 | |
628 | $context = new SerializationContext(); | |
629 | $context->setSerializeNull(true); | |
630 | ||
631 | $json = $this->get('serializer')->serialize($data, 'json', $context); | |
632 | ||
633 | return (new JsonResponse())->setJson($json); | |
634 | } | |
635 | ||
636 | /** | |
637 | * Update or Insert a new entry. | |
638 | * | |
639 | * @param Entry $entry | |
640 | * @param Request $request | |
641 | * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting) | |
642 | */ | |
643 | private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false) | |
644 | { | |
645 | $title = $request->request->get('title'); | |
646 | $tags = $request->request->get('tags', []); | |
647 | $isArchived = $request->request->get('archive'); | |
648 | $isStarred = $request->request->get('starred'); | |
649 | $isPublic = $request->request->get('public'); | |
650 | $content = $request->request->get('content'); | |
651 | $language = $request->request->get('language'); | |
652 | $picture = $request->request->get('preview_picture'); | |
653 | $publishedAt = $request->request->get('published_at'); | |
654 | $authors = $request->request->get('authors', ''); | |
655 | ||
656 | try { | |
657 | $this->get('wallabag_core.content_proxy')->updateEntry( | |
658 | $entry, | |
659 | $entry->getUrl(), | |
660 | [ | |
661 | 'title' => !empty($title) ? $title : $entry->getTitle(), | |
662 | 'html' => !empty($content) ? $content : $entry->getContent(), | |
663 | 'url' => $entry->getUrl(), | |
664 | 'language' => !empty($language) ? $language : $entry->getLanguage(), | |
665 | 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(), | |
666 | // faking the open graph preview picture | |
667 | 'open_graph' => [ | |
668 | 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(), | |
669 | ], | |
670 | 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(), | |
671 | ], | |
672 | $disableContentUpdate | |
673 | ); | |
674 | } catch (\Exception $e) { | |
675 | $this->get('logger')->error('Error while saving an entry', [ | |
676 | 'exception' => $e, | |
677 | 'entry' => $entry, | |
678 | ]); | |
679 | } | |
680 | ||
681 | if (null !== $isArchived) { | |
682 | $entry->setArchived((bool) $isArchived); | |
683 | } | |
684 | ||
685 | if (null !== $isStarred) { | |
686 | $entry->setStarred((bool) $isStarred); | |
687 | } | |
688 | ||
689 | if (!empty($tags)) { | |
690 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | |
691 | } | |
692 | ||
693 | if (null !== $isPublic) { | |
694 | if (true === (bool) $isPublic && null === $entry->getUid()) { | |
695 | $entry->generateUid(); | |
696 | } elseif (false === (bool) $isPublic) { | |
697 | $entry->cleanUid(); | |
698 | } | |
699 | } | |
700 | ||
701 | $em = $this->getDoctrine()->getManager(); | |
702 | $em->persist($entry); | |
703 | $em->flush(); | |
704 | ||
705 | // entry saved, dispatch event about it! | |
706 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
707 | } | |
708 | ||
709 | /** | |
710 | * Return information about the entry if it exist and depending on the id or not. | |
711 | * | |
712 | * @param Entry|null $entry | |
713 | * @param bool $returnId | |
714 | * | |
715 | * @return bool|int | |
716 | */ | |
717 | private function returnExistInformation($entry, $returnId) | |
718 | { | |
719 | if ($returnId) { | |
720 | return $entry instanceof Entry ? $entry->getId() : null; | |
721 | } | |
722 | ||
723 | return $entry instanceof Entry; | |
724 | } | |
725 | } |