]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/WallabagRestController.php
Merge pull request #2363 from wallabag/clean-composer
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / WallabagRestController.php
CommitLineData
f8bf8952
NL
1<?php
2
769e19dc 3namespace Wallabag\ApiBundle\Controller;
f8bf8952 4
fcb1fba5 5use FOS\RestBundle\Controller\FOSRestController;
619cc453
JB
6use Hateoas\Configuration\Route;
7use Hateoas\Representation\Factory\PagerfantaFactory;
f8bf8952 8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
27f15aa4 9use Symfony\Component\HttpFoundation\Request;
60faee00 10use Symfony\Component\HttpFoundation\JsonResponse;
1d76102a 11use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
b0b893ea 12use Symfony\Component\Security\Core\Exception\AccessDeniedException;
be463487 13use Wallabag\CoreBundle\Entity\Entry;
653e8be4 14use Wallabag\CoreBundle\Entity\Tag;
f8bf8952 15
fcb1fba5 16class WallabagRestController extends FOSRestController
f8bf8952 17{
77273253
NL
18 private function validateAuthentication()
19 {
18f8f32f 20 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
77273253
NL
21 throw new AccessDeniedException();
22 }
23 }
24
6273fefd 25 /**
3583cadf 26 * Check if an entry exist by url.
6273fefd
JB
27 *
28 * @ApiDoc(
29 * parameters={
30 * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}
31 * }
32 * )
33 *
34 * @return JsonResponse
35 */
36 public function getEntriesExistsAction(Request $request)
37 {
38 $this->validateAuthentication();
39
40 $url = $request->query->get('url', '');
41
42 if (empty($url)) {
43 throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$user->getId());
44 }
45
46 $res = $this->getDoctrine()
47 ->getRepository('WallabagCoreBundle:Entry')
48 ->findByUrlAndUserId($url, $this->getUser()->getId());
49
50 $exists = false === $res ? false : true;
51
52 $json = $this->get('serializer')->serialize(['exists' => $exists], 'json');
53
54 return (new JsonResponse())->setJson($json);
55 }
56
f8bf8952 57 /**
a8c90c5c 58 * Retrieve all entries. It could be filtered by many options.
f8bf8952
NL
59 *
60 * @ApiDoc(
a8c90c5c 61 * parameters={
189ef634
TC
62 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."},
63 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."},
a8c90c5c
NL
64 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
65 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
66 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
67 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
189ef634 68 * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
e5fb89e5 69 * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."},
a8c90c5c 70 * }
f8bf8952 71 * )
4346a860 72 *
60faee00 73 * @return JsonResponse
f8bf8952 74 */
27f15aa4 75 public function getEntriesAction(Request $request)
f8bf8952 76 {
77273253
NL
77 $this->validateAuthentication();
78
0135c98b
NL
79 $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive');
80 $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred');
8ce32af6
JB
81 $sort = $request->query->get('sort', 'created');
82 $order = $request->query->get('order', 'desc');
83 $page = (int) $request->query->get('page', 1);
84 $perPage = (int) $request->query->get('perPage', 30);
28803f10 85 $tags = $request->query->get('tags', '');
c3f8b428 86 $since = $request->query->get('since', 0);
a8c90c5c 87
fc732227 88 $pager = $this->getDoctrine()
be463487 89 ->getRepository('WallabagCoreBundle:Entry')
28803f10 90 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags);
a8c90c5c 91
6e22bd73
WD
92 $pager->setCurrentPage($page);
93 $pager->setMaxPerPage($perPage);
94
8ce32af6 95 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
6e22bd73
WD
96 $paginatedCollection = $pagerfantaFactory->createRepresentation(
97 $pager,
c3f8b428
JB
98 new Route(
99 'api_get_entries',
100 [
101 'archive' => $isArchived,
102 'starred' => $isStarred,
103 'sort' => $sort,
104 'order' => $order,
105 'page' => $page,
106 'perPage' => $perPage,
107 'tags' => $tags,
108 'since' => $since,
109 ],
110 UrlGeneratorInterface::ABSOLUTE_URL
111 )
6e22bd73
WD
112 );
113
114 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
0f006880 115
60faee00 116 return (new JsonResponse())->setJson($json);
f8bf8952
NL
117 }
118
119 /**
4346a860 120 * Retrieve a single entry.
f8bf8952
NL
121 *
122 * @ApiDoc(
123 * requirements={
124 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
125 * }
126 * )
4346a860 127 *
60faee00 128 * @return JsonResponse
f8bf8952 129 */
be463487 130 public function getEntryAction(Entry $entry)
f8bf8952 131 {
77273253 132 $this->validateAuthentication();
fcb1fba5 133 $this->validateUserAccess($entry->getUser()->getId());
092ca707 134
aa4d6562 135 $json = $this->get('serializer')->serialize($entry, 'json');
0f006880 136
60faee00 137 return (new JsonResponse())->setJson($json);
f8bf8952
NL
138 }
139
140 /**
4346a860 141 * Create an entry.
f8bf8952
NL
142 *
143 * @ApiDoc(
a8c90c5c
NL
144 * parameters={
145 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
146 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
147 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
189ef634
TC
148 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"},
149 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"},
a8c90c5c 150 * }
f8bf8952 151 * )
4346a860 152 *
60faee00 153 * @return JsonResponse
f8bf8952 154 */
843dbe51 155 public function postEntriesAction(Request $request)
f8bf8952 156 {
77273253
NL
157 $this->validateAuthentication();
158
c3235553 159 $url = $request->request->get('url');
51a15609 160 $title = $request->request->get('title');
873e3806
YE
161 $isArchived = $request->request->get('archive');
162 $isStarred = $request->request->get('starred');
c3235553 163
3107f92a
TC
164 $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId());
165
166 if (false === $entry) {
167 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
168 new Entry($this->getUser()),
169 $url
170 );
171 }
092ca707 172
51a15609
NL
173 if (!is_null($title)) {
174 $entry->setTitle($title);
175 }
176
0ca374e6
NL
177 $tags = $request->request->get('tags', '');
178 if (!empty($tags)) {
c2656f96 179 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
0ca374e6 180 }
092ca707 181
bc2b947c
TC
182 if (!is_null($isStarred)) {
183 $entry->setStarred((bool) $isStarred);
184 }
816ad405 185
bc2b947c
TC
186 if (!is_null($isArchived)) {
187 $entry->setArchived((bool) $isArchived);
188 }
816ad405 189
843dbe51
NL
190 $em = $this->getDoctrine()->getManager();
191 $em->persist($entry);
816ad405 192
843dbe51
NL
193 $em->flush();
194
aa4d6562
NL
195 $json = $this->get('serializer')->serialize($entry, 'json');
196
60faee00 197 return (new JsonResponse())->setJson($json);
f8bf8952
NL
198 }
199
200 /**
4346a860 201 * Change several properties of an entry.
f8bf8952
NL
202 *
203 * @ApiDoc(
204 * requirements={
205 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
a8c90c5c
NL
206 * },
207 * parameters={
208 * {"name"="title", "dataType"="string", "required"=false},
209 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
189ef634
TC
210 * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."},
211 * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."},
1d147791 212 * }
f8bf8952 213 * )
4346a860 214 *
60faee00 215 * @return JsonResponse
f8bf8952 216 */
be463487 217 public function patchEntriesAction(Entry $entry, Request $request)
f8bf8952 218 {
77273253 219 $this->validateAuthentication();
fcb1fba5 220 $this->validateUserAccess($entry->getUser()->getId());
092ca707 221
8ce32af6 222 $title = $request->request->get('title');
614a0bfd
YE
223 $isArchived = $request->request->get('archive');
224 $isStarred = $request->request->get('starred');
2c093b03
NL
225
226 if (!is_null($title)) {
227 $entry->setTitle($title);
228 }
229
230 if (!is_null($isArchived)) {
189ef634 231 $entry->setArchived((bool) $isArchived);
2c093b03
NL
232 }
233
2c093b03 234 if (!is_null($isStarred)) {
189ef634 235 $entry->setStarred((bool) $isStarred);
2c093b03
NL
236 }
237
0ca374e6
NL
238 $tags = $request->request->get('tags', '');
239 if (!empty($tags)) {
c2656f96 240 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
0ca374e6 241 }
092ca707 242
2c093b03 243 $em = $this->getDoctrine()->getManager();
2c093b03
NL
244 $em->flush();
245
0ca374e6
NL
246 $json = $this->get('serializer')->serialize($entry, 'json');
247
60faee00 248 return (new JsonResponse())->setJson($json);
f8bf8952
NL
249 }
250
251 /**
4346a860 252 * Delete **permanently** an entry.
f8bf8952
NL
253 *
254 * @ApiDoc(
a8c90c5c
NL
255 * requirements={
256 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
257 * }
f8bf8952 258 * )
4346a860 259 *
60faee00 260 * @return JsonResponse
f8bf8952 261 */
be463487 262 public function deleteEntriesAction(Entry $entry)
f8bf8952 263 {
77273253 264 $this->validateAuthentication();
fcb1fba5 265 $this->validateUserAccess($entry->getUser()->getId());
092ca707 266
42a90646 267 $em = $this->getDoctrine()->getManager();
1d147791 268 $em->remove($entry);
42a90646
NL
269 $em->flush();
270
1d147791
NL
271 $json = $this->get('serializer')->serialize($entry, 'json');
272
60faee00 273 return (new JsonResponse())->setJson($json);
f8bf8952
NL
274 }
275
276 /**
4346a860 277 * Retrieve all tags for an entry.
f8bf8952
NL
278 *
279 * @ApiDoc(
280 * requirements={
281 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
282 * }
283 * )
8eedc8cf 284 *
60faee00 285 * @return JsonResponse
f8bf8952 286 */
be463487 287 public function getEntriesTagsAction(Entry $entry)
7df80cb3 288 {
77273253 289 $this->validateAuthentication();
fcb1fba5 290 $this->validateUserAccess($entry->getUser()->getId());
092ca707 291
1bd12b62 292 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
0a018fe0 293
60faee00 294 return (new JsonResponse())->setJson($json);
f8bf8952
NL
295 }
296
297 /**
4346a860 298 * Add one or more tags to an entry.
f8bf8952
NL
299 *
300 * @ApiDoc(
301 * requirements={
302 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
a8c90c5c
NL
303 * },
304 * parameters={
305 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
306 * }
f8bf8952 307 * )
8eedc8cf 308 *
60faee00 309 * @return JsonResponse
f8bf8952 310 */
a36737f4 311 public function postEntriesTagsAction(Request $request, Entry $entry)
7df80cb3 312 {
77273253 313 $this->validateAuthentication();
fcb1fba5 314 $this->validateUserAccess($entry->getUser()->getId());
a36737f4 315
0ca374e6
NL
316 $tags = $request->request->get('tags', '');
317 if (!empty($tags)) {
c2656f96 318 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
0ca374e6 319 }
092ca707 320
a36737f4
NL
321 $em = $this->getDoctrine()->getManager();
322 $em->persist($entry);
323 $em->flush();
324
325 $json = $this->get('serializer')->serialize($entry, 'json');
326
60faee00 327 return (new JsonResponse())->setJson($json);
f8bf8952
NL
328 }
329
330 /**
4346a860 331 * Permanently remove one tag for an entry.
f8bf8952
NL
332 *
333 * @ApiDoc(
334 * requirements={
769e19dc 335 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"},
f8bf8952
NL
336 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
337 * }
338 * )
8eedc8cf 339 *
60faee00 340 * @return JsonResponse
f8bf8952 341 */
653e8be4 342 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
f8bf8952 343 {
77273253 344 $this->validateAuthentication();
fcb1fba5 345 $this->validateUserAccess($entry->getUser()->getId());
092ca707
NL
346
347 $entry->removeTag($tag);
348 $em = $this->getDoctrine()->getManager();
349 $em->persist($entry);
350 $em->flush();
351
352 $json = $this->get('serializer')->serialize($entry, 'json');
353
60faee00 354 return (new JsonResponse())->setJson($json);
f8bf8952
NL
355 }
356
357 /**
4346a860 358 * Retrieve all tags.
f8bf8952 359 *
092ca707 360 * @ApiDoc()
8eedc8cf 361 *
60faee00 362 * @return JsonResponse
f8bf8952 363 */
092ca707 364 public function getTagsAction()
7df80cb3 365 {
77273253 366 $this->validateAuthentication();
fc732227
JB
367
368 $tags = $this->getDoctrine()
369 ->getRepository('WallabagCoreBundle:Tag')
faa86e06 370 ->findAllTagsWithEntries($this->getUser()->getId());
fc732227
JB
371
372 $json = $this->get('serializer')->serialize($tags, 'json');
092ca707 373
60faee00 374 return (new JsonResponse())->setJson($json);
f8bf8952
NL
375 }
376
f8bf8952 377 /**
4346a860 378 * Permanently remove one tag from **every** entry.
f8bf8952
NL
379 *
380 * @ApiDoc(
381 * requirements={
a0e1eafc 382 * {"name"="tag", "dataType"="string", "required"=true, "requirement"="\w+", "description"="Tag as a string"}
f8bf8952
NL
383 * }
384 * )
8eedc8cf 385 *
60faee00 386 * @return JsonResponse
f8bf8952 387 */
a0e1eafc 388 public function deleteTagLabelAction(Request $request)
f8bf8952 389 {
77273253 390 $this->validateAuthentication();
a0e1eafc
JB
391 $label = $request->request->get('tag', '');
392
393 $tag = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($label);
394
395 if (empty($tag)) {
396 throw $this->createNotFoundException('Tag not found');
397 }
fc732227
JB
398
399 $this->getDoctrine()
400 ->getRepository('WallabagCoreBundle:Entry')
401 ->removeTag($this->getUser()->getId(), $tag);
092ca707 402
092ca707
NL
403 $json = $this->get('serializer')->serialize($tag, 'json');
404
60faee00 405 return (new JsonResponse())->setJson($json);
769e19dc 406 }
4da01f49
TC
407
408 /**
a0e1eafc 409 * Permanently remove some tags from **every** entry.
4da01f49
TC
410 *
411 * @ApiDoc(
412 * requirements={
a0e1eafc 413 * {"name"="tags", "dataType"="string", "required"=true, "format"="tag1,tag2", "description"="Tags as strings (comma splitted)"}
4da01f49
TC
414 * }
415 * )
416 *
60faee00 417 * @return JsonResponse
4da01f49 418 */
a0e1eafc 419 public function deleteTagsLabelAction(Request $request)
4da01f49
TC
420 {
421 $this->validateAuthentication();
4da01f49 422
a0e1eafc
JB
423 $tagsLabels = $request->request->get('tags', '');
424
425 $tags = [];
426
427 foreach (explode(',', $tagsLabels) as $tagLabel) {
428 $tagEntity = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
429
430 if (!empty($tagEntity)) {
431 $tags[] = $tagEntity;
432 }
433 }
434
435 if (empty($tags)) {
436 throw $this->createNotFoundException('Tags not found');
437 }
438
4da01f49
TC
439 $this->getDoctrine()
440 ->getRepository('WallabagCoreBundle:Entry')
a0e1eafc 441 ->removeTags($this->getUser()->getId(), $tags);
4da01f49 442
a0e1eafc 443 $json = $this->get('serializer')->serialize($tags, 'json');
4da01f49 444
60faee00 445 return (new JsonResponse())->setJson($json);
4da01f49
TC
446 }
447
448 /**
a0e1eafc 449 * Permanently remove one tag from **every** entry.
4da01f49
TC
450 *
451 * @ApiDoc(
452 * requirements={
a0e1eafc 453 * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag"}
4da01f49
TC
454 * }
455 * )
456 *
60faee00 457 * @return JsonResponse
4da01f49 458 */
a0e1eafc 459 public function deleteTagAction(Tag $tag)
4da01f49
TC
460 {
461 $this->validateAuthentication();
462
4da01f49
TC
463 $this->getDoctrine()
464 ->getRepository('WallabagCoreBundle:Entry')
a0e1eafc 465 ->removeTag($this->getUser()->getId(), $tag);
4da01f49 466
a0e1eafc 467 $json = $this->get('serializer')->serialize($tag, 'json');
4da01f49 468
60faee00 469 return (new JsonResponse())->setJson($json);
4da01f49
TC
470 }
471
2b477030 472 /**
6f8310b4
TC
473 * Retrieve version number.
474 *
475 * @ApiDoc()
2b477030 476 *
60faee00 477 * @return JsonResponse
2b477030
V
478 */
479 public function getVersionAction()
480 {
481 $version = $this->container->getParameter('wallabag_core.version');
482
483 $json = $this->get('serializer')->serialize($version, 'json');
484
60faee00 485 return (new JsonResponse())->setJson($json);
2b477030 486 }
769e19dc
J
487
488 /**
489 * Validate that the first id is equal to the second one.
4346a860 490 * If not, throw exception. It means a user try to access information from an other user.
769e19dc 491 *
4346a860 492 * @param int $requestUserId User id from the requested source
769e19dc 493 */
fcb1fba5 494 private function validateUserAccess($requestUserId)
769e19dc 495 {
18f8f32f 496 $user = $this->get('security.token_storage')->getToken()->getUser();
fcb1fba5
NL
497 if ($requestUserId != $user->getId()) {
498 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$requestUserId.', logged user id: '.$user->getId());
769e19dc
J
499 }
500 }
7df80cb3 501}