]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/WallabagRestController.php
81974371402fa98a7c3ae22fcadd3f9664d1f256
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / WallabagRestController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\JsonResponse;
9 use Wallabag\CoreBundle\Entity\Entry;
10 use Wallabag\CoreBundle\Entity\Tag;
11 use Wallabag\CoreBundle\Service\Extractor;
12
13 class WallabagRestController extends Controller
14 {
15 /**
16 * @param Entry $entry
17 * @param string $tags
18 */
19 private function assignTagsToEntry(Entry $entry, $tags)
20 {
21 foreach (explode(',', $tags) as $label) {
22 $label = trim($label);
23 $tagEntity = $this
24 ->getDoctrine()
25 ->getRepository('WallabagCoreBundle:Tag')
26 ->findOneByLabel($label);
27
28 if (is_null($tagEntity)) {
29 $tagEntity = new Tag($this->getUser());
30 $tagEntity->setLabel($label);
31 }
32
33 // only add the tag on the entry if the relation doesn't exist
34 if (!$entry->getTags()->contains($tagEntity)) {
35 $entry->addTag($tagEntity);
36 }
37 }
38 }
39
40 /**
41 * Retrieve salt for a giver user.
42 *
43 * @ApiDoc()
44 * @return array
45 */
46 public function getSaltAction($username)
47 {
48 $user = $this
49 ->getDoctrine()
50 ->getRepository('WallabagCoreBundle:User')
51 ->findOneByUsername($username);
52
53 if (is_null($user)) {
54 throw $this->createNotFoundException();
55 }
56
57 return array($user->getSalt() ?: null);
58 }
59 /**
60 * Retrieve all entries. It could be filtered by many options.
61 *
62 * @ApiDoc(
63 * parameters={
64 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by archived status."},
65 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false, all entries by default", "description"="filter by starred status."},
66 * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."},
67 * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."},
68 * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."},
69 * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."},
70 * {"name"="tags", "dataType"="string", "required"=false, "format"="api%2Crest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."},
71 * }
72 * )
73 * @return Entry
74 */
75 public function getEntriesAction(Request $request)
76 {
77 $isArchived = $request->query->get('archive');
78 $isStarred = $request->query->get('star');
79 $sort = $request->query->get('sort', 'created');
80 $order = $request->query->get('order', 'desc');
81 $page = $request->query->get('page', 1);
82 $perPage = $request->query->get('perPage', 30);
83 $tags = $request->query->get('tags', array());
84
85 $entries = $this
86 ->getDoctrine()
87 ->getRepository('WallabagCoreBundle:Entry')
88 ->findEntries($this->getUser()->getId(), $isArchived, $isStarred, $sort, $order);
89
90 if (!($entries)) {
91 throw $this->createNotFoundException();
92 }
93
94 $json = $this->get('serializer')->serialize($entries, 'json');
95
96 return new JsonResponse($json, 200);
97 }
98
99 /**
100 * Retrieve a single entry
101 *
102 * @ApiDoc(
103 * requirements={
104 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
105 * }
106 * )
107 * @return Entry
108 */
109 public function getEntryAction(Entry $entry)
110 {
111 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
112 throw $this->createAccessDeniedException();
113 }
114
115 $json = $this->get('serializer')->serialize($entry, 'json');
116
117 return new JsonResponse($json, 200);
118 }
119
120 /**
121 * Create an entry
122 *
123 * @ApiDoc(
124 * parameters={
125 * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."},
126 * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."},
127 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
128 * }
129 * )
130 * @return Entry
131 */
132 public function postEntriesAction(Request $request)
133 {
134 $url = $request->request->get('url');
135
136 $content = Extractor::extract($url);
137 $entry = new Entry($this->getUser());
138 $entry->setUrl($url);
139 $entry->setTitle($request->request->get('title') ?: $content->getTitle());
140 $entry->setContent($content->getBody());
141
142 $tags = $request->request->get('tags', '');
143 if (!empty($tags)) {
144 $this->assignTagsToEntry($entry, $tags);
145 }
146
147 $em = $this->getDoctrine()->getManager();
148 $em->persist($entry);
149 $em->flush();
150
151 $json = $this->get('serializer')->serialize($entry, 'json');
152
153 return new JsonResponse($json, 200);
154 }
155
156 /**
157 * Change several properties of an entry
158 *
159 * @ApiDoc(
160 * requirements={
161 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
162 * },
163 * parameters={
164 * {"name"="title", "dataType"="string", "required"=false},
165 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
166 * {"name"="archive", "dataType"="boolean", "required"=false, "format"="true or false", "description"="archived the entry."},
167 * {"name"="star", "dataType"="boolean", "required"=false, "format"="true or false", "description"="starred the entry."},
168 * }
169 * )
170 * @return Entry
171 */
172 public function patchEntriesAction(Entry $entry, Request $request)
173 {
174 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
175 throw $this->createAccessDeniedException();
176 }
177
178 $title = $request->request->get("title");
179 $isArchived = $request->request->get("archive");
180 $isStarred = $request->request->get("star");
181
182 if (!is_null($title)) {
183 $entry->setTitle($title);
184 }
185
186 if (!is_null($isArchived)) {
187 $entry->setArchived($isArchived);
188 }
189
190 if (!is_null($isStarred)) {
191 $entry->setStarred($isStarred);
192 }
193
194 $tags = $request->request->get('tags', '');
195 if (!empty($tags)) {
196 $this->assignTagsToEntry($entry, $tags);
197 }
198
199 $em = $this->getDoctrine()->getManager();
200 $em->flush();
201
202 $json = $this->get('serializer')->serialize($entry, 'json');
203
204 return new JsonResponse($json, 200);
205 }
206
207 /**
208 * Delete **permanently** an entry
209 *
210 * @ApiDoc(
211 * requirements={
212 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
213 * }
214 * )
215 * @return Entry
216 */
217 public function deleteEntriesAction(Entry $entry)
218 {
219 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
220 throw $this->createAccessDeniedException();
221 }
222
223 $em = $this->getDoctrine()->getManager();
224 $em->remove($entry);
225 $em->flush();
226
227 $json = $this->get('serializer')->serialize($entry, 'json');
228
229 return new JsonResponse($json, 200);
230 }
231
232 /**
233 * Retrieve all tags for an entry
234 *
235 * @ApiDoc(
236 * requirements={
237 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
238 * }
239 * )
240 */
241 public function getEntriesTagsAction(Entry $entry)
242 {
243 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
244 throw $this->createAccessDeniedException();
245 }
246
247 $json = $this->get('serializer')->serialize($entry->getTags(), 'json');
248
249 return new JsonResponse($json, 200);
250 }
251
252 /**
253 * Add one or more tags to an entry
254 *
255 * @ApiDoc(
256 * requirements={
257 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
258 * },
259 * parameters={
260 * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."},
261 * }
262 * )
263 */
264 public function postEntriesTagsAction(Request $request, Entry $entry)
265 {
266 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
267 throw $this->createAccessDeniedException();
268 }
269
270 $tags = $request->request->get('tags', '');
271 if (!empty($tags)) {
272 $this->assignTagsToEntry($entry, $tags);
273 }
274
275 $em = $this->getDoctrine()->getManager();
276 $em->persist($entry);
277 $em->flush();
278
279 $json = $this->get('serializer')->serialize($entry, 'json');
280
281 return new JsonResponse($json, 200);
282 }
283
284 /**
285 * Permanently remove one tag for an entry
286 *
287 * @ApiDoc(
288 * requirements={
289 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"},
290 * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"}
291 * }
292 * )
293 */
294 public function deleteEntriesTagsAction(Entry $entry, Tag $tag)
295 {
296 if ($entry->getUser()->getId() != $this->getUser()->getId()) {
297 throw $this->createAccessDeniedException();
298 }
299
300 $entry->removeTag($tag);
301 $em = $this->getDoctrine()->getManager();
302 $em->persist($entry);
303 $em->flush();
304
305 $json = $this->get('serializer')->serialize($entry, 'json');
306
307 return new JsonResponse($json, 200);
308 }
309
310 /**
311 * Retrieve all tags
312 *
313 * @ApiDoc()
314 */
315 public function getTagsAction()
316 {
317 $json = $this->get('serializer')->serialize($this->getUser()->getTags(), 'json');
318
319 return new JsonResponse($json, 200);
320 }
321
322 /**
323 * Permanently remove one tag from **every** entry
324 *
325 * @ApiDoc(
326 * requirements={
327 * {"name"="tag", "dataType"="string", "requirement"="\w+", "description"="The tag"}
328 * }
329 * )
330 */
331 public function deleteTagAction(Tag $tag)
332 {
333 if ($tag->getUser()->getId() != $this->getUser()->getId()) {
334 throw $this->createAccessDeniedException();
335 }
336
337 $em = $this->getDoctrine()->getManager();
338 $em->remove($tag);
339 $em->flush();
340
341 $json = $this->get('serializer')->serialize($tag, 'json');
342
343 return new JsonResponse($json, 200);
344 }
345 }