From ca9a83ee511a1e9c54d375b76509de699bd2164a Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Fri, 24 Feb 2017 11:31:31 +0100 Subject: return id of article instead of true if entry exists --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 2c2ec0c1..af5f7603 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -41,7 +41,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $results[$url] = false === $res ? false : true; + $results[$url] = $res instanceof Entry ? $res->getId() : false; } $json = $this->get('serializer')->serialize($results, 'json'); @@ -60,7 +60,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $exists = false === $res ? false : true; + $exists = $res instanceof Entry ? $res->getId() : false; $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); -- cgit v1.2.3 From d1fc590211b8dc7360bf5b7ee01c67ccff0577ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 24 Apr 2017 11:12:41 +0200 Subject: Added API endpoint to handle a list of URL and to add/delete tags --- .../ApiBundle/Controller/EntryRestController.php | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index af5f7603..fc46e782 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -438,4 +438,72 @@ class EntryRestController extends WallabagRestController return (new JsonResponse())->setJson($json); } + + /** + * Handles an entries list and add / delete to them some tags. + * + * @ApiDoc( + * parameters={ + * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2','action': 'delete'}, {'url': 'http://...','tags': 'tag1, tag2','action': 'add'}]", "description"="Urls (as an array) to handle."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesTagsListAction(Request $request) + { + $this->validateAuthentication(); + + $list = json_decode($request->query->get('list', [])); + $results = []; + + // handle multiple urls + if (!empty($list)) { + $results = []; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); + + $results[$key]['url'] = $element->url; + $results[$key]['action'] = $element->action; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + $tags = $element->tags; + + if (false !== $entry && !(empty($tags))) { + switch ($element->action) { + case 'delete': + $tags = explode(',', $tags); + foreach ($tags as $label) { + $label = trim($label); + + $tag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($label); + + if (false !== $tag) { + $entry->removeTag($tag); + } + } + + break; + case 'add': + $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + + break; + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + } + } + } + + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } } -- cgit v1.2.3 From 80299ed282d4f18ef92a79f29f9346b96acde468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 24 Apr 2017 12:24:17 +0200 Subject: Added endpoint to handle URL list to add/delete tags --- .../ApiBundle/Controller/EntryRestController.php | 85 ++++++++++++++++------ 1 file changed, 61 insertions(+), 24 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index fc46e782..5ccaa4ef 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -440,17 +440,17 @@ class EntryRestController extends WallabagRestController } /** - * Handles an entries list and add / delete to them some tags. + * Handles an entries list delete tags from them. * * @ApiDoc( * parameters={ - * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...','tags': 'tag1, tag2','action': 'delete'}, {'url': 'http://...','tags': 'tag1, tag2','action': 'add'}]", "description"="Urls (as an array) to handle."} + * {"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."} * } * ) * * @return JsonResponse */ - public function postEntriesTagsListAction(Request $request) + public function deleteEntriesTagsListAction(Request $request) { $this->validateAuthentication(); @@ -467,32 +467,22 @@ class EntryRestController extends WallabagRestController ); $results[$key]['url'] = $element->url; - $results[$key]['action'] = $element->action; $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; $tags = $element->tags; if (false !== $entry && !(empty($tags))) { - switch ($element->action) { - case 'delete': - $tags = explode(',', $tags); - foreach ($tags as $label) { - $label = trim($label); - - $tag = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabel($label); - - if (false !== $tag) { - $entry->removeTag($tag); - } - } - - break; - case 'add': - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); - - break; + $tags = explode(',', $tags); + foreach ($tags as $label) { + $label = trim($label); + + $tag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($label); + + if (false !== $tag) { + $entry->removeTag($tag); + } } $em = $this->getDoctrine()->getManager(); @@ -506,4 +496,51 @@ class EntryRestController extends WallabagRestController return (new JsonResponse())->setJson($json); } + + /** + * Handles an entries list and add tags to them. + * + * @ApiDoc( + * parameters={ + * {"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."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesTagsListAction(Request $request) + { + $this->validateAuthentication(); + + $list = json_decode($request->query->get('list', [])); + $results = []; + + // handle multiple urls + if (!empty($list)) { + $results = []; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); + + $results[$key]['url'] = $element->url; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + $tags = $element->tags; + + if (false !== $entry && !(empty($tags))) { + $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + } + } + } + + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } } -- cgit v1.2.3 From dcbebc17aaa50ea16eb0b7e379c14ebbcf0a645a Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 29 Apr 2017 12:58:39 +0200 Subject: Fix tests --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 5ccaa4ef..7590efbb 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -459,7 +459,6 @@ class EntryRestController extends WallabagRestController // handle multiple urls if (!empty($list)) { - $results = []; foreach ($list as $key => $element) { $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( $element->url, @@ -517,7 +516,6 @@ class EntryRestController extends WallabagRestController // handle multiple urls if (!empty($list)) { - $results = []; foreach ($list as $key => $element) { $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( $element->url, -- cgit v1.2.3 From 1eca7831a69b9470b92dcc72e1ce51b42b291338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 24 Apr 2017 10:22:57 +0200 Subject: Added API endpoint to handle a list of URL By passing an array, you can add / delete URL in mass (bulk request) --- .../ApiBundle/Controller/EntryRestController.php | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7590efbb..3833ce3c 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -172,6 +172,77 @@ class EntryRestController extends WallabagRestController ->exportAs($request->attributes->get('_format')); } + /** + * Handles an entries list and create or remove URL. + * + * @ApiDoc( + * parameters={ + * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesListAction(Request $request) + { + $this->validateAuthentication(); + + $list = json_decode($request->query->get('list', [])); + $results = []; + + // handle multiple urls + if (!empty($list)) { + $results = []; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); + + $results[$key]['url'] = $element->url; + $results[$key]['action'] = $element->action; + + switch ($element->action) { + case 'delete': + if (false !== $entry) { + $em = $this->getDoctrine()->getManager(); + $em->remove($entry); + $em->flush(); + + // entry deleted, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); + } + + $results[$key]['entry'] = $entry instanceof Entry ? true : false; + + break; + case 'add': + if (false === $entry) { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + new Entry($this->getUser()), + $element->url + ); + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + + break; + } + } + } + + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } + /** * Create an entry. * -- cgit v1.2.3 From a7abcc7b7a5e3417eff70e2a5993558f83fc5d5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 24 Apr 2017 11:31:00 +0200 Subject: Splitted the endpoint in two --- .../ApiBundle/Controller/EntryRestController.php | 96 ++++++++++++++-------- 1 file changed, 62 insertions(+), 34 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 3833ce3c..0c98c242 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -173,68 +173,96 @@ class EntryRestController extends WallabagRestController } /** - * Handles an entries list and create or remove URL. + * Handles an entries list and delete URL. * * @ApiDoc( * parameters={ - * {"name"="list", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...', 'action': 'delete'}, {'url': 'http://...', 'action': 'add'}]", "description"="Urls (as an array) to handle."} + * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} * } * ) * * @return JsonResponse */ - public function postEntriesListAction(Request $request) + public function deleteEntriesListAction(Request $request) { $this->validateAuthentication(); - $list = json_decode($request->query->get('list', [])); + $urls = json_decode($request->query->get('urls', [])); $results = []; // handle multiple urls - if (!empty($list)) { + if (!empty($urls)) { $results = []; - foreach ($list as $key => $element) { + foreach ($urls as $key => $url) { $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $element->url, + $url, $this->getUser()->getId() ); - $results[$key]['url'] = $element->url; - $results[$key]['action'] = $element->action; + $results[$key]['url'] = $url; - switch ($element->action) { - case 'delete': - if (false !== $entry) { - $em = $this->getDoctrine()->getManager(); - $em->remove($entry); - $em->flush(); + if (false !== $entry) { + $em = $this->getDoctrine()->getManager(); + $em->remove($entry); + $em->flush(); - // entry deleted, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); - } + // entry deleted, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); + } - $results[$key]['entry'] = $entry instanceof Entry ? true : false; + $results[$key]['entry'] = $entry instanceof Entry ? true : false; + } + } - break; - case 'add': - if (false === $entry) { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry( - new Entry($this->getUser()), - $element->url - ); - } + $json = $this->get('serializer')->serialize($results, 'json'); + + return (new JsonResponse())->setJson($json); + } + + /** + * Handles an entries list and create URL. + * + * @ApiDoc( + * parameters={ + * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} + * } + * ) + * + * @return JsonResponse + */ + public function postEntriesListAction(Request $request) + { + $this->validateAuthentication(); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); + $urls = json_decode($request->query->get('urls', [])); + $results = []; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + // handle multiple urls + if (!empty($urls)) { + $results = []; + foreach ($urls as $key => $url) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $url, + $this->getUser()->getId() + ); - // entry saved, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + $results[$key]['url'] = $url; - break; + if (false === $entry) { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + new Entry($this->getUser()), + $url + ); } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); } } -- cgit v1.2.3 From 719ba257d3d6495eaa0f4ea749b5aa4740f94dda Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 29 Apr 2017 12:24:27 +0200 Subject: Fix tests --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 1 - 1 file changed, 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 0c98c242..ae6f0e3f 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -239,7 +239,6 @@ class EntryRestController extends WallabagRestController // handle multiple urls if (!empty($urls)) { - $results = []; foreach ($urls as $key => $url) { $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( $url, -- cgit v1.2.3 From efd351c98fa0caa4c8df9c7ff6965c537524f12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 1 May 2017 09:21:59 +0200 Subject: Added limit --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index ae6f0e3f..7c3e778e 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -5,6 +5,7 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; use Nelmio\ApiDocBundle\Annotation\ApiDoc; +use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -229,6 +230,8 @@ class EntryRestController extends WallabagRestController * ) * * @return JsonResponse + * + * @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached */ public function postEntriesListAction(Request $request) { @@ -237,6 +240,12 @@ class EntryRestController extends WallabagRestController $urls = json_decode($request->query->get('urls', [])); $results = []; + $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); + + if (count($urls) > $limit) { + throw new Exception('API limit reached'); + } + // handle multiple urls if (!empty($urls)) { foreach ($urls as $key => $url) { -- cgit v1.2.3 From 72db15ca5d7950a604f359056fc6a627f25e4ee4 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 5 May 2017 12:05:50 +0200 Subject: Little refacto and send 400 on reaching urls limit --- .../ApiBundle/Controller/EntryRestController.php | 196 ++++++++++----------- 1 file changed, 94 insertions(+), 102 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7c3e778e..dbff6065 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -5,7 +5,7 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; use Nelmio\ApiDocBundle\Annotation\ApiDoc; -use Symfony\Component\Config\Definition\Exception\Exception; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; @@ -45,9 +45,7 @@ class EntryRestController extends WallabagRestController $results[$url] = $res instanceof Entry ? $res->getId() : false; } - $json = $this->get('serializer')->serialize($results, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } // let's see if it is a simple url? @@ -63,9 +61,7 @@ class EntryRestController extends WallabagRestController $exists = $res instanceof Entry ? $res->getId() : false; - $json = $this->get('serializer')->serialize(['exists' => $exists], 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse(['exists' => $exists]); } /** @@ -125,9 +121,7 @@ class EntryRestController extends WallabagRestController ) ); - $json = $this->get('serializer')->serialize($paginatedCollection, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($paginatedCollection); } /** @@ -146,9 +140,7 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -189,35 +181,35 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $urls = json_decode($request->query->get('urls', [])); + + if (empty($urls)) { + return $this->sendResponse([]); + } + $results = []; // handle multiple urls - if (!empty($urls)) { - $results = []; - foreach ($urls as $key => $url) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $url, - $this->getUser()->getId() - ); - - $results[$key]['url'] = $url; + foreach ($urls as $key => $url) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $url, + $this->getUser()->getId() + ); - if (false !== $entry) { - $em = $this->getDoctrine()->getManager(); - $em->remove($entry); - $em->flush(); + $results[$key]['url'] = $url; - // entry deleted, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); - } + if (false !== $entry) { + $em = $this->getDoctrine()->getManager(); + $em->remove($entry); + $em->flush(); - $results[$key]['entry'] = $entry instanceof Entry ? true : false; + // entry deleted, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); } - } - $json = $this->get('serializer')->serialize($results, 'json'); + $results[$key]['entry'] = $entry instanceof Entry ? true : false; + } - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } /** @@ -231,7 +223,7 @@ class EntryRestController extends WallabagRestController * * @return JsonResponse * - * @throws Symfony\Component\Config\Definition\Exception\Exception When limit is reached + * @throws HttpException When limit is reached */ public function postEntriesListAction(Request $request) { @@ -243,7 +235,7 @@ class EntryRestController extends WallabagRestController $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); if (count($urls) > $limit) { - throw new Exception('API limit reached'); + throw new HttpException(400, 'API limit reached'); } // handle multiple urls @@ -274,9 +266,7 @@ class EntryRestController extends WallabagRestController } } - $json = $this->get('serializer')->serialize($results, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } /** @@ -336,9 +326,7 @@ class EntryRestController extends WallabagRestController // entry saved, dispatch event about it! $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -387,9 +375,7 @@ class EntryRestController extends WallabagRestController $em = $this->getDoctrine()->getManager(); $em->flush(); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -432,9 +418,7 @@ class EntryRestController extends WallabagRestController // entry saved, dispatch event about it! $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -460,9 +444,7 @@ class EntryRestController extends WallabagRestController // entry deleted, dispatch event about it! $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -481,9 +463,7 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry->getTags()); } /** @@ -514,9 +494,7 @@ class EntryRestController extends WallabagRestController $em->persist($entry); $em->flush(); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -541,9 +519,7 @@ class EntryRestController extends WallabagRestController $em->persist($entry); $em->flush(); - $json = $this->get('serializer')->serialize($entry, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($entry); } /** @@ -562,45 +538,46 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $list = json_decode($request->query->get('list', [])); - $results = []; + + if (empty($list)) { + return $this->sendResponse([]); + } // handle multiple urls - if (!empty($list)) { - foreach ($list as $key => $element) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $element->url, - $this->getUser()->getId() - ); + $results = []; - $results[$key]['url'] = $element->url; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); - $tags = $element->tags; + $results[$key]['url'] = $element->url; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; - if (false !== $entry && !(empty($tags))) { - $tags = explode(',', $tags); - foreach ($tags as $label) { - $label = trim($label); + $tags = $element->tags; - $tag = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Tag') - ->findOneByLabel($label); + if (false !== $entry && !(empty($tags))) { + $tags = explode(',', $tags); + foreach ($tags as $label) { + $label = trim($label); - if (false !== $tag) { - $entry->removeTag($tag); - } - } + $tag = $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Tag') + ->findOneByLabel($label); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); + if (false !== $tag) { + $entry->removeTag($tag); + } } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); } } - $json = $this->get('serializer')->serialize($results, 'json'); - - return (new JsonResponse())->setJson($json); + return $this->sendResponse($results); } /** @@ -619,32 +596,47 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $list = json_decode($request->query->get('list', [])); + + if (empty($list)) { + return $this->sendResponse([]); + } + $results = []; // handle multiple urls - if (!empty($list)) { - foreach ($list as $key => $element) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $element->url, - $this->getUser()->getId() - ); + foreach ($list as $key => $element) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $element->url, + $this->getUser()->getId() + ); - $results[$key]['url'] = $element->url; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + $results[$key]['url'] = $element->url; + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; - $tags = $element->tags; + $tags = $element->tags; - if (false !== $entry && !(empty($tags))) { - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + if (false !== $entry && !(empty($tags))) { + $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); - } + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); } } - $json = $this->get('serializer')->serialize($results, 'json'); + return $this->sendResponse($results); + } + + /** + * Shortcut to send data serialized in json. + * + * @param mixed $data + * + * @return JsonResponse + */ + private function sendResponse($data) + { + $json = $this->get('serializer')->serialize($data, 'json'); return (new JsonResponse())->setJson($json); } -- cgit v1.2.3 From 6bc6fb1f60e7b81a21f844dca025671a2f4a4564 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 27 May 2017 22:08:14 +0200 Subject: Move Tags assigner to a separate file Signed-off-by: Thomas Citharel --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 4801811d..31bb67fd 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -318,7 +318,7 @@ class EntryRestController extends WallabagRestController $tags = $request->request->get('tags', ''); if (!empty($tags)) { - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } if (!is_null($isStarred)) { @@ -379,7 +379,7 @@ class EntryRestController extends WallabagRestController $tags = $request->request->get('tags', ''); if (!empty($tags)) { - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } $em = $this->getDoctrine()->getManager(); @@ -497,7 +497,7 @@ class EntryRestController extends WallabagRestController $tags = $request->request->get('tags', ''); if (!empty($tags)) { - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } $em = $this->getDoctrine()->getManager(); @@ -626,7 +626,7 @@ class EntryRestController extends WallabagRestController $tags = $element->tags; if (false !== $entry && !(empty($tags))) { - $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags); + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); $em = $this->getDoctrine()->getManager(); $em->persist($entry); -- cgit v1.2.3 From e668a8124c46d47add4248963d77f3b29b37b3ce Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 11 May 2017 08:14:29 +0200 Subject: Allow other fields to be send using API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Entry API can now have these new fields: - content - language - preview_picture - published_at Re-use the ContentProxy to be able to do the same using the web UI (in the future). htmLawed is used to clean stuff from content, I hope it’ll be enough to avoid security breach. Lower content validation when we want to update an entry with content already defined. Before, language & content_type were required. If there weren’t provided, we re-fetched the content using graby. I think these fields aren’t required for an entry to be created. So I removed them. Which means some import from the v1 export won’t be re-fetched since they provide content, url & title. Also, remove liberation link from Readability import to avoid overlaping import (from wallabag v1, which had the same link) --- .../ApiBundle/Controller/EntryRestController.php | 46 +++++++++++++++------- 1 file changed, 31 insertions(+), 15 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 31bb67fd..dfd04fb4 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -280,6 +280,10 @@ class EntryRestController extends WallabagRestController * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, + * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, + * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, + * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, + * {"name"="published_at", "dataType"="datetime", "format"="YYYY-MM-DDTHH:II:SS+TZ", "required"=false, "description"="Published date of the entry"}, * } * ) * @@ -293,30 +297,42 @@ class EntryRestController extends WallabagRestController $title = $request->request->get('title'); $isArchived = $request->request->get('archive'); $isStarred = $request->request->get('starred'); + $content = $request->request->get('content'); + $language = $request->request->get('language'); + $picture = $request->request->get('preview_picture'); + $publishedAt = $request->request->get('published_at'); $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); if (false === $entry) { $entry = new Entry($this->getUser()); - try { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry( - $entry, - $url - ); - } catch (\Exception $e) { - $this->get('logger')->error('Error while saving an entry', [ - 'exception' => $e, - 'entry' => $entry, - ]); - $entry->setUrl($url); - } } - if (!is_null($title)) { - $entry->setTitle($title); + try { + $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + $entry, + $url, + [ + 'title' => $title, + 'html' => $content, + 'url' => $url, + 'language' => $language, + 'date' => $publishedAt, + // faking the preview picture + 'open_graph' => [ + 'og_image' => $picture, + ], + ] + ); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + $entry->setUrl($url); } - $tags = $request->request->get('tags', ''); + $tags = $request->request->get('tags', []); if (!empty($tags)) { $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } -- cgit v1.2.3 From fb436e8ca0c7468b9698050df0b78447e2d0854f Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 11 May 2017 20:10:22 +0200 Subject: Add support for authors --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index dfd04fb4..e6bbe552 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -284,6 +284,7 @@ class EntryRestController extends WallabagRestController * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, * {"name"="published_at", "dataType"="datetime", "format"="YYYY-MM-DDTHH:II:SS+TZ", "required"=false, "description"="Published date of the entry"}, + * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, * } * ) * @@ -295,12 +296,14 @@ class EntryRestController extends WallabagRestController $url = $request->request->get('url'); $title = $request->request->get('title'); + $tags = $request->request->get('tags', []); $isArchived = $request->request->get('archive'); $isStarred = $request->request->get('starred'); $content = $request->request->get('content'); $language = $request->request->get('language'); $picture = $request->request->get('preview_picture'); $publishedAt = $request->request->get('published_at'); + $authors = $request->request->get('authors', ''); $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); @@ -322,6 +325,7 @@ class EntryRestController extends WallabagRestController 'open_graph' => [ 'og_image' => $picture, ], + 'authors' => explode(',', $authors), ] ); } catch (\Exception $e) { @@ -332,7 +336,7 @@ class EntryRestController extends WallabagRestController $entry->setUrl($url); } - $tags = $request->request->get('tags', []); + if (!empty($tags)) { $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } -- cgit v1.2.3 From 74a75f7d430eb7a69cd377194e52012db34d39b4 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 12 May 2017 07:53:21 +0200 Subject: Use graby ContentExtractor to clean html It might be better to re-use some graby functionalities to clean html instead of building a new system. --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 1 - 1 file changed, 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index e6bbe552..0930c109 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -336,7 +336,6 @@ class EntryRestController extends WallabagRestController $entry->setUrl($url); } - if (!empty($tags)) { $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } -- cgit v1.2.3 From 9e349f08a651c43c6d5dd890303ed529c38c4fde Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 24 May 2017 16:02:49 +0200 Subject: Improve docs --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 0930c109..cc2cca64 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -273,6 +273,9 @@ class EntryRestController extends WallabagRestController /** * Create an entry. * + * 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**. + * Otherwise, content will be fetched as normal from the url and values will be overwritten. + * * @ApiDoc( * parameters={ * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, -- cgit v1.2.3 From f0378b4d7c7b8c971239445f3a2a1535abab7d00 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 24 May 2017 16:44:03 +0200 Subject: Forced date can now be a timestamp too Add adding more tests for forced content --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index cc2cca64..c3ba1858 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -286,7 +286,7 @@ class EntryRestController extends WallabagRestController * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, - * {"name"="published_at", "dataType"="datetime", "format"="YYYY-MM-DDTHH:II:SS+TZ", "required"=false, "description"="Published date of the entry"}, + * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, * } * ) -- cgit v1.2.3 From 7aba665e484c5c36ee029219a999a427d864ff22 Mon Sep 17 00:00:00 2001 From: Jerome Charaoui Date: Tue, 6 Dec 2016 22:17:44 -0500 Subject: Avoid returning objects passed by reference. Objects are always passed by reference, so it doesn't make sense to return an object which is passed by reference as it will always be the same object. This change makes the code a bit more readable. --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index c3ba1858..d154c18b 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -315,7 +315,7 @@ class EntryRestController extends WallabagRestController } try { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry( + $this->get('wallabag_core.content_proxy')->updateEntry( $entry, $url, [ @@ -428,7 +428,7 @@ class EntryRestController extends WallabagRestController $this->validateUserAccess($entry->getUser()->getId()); try { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); + $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); } catch (\Exception $e) { $this->get('logger')->error('Error while saving an entry', [ 'exception' => $e, -- cgit v1.2.3 From d5c2cc54b5490b0bec46f39d7706d5d46869e872 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 30 May 2017 17:48:24 +0200 Subject: Fix tests --- .../ApiBundle/Controller/EntryRestController.php | 45 +++++++++++----------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index d154c18b..93c8157e 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -231,7 +231,6 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $urls = json_decode($request->query->get('urls', [])); - $results = []; $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); @@ -239,32 +238,34 @@ class EntryRestController extends WallabagRestController throw new HttpException(400, 'API limit reached'); } + $results = []; + if (empty($urls)) { + return $this->sendResponse($results); + } + // handle multiple urls - if (!empty($urls)) { - foreach ($urls as $key => $url) { - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( - $url, - $this->getUser()->getId() - ); - - $results[$key]['url'] = $url; - - if (false === $entry) { - $entry = $this->get('wallabag_core.content_proxy')->updateEntry( - new Entry($this->getUser()), - $url - ); - } + foreach ($urls as $key => $url) { + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $url, + $this->getUser()->getId() + ); - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); + $results[$key]['url'] = $url; - $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + if (false === $entry) { + $entry = new Entry($this->getUser()); - // entry saved, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + $this->get('wallabag_core.content_proxy')->updateEntry($entry, $url); } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); } return $this->sendResponse($results); -- cgit v1.2.3 From 645291e8feb0f3e977b9518da7d731fda8cf1f30 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 2 Jun 2017 20:52:49 +0200 Subject: Add ability to patch an entry with more fields Like when we create an entry, we can now patch an entry with new fields: - content - language - preview_picture - published_at - authors --- .../ApiBundle/Controller/EntryRestController.php | 38 ++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 93c8157e..7135e616 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -374,6 +374,11 @@ class EntryRestController extends WallabagRestController * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, + * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, + * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, + * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, + * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, + * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, * } * ) * @@ -385,11 +390,39 @@ class EntryRestController extends WallabagRestController $this->validateUserAccess($entry->getUser()->getId()); $title = $request->request->get('title'); + $tags = $request->request->get('tags', ''); $isArchived = $request->request->get('archive'); $isStarred = $request->request->get('starred'); + $content = $request->request->get('content'); + $language = $request->request->get('language'); + $picture = $request->request->get('preview_picture'); + $publishedAt = $request->request->get('published_at'); + $authors = $request->request->get('authors', ''); - if (!is_null($title)) { - $entry->setTitle($title); + try { + $this->get('wallabag_core.content_proxy')->updateEntry( + $entry, + $entry->getUrl(), + [ + 'title' => !empty($title) ? $title : $entry->getTitle(), + 'html' => !empty($content) ? $content : $entry->getContent(), + 'url' => $entry->getUrl(), + 'language' => $language, + 'date' => $publishedAt, + // faking the preview picture + 'open_graph' => [ + 'og_image' => $picture, + ], + 'authors' => is_string($authors) ? explode(',', $authors) : [], + ], + // we don't want the content to be update by fetching the url + true + ); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); } if (!is_null($isArchived)) { @@ -400,7 +433,6 @@ class EntryRestController extends WallabagRestController $entry->setStarred((bool) $isStarred); } - $tags = $request->request->get('tags', ''); if (!empty($tags)) { $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } -- cgit v1.2.3 From db0c48af361eb20fbfcac625869de6f26112d3f7 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 7 Jun 2017 15:07:55 +0200 Subject: Refactorize the way to save an Entry in the API --- .../ApiBundle/Controller/EntryRestController.php | 172 +++++++++------------ 1 file changed, 70 insertions(+), 102 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 7135e616..09b73ccb 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -299,65 +299,18 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $url = $request->request->get('url'); - $title = $request->request->get('title'); - $tags = $request->request->get('tags', []); - $isArchived = $request->request->get('archive'); - $isStarred = $request->request->get('starred'); - $content = $request->request->get('content'); - $language = $request->request->get('language'); - $picture = $request->request->get('preview_picture'); - $publishedAt = $request->request->get('published_at'); - $authors = $request->request->get('authors', ''); - $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($url, $this->getUser()->getId()); + $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( + $url, + $this->getUser()->getId() + ); if (false === $entry) { $entry = new Entry($this->getUser()); - } - - try { - $this->get('wallabag_core.content_proxy')->updateEntry( - $entry, - $url, - [ - 'title' => $title, - 'html' => $content, - 'url' => $url, - 'language' => $language, - 'date' => $publishedAt, - // faking the preview picture - 'open_graph' => [ - 'og_image' => $picture, - ], - 'authors' => explode(',', $authors), - ] - ); - } catch (\Exception $e) { - $this->get('logger')->error('Error while saving an entry', [ - 'exception' => $e, - 'entry' => $entry, - ]); $entry->setUrl($url); } - if (!empty($tags)) { - $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); - } - - if (!is_null($isStarred)) { - $entry->setStarred((bool) $isStarred); - } - - if (!is_null($isArchived)) { - $entry->setArchived((bool) $isArchived); - } - - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); - - // entry saved, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + $this->upsertEntry($entry, $request); return $this->sendResponse($entry); } @@ -389,56 +342,7 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - $title = $request->request->get('title'); - $tags = $request->request->get('tags', ''); - $isArchived = $request->request->get('archive'); - $isStarred = $request->request->get('starred'); - $content = $request->request->get('content'); - $language = $request->request->get('language'); - $picture = $request->request->get('preview_picture'); - $publishedAt = $request->request->get('published_at'); - $authors = $request->request->get('authors', ''); - - try { - $this->get('wallabag_core.content_proxy')->updateEntry( - $entry, - $entry->getUrl(), - [ - 'title' => !empty($title) ? $title : $entry->getTitle(), - 'html' => !empty($content) ? $content : $entry->getContent(), - 'url' => $entry->getUrl(), - 'language' => $language, - 'date' => $publishedAt, - // faking the preview picture - 'open_graph' => [ - 'og_image' => $picture, - ], - 'authors' => is_string($authors) ? explode(',', $authors) : [], - ], - // we don't want the content to be update by fetching the url - true - ); - } catch (\Exception $e) { - $this->get('logger')->error('Error while saving an entry', [ - 'exception' => $e, - 'entry' => $entry, - ]); - } - - if (!is_null($isArchived)) { - $entry->setArchived((bool) $isArchived); - } - - if (!is_null($isStarred)) { - $entry->setStarred((bool) $isStarred); - } - - if (!empty($tags)) { - $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); - } - - $em = $this->getDoctrine()->getManager(); - $em->flush(); + $this->upsertEntry($entry, $request, true); return $this->sendResponse($entry); } @@ -705,4 +609,68 @@ class EntryRestController extends WallabagRestController return (new JsonResponse())->setJson($json); } + + /** + * Update or Insert a new entry. + * + * @param Entry $entry + * @param Request $request + * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting) + */ + private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false) + { + $title = $request->request->get('title'); + $tags = $request->request->get('tags', []); + $isArchived = $request->request->get('archive'); + $isStarred = $request->request->get('starred'); + $content = $request->request->get('content'); + $language = $request->request->get('language'); + $picture = $request->request->get('preview_picture'); + $publishedAt = $request->request->get('published_at'); + $authors = $request->request->get('authors', ''); + + try { + $this->get('wallabag_core.content_proxy')->updateEntry( + $entry, + $entry->getUrl(), + [ + 'title' => !empty($title) ? $title : $entry->getTitle(), + 'html' => !empty($content) ? $content : $entry->getContent(), + 'url' => $entry->getUrl(), + 'language' => !empty($language) ? $language : $entry->getLanguage(), + 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(), + // faking the open graph preview picture + 'open_graph' => [ + 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(), + ], + 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(), + ], + $disableContentUpdate + ); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + } + + if (!is_null($isArchived)) { + $entry->setArchived((bool) $isArchived); + } + + if (!is_null($isStarred)) { + $entry->setStarred((bool) $isStarred); + } + + if (!empty($tags)) { + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + } } -- cgit v1.2.3 From 1112e54772c9308ee3d7417869b5b8ef9b2b9812 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 10 Jun 2017 15:31:57 +0200 Subject: Add public filter/field in the API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Listing entries can now be filtered by “public”. Creating or patching an entry can now set is to public or remove the public. Entry response now include “is_public” boolean field --- .../ApiBundle/Controller/EntryRestController.php | 27 +++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 09b73ccb..ae9a93aa 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -77,6 +77,7 @@ class EntryRestController extends WallabagRestController * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, + * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"}, * } * ) * @@ -88,6 +89,7 @@ class EntryRestController extends WallabagRestController $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); + $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public'); $sort = $request->query->get('sort', 'created'); $order = $request->query->get('order', 'desc'); $page = (int) $request->query->get('page', 1); @@ -96,9 +98,16 @@ class EntryRestController extends WallabagRestController $since = $request->query->get('since', 0); /** @var \Pagerfanta\Pagerfanta $pager */ - $pager = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order, $since, $tags); + $pager = $this->get('wallabag_core.entry_repository')->findEntries( + $this->getUser()->getId(), + $isArchived, + $isStarred, + $isPublic, + $sort, + $order, + $since, + $tags + ); $pager->setMaxPerPage($perPage); $pager->setCurrentPage($page); @@ -111,6 +120,7 @@ class EntryRestController extends WallabagRestController [ 'archive' => $isArchived, 'starred' => $isStarred, + 'public' => $isPublic, 'sort' => $sort, 'order' => $order, 'page' => $page, @@ -289,6 +299,7 @@ class EntryRestController extends WallabagRestController * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, + * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, * } * ) * @@ -332,6 +343,7 @@ class EntryRestController extends WallabagRestController * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, + * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, * } * ) * @@ -623,6 +635,7 @@ class EntryRestController extends WallabagRestController $tags = $request->request->get('tags', []); $isArchived = $request->request->get('archive'); $isStarred = $request->request->get('starred'); + $isPublic = $request->request->get('public'); $content = $request->request->get('content'); $language = $request->request->get('language'); $picture = $request->request->get('preview_picture'); @@ -666,6 +679,14 @@ class EntryRestController extends WallabagRestController $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } + if (!is_null($isPublic)) { + $entry->cleanUid(); + + if (true === (bool) $isPublic && null === $entry->getUid()) { + $entry->generateUid(); + } + } + $em = $this->getDoctrine()->getManager(); $em->persist($entry); $em->flush(); -- cgit v1.2.3 From a9c6577f0c51dd4282e022fa4b41cbb7b0e201b3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 10 Jun 2017 15:37:25 +0200 Subject: Few fixes --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index ae9a93aa..afd708e6 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -680,10 +680,10 @@ class EntryRestController extends WallabagRestController } if (!is_null($isPublic)) { - $entry->cleanUid(); - if (true === (bool) $isPublic && null === $entry->getUid()) { $entry->generateUid(); + } else if (false === (bool) $isPublic) { + $entry->cleanUid(); } } -- cgit v1.2.3 From d9da186fb8427ca82d1fd7c4e2ef3f5032781f23 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 10 Jun 2017 16:08:24 +0200 Subject: CS --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index afd708e6..768c4fdc 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -682,7 +682,7 @@ class EntryRestController extends WallabagRestController if (!is_null($isPublic)) { if (true === (bool) $isPublic && null === $entry->getUid()) { $entry->generateUid(); - } else if (false === (bool) $isPublic) { + } elseif (false === (bool) $isPublic) { $entry->cleanUid(); } } -- cgit v1.2.3 From 18696f77fdc76f7055d84460d459051a6bb7253a Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 23 Jun 2017 08:30:23 +0200 Subject: Avoid BC on exists endpoint --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 768c4fdc..793b91ba 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -18,9 +18,12 @@ class EntryRestController extends WallabagRestController { /** * Check if an entry exist by url. + * Return ID if entry(ies) exist (and if you give the return_id parameter). + * Otherwise it returns false. * * @ApiDoc( * parameters={ + * {"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"}, * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, * {"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,6 +35,7 @@ class EntryRestController extends WallabagRestController { $this->validateAuthentication(); + $returnId = (null === $request->query->get('return_id')) ? 0 : (bool) $request->query->get('return_id'); $urls = $request->query->get('urls', []); // handle multiple urls first @@ -42,7 +46,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $results[$url] = $res instanceof Entry ? $res->getId() : false; + $results[$url] = $res instanceof Entry ? ($returnId ? $res->getId() : true) : false; } return $this->sendResponse($results); @@ -59,7 +63,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $exists = $res instanceof Entry ? $res->getId() : false; + $exists = $res instanceof Entry ? ($returnId ? $res->getId() : true) : false; return $this->sendResponse(['exists' => $exists]); } -- cgit v1.2.3 From 39ffaba3232b6378f47883615c58a9ffba668af3 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 28 Jun 2017 08:15:06 +0200 Subject: Return null instead of false --- .../ApiBundle/Controller/EntryRestController.php | 30 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 793b91ba..9786c4d3 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -4,6 +4,7 @@ namespace Wallabag\ApiBundle\Controller; use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; +use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpFoundation\Request; @@ -21,6 +22,8 @@ class EntryRestController extends WallabagRestController * Return ID if entry(ies) exist (and if you give the return_id parameter). * Otherwise it returns false. * + * @todo Remove that `return_id` in the next major release + * * @ApiDoc( * parameters={ * {"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"}, @@ -46,7 +49,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $results[$url] = $res instanceof Entry ? ($returnId ? $res->getId() : true) : false; + $results[$url] = $this->returnExistInformation($res, $returnId); } return $this->sendResponse($results); @@ -63,7 +66,7 @@ class EntryRestController extends WallabagRestController ->getRepository('WallabagCoreBundle:Entry') ->findByUrlAndUserId($url, $this->getUser()->getId()); - $exists = $res instanceof Entry ? ($returnId ? $res->getId() : true) : false; + $exists = $this->returnExistInformation($res, $returnId); return $this->sendResponse(['exists' => $exists]); } @@ -621,7 +624,11 @@ class EntryRestController extends WallabagRestController */ private function sendResponse($data) { - $json = $this->get('serializer')->serialize($data, 'json'); + // https://github.com/schmittjoh/JMSSerializerBundle/issues/293 + $context = new SerializationContext(); + $context->setSerializeNull(true); + + $json = $this->get('serializer')->serialize($data, 'json', $context); return (new JsonResponse())->setJson($json); } @@ -698,4 +705,21 @@ class EntryRestController extends WallabagRestController // entry saved, dispatch event about it! $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); } + + /** + * Return information about the entry if it exist and depending on the id or not. + * + * @param Entry|null $entry + * @param bool $returnId + * + * @return bool|int + */ + private function returnExistInformation($entry, $returnId) + { + if ($returnId) { + return $entry instanceof Entry ? $entry->getId() : null; + } + + return $entry instanceof Entry ? true : false; + } } -- cgit v1.2.3 From 331e5b026189ff6433014659f32304b1706015e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Benoist?= Date: Wed, 28 Jun 2017 09:18:22 +0200 Subject: CS --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 9786c4d3..c7938633 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -38,7 +38,7 @@ class EntryRestController extends WallabagRestController { $this->validateAuthentication(); - $returnId = (null === $request->query->get('return_id')) ? 0 : (bool) $request->query->get('return_id'); + $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); $urls = $request->query->get('urls', []); // handle multiple urls first @@ -720,6 +720,6 @@ class EntryRestController extends WallabagRestController return $entry instanceof Entry ? $entry->getId() : null; } - return $entry instanceof Entry ? true : false; + return $entry instanceof Entry; } } -- cgit v1.2.3 From f808b01692a835673f328d7221ba8c212caa9b61 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 1 Jul 2017 09:52:38 +0200 Subject: Add a real configuration for CS-Fixer --- .../ApiBundle/Controller/EntryRestController.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index c7938633..ca460c84 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -6,14 +6,14 @@ use Hateoas\Configuration\Route; use Hateoas\Representation\Factory\PagerfantaFactory; use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Tag; -use Wallabag\CoreBundle\Event\EntrySavedEvent; use Wallabag\CoreBundle\Event\EntryDeletedEvent; +use Wallabag\CoreBundle\Event\EntrySavedEvent; class EntryRestController extends WallabagRestController { @@ -59,7 +59,7 @@ class EntryRestController extends WallabagRestController $url = $request->query->get('url', ''); if (empty($url)) { - throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId()); + throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); } $res = $this->getDoctrine() @@ -239,9 +239,9 @@ class EntryRestController extends WallabagRestController * } * ) * - * @return JsonResponse - * * @throws HttpException When limit is reached + * + * @return JsonResponse */ public function postEntriesListAction(Request $request) { @@ -678,11 +678,11 @@ class EntryRestController extends WallabagRestController ]); } - if (!is_null($isArchived)) { + if (null !== $isArchived) { $entry->setArchived((bool) $isArchived); } - if (!is_null($isStarred)) { + if (null !== $isStarred) { $entry->setStarred((bool) $isStarred); } @@ -690,7 +690,7 @@ class EntryRestController extends WallabagRestController $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); } - if (!is_null($isPublic)) { + if (null !== $isPublic) { if (true === (bool) $isPublic && null === $entry->getUid()) { $entry->generateUid(); } elseif (false === (bool) $isPublic) { -- cgit v1.2.3 From a05b61159e147776f63baee731b5026796e5f7ae Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 30 Jun 2017 16:54:26 +0200 Subject: Fix PATCH method The PATCH method for the entry should only update what user sent to us and not the whole entry as it was before. Also, sending tags when patching an entry will now remove all current tags & assocatied new ones. --- .../ApiBundle/Controller/EntryRestController.php | 211 ++++++++++++++------- 1 file changed, 142 insertions(+), 69 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index ca460c84..a2e913af 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -299,8 +299,8 @@ class EntryRestController extends WallabagRestController * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, - * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, + * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, @@ -328,7 +328,58 @@ class EntryRestController extends WallabagRestController $entry->setUrl($url); } - $this->upsertEntry($entry, $request); + $data = $this->retrieveValueFromRequest($request); + + try { + $this->get('wallabag_core.content_proxy')->updateEntry( + $entry, + $entry->getUrl(), + [ + 'title' => !empty($data['title']) ? $data['title'] : $entry->getTitle(), + 'html' => !empty($data['content']) ? $data['content'] : $entry->getContent(), + 'url' => $entry->getUrl(), + 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), + 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), + // faking the open graph preview picture + 'open_graph' => [ + 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), + ], + 'authors' => is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), + ] + ); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + } + + if (!is_null($data['isArchived'])) { + $entry->setArchived((bool) $data['isArchived']); + } + + if (!is_null($data['isStarred'])) { + $entry->setStarred((bool) $data['isStarred']); + } + + if (!empty($data['tags'])) { + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); + } + + if (!is_null($data['isPublic'])) { + if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { + $entry->generateUid(); + } elseif (false === (bool) $data['isPublic']) { + $entry->cleanUid(); + } + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); return $this->sendResponse($entry); } @@ -361,7 +412,78 @@ class EntryRestController extends WallabagRestController $this->validateAuthentication(); $this->validateUserAccess($entry->getUser()->getId()); - $this->upsertEntry($entry, $request, true); + $contentProxy = $this->get('wallabag_core.content_proxy'); + + $data = $this->retrieveValueFromRequest($request); + + // this is a special case where user want to manually update the entry content + // the ContentProxy will only cleanup the html + // and also we force to not re-fetch the content in case of error + if (!empty($data['content'])) { + try { + $contentProxy->updateEntry( + $entry, + $entry->getUrl(), + [ + 'html' => $data['content'], + ], + true + ); + } catch (\Exception $e) { + $this->get('logger')->error('Error while saving an entry', [ + 'exception' => $e, + 'entry' => $entry, + ]); + } + } + + if (!empty($data['title'])) { + $entry->setTitle($data['title']); + } + + if (!empty($data['language'])) { + $contentProxy->updateLanguage($entry, $data['language']); + } + + if (!empty($data['authors']) && is_string($data['authors'])) { + $entry->setPublishedBy(explode(',', $data['authors'])); + } + + if (!empty($data['picture'])) { + $contentProxy->updatePreviewPicture($entry, $data['picture']); + } + + if (!empty($data['publishedAt'])) { + $contentProxy->updatePublishedAt($entry, $data['publishedAt']); + } + + if (!is_null($data['isArchived'])) { + $entry->setArchived((bool) $data['isArchived']); + } + + if (!is_null($data['isStarred'])) { + $entry->setStarred((bool) $data['isStarred']); + } + + if (!empty($data['tags'])) { + $entry->removeAllTags(); + $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); + } + + if (!is_null($data['isPublic'])) { + if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { + $entry->generateUid(); + } elseif (false === (bool) $data['isPublic']) { + $entry->cleanUid(); + } + } + + $em = $this->getDoctrine()->getManager(); + $em->persist($entry); + $em->flush(); + + // entry saved, dispatch event about it! + $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); return $this->sendResponse($entry); } @@ -634,76 +756,27 @@ class EntryRestController extends WallabagRestController } /** - * Update or Insert a new entry. + * Retrieve value from the request. + * Used for POST & PATCH on a an entry. * - * @param Entry $entry * @param Request $request - * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting) + * + * @return array */ - private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false) + private function retrieveValueFromRequest(Request $request) { - $title = $request->request->get('title'); - $tags = $request->request->get('tags', []); - $isArchived = $request->request->get('archive'); - $isStarred = $request->request->get('starred'); - $isPublic = $request->request->get('public'); - $content = $request->request->get('content'); - $language = $request->request->get('language'); - $picture = $request->request->get('preview_picture'); - $publishedAt = $request->request->get('published_at'); - $authors = $request->request->get('authors', ''); - - try { - $this->get('wallabag_core.content_proxy')->updateEntry( - $entry, - $entry->getUrl(), - [ - 'title' => !empty($title) ? $title : $entry->getTitle(), - 'html' => !empty($content) ? $content : $entry->getContent(), - 'url' => $entry->getUrl(), - 'language' => !empty($language) ? $language : $entry->getLanguage(), - 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(), - // faking the open graph preview picture - 'open_graph' => [ - 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(), - ], - 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(), - ], - $disableContentUpdate - ); - } catch (\Exception $e) { - $this->get('logger')->error('Error while saving an entry', [ - 'exception' => $e, - 'entry' => $entry, - ]); - } - - if (null !== $isArchived) { - $entry->setArchived((bool) $isArchived); - } - - if (null !== $isStarred) { - $entry->setStarred((bool) $isStarred); - } - - if (!empty($tags)) { - $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); - } - - if (null !== $isPublic) { - if (true === (bool) $isPublic && null === $entry->getUid()) { - $entry->generateUid(); - } elseif (false === (bool) $isPublic) { - $entry->cleanUid(); - } - } - - $em = $this->getDoctrine()->getManager(); - $em->persist($entry); - $em->flush(); - - // entry saved, dispatch event about it! - $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); + return [ + 'title' => $request->request->get('title'), + 'tags' => $request->request->get('tags', []), + 'isArchived' => $request->request->get('archive'), + 'isStarred' => $request->request->get('starred'), + 'isPublic' => $request->request->get('public'), + 'content' => $request->request->get('content'), + 'language' => $request->request->get('language'), + 'picture' => $request->request->get('preview_picture'), + 'publishedAt' => $request->request->get('published_at'), + 'authors' => $request->request->get('authors', ''), + ]; } /** -- cgit v1.2.3 From c18a2476b601bc6b9893462d9be680c2e13c89e8 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 3 Jul 2017 13:56:39 +0200 Subject: CS --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index a2e913af..8a206124 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -354,11 +354,11 @@ class EntryRestController extends WallabagRestController ]); } - if (!is_null($data['isArchived'])) { + if (null !== $data['isArchived']) { $entry->setArchived((bool) $data['isArchived']); } - if (!is_null($data['isStarred'])) { + if (null !== $data['isStarred']) { $entry->setStarred((bool) $data['isStarred']); } @@ -366,7 +366,7 @@ class EntryRestController extends WallabagRestController $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); } - if (!is_null($data['isPublic'])) { + if (null !== $data['isPublic']) { if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { $entry->generateUid(); } elseif (false === (bool) $data['isPublic']) { @@ -457,11 +457,11 @@ class EntryRestController extends WallabagRestController $contentProxy->updatePublishedAt($entry, $data['publishedAt']); } - if (!is_null($data['isArchived'])) { + if (null !== $data['isArchived']) { $entry->setArchived((bool) $data['isArchived']); } - if (!is_null($data['isStarred'])) { + if (null !== $data['isStarred']) { $entry->setStarred((bool) $data['isStarred']); } @@ -470,7 +470,7 @@ class EntryRestController extends WallabagRestController $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); } - if (!is_null($data['isPublic'])) { + if (null !== $data['isPublic']) { if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { $entry->generateUid(); } elseif (false === (bool) $data['isPublic']) { -- cgit v1.2.3 From 07320a2bd25c6ace1f9b1aa06b1b08e8dbf4f4fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Gilli?= Date: Sat, 8 Jul 2017 17:55:58 +0200 Subject: Use the article domain as author for export files When exporting an entry, use the domain name as author name for epub, mobi and pdf formats, instead of 'wallabag'. Change the author from array to string, because for now, there is always only one author. --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 1 + 1 file changed, 1 insertion(+) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 8a206124..9277e1a1 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -180,6 +180,7 @@ class EntryRestController extends WallabagRestController return $this->get('wallabag_core.helper.entries_export') ->setEntries($entry) ->updateTitle('entry') + ->updateAuthor('entry') ->exportAs($request->attributes->get('_format')); } -- cgit v1.2.3 From 52b84c11a5b5474cd45271d937a46c6adfdf2749 Mon Sep 17 00:00:00 2001 From: Nicolas Hart Date: Sat, 29 Jul 2017 22:51:50 +0200 Subject: Fix some namespaces and phpdoc --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 1 + 1 file changed, 1 insertion(+) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 9277e1a1..bc1b6f92 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -8,6 +8,7 @@ use JMS\Serializer\SerializationContext; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Wallabag\CoreBundle\Entity\Entry; -- cgit v1.2.3 From a991c46eedec0efb24d0a9974b1c7fcabf8cfa66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20D?= Date: Wed, 23 Aug 2017 23:06:40 +0200 Subject: Set a starred_at field when an entry is starred. This date is used to sort starred entries. Can not use Entry::timestamps method otherwise starred_at will be updated each time entry is updated. Add an updateStar method into Entry class A migration script has been added in order to set starred_at field. --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index bc1b6f92..6db97731 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -361,7 +361,7 @@ class EntryRestController extends WallabagRestController } if (null !== $data['isStarred']) { - $entry->setStarred((bool) $data['isStarred']); + $entry->updateStar((bool) $data['isStarred']); } if (!empty($data['tags'])) { @@ -464,7 +464,7 @@ class EntryRestController extends WallabagRestController } if (null !== $data['isStarred']) { - $entry->setStarred((bool) $data['isStarred']); + $entry->updateStar((bool) $data['isStarred']); } if (!empty($data['tags'])) { -- cgit v1.2.3 From f40c88eb1fa349aab600f9c1c94364f317fe62dd Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 9 Oct 2017 16:45:09 +0200 Subject: Jump to Symfony 3.3 & update others deps Also update tests urls --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 6db97731..86e72335 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -752,7 +752,7 @@ class EntryRestController extends WallabagRestController $context = new SerializationContext(); $context->setSerializeNull(true); - $json = $this->get('serializer')->serialize($data, 'json', $context); + $json = $this->get('jms_serializer')->serialize($data, 'json', $context); return (new JsonResponse())->setJson($json); } -- cgit v1.2.3 From 0978bd849e77b1157b21f8e231965521e535bacd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Viande?= Date: Thu, 12 Oct 2017 07:28:42 +0200 Subject: Fix #3361 Entry Rest controller getEntries cast tags params to string --- src/Wallabag/ApiBundle/Controller/EntryRestController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/ApiBundle/Controller/EntryRestController.php') diff --git a/src/Wallabag/ApiBundle/Controller/EntryRestController.php b/src/Wallabag/ApiBundle/Controller/EntryRestController.php index 86e72335..6f161a08 100644 --- a/src/Wallabag/ApiBundle/Controller/EntryRestController.php +++ b/src/Wallabag/ApiBundle/Controller/EntryRestController.php @@ -102,7 +102,7 @@ class EntryRestController extends WallabagRestController $order = $request->query->get('order', 'desc'); $page = (int) $request->query->get('page', 1); $perPage = (int) $request->query->get('perPage', 30); - $tags = $request->query->get('tags', ''); + $tags = is_array($request->query->get('tags')) ? '' : (string) $request->query->get('tags', ''); $since = $request->query->get('since', 0); /** @var \Pagerfanta\Pagerfanta $pager */ -- cgit v1.2.3