]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ApiBundle/Controller/EntryRestController.php
Add support for authors
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / EntryRestController.php
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 $results = [];
235
236 $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions');
237
238 if (count($urls) > $limit) {
239 throw new HttpException(400, 'API limit reached');
240 }
241
242 // handle multiple urls
243 if (!empty($urls)) {
244 foreach ($urls as $key => $url) {
245 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
246 $url,
247 $this->getUser()->getId()
248 );
249
250 $results[$key]['url'] = $url;
251
252 if (false === $entry) {
253 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
254 new Entry($this->getUser()),
255 $url
256 );
257 }
258
259 $em = $this->getDoctrine()->getManager();
260 $em->persist($entry);
261 $em->flush();
262
263 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
264
265 // entry saved, dispatch event about it!
266 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
267 }
268 }
269
270 return $this->sendResponse($results);
271 }
272
273 /**
274 * Create an entry.
275 *
276 * @ApiDoc(
277 * parameters={
278 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
279 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
280 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
281 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
282 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
283 * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"},
284 * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"},
285 * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"},
286 * {"name"="published_at", "dataType"="datetime", "format"="YYYY-MM-DDTHH:II:SS+TZ", "required"=false, "description"="Published date of the entry"},
287 * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"},
288 * }
289 * )
290 *
291 * @return JsonResponse
292 */
293 public function postEntriesAction(Request $request)
294 {
295 $this->validateAuthentication();
296
297 $url = $request->request->get('url');
298 $title = $request->request->get('title');
299 $tags = $request->request->get('tags', []);
300 $isArchived = $request->request->get('archive');
301 $isStarred = $request->request->get('starred');
302 $content = $request->request->get('content');
303 $language = $request->request->get('language');
304 $picture = $request->request->get('preview_picture');
305 $publishedAt = $request->request->get('published_at');
306 $authors = $request->request->get('authors', '');
307
308 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
309
310 if (false === $entry) {
311 $entry = new Entry($this->getUser());
312 }
313
314 try {
315 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
316 $entry,
317 $url,
318 [
319 'title' => $title,
320 'html' => $content,
321 'url' => $url,
322 'language' => $language,
323 'date' => $publishedAt,
324 // faking the preview picture
325 'open_graph' => [
326 'og_image' => $picture,
327 ],
328 'authors' => explode(',', $authors),
329 ]
330 );
331 } catch (\Exception $e) {
332 $this->get('logger')->error('Error while saving an entry', [
333 'exception' => $e,
334 'entry' => $entry,
335 ]);
336 $entry->setUrl($url);
337 }
338
339
340 if (!empty($tags)) {
341 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
342 }
343
344 if (!is_null($isStarred)) {
345 $entry->setStarred((bool) $isStarred);
346 }
347
348 if (!is_null($isArchived)) {
349 $entry->setArchived((bool) $isArchived);
350 }
351
352 $em = $this->getDoctrine()->getManager();
353 $em->persist($entry);
354 $em->flush();
355
356 // entry saved, dispatch event about it!
357 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
358
359 return $this->sendResponse($entry);
360 }
361
362 /**
363 * Change several properties of an entry.
364 *
365 * @ApiDoc(
366 * requirements={
367 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
368 * },
369 * parameters={
370 * {"name"="title", "dataType"="string", "required"=false},
371 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
372 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
373 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
374 * }
375 * )
376 *
377 * @return JsonResponse
378 */
379 public function patchEntriesAction(Entry $entry, Request $request)
380 {
381 $this->validateAuthentication();
382 $this->validateUserAccess($entry->getUser()->getId());
383
384 $title = $request->request->get('title');
385 $isArchived = $request->request->get('archive');
386 $isStarred = $request->request->get('starred');
387
388 if (!is_null($title)) {
389 $entry->setTitle($title);
390 }
391
392 if (!is_null($isArchived)) {
393 $entry->setArchived((bool) $isArchived);
394 }
395
396 if (!is_null($isStarred)) {
397 $entry->setStarred((bool) $isStarred);
398 }
399
400 $tags = $request->request->get('tags', '');
401 if (!empty($tags)) {
402 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
403 }
404
405 $em = $this->getDoctrine()->getManager();
406 $em->flush();
407
408 return $this->sendResponse($entry);
409 }
410
411 /**
412 * Reload an entry.
413 * 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).
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 patchEntriesReloadAction(Entry $entry)
424 {
425 $this->validateAuthentication();
426 $this->validateUserAccess($entry->getUser()->getId());
427
428 try {
429 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
430 } catch (\Exception $e) {
431 $this->get('logger')->error('Error while saving an entry', [
432 'exception' => $e,
433 'entry' => $entry,
434 ]);
435
436 return new JsonResponse([], 304);
437 }
438
439 // if refreshing entry failed, don't save it
440 if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) {
441 return new JsonResponse([], 304);
442 }
443
444 $em = $this->getDoctrine()->getManager();
445 $em->persist($entry);
446 $em->flush();
447
448 // entry saved, dispatch event about it!
449 $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
450
451 return $this->sendResponse($entry);
452 }
453
454 /**
455 * Delete **permanently** an entry.
456 *
457 * @ApiDoc(
458 * requirements={
459 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
460 * }
461 * )
462 *
463 * @return JsonResponse
464 */
465 public function deleteEntriesAction(Entry $entry)
466 {
467 $this->validateAuthentication();
468 $this->validateUserAccess($entry->getUser()->getId());
469
470 $em = $this->getDoctrine()->getManager();
471 $em->remove($entry);
472 $em->flush();
473
474 // entry deleted, dispatch event about it!
475 $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry));
476
477 return $this->sendResponse($entry);
478 }
479
480 /**
481 * Retrieve all tags for an entry.
482 *
483 * @ApiDoc(
484 * requirements={
485 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
486 * }
487 * )
488 *
489 * @return JsonResponse
490 */
491 public function getEntriesTagsAction(Entry $entry)
492 {
493 $this->validateAuthentication();
494 $this->validateUserAccess($entry->getUser()->getId());
495
496 return $this->sendResponse($entry->getTags());
497 }
498
499 /**
500 * Add one or more tags to an entry.
501 *
502 * @ApiDoc(
503 * requirements={
504 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
505 * },
506 * parameters={
507 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
508 * }
509 * )
510 *
511 * @return JsonResponse
512 */
513 public function postEntriesTagsAction(Request $request, Entry $entry)
514 {
515 $this->validateAuthentication();
516 $this->validateUserAccess($entry->getUser()->getId());
517
518 $tags = $request->request->get('tags', '');
519 if (!empty($tags)) {
520 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
521 }
522
523 $em = $this->getDoctrine()->getManager();
524 $em->persist($entry);
525 $em->flush();
526
527 return $this->sendResponse($entry);
528 }
529
530 /**
531 * Permanently remove one tag for an entry.
532 *
533 * @ApiDoc(
534 * requirements={
535 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
536 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
537 * }
538 * )
539 *
540 * @return JsonResponse
541 */
542 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
543 {
544 $this->validateAuthentication();
545 $this->validateUserAccess($entry->getUser()->getId());
546
547 $entry->removeTag($tag);
548 $em = $this->getDoctrine()->getManager();
549 $em->persist($entry);
550 $em->flush();
551
552 return $this->sendResponse($entry);
553 }
554
555 /**
556 * Handles an entries list delete tags from them.
557 *
558 * @ApiDoc(
559 * parameters={
560 * {"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."}
561 * }
562 * )
563 *
564 * @return JsonResponse
565 */
566 public function deleteEntriesTagsListAction(Request $request)
567 {
568 $this->validateAuthentication();
569
570 $list = json_decode($request->query->get('list', []));
571
572 if (empty($list)) {
573 return $this->sendResponse([]);
574 }
575
576 // handle multiple urls
577 $results = [];
578
579 foreach ($list as $key => $element) {
580 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
581 $element->url,
582 $this->getUser()->getId()
583 );
584
585 $results[$key]['url'] = $element->url;
586 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
587
588 $tags = $element->tags;
589
590 if (false !== $entry && !(empty($tags))) {
591 $tags = explode(',', $tags);
592 foreach ($tags as $label) {
593 $label = trim($label);
594
595 $tag = $this->getDoctrine()
596 ->getRepository('WallabagCoreBundle:Tag')
597 ->findOneByLabel($label);
598
599 if (false !== $tag) {
600 $entry->removeTag($tag);
601 }
602 }
603
604 $em = $this->getDoctrine()->getManager();
605 $em->persist($entry);
606 $em->flush();
607 }
608 }
609
610 return $this->sendResponse($results);
611 }
612
613 /**
614 * Handles an entries list and add tags to them.
615 *
616 * @ApiDoc(
617 * parameters={
618 * {"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."}
619 * }
620 * )
621 *
622 * @return JsonResponse
623 */
624 public function postEntriesTagsListAction(Request $request)
625 {
626 $this->validateAuthentication();
627
628 $list = json_decode($request->query->get('list', []));
629
630 if (empty($list)) {
631 return $this->sendResponse([]);
632 }
633
634 $results = [];
635
636 // handle multiple urls
637 foreach ($list as $key => $element) {
638 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId(
639 $element->url,
640 $this->getUser()->getId()
641 );
642
643 $results[$key]['url'] = $element->url;
644 $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false;
645
646 $tags = $element->tags;
647
648 if (false !== $entry && !(empty($tags))) {
649 $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
650
651 $em = $this->getDoctrine()->getManager();
652 $em->persist($entry);
653 $em->flush();
654 }
655 }
656
657 return $this->sendResponse($results);
658 }
659
660 /**
661 * Shortcut to send data serialized in json.
662 *
663 * @param mixed $data
664 *
665 * @return JsonResponse
666 */
667 private function sendResponse($data)
668 {
669 $json = $this->get('serializer')->serialize($data, 'json');
670
671 return (new JsonResponse())->setJson($json);
672 }
673 }