3 namespace Wallabag\CoreBundle\Controller
;
5 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Component\HttpFoundation\Response
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\CoreBundle\Entity\Tag
;
11 use Wallabag\CoreBundle\Service\Extractor
;
12 use Hateoas\Configuration\Route
;
13 use Hateoas\Representation\Factory\PagerfantaFactory
;
15 class WallabagRestController
extends Controller
21 private function assignTagsToEntry(Entry
$entry, $tags)
23 foreach (explode(',', $tags) as $label) {
24 $label = trim($label);
27 ->getRepository('WallabagCoreBundle:Tag')
28 ->findOneByLabel($label);
30 if (is_null($tagEntity)) {
31 $tagEntity = new Tag($this->getUser());
32 $tagEntity->setLabel($label);
35 // only add the tag on the entry if the relation doesn't exist
36 if (!$entry->getTags()->contains($tagEntity)) {
37 $entry->addTag($tagEntity);
43 * Retrieve salt for a giver user.
47 * {"name"="username", "dataType"="string", "required"=true, "description"="username"}
52 public function getSaltAction($username)
56 ->getRepository('WallabagCoreBundle:User')
57 ->findOneByUsername($username);
60 throw $this->createNotFoundException();
63 return array($user->getSalt() ?: null);
66 * Retrieve all entries. It could be filtered by many options.
70 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."},
71 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."},
72 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
73 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
74 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
75 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
76 * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
81 public function getEntriesAction(Request
$request)
83 $isArchived = $request->query
->get('archive');
84 $isStarred = $request->query
->get('star');
85 $sort = $request->query
->get('sort', 'created');
86 $order = $request->query
->get('order', 'desc');
87 $page = (int) $request->query
->get('page', 1);
88 $perPage = (int) $request->query
->get('perPage', 30);
89 $tags = $request->query
->get('tags', array());
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
96 if (0 === $pager->getNbResults()) {
97 throw $this->createNotFoundException();
100 $pager->setCurrentPage($page);
101 $pager->setMaxPerPage($perPage);
103 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
104 $paginatedCollection = $pagerfantaFactory->createRepresentation(
106 new Route('api_get_entries', [], $absolute = true)
109 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
111 return new Response($json, 200, array('application/json'));
115 * Retrieve a single entry
119 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
124 public function getEntryAction(Entry
$entry)
126 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
127 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
130 $json = $this->get('serializer')->serialize($entry, 'json');
132 return new Response($json, 200, array('application/json'));
140 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
141 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
142 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
147 public function postEntriesAction(Request
$request)
149 $url = $request->request
->get('url');
151 $content = Extractor
::extract($url);
152 $entry = new Entry($this->getUser());
153 $entry->setUrl($url);
154 $entry->setTitle($request->request
->get('title') ?: $content->getTitle());
155 $entry->setContent($content->getBody());
157 $tags = $request->request
->get('tags', '');
159 $this->assignTagsToEntry($entry, $tags);
162 $em = $this->getDoctrine()->getManager();
163 $em->persist($entry);
166 $json = $this->get('serializer')->serialize($entry, 'json');
168 return new Response($json, 200, array('application/json'));
172 * Change several properties of an entry
176 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
179 * {"name"="title", "dataType"="string", "required"=false},
180 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
181 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."},
182 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."},
187 public function patchEntriesAction(Entry
$entry, Request
$request)
189 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
190 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
193 $title = $request->request
->get("title");
194 $isArchived = $request->request
->get("archive");
195 $isStarred = $request->request
->get("star");
197 if (!is_null($title)) {
198 $entry->setTitle($title);
201 if (!is_null($isArchived)) {
202 $entry->setArchived($isArchived);
205 if (!is_null($isStarred)) {
206 $entry->setStarred($isStarred);
209 $tags = $request->request
->get('tags', '');
211 $this->assignTagsToEntry($entry, $tags);
214 $em = $this->getDoctrine()->getManager();
217 $json = $this->get('serializer')->serialize($entry, 'json');
219 return new Response($json, 200, array('application/json'));
223 * Delete **permanently** an entry
227 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
232 public function deleteEntriesAction(Entry
$entry)
234 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
235 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
238 $em = $this->getDoctrine()->getManager();
242 $json = $this->get('serializer')->serialize($entry, 'json');
244 return new Response($json, 200, array('application/json'));
248 * Retrieve all tags for an entry
252 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
256 public function getEntriesTagsAction(Entry
$entry)
258 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
259 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
262 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
264 return new Response($json, 200, array('application/json'));
268 * Add one or more tags to an entry
272 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
275 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
279 public function postEntriesTagsAction(Request
$request, Entry
$entry)
281 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
282 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
285 $tags = $request->request
->get('tags', '');
287 $this->assignTagsToEntry($entry, $tags);
290 $em = $this->getDoctrine()->getManager();
291 $em->persist($entry);
294 $json = $this->get('serializer')->serialize($entry, 'json');
296 return new Response($json, 200, array('application/json'));
300 * Permanently remove one tag for an entry
304 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"},
305 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
309 public function deleteEntriesTagsAction(Entry
$entry, Tag
$tag)
311 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
312 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId());
315 $entry->removeTag($tag);
316 $em = $this->getDoctrine()->getManager();
317 $em->persist($entry);
320 $json = $this->get('serializer')->serialize($entry, 'json');
322 return new Response($json, 200, array('application/json'));
330 public function getTagsAction()
332 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
334 return new Response($json, 200, array('application/json'));
338 * Permanently remove one tag from **every** entry
342 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}
346 public function deleteTagAction(Tag
$tag)
348 if ($tag->getUser()->getId() != $this->getUser()->getId()) {
349 throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$tag->getUser()->getId().', logged user id: '.$this->getUser()->getId());
352 $em = $this->getDoctrine()->getManager();
356 $json = $this->get('serializer')->serialize($tag, 'json');
358 return new Response($json, 200, array('application/json'));