diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/EntryRestController.php | 254 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Form/Type/ClientType.php | 6 |
2 files changed, 182 insertions, 78 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7590efbb..632b16d9 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php | |||
@@ -5,6 +5,7 @@ namespace Wallabag\ApiBundle\Controller; | |||
5 | use Hateoas\Configuration\Route; | 5 | use Hateoas\Configuration\Route; |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | 6 | use Hateoas\Representation\Factory\PagerfantaFactory; |
7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | 7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
8 | use Symfony\Component\HttpKernel\Exception\HttpException; | ||
8 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
9 | use Symfony\Component\HttpFoundation\JsonResponse; | 10 | use Symfony\Component\HttpFoundation\JsonResponse; |
10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | 11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
@@ -44,9 +45,7 @@ class EntryRestController extends WallabagRestController | |||
44 | $results[$url] = $res instanceof Entry ? $res->getId() : false; | 45 | $results[$url] = $res instanceof Entry ? $res->getId() : false; |
45 | } | 46 | } |
46 | 47 | ||
47 | $json = $this->get('serializer')->serialize($results, 'json'); | 48 | return $this->sendResponse($results); |
48 | |||
49 | return (new JsonResponse())->setJson($json); | ||
50 | } | 49 | } |
51 | 50 | ||
52 | // let's see if it is a simple url? | 51 | // let's see if it is a simple url? |
@@ -62,9 +61,7 @@ class EntryRestController extends WallabagRestController | |||
62 | 61 | ||
63 | $exists = $res instanceof Entry ? $res->getId() : false; | 62 | $exists = $res instanceof Entry ? $res->getId() : false; |
64 | 63 | ||
65 | $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); | 64 | return $this->sendResponse(['exists' => $exists]); |
66 | |||
67 | return (new JsonResponse())->setJson($json); | ||
68 | } | 65 | } |
69 | 66 | ||
70 | /** | 67 | /** |
@@ -98,12 +95,13 @@ class EntryRestController extends WallabagRestController | |||
98 | $tags = $request->query->get('tags', ''); | 95 | $tags = $request->query->get('tags', ''); |
99 | $since = $request->query->get('since', 0); | 96 | $since = $request->query->get('since', 0); |
100 | 97 | ||
98 | /** @var \Pagerfanta\Pagerfanta $pager */ | ||
101 | $pager = $this->getDoctrine() | 99 | $pager = $this->getDoctrine() |
102 | ->getRepository('WallabagCoreBundle:Entry') | 100 | ->getRepository('WallabagCoreBundle:Entry') |
103 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); | 101 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); |
104 | 102 | ||
105 | $pager->setCurrentPage($page); | ||
106 | $pager->setMaxPerPage($perPage); | 103 | $pager->setMaxPerPage($perPage); |
104 | $pager->setCurrentPage($page); | ||
107 | 105 | ||
108 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | 106 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); |
109 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | 107 | $paginatedCollection = $pagerfantaFactory->createRepresentation( |
@@ -124,9 +122,7 @@ class EntryRestController extends WallabagRestController | |||
124 | ) | 122 | ) |
125 | ); | 123 | ); |
126 | 124 | ||
127 | $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); | 125 | return $this->sendResponse($paginatedCollection); |
128 | |||
129 | return (new JsonResponse())->setJson($json); | ||
130 | } | 126 | } |
131 | 127 | ||
132 | /** | 128 | /** |
@@ -145,9 +141,7 @@ class EntryRestController extends WallabagRestController | |||
145 | $this->validateAuthentication(); | 141 | $this->validateAuthentication(); |
146 | $this->validateUserAccess($entry->getUser()->getId()); | 142 | $this->validateUserAccess($entry->getUser()->getId()); |
147 | 143 | ||
148 | $json = $this->get('serializer')->serialize($entry, 'json'); | 144 | return $this->sendResponse($entry); |
149 | |||
150 | return (new JsonResponse())->setJson($json); | ||
151 | } | 145 | } |
152 | 146 | ||
153 | /** | 147 | /** |
@@ -173,6 +167,110 @@ class EntryRestController extends WallabagRestController | |||
173 | } | 167 | } |
174 | 168 | ||
175 | /** | 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 | /** | ||
176 | * Create an entry. | 274 | * Create an entry. |
177 | * | 275 | * |
178 | * @ApiDoc( | 276 | * @ApiDoc( |
@@ -229,9 +327,7 @@ class EntryRestController extends WallabagRestController | |||
229 | // entry saved, dispatch event about it! | 327 | // entry saved, dispatch event about it! |
230 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | 328 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); |
231 | 329 | ||
232 | $json = $this->get('serializer')->serialize($entry, 'json'); | 330 | return $this->sendResponse($entry); |
233 | |||
234 | return (new JsonResponse())->setJson($json); | ||
235 | } | 331 | } |
236 | 332 | ||
237 | /** | 333 | /** |
@@ -280,9 +376,7 @@ class EntryRestController extends WallabagRestController | |||
280 | $em = $this->getDoctrine()->getManager(); | 376 | $em = $this->getDoctrine()->getManager(); |
281 | $em->flush(); | 377 | $em->flush(); |
282 | 378 | ||
283 | $json = $this->get('serializer')->serialize($entry, 'json'); | 379 | return $this->sendResponse($entry); |
284 | |||
285 | return (new JsonResponse())->setJson($json); | ||
286 | } | 380 | } |
287 | 381 | ||
288 | /** | 382 | /** |
@@ -325,9 +419,7 @@ class EntryRestController extends WallabagRestController | |||
325 | // entry saved, dispatch event about it! | 419 | // entry saved, dispatch event about it! |
326 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | 420 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); |
327 | 421 | ||
328 | $json = $this->get('serializer')->serialize($entry, 'json'); | 422 | return $this->sendResponse($entry); |
329 | |||
330 | return (new JsonResponse())->setJson($json); | ||
331 | } | 423 | } |
332 | 424 | ||
333 | /** | 425 | /** |
@@ -353,9 +445,7 @@ class EntryRestController extends WallabagRestController | |||
353 | // entry deleted, dispatch event about it! | 445 | // entry deleted, dispatch event about it! |
354 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | 446 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); |
355 | 447 | ||
356 | $json = $this->get('serializer')->serialize($entry, 'json'); | 448 | return $this->sendResponse($entry); |
357 | |||
358 | return (new JsonResponse())->setJson($json); | ||
359 | } | 449 | } |
360 | 450 | ||
361 | /** | 451 | /** |
@@ -374,9 +464,7 @@ class EntryRestController extends WallabagRestController | |||
374 | $this->validateAuthentication(); | 464 | $this->validateAuthentication(); |
375 | $this->validateUserAccess($entry->getUser()->getId()); | 465 | $this->validateUserAccess($entry->getUser()->getId()); |
376 | 466 | ||
377 | $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); | 467 | return $this->sendResponse($entry->getTags()); |
378 | |||
379 | return (new JsonResponse())->setJson($json); | ||
380 | } | 468 | } |
381 | 469 | ||
382 | /** | 470 | /** |
@@ -407,9 +495,7 @@ class EntryRestController extends WallabagRestController | |||
407 | $em->persist($entry); | 495 | $em->persist($entry); |
408 | $em->flush(); | 496 | $em->flush(); |
409 | 497 | ||
410 | $json = $this->get('serializer')->serialize($entry, 'json'); | 498 | return $this->sendResponse($entry); |
411 | |||
412 | return (new JsonResponse())->setJson($json); | ||
413 | } | 499 | } |
414 | 500 | ||
415 | /** | 501 | /** |
@@ -434,9 +520,7 @@ class EntryRestController extends WallabagRestController | |||
434 | $em->persist($entry); | 520 | $em->persist($entry); |
435 | $em->flush(); | 521 | $em->flush(); |
436 | 522 | ||
437 | $json = $this->get('serializer')->serialize($entry, 'json'); | 523 | return $this->sendResponse($entry); |
438 | |||
439 | return (new JsonResponse())->setJson($json); | ||
440 | } | 524 | } |
441 | 525 | ||
442 | /** | 526 | /** |
@@ -455,45 +539,46 @@ class EntryRestController extends WallabagRestController | |||
455 | $this->validateAuthentication(); | 539 | $this->validateAuthentication(); |
456 | 540 | ||
457 | $list = json_decode($request->query->get('list', [])); | 541 | $list = json_decode($request->query->get('list', [])); |
458 | $results = []; | 542 | |
543 | if (empty($list)) { | ||
544 | return $this->sendResponse([]); | ||
545 | } | ||
459 | 546 | ||
460 | // handle multiple urls | 547 | // handle multiple urls |
461 | if (!empty($list)) { | 548 | $results = []; |
462 | foreach ($list as $key => $element) { | ||
463 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | ||
464 | $element->url, | ||
465 | $this->getUser()->getId() | ||
466 | ); | ||
467 | 549 | ||
468 | $results[$key]['url'] = $element->url; | 550 | foreach ($list as $key => $element) { |
469 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | 551 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
552 | $element->url, | ||
553 | $this->getUser()->getId() | ||
554 | ); | ||
470 | 555 | ||
471 | $tags = $element->tags; | 556 | $results[$key]['url'] = $element->url; |
557 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | ||
472 | 558 | ||
473 | if (false !== $entry && !(empty($tags))) { | 559 | $tags = $element->tags; |
474 | $tags = explode(',', $tags); | ||
475 | foreach ($tags as $label) { | ||
476 | $label = trim($label); | ||
477 | 560 | ||
478 | $tag = $this->getDoctrine() | 561 | if (false !== $entry && !(empty($tags))) { |
479 | ->getRepository('WallabagCoreBundle:Tag') | 562 | $tags = explode(',', $tags); |
480 | ->findOneByLabel($label); | 563 | foreach ($tags as $label) { |
564 | $label = trim($label); | ||
481 | 565 | ||
482 | if (false !== $tag) { | 566 | $tag = $this->getDoctrine() |
483 | $entry->removeTag($tag); | 567 | ->getRepository('WallabagCoreBundle:Tag') |
484 | } | 568 | ->findOneByLabel($label); |
485 | } | ||
486 | 569 | ||
487 | $em = $this->getDoctrine()->getManager(); | 570 | if (false !== $tag) { |
488 | $em->persist($entry); | 571 | $entry->removeTag($tag); |
489 | $em->flush(); | 572 | } |
490 | } | 573 | } |
574 | |||
575 | $em = $this->getDoctrine()->getManager(); | ||
576 | $em->persist($entry); | ||
577 | $em->flush(); | ||
491 | } | 578 | } |
492 | } | 579 | } |
493 | 580 | ||
494 | $json = $this->get('serializer')->serialize($results, 'json'); | 581 | return $this->sendResponse($results); |
495 | |||
496 | return (new JsonResponse())->setJson($json); | ||
497 | } | 582 | } |
498 | 583 | ||
499 | /** | 584 | /** |
@@ -512,32 +597,47 @@ class EntryRestController extends WallabagRestController | |||
512 | $this->validateAuthentication(); | 597 | $this->validateAuthentication(); |
513 | 598 | ||
514 | $list = json_decode($request->query->get('list', [])); | 599 | $list = json_decode($request->query->get('list', [])); |
600 | |||
601 | if (empty($list)) { | ||
602 | return $this->sendResponse([]); | ||
603 | } | ||
604 | |||
515 | $results = []; | 605 | $results = []; |
516 | 606 | ||
517 | // handle multiple urls | 607 | // handle multiple urls |
518 | if (!empty($list)) { | 608 | foreach ($list as $key => $element) { |
519 | foreach ($list as $key => $element) { | 609 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
520 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | 610 | $element->url, |
521 | $element->url, | 611 | $this->getUser()->getId() |
522 | $this->getUser()->getId() | 612 | ); |
523 | ); | ||
524 | 613 | ||
525 | $results[$key]['url'] = $element->url; | 614 | $results[$key]['url'] = $element->url; |
526 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | 615 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; |
527 | 616 | ||
528 | $tags = $element->tags; | 617 | $tags = $element->tags; |
529 | 618 | ||
530 | if (false !== $entry && !(empty($tags))) { | 619 | if (false !== $entry && !(empty($tags))) { |
531 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); | 620 | $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); |
532 | 621 | ||
533 | $em = $this->getDoctrine()->getManager(); | 622 | $em = $this->getDoctrine()->getManager(); |
534 | $em->persist($entry); | 623 | $em->persist($entry); |
535 | $em->flush(); | 624 | $em->flush(); |
536 | } | ||
537 | } | 625 | } |
538 | } | 626 | } |
539 | 627 | ||
540 | $json = $this->get('serializer')->serialize($results, 'json'); | 628 | return $this->sendResponse($results); |
629 | } | ||
630 | |||
631 | /** | ||
632 | * Shortcut to send data serialized in json. | ||
633 | * | ||
634 | * @param mixed $data | ||
635 | * | ||
636 | * @return JsonResponse | ||
637 | */ | ||
638 | private function sendResponse($data) | ||
639 | { | ||
640 | $json = $this->get('serializer')->serialize($data, 'json'); | ||
541 | 641 | ||
542 | return (new JsonResponse())->setJson($json); | 642 | return (new JsonResponse())->setJson($json); |
543 | } | 643 | } |
diff --git a/src/Wallabag/ApiBundle/Form/Type/ClientType.php b/src/Wallabag/ApiBundle/Form/Type/ClientType.php index 0ea1a9c5..eaea4feb 100644 --- a/src/Wallabag/ApiBundle/Form/Type/ClientType.php +++ b/src/Wallabag/ApiBundle/Form/Type/ClientType.php | |||
@@ -16,7 +16,11 @@ class ClientType extends AbstractType | |||
16 | { | 16 | { |
17 | $builder | 17 | $builder |
18 | ->add('name', TextType::class, ['label' => 'developer.client.form.name_label']) | 18 | ->add('name', TextType::class, ['label' => 'developer.client.form.name_label']) |
19 | ->add('redirect_uris', UrlType::class, ['required' => false, 'label' => 'developer.client.form.redirect_uris_label']) | 19 | ->add('redirect_uris', UrlType::class, [ |
20 | 'required' => false, | ||
21 | 'label' => 'developer.client.form.redirect_uris_label', | ||
22 | 'property_path' => 'redirectUris', | ||
23 | ]) | ||
20 | ->add('save', SubmitType::class, ['label' => 'developer.client.form.save_label']) | 24 | ->add('save', SubmitType::class, ['label' => 'developer.client.form.save_label']) |
21 | ; | 25 | ; |
22 | 26 | ||