aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Controller
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller')
-rw-r--r--src/Wallabag/CoreBundle/Controller/WallabagRestController.php360
1 files changed, 0 insertions, 360 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php b/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
deleted file mode 100644
index 14f42c48..00000000
--- a/src/Wallabag/CoreBundle/Controller/WallabagRestController.php
+++ /dev/null
@@ -1,360 +0,0 @@
1<?php
2
3namespace Wallabag\CoreBundle\Controller;
4
5use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7use Symfony\Component\HttpFoundation\Request;
8use Symfony\Component\HttpFoundation\Response;
9use Wallabag\CoreBundle\Entity\Entry;
10use Wallabag\CoreBundle\Entity\Tag;
11use Wallabag\CoreBundle\Service\Extractor;
12use Hateoas\Configuration\Route;
13use Hateoas\Representation\Factory\PagerfantaFactory;
14
15class WallabagRestController extends Controller
16{
17 /**
18 * @param Entry $entry
19 * @param string $tags
20 */
21 private function assignTagsToEntry(Entry $entry, $tags)
22 {
23 foreach (explode(',', $tags) as $label) {
24 $label = trim($label);
25 $tagEntity = $this
26 ->getDoctrine()
27 ->getRepository('WallabagCoreBundle:Tag')
28 ->findOneByLabel($label);
29
30 if (is_null($tagEntity)) {
31 $tagEntity = new Tag($this->getUser());
32 $tagEntity->setLabel($label);
33 }
34
35 // only add the tag on the entry if the relation doesn't exist
36 if (!$entry->getTags()->contains($tagEntity)) {
37 $entry->addTag($tagEntity);
38 }
39 }
40 }
41
42 /**
43 * Retrieve salt for a giver user.
44 *
45 * @ApiDoc(
46 * parameters={
47 * {"name"="username", "dataType"="string", "required"=true, "description"="username"}
48 * }
49 * )
50 * @return array
51 */
52 public function getSaltAction($username)
53 {
54 $user = $this
55 ->getDoctrine()
56 ->getRepository('WallabagCoreBundle:User')
57 ->findOneByUsername($username);
58
59 if (is_null($user)) {
60 throw $this->createNotFoundException();
61 }
62
63 return array($user->getSalt() ?: null);
64 }
65 /**
66 * Retrieve all entries. It could be filtered by many options.
67 *
68 * @ApiDoc(
69 * parameters={
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."},
77 * }
78 * )
79 * @return Entry
80 */
81 public function getEntriesAction(Request $request)
82 {
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());
90
91 $pager = $this
92 ->getDoctrine()
93 ->getRepository('WallabagCoreBundle:Entry')
94 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
95
96 if (0 === $pager->getNbResults()) {
97 throw $this->createNotFoundException();
98 }
99
100 $pager->setCurrentPage($page);
101 $pager->setMaxPerPage($perPage);
102
103 $pagerfantaFactory = new PagerfantaFactory('page', 'perPage');
104 $paginatedCollection = $pagerfantaFactory->createRepresentation(
105 $pager,
106 new Route('api_get_entries', [], $absolute = true)
107 );
108
109 $json = $this->get('serializer')->serialize($paginatedCollection, 'json');
110
111 return new Response($json, 200, array('application/json'));
112 }
113
114 /**
115 * Retrieve a single entry
116 *
117 * @ApiDoc(
118 * requirements={
119 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
120 * }
121 * )
122 * @return Entry
123 */
124 public function getEntryAction(Entry $entry)
125 {
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());
128 }
129
130 $json = $this->get('serializer')->serialize($entry, 'json');
131
132 return new Response($json, 200, array('application/json'));
133 }
134
135 /**
136 * Create an entry
137 *
138 * @ApiDoc(
139 * parameters={
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."},
143 * }
144 * )
145 * @return Entry
146 */
147 public function postEntriesAction(Request $request)
148 {
149 $url = $request->request->get('url');
150
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());
156
157 $tags = $request->request->get('tags', '');
158 if (!empty($tags)) {
159 $this->assignTagsToEntry($entry, $tags);
160 }
161
162 $em = $this->getDoctrine()->getManager();
163 $em->persist($entry);
164 $em->flush();
165
166 $json = $this->get('serializer')->serialize($entry, 'json');
167
168 return new Response($json, 200, array('application/json'));
169 }
170
171 /**
172 * Change several properties of an entry
173 *
174 * @ApiDoc(
175 * requirements={
176 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
177 * },
178 * parameters={
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."},
183 * }
184 * )
185 * @return Entry
186 */
187 public function patchEntriesAction(Entry $entry, Request $request)
188 {
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());
191 }
192
193 $title = $request->request->get("title");
194 $isArchived = $request->request->get("archive");
195 $isStarred = $request->request->get("star");
196
197 if (!is_null($title)) {
198 $entry->setTitle($title);
199 }
200
201 if (!is_null($isArchived)) {
202 $entry->setArchived($isArchived);
203 }
204
205 if (!is_null($isStarred)) {
206 $entry->setStarred($isStarred);
207 }
208
209 $tags = $request->request->get('tags', '');
210 if (!empty($tags)) {
211 $this->assignTagsToEntry($entry, $tags);
212 }
213
214 $em = $this->getDoctrine()->getManager();
215 $em->flush();
216
217 $json = $this->get('serializer')->serialize($entry, 'json');
218
219 return new Response($json, 200, array('application/json'));
220 }
221
222 /**
223 * Delete **permanently** an entry
224 *
225 * @ApiDoc(
226 * requirements={
227 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
228 * }
229 * )
230 * @return Entry
231 */
232 public function deleteEntriesAction(Entry $entry)
233 {
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());
236 }
237
238 $em = $this->getDoctrine()->getManager();
239 $em->remove($entry);
240 $em->flush();
241
242 $json = $this->get('serializer')->serialize($entry, 'json');
243
244 return new Response($json, 200, array('application/json'));
245 }
246
247 /**
248 * Retrieve all tags for an entry
249 *
250 * @ApiDoc(
251 * requirements={
252 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
253 * }
254 * )
255 */
256 public function getEntriesTagsAction(Entry $entry)
257 {
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());
260 }
261
262 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
263
264 return new Response($json, 200, array('application/json'));
265 }
266
267 /**
268 * Add one or more tags to an entry
269 *
270 * @ApiDoc(
271 * requirements={
272 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
273 * },
274 * parameters={
275 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
276 * }
277 * )
278 */
279 public function postEntriesTagsAction(Request $request, Entry $entry)
280 {
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());
283 }
284
285 $tags = $request->request->get('tags', '');
286 if (!empty($tags)) {
287 $this->assignTagsToEntry($entry, $tags);
288 }
289
290 $em = $this->getDoctrine()->getManager();
291 $em->persist($entry);
292 $em->flush();
293
294 $json = $this->get('serializer')->serialize($entry, 'json');
295
296 return new Response($json, 200, array('application/json'));
297 }
298
299 /**
300 * Permanently remove one tag for an entry
301 *
302 * @ApiDoc(
303 * requirements={
304 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"},
305 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
306 * }
307 * )
308 */
309 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
310 {
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());
313 }
314
315 $entry->removeTag($tag);
316 $em = $this->getDoctrine()->getManager();
317 $em->persist($entry);
318 $em->flush();
319
320 $json = $this->get('serializer')->serialize($entry, 'json');
321
322 return new Response($json, 200, array('application/json'));
323 }
324
325 /**
326 * Retrieve all tags
327 *
328 * @ApiDoc()
329 */
330 public function getTagsAction()
331 {
332 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
333
334 return new Response($json, 200, array('application/json'));
335 }
336
337 /**
338 * Permanently remove one tag from **every** entry
339 *
340 * @ApiDoc(
341 * requirements={
342 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}
343 * }
344 * )
345 */
346 public function deleteTagAction(Tag $tag)
347 {
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());
350 }
351
352 $em = $this->getDoctrine()->getManager();
353 $em->remove($tag);
354 $em->flush();
355
356 $json = $this->get('serializer')->serialize($tag, 'json');
357
358 return new Response($json, 200, array('application/json'));
359 }
360}