]>
Commit | Line | Data |
---|---|---|
f8bf8952 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | |
4 | ||
5 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
f8bf8952 | 6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
27f15aa4 | 7 | use Symfony\Component\HttpFoundation\Request; |
b0cce9e6 | 8 | use Symfony\Component\HttpFoundation\Response; |
be463487 | 9 | use Wallabag\CoreBundle\Entity\Entry; |
653e8be4 | 10 | use Wallabag\CoreBundle\Entity\Tag; |
843dbe51 | 11 | use Wallabag\CoreBundle\Service\Extractor; |
6e22bd73 WD |
12 | use Hateoas\Configuration\Route; |
13 | use Hateoas\Representation\Factory\PagerfantaFactory; | |
f8bf8952 | 14 | |
a8c90c5c | 15 | class WallabagRestController extends Controller |
f8bf8952 | 16 | { |
092ca707 NL |
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 | ||
c5e8ba25 NL |
42 | /** |
43 | * Retrieve salt for a giver user. | |
44 | * | |
efad7e53 NL |
45 | * @ApiDoc( |
46 | * parameters={ | |
47 | * {"name"="username", "dataType"="string", "required"=true, "description"="username"} | |
48 | * } | |
49 | * ) | |
874e3e10 | 50 | * @return array |
c5e8ba25 | 51 | */ |
6ee416a0 | 52 | public function getSaltAction($username) |
c5e8ba25 NL |
53 | { |
54 | $user = $this | |
55 | ->getDoctrine() | |
56 | ->getRepository('WallabagCoreBundle:User') | |
6ee416a0 | 57 | ->findOneByUsername($username); |
c5e8ba25 NL |
58 | |
59 | if (is_null($user)) { | |
60 | throw $this->createNotFoundException(); | |
61 | } | |
62 | ||
f170f315 | 63 | return array($user->getSalt() ?: null); |
c5e8ba25 | 64 | } |
f8bf8952 | 65 | /** |
a8c90c5c | 66 | * Retrieve all entries. It could be filtered by many options. |
f8bf8952 NL |
67 | * |
68 | * @ApiDoc( | |
a8c90c5c | 69 | * parameters={ |
2c093b03 NL |
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."}, | |
a8c90c5c NL |
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 | * } | |
f8bf8952 | 78 | * ) |
be463487 | 79 | * @return Entry |
f8bf8952 | 80 | */ |
27f15aa4 | 81 | public function getEntriesAction(Request $request) |
f8bf8952 | 82 | { |
6e334aba NL |
83 | $isArchived = $request->query->get('archive'); |
84 | $isStarred = $request->query->get('star'); | |
a8c90c5c NL |
85 | $sort = $request->query->get('sort', 'created'); |
86 | $order = $request->query->get('order', 'desc'); | |
6e22bd73 WD |
87 | $page = (int) $request->query->get('page', 1); |
88 | $perPage = (int) $request->query->get('perPage', 30); | |
a8c90c5c NL |
89 | $tags = $request->query->get('tags', array()); |
90 | ||
6e22bd73 | 91 | $pager = $this |
a8c90c5c | 92 | ->getDoctrine() |
be463487 | 93 | ->getRepository('WallabagCoreBundle:Entry') |
1d147791 | 94 | ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order); |
a8c90c5c | 95 | |
6e22bd73 | 96 | if (0 === $pager->getNbResults()) { |
a8c90c5c NL |
97 | throw $this->createNotFoundException(); |
98 | } | |
99 | ||
6e22bd73 WD |
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'); | |
0f006880 | 110 | |
db2b4bf6 | 111 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
112 | } |
113 | ||
114 | /** | |
a8c90c5c | 115 | * Retrieve a single entry |
f8bf8952 NL |
116 | * |
117 | * @ApiDoc( | |
118 | * requirements={ | |
119 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
120 | * } | |
121 | * ) | |
be463487 | 122 | * @return Entry |
f8bf8952 | 123 | */ |
be463487 | 124 | public function getEntryAction(Entry $entry) |
f8bf8952 | 125 | { |
092ca707 | 126 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 127 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
092ca707 NL |
128 | } |
129 | ||
aa4d6562 | 130 | $json = $this->get('serializer')->serialize($entry, 'json'); |
0f006880 | 131 | |
db2b4bf6 | 132 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
133 | } |
134 | ||
135 | /** | |
a8c90c5c | 136 | * Create an entry |
f8bf8952 NL |
137 | * |
138 | * @ApiDoc( | |
a8c90c5c NL |
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 | * } | |
f8bf8952 | 144 | * ) |
be463487 | 145 | * @return Entry |
f8bf8952 | 146 | */ |
843dbe51 | 147 | public function postEntriesAction(Request $request) |
f8bf8952 | 148 | { |
c3235553 NL |
149 | $url = $request->request->get('url'); |
150 | ||
151 | $content = Extractor::extract($url); | |
d4c029f4 | 152 | $entry = new Entry($this->getUser()); |
c3235553 | 153 | $entry->setUrl($url); |
9e0fff7c | 154 | $entry->setTitle($request->request->get('title') ?: $content->getTitle()); |
843dbe51 | 155 | $entry->setContent($content->getBody()); |
092ca707 | 156 | |
0ca374e6 NL |
157 | $tags = $request->request->get('tags', ''); |
158 | if (!empty($tags)) { | |
159 | $this->assignTagsToEntry($entry, $tags); | |
160 | } | |
092ca707 | 161 | |
843dbe51 NL |
162 | $em = $this->getDoctrine()->getManager(); |
163 | $em->persist($entry); | |
164 | $em->flush(); | |
165 | ||
aa4d6562 NL |
166 | $json = $this->get('serializer')->serialize($entry, 'json'); |
167 | ||
db2b4bf6 | 168 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
169 | } |
170 | ||
171 | /** | |
a8c90c5c | 172 | * Change several properties of an entry |
f8bf8952 NL |
173 | * |
174 | * @ApiDoc( | |
175 | * requirements={ | |
176 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
a8c90c5c NL |
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."}, | |
2c093b03 NL |
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."}, | |
1d147791 | 183 | * } |
f8bf8952 | 184 | * ) |
be463487 | 185 | * @return Entry |
f8bf8952 | 186 | */ |
be463487 | 187 | public function patchEntriesAction(Entry $entry, Request $request) |
f8bf8952 | 188 | { |
092ca707 | 189 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 190 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
092ca707 NL |
191 | } |
192 | ||
2c093b03 | 193 | $title = $request->request->get("title"); |
2c093b03 | 194 | $isArchived = $request->request->get("archive"); |
2c093b03 NL |
195 | $isStarred = $request->request->get("star"); |
196 | ||
197 | if (!is_null($title)) { | |
198 | $entry->setTitle($title); | |
199 | } | |
200 | ||
201 | if (!is_null($isArchived)) { | |
905ae369 | 202 | $entry->setArchived($isArchived); |
2c093b03 NL |
203 | } |
204 | ||
2c093b03 | 205 | if (!is_null($isStarred)) { |
905ae369 | 206 | $entry->setStarred($isStarred); |
2c093b03 NL |
207 | } |
208 | ||
0ca374e6 NL |
209 | $tags = $request->request->get('tags', ''); |
210 | if (!empty($tags)) { | |
211 | $this->assignTagsToEntry($entry, $tags); | |
212 | } | |
092ca707 | 213 | |
2c093b03 | 214 | $em = $this->getDoctrine()->getManager(); |
2c093b03 NL |
215 | $em->flush(); |
216 | ||
0ca374e6 NL |
217 | $json = $this->get('serializer')->serialize($entry, 'json'); |
218 | ||
db2b4bf6 | 219 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
220 | } |
221 | ||
222 | /** | |
a8c90c5c | 223 | * Delete **permanently** an entry |
f8bf8952 NL |
224 | * |
225 | * @ApiDoc( | |
a8c90c5c NL |
226 | * requirements={ |
227 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
228 | * } | |
f8bf8952 | 229 | * ) |
be463487 | 230 | * @return Entry |
f8bf8952 | 231 | */ |
be463487 | 232 | public function deleteEntriesAction(Entry $entry) |
f8bf8952 | 233 | { |
092ca707 | 234 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 235 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
092ca707 NL |
236 | } |
237 | ||
42a90646 | 238 | $em = $this->getDoctrine()->getManager(); |
1d147791 | 239 | $em->remove($entry); |
42a90646 NL |
240 | $em->flush(); |
241 | ||
1d147791 NL |
242 | $json = $this->get('serializer')->serialize($entry, 'json'); |
243 | ||
db2b4bf6 | 244 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
245 | } |
246 | ||
247 | /** | |
a8c90c5c | 248 | * Retrieve all tags for an entry |
f8bf8952 NL |
249 | * |
250 | * @ApiDoc( | |
251 | * requirements={ | |
252 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
253 | * } | |
254 | * ) | |
255 | */ | |
be463487 | 256 | public function getEntriesTagsAction(Entry $entry) |
7df80cb3 | 257 | { |
092ca707 | 258 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 259 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
092ca707 NL |
260 | } |
261 | ||
1bd12b62 | 262 | $json = $this->get('serializer')->serialize($entry->getTags(), 'json'); |
0a018fe0 | 263 | |
b0cce9e6 | 264 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
265 | } |
266 | ||
267 | /** | |
a8c90c5c | 268 | * Add one or more tags to an entry |
f8bf8952 NL |
269 | * |
270 | * @ApiDoc( | |
271 | * requirements={ | |
272 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
a8c90c5c NL |
273 | * }, |
274 | * parameters={ | |
275 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
276 | * } | |
f8bf8952 NL |
277 | * ) |
278 | */ | |
a36737f4 | 279 | public function postEntriesTagsAction(Request $request, Entry $entry) |
7df80cb3 | 280 | { |
092ca707 | 281 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 282 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
a36737f4 NL |
283 | } |
284 | ||
0ca374e6 NL |
285 | $tags = $request->request->get('tags', ''); |
286 | if (!empty($tags)) { | |
287 | $this->assignTagsToEntry($entry, $tags); | |
288 | } | |
092ca707 | 289 | |
a36737f4 NL |
290 | $em = $this->getDoctrine()->getManager(); |
291 | $em->persist($entry); | |
292 | $em->flush(); | |
293 | ||
294 | $json = $this->get('serializer')->serialize($entry, 'json'); | |
295 | ||
db2b4bf6 | 296 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
297 | } |
298 | ||
299 | /** | |
a8c90c5c | 300 | * Permanently remove one tag for an entry |
f8bf8952 NL |
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 | */ | |
653e8be4 | 309 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) |
f8bf8952 | 310 | { |
092ca707 | 311 | if ($entry->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 312 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$entry->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
092ca707 NL |
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 | ||
db2b4bf6 | 322 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
323 | } |
324 | ||
325 | /** | |
a8c90c5c | 326 | * Retrieve all tags |
f8bf8952 | 327 | * |
092ca707 | 328 | * @ApiDoc() |
f8bf8952 | 329 | */ |
092ca707 | 330 | public function getTagsAction() |
7df80cb3 | 331 | { |
092ca707 NL |
332 | $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json'); |
333 | ||
db2b4bf6 | 334 | return new Response($json, 200, array('application/json')); |
f8bf8952 NL |
335 | } |
336 | ||
f8bf8952 | 337 | /** |
a8c90c5c | 338 | * Permanently remove one tag from **every** entry |
f8bf8952 NL |
339 | * |
340 | * @ApiDoc( | |
341 | * requirements={ | |
342 | * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"} | |
343 | * } | |
344 | * ) | |
345 | */ | |
653e8be4 | 346 | public function deleteTagAction(Tag $tag) |
f8bf8952 | 347 | { |
092ca707 | 348 | if ($tag->getUser()->getId() != $this->getUser()->getId()) { |
efad7e53 | 349 | throw $this->createAccessDeniedException('Access forbidden. Entry user id: '.$tag->getUser()->getId().', logged user id: '.$this->getUser()->getId()); |
092ca707 NL |
350 | } |
351 | ||
352 | $em = $this->getDoctrine()->getManager(); | |
353 | $em->remove($tag); | |
354 | $em->flush(); | |
355 | ||
356 | $json = $this->get('serializer')->serialize($tag, 'json'); | |
357 | ||
db2b4bf6 | 358 | return new Response($json, 200, array('application/json')); |
f8bf8952 | 359 | } |
7df80cb3 | 360 | } |