]>
Commit | Line | Data |
---|---|---|
900c8448 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
5 | use Hateoas\Configuration\Route; | |
6 | use Hateoas\Representation\Factory\PagerfantaFactory; | |
7 | use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |
900c8448 | 8 | use Symfony\Component\HttpFoundation\JsonResponse; |
f808b016 | 9 | use Symfony\Component\HttpFoundation\Request; |
52b84c11 | 10 | use Symfony\Component\HttpFoundation\Response; |
78e3fafa | 11 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
f808b016 | 12 | use Symfony\Component\HttpKernel\Exception\HttpException; |
900c8448 NL |
13 | use Wallabag\CoreBundle\Entity\Entry; |
14 | use Wallabag\CoreBundle\Entity\Tag; | |
5a619812 | 15 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; |
f808b016 | 16 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
900c8448 NL |
17 | |
18 | class EntryRestController extends WallabagRestController | |
19 | { | |
20 | /** | |
21 | * Check if an entry exist by url. | |
18696f77 JB |
22 | * Return ID if entry(ies) exist (and if you give the return_id parameter). |
23 | * Otherwise it returns false. | |
900c8448 | 24 | * |
39ffaba3 JB |
25 | * @todo Remove that `return_id` in the next major release |
26 | * | |
900c8448 NL |
27 | * @ApiDoc( |
28 | * parameters={ | |
18696f77 | 29 | * {"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"}, |
9c2b2aae JB |
30 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="DEPRECATED, use hashed_url instead"}, |
31 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="DEPRECATED, use hashed_urls instead"}, | |
76bc05eb JB |
32 | * {"name"="hashed_url", "dataType"="string", "required"=false, "format"="A hashed url", "description"="Hashed url using SHA1 to check if it exists"}, |
33 | * {"name"="hashed_urls", "dataType"="string", "required"=false, "format"="An array of hashed urls (?hashed_urls[]=xxx...&hashed_urls[]=xxx...)", "description"="An array of hashed urls using SHA1 to check if they exist"} | |
900c8448 NL |
34 | * } |
35 | * ) | |
36 | * | |
37 | * @return JsonResponse | |
38 | */ | |
39 | public function getEntriesExistsAction(Request $request) | |
40 | { | |
41 | $this->validateAuthentication(); | |
9c2b2aae | 42 | $repo = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); |
900c8448 | 43 | |
331e5b02 | 44 | $returnId = (null === $request->query->get('return_id')) ? false : (bool) $request->query->get('return_id'); |
900c8448 | 45 | |
9c2b2aae JB |
46 | $urls = $request->query->get('urls', []); |
47 | $hashedUrls = $request->query->get('hashed_urls', []); | |
bfe02a0b | 48 | |
900c8448 | 49 | // handle multiple urls first |
bfe02a0b | 50 | if (!empty($hashedUrls)) { |
900c8448 | 51 | $results = []; |
bfe02a0b | 52 | foreach ($hashedUrls as $hashedUrl) { |
9c2b2aae | 53 | $res = $repo->findByHashedUrlAndUserId($hashedUrl, $this->getUser()->getId()); |
900c8448 | 54 | |
bfe02a0b | 55 | $results[$hashedUrl] = $this->returnExistInformation($res, $returnId); |
900c8448 NL |
56 | } |
57 | ||
72db15ca | 58 | return $this->sendResponse($results); |
900c8448 NL |
59 | } |
60 | ||
9c2b2aae JB |
61 | // @deprecated, to be remove in 3.0 |
62 | if (!empty($urls)) { | |
63 | $results = []; | |
64 | foreach ($urls as $url) { | |
65 | $res = $repo->findByUrlAndUserId($url, $this->getUser()->getId()); | |
66 | ||
67 | $results[$url] = $this->returnExistInformation($res, $returnId); | |
68 | } | |
69 | ||
70 | return $this->sendResponse($results); | |
71 | } | |
72 | ||
900c8448 | 73 | // let's see if it is a simple url? |
9c2b2aae JB |
74 | $url = $request->query->get('url', ''); |
75 | $hashedUrl = $request->query->get('hashed_url', ''); | |
bfe02a0b | 76 | |
9c2b2aae JB |
77 | if (empty($url) && empty($hashedUrl)) { |
78 | throw $this->createAccessDeniedException('URL is empty?, logged user id: ' . $this->getUser()->getId()); | |
79 | } | |
900c8448 | 80 | |
9c2b2aae JB |
81 | $method = 'findByUrlAndUserId'; |
82 | if (!empty($hashedUrl)) { | |
83 | $method = 'findByHashedUrlAndUserId'; | |
84 | $url = $hashedUrl; | |
900c8448 NL |
85 | } |
86 | ||
9c2b2aae | 87 | $res = $repo->$method($url, $this->getUser()->getId()); |
900c8448 | 88 | |
bfe02a0b | 89 | return $this->sendResponse(['exists' => $this->returnExistInformation($res, $returnId)]); |
900c8448 NL |
90 | } |
91 | ||
92 | /** | |
93 | * Retrieve all entries. It could be filtered by many options. | |
94 | * | |
95 | * @ApiDoc( | |
96 | * parameters={ | |
97 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, | |
98 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, | |
0e70e812 | 99 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated' or 'archived', default 'created'", "description"="sort entries by date."}, |
900c8448 NL |
100 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, |
101 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | |
102 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | |
103 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | |
104 | * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, | |
1112e547 | 105 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"}, |
2c290747 | 106 | * {"name"="detail", "dataType"="string", "required"=false, "format"="metadata or full, metadata by default", "description"="include content field if 'full'. 'full' by default for backward compatibility."}, |
900c8448 NL |
107 | * } |
108 | * ) | |
109 | * | |
110 | * @return JsonResponse | |
111 | */ | |
112 | public function getEntriesAction(Request $request) | |
113 | { | |
114 | $this->validateAuthentication(); | |
115 | ||
116 | $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); | |
117 | $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); | |
1112e547 | 118 | $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public'); |
78e3fafa JB |
119 | $sort = strtolower($request->query->get('sort', 'created')); |
120 | $order = strtolower($request->query->get('order', 'desc')); | |
900c8448 NL |
121 | $page = (int) $request->query->get('page', 1); |
122 | $perPage = (int) $request->query->get('perPage', 30); | |
2a1ceb67 | 123 | $tags = \is_array($request->query->get('tags')) ? '' : (string) $request->query->get('tags', ''); |
900c8448 | 124 | $since = $request->query->get('since', 0); |
2c290747 | 125 | $detail = strtolower($request->query->get('detail', 'full')); |
900c8448 | 126 | |
78e3fafa JB |
127 | try { |
128 | /** @var \Pagerfanta\Pagerfanta $pager */ | |
129 | $pager = $this->get('wallabag_core.entry_repository')->findEntries( | |
130 | $this->getUser()->getId(), | |
131 | $isArchived, | |
132 | $isStarred, | |
133 | $isPublic, | |
134 | $sort, | |
135 | $order, | |
136 | $since, | |
2c290747 KD |
137 | $tags, |
138 | $detail | |
78e3fafa JB |
139 | ); |
140 | } catch (\Exception $e) { | |
141 | throw new BadRequestHttpException($e->getMessage()); | |
142 | } | |
900c8448 | 143 | |
900c8448 | 144 | $pager->setMaxPerPage($perPage); |
b60a666d | 145 | $pager->setCurrentPage($page); |
900c8448 NL |
146 | |
147 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | |
148 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | |
149 | $pager, | |
150 | new Route( | |
151 | 'api_get_entries', | |
152 | [ | |
153 | 'archive' => $isArchived, | |
154 | 'starred' => $isStarred, | |
1112e547 | 155 | 'public' => $isPublic, |
900c8448 NL |
156 | 'sort' => $sort, |
157 | 'order' => $order, | |
158 | 'page' => $page, | |
159 | 'perPage' => $perPage, | |
160 | 'tags' => $tags, | |
161 | 'since' => $since, | |
2c290747 | 162 | 'detail' => $detail, |
900c8448 | 163 | ], |
80104254 | 164 | true |
900c8448 NL |
165 | ) |
166 | ); | |
167 | ||
72db15ca | 168 | return $this->sendResponse($paginatedCollection); |
900c8448 NL |
169 | } |
170 | ||
171 | /** | |
172 | * Retrieve a single entry. | |
173 | * | |
174 | * @ApiDoc( | |
175 | * requirements={ | |
176 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
177 | * } | |
178 | * ) | |
179 | * | |
180 | * @return JsonResponse | |
181 | */ | |
182 | public function getEntryAction(Entry $entry) | |
183 | { | |
184 | $this->validateAuthentication(); | |
185 | $this->validateUserAccess($entry->getUser()->getId()); | |
186 | ||
72db15ca | 187 | return $this->sendResponse($entry); |
900c8448 NL |
188 | } |
189 | ||
864c1dd2 JB |
190 | /** |
191 | * Retrieve a single entry as a predefined format. | |
192 | * | |
193 | * @ApiDoc( | |
194 | * requirements={ | |
195 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
196 | * } | |
197 | * ) | |
198 | * | |
199 | * @return Response | |
200 | */ | |
201 | public function getEntryExportAction(Entry $entry, Request $request) | |
202 | { | |
203 | $this->validateAuthentication(); | |
204 | $this->validateUserAccess($entry->getUser()->getId()); | |
205 | ||
206 | return $this->get('wallabag_core.helper.entries_export') | |
207 | ->setEntries($entry) | |
208 | ->updateTitle('entry') | |
07320a2b | 209 | ->updateAuthor('entry') |
864c1dd2 JB |
210 | ->exportAs($request->attributes->get('_format')); |
211 | } | |
212 | ||
1eca7831 | 213 | /** |
a7abcc7b | 214 | * Handles an entries list and delete URL. |
1eca7831 NL |
215 | * |
216 | * @ApiDoc( | |
217 | * parameters={ | |
a7abcc7b | 218 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to delete."} |
1eca7831 NL |
219 | * } |
220 | * ) | |
221 | * | |
222 | * @return JsonResponse | |
223 | */ | |
a7abcc7b | 224 | public function deleteEntriesListAction(Request $request) |
1eca7831 NL |
225 | { |
226 | $this->validateAuthentication(); | |
227 | ||
a7abcc7b | 228 | $urls = json_decode($request->query->get('urls', [])); |
72db15ca JB |
229 | |
230 | if (empty($urls)) { | |
231 | return $this->sendResponse([]); | |
232 | } | |
233 | ||
1eca7831 NL |
234 | $results = []; |
235 | ||
236 | // handle multiple urls | |
72db15ca JB |
237 | foreach ($urls as $key => $url) { |
238 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
239 | $url, | |
240 | $this->getUser()->getId() | |
241 | ); | |
a7abcc7b | 242 | |
72db15ca | 243 | $results[$key]['url'] = $url; |
a7abcc7b | 244 | |
72db15ca JB |
245 | if (false !== $entry) { |
246 | $em = $this->getDoctrine()->getManager(); | |
247 | $em->remove($entry); | |
248 | $em->flush(); | |
1eca7831 | 249 | |
72db15ca JB |
250 | // entry deleted, dispatch event about it! |
251 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
a7abcc7b | 252 | } |
1eca7831 | 253 | |
72db15ca JB |
254 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; |
255 | } | |
1eca7831 | 256 | |
72db15ca | 257 | return $this->sendResponse($results); |
a7abcc7b | 258 | } |
1eca7831 | 259 | |
a7abcc7b NL |
260 | /** |
261 | * Handles an entries list and create URL. | |
262 | * | |
263 | * @ApiDoc( | |
264 | * parameters={ | |
265 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} | |
266 | * } | |
267 | * ) | |
268 | * | |
72db15ca | 269 | * @throws HttpException When limit is reached |
f808b016 JB |
270 | * |
271 | * @return JsonResponse | |
a7abcc7b NL |
272 | */ |
273 | public function postEntriesListAction(Request $request) | |
274 | { | |
275 | $this->validateAuthentication(); | |
1eca7831 | 276 | |
a7abcc7b | 277 | $urls = json_decode($request->query->get('urls', [])); |
1eca7831 | 278 | |
efd351c9 NL |
279 | $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); |
280 | ||
2a1ceb67 | 281 | if (\count($urls) > $limit) { |
72db15ca | 282 | throw new HttpException(400, 'API limit reached'); |
efd351c9 NL |
283 | } |
284 | ||
d5c2cc54 JB |
285 | $results = []; |
286 | if (empty($urls)) { | |
287 | return $this->sendResponse($results); | |
288 | } | |
289 | ||
a7abcc7b | 290 | // handle multiple urls |
d5c2cc54 JB |
291 | foreach ($urls as $key => $url) { |
292 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
293 | $url, | |
294 | $this->getUser()->getId() | |
295 | ); | |
1eca7831 | 296 | |
d5c2cc54 | 297 | $results[$key]['url'] = $url; |
1eca7831 | 298 | |
d5c2cc54 JB |
299 | if (false === $entry) { |
300 | $entry = new Entry($this->getUser()); | |
a7abcc7b | 301 | |
d5c2cc54 JB |
302 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $url); |
303 | } | |
a7abcc7b | 304 | |
d5c2cc54 JB |
305 | $em = $this->getDoctrine()->getManager(); |
306 | $em->persist($entry); | |
307 | $em->flush(); | |
a7abcc7b | 308 | |
d5c2cc54 JB |
309 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; |
310 | ||
311 | // entry saved, dispatch event about it! | |
312 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
1eca7831 NL |
313 | } |
314 | ||
72db15ca | 315 | return $this->sendResponse($results); |
1eca7831 NL |
316 | } |
317 | ||
900c8448 NL |
318 | /** |
319 | * Create an entry. | |
320 | * | |
9e349f08 JB |
321 | * 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**. |
322 | * Otherwise, content will be fetched as normal from the url and values will be overwritten. | |
323 | * | |
900c8448 NL |
324 | * @ApiDoc( |
325 | * parameters={ | |
326 | * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, | |
327 | * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, | |
328 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
900c8448 | 329 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, |
a05b6115 | 330 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, |
e668a812 JB |
331 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, |
332 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
333 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
f0378b4d | 334 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, |
fb436e8c | 335 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, |
1112e547 | 336 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, |
3b771f51 | 337 | * {"name"="origin_url", "dataType"="string", "required"=false, "format"="http://www.test.com/article.html", "description"="Origin url for the entry (from where you found it)."}, |
900c8448 NL |
338 | * } |
339 | * ) | |
340 | * | |
341 | * @return JsonResponse | |
342 | */ | |
343 | public function postEntriesAction(Request $request) | |
344 | { | |
345 | $this->validateAuthentication(); | |
346 | ||
347 | $url = $request->request->get('url'); | |
900c8448 | 348 | |
db0c48af JB |
349 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
350 | $url, | |
351 | $this->getUser()->getId() | |
352 | ); | |
900c8448 NL |
353 | |
354 | if (false === $entry) { | |
08f29ae7 | 355 | $entry = new Entry($this->getUser()); |
e668a812 | 356 | $entry->setUrl($url); |
900c8448 NL |
357 | } |
358 | ||
a05b6115 JB |
359 | $data = $this->retrieveValueFromRequest($request); |
360 | ||
361 | try { | |
362 | $this->get('wallabag_core.content_proxy')->updateEntry( | |
363 | $entry, | |
364 | $entry->getUrl(), | |
365 | [ | |
366 | 'title' => !empty($data['title']) ? $data['title'] : $entry->getTitle(), | |
367 | 'html' => !empty($data['content']) ? $data['content'] : $entry->getContent(), | |
368 | 'url' => $entry->getUrl(), | |
369 | 'language' => !empty($data['language']) ? $data['language'] : $entry->getLanguage(), | |
370 | 'date' => !empty($data['publishedAt']) ? $data['publishedAt'] : $entry->getPublishedAt(), | |
371 | // faking the open graph preview picture | |
372 | 'open_graph' => [ | |
373 | 'og_image' => !empty($data['picture']) ? $data['picture'] : $entry->getPreviewPicture(), | |
374 | ], | |
2a1ceb67 | 375 | 'authors' => \is_string($data['authors']) ? explode(',', $data['authors']) : $entry->getPublishedBy(), |
a05b6115 JB |
376 | ] |
377 | ); | |
378 | } catch (\Exception $e) { | |
379 | $this->get('logger')->error('Error while saving an entry', [ | |
380 | 'exception' => $e, | |
381 | 'entry' => $entry, | |
382 | ]); | |
383 | } | |
384 | ||
c18a2476 | 385 | if (null !== $data['isArchived']) { |
7975395d | 386 | $entry->updateArchived((bool) $data['isArchived']); |
a05b6115 JB |
387 | } |
388 | ||
c18a2476 | 389 | if (null !== $data['isStarred']) { |
a991c46e | 390 | $entry->updateStar((bool) $data['isStarred']); |
a05b6115 JB |
391 | } |
392 | ||
393 | if (!empty($data['tags'])) { | |
394 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); | |
395 | } | |
396 | ||
e0ef1a1c KD |
397 | if (!empty($data['origin_url'])) { |
398 | $entry->setOriginUrl($data['origin_url']); | |
399 | } | |
400 | ||
c18a2476 | 401 | if (null !== $data['isPublic']) { |
a05b6115 JB |
402 | if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { |
403 | $entry->generateUid(); | |
404 | } elseif (false === (bool) $data['isPublic']) { | |
405 | $entry->cleanUid(); | |
406 | } | |
407 | } | |
408 | ||
af29e1bf KD |
409 | if (empty($entry->getDomainName())) { |
410 | $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); | |
411 | } | |
412 | ||
413 | if (empty($entry->getTitle())) { | |
414 | $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); | |
415 | } | |
416 | ||
a05b6115 JB |
417 | $em = $this->getDoctrine()->getManager(); |
418 | $em->persist($entry); | |
419 | $em->flush(); | |
420 | ||
421 | // entry saved, dispatch event about it! | |
422 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
5a619812 | 423 | |
72db15ca | 424 | return $this->sendResponse($entry); |
900c8448 NL |
425 | } |
426 | ||
427 | /** | |
428 | * Change several properties of an entry. | |
429 | * | |
430 | * @ApiDoc( | |
431 | * requirements={ | |
432 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
433 | * }, | |
434 | * parameters={ | |
435 | * {"name"="title", "dataType"="string", "required"=false}, | |
436 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
437 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | |
438 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | |
645291e8 JB |
439 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, |
440 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
441 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
442 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | |
443 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | |
1112e547 | 444 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, |
3b771f51 | 445 | * {"name"="origin_url", "dataType"="string", "required"=false, "format"="http://www.test.com/article.html", "description"="Origin url for the entry (from where you found it)."}, |
900c8448 NL |
446 | * } |
447 | * ) | |
448 | * | |
449 | * @return JsonResponse | |
450 | */ | |
451 | public function patchEntriesAction(Entry $entry, Request $request) | |
452 | { | |
453 | $this->validateAuthentication(); | |
454 | $this->validateUserAccess($entry->getUser()->getId()); | |
455 | ||
a05b6115 JB |
456 | $contentProxy = $this->get('wallabag_core.content_proxy'); |
457 | ||
458 | $data = $this->retrieveValueFromRequest($request); | |
459 | ||
460 | // this is a special case where user want to manually update the entry content | |
461 | // the ContentProxy will only cleanup the html | |
462 | // and also we force to not re-fetch the content in case of error | |
463 | if (!empty($data['content'])) { | |
464 | try { | |
465 | $contentProxy->updateEntry( | |
466 | $entry, | |
467 | $entry->getUrl(), | |
468 | [ | |
469 | 'html' => $data['content'], | |
470 | ], | |
471 | true | |
472 | ); | |
473 | } catch (\Exception $e) { | |
474 | $this->get('logger')->error('Error while saving an entry', [ | |
475 | 'exception' => $e, | |
476 | 'entry' => $entry, | |
477 | ]); | |
478 | } | |
479 | } | |
480 | ||
481 | if (!empty($data['title'])) { | |
482 | $entry->setTitle($data['title']); | |
483 | } | |
484 | ||
485 | if (!empty($data['language'])) { | |
486 | $contentProxy->updateLanguage($entry, $data['language']); | |
487 | } | |
488 | ||
2a1ceb67 | 489 | if (!empty($data['authors']) && \is_string($data['authors'])) { |
a05b6115 JB |
490 | $entry->setPublishedBy(explode(',', $data['authors'])); |
491 | } | |
492 | ||
493 | if (!empty($data['picture'])) { | |
494 | $contentProxy->updatePreviewPicture($entry, $data['picture']); | |
495 | } | |
496 | ||
497 | if (!empty($data['publishedAt'])) { | |
498 | $contentProxy->updatePublishedAt($entry, $data['publishedAt']); | |
499 | } | |
500 | ||
c18a2476 | 501 | if (null !== $data['isArchived']) { |
7975395d | 502 | $entry->updateArchived((bool) $data['isArchived']); |
a05b6115 JB |
503 | } |
504 | ||
c18a2476 | 505 | if (null !== $data['isStarred']) { |
a991c46e | 506 | $entry->updateStar((bool) $data['isStarred']); |
a05b6115 JB |
507 | } |
508 | ||
509 | if (!empty($data['tags'])) { | |
510 | $entry->removeAllTags(); | |
511 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $data['tags']); | |
512 | } | |
513 | ||
c18a2476 | 514 | if (null !== $data['isPublic']) { |
a05b6115 JB |
515 | if (true === (bool) $data['isPublic'] && null === $entry->getUid()) { |
516 | $entry->generateUid(); | |
517 | } elseif (false === (bool) $data['isPublic']) { | |
518 | $entry->cleanUid(); | |
519 | } | |
520 | } | |
521 | ||
e0ef1a1c KD |
522 | if (!empty($data['origin_url'])) { |
523 | $entry->setOriginUrl($data['origin_url']); | |
524 | } | |
525 | ||
af29e1bf KD |
526 | if (empty($entry->getDomainName())) { |
527 | $this->get('wallabag_core.content_proxy')->setEntryDomainName($entry); | |
528 | } | |
529 | ||
530 | if (empty($entry->getTitle())) { | |
531 | $this->get('wallabag_core.content_proxy')->setDefaultEntryTitle($entry); | |
532 | } | |
533 | ||
a05b6115 JB |
534 | $em = $this->getDoctrine()->getManager(); |
535 | $em->persist($entry); | |
536 | $em->flush(); | |
537 | ||
538 | // entry saved, dispatch event about it! | |
539 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
900c8448 | 540 | |
72db15ca | 541 | return $this->sendResponse($entry); |
900c8448 NL |
542 | } |
543 | ||
0a6f4568 JB |
544 | /** |
545 | * Reload an entry. | |
5cd0857e | 546 | * An empty response with HTTP Status 304 will be send if we weren't able to update the content (because it hasn't changed or we got an error). |
0a6f4568 JB |
547 | * |
548 | * @ApiDoc( | |
549 | * requirements={ | |
550 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
551 | * } | |
552 | * ) | |
553 | * | |
554 | * @return JsonResponse | |
555 | */ | |
556 | public function patchEntriesReloadAction(Entry $entry) | |
557 | { | |
558 | $this->validateAuthentication(); | |
559 | $this->validateUserAccess($entry->getUser()->getId()); | |
560 | ||
0a6f4568 | 561 | try { |
7aba665e | 562 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); |
0a6f4568 JB |
563 | } catch (\Exception $e) { |
564 | $this->get('logger')->error('Error while saving an entry', [ | |
565 | 'exception' => $e, | |
566 | 'entry' => $entry, | |
567 | ]); | |
568 | ||
5cd0857e | 569 | return new JsonResponse([], 304); |
0a6f4568 JB |
570 | } |
571 | ||
572 | // if refreshing entry failed, don't save it | |
573 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
5cd0857e | 574 | return new JsonResponse([], 304); |
0a6f4568 JB |
575 | } |
576 | ||
577 | $em = $this->getDoctrine()->getManager(); | |
578 | $em->persist($entry); | |
579 | $em->flush(); | |
580 | ||
581 | // entry saved, dispatch event about it! | |
582 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
583 | ||
72db15ca | 584 | return $this->sendResponse($entry); |
0a6f4568 JB |
585 | } |
586 | ||
900c8448 NL |
587 | /** |
588 | * Delete **permanently** an entry. | |
589 | * | |
590 | * @ApiDoc( | |
591 | * requirements={ | |
592 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
50830204 KD |
593 | * }, |
594 | * parameters={ | |
595 | * {"name"="expect", "dataType"="string", "required"=false, "format"="id or entry", "description"="Only returns the id instead of the deleted entry's full entity if 'id' is specified. Default to entry"}, | |
900c8448 NL |
596 | * } |
597 | * ) | |
598 | * | |
599 | * @return JsonResponse | |
600 | */ | |
50830204 | 601 | public function deleteEntriesAction(Entry $entry, Request $request) |
900c8448 | 602 | { |
50830204 KD |
603 | $expect = $request->query->get('expect', 'entry'); |
604 | if (!\in_array($expect, ['id', 'entry'], true)) { | |
605 | throw new BadRequestHttpException(sprintf("expect: 'id' or 'entry' expected, %s given", $expect)); | |
606 | } | |
900c8448 NL |
607 | $this->validateAuthentication(); |
608 | $this->validateUserAccess($entry->getUser()->getId()); | |
609 | ||
50830204 KD |
610 | $response = $this->sendResponse([ |
611 | 'id' => $entry->getId(), | |
612 | ]); | |
613 | // We clone $entry to keep id in returned object | |
614 | if ('entry' === $expect) { | |
615 | $e = clone $entry; | |
616 | $response = $this->sendResponse($e); | |
617 | } | |
f5ea67e4 | 618 | |
900c8448 NL |
619 | $em = $this->getDoctrine()->getManager(); |
620 | $em->remove($entry); | |
621 | $em->flush(); | |
622 | ||
5a619812 JB |
623 | // entry deleted, dispatch event about it! |
624 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
625 | ||
50830204 | 626 | return $response; |
900c8448 NL |
627 | } |
628 | ||
629 | /** | |
630 | * Retrieve all tags for an entry. | |
631 | * | |
632 | * @ApiDoc( | |
633 | * requirements={ | |
634 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
635 | * } | |
636 | * ) | |
637 | * | |
638 | * @return JsonResponse | |
639 | */ | |
640 | public function getEntriesTagsAction(Entry $entry) | |
641 | { | |
642 | $this->validateAuthentication(); | |
643 | $this->validateUserAccess($entry->getUser()->getId()); | |
644 | ||
72db15ca | 645 | return $this->sendResponse($entry->getTags()); |
900c8448 NL |
646 | } |
647 | ||
648 | /** | |
649 | * Add one or more tags to an entry. | |
650 | * | |
651 | * @ApiDoc( | |
652 | * requirements={ | |
653 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
654 | * }, | |
655 | * parameters={ | |
656 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
657 | * } | |
658 | * ) | |
659 | * | |
660 | * @return JsonResponse | |
661 | */ | |
662 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
663 | { | |
664 | $this->validateAuthentication(); | |
665 | $this->validateUserAccess($entry->getUser()->getId()); | |
666 | ||
667 | $tags = $request->request->get('tags', ''); | |
668 | if (!empty($tags)) { | |
6bc6fb1f | 669 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
900c8448 NL |
670 | } |
671 | ||
672 | $em = $this->getDoctrine()->getManager(); | |
673 | $em->persist($entry); | |
674 | $em->flush(); | |
675 | ||
72db15ca | 676 | return $this->sendResponse($entry); |
900c8448 NL |
677 | } |
678 | ||
679 | /** | |
680 | * Permanently remove one tag for an entry. | |
681 | * | |
682 | * @ApiDoc( | |
683 | * requirements={ | |
684 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"}, | |
685 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
686 | * } | |
687 | * ) | |
688 | * | |
689 | * @return JsonResponse | |
690 | */ | |
691 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
692 | { | |
693 | $this->validateAuthentication(); | |
694 | $this->validateUserAccess($entry->getUser()->getId()); | |
695 | ||
696 | $entry->removeTag($tag); | |
697 | $em = $this->getDoctrine()->getManager(); | |
698 | $em->persist($entry); | |
699 | $em->flush(); | |
700 | ||
72db15ca | 701 | return $this->sendResponse($entry); |
900c8448 | 702 | } |
d1fc5902 NL |
703 | |
704 | /** | |
80299ed2 | 705 | * Handles an entries list delete tags from them. |
d1fc5902 NL |
706 | * |
707 | * @ApiDoc( | |
708 | * parameters={ | |
80299ed2 | 709 | * {"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."} |
d1fc5902 NL |
710 | * } |
711 | * ) | |
712 | * | |
713 | * @return JsonResponse | |
714 | */ | |
80299ed2 | 715 | public function deleteEntriesTagsListAction(Request $request) |
d1fc5902 NL |
716 | { |
717 | $this->validateAuthentication(); | |
718 | ||
719 | $list = json_decode($request->query->get('list', [])); | |
72db15ca JB |
720 | |
721 | if (empty($list)) { | |
722 | return $this->sendResponse([]); | |
723 | } | |
d1fc5902 NL |
724 | |
725 | // handle multiple urls | |
72db15ca | 726 | $results = []; |
d1fc5902 | 727 | |
72db15ca JB |
728 | foreach ($list as $key => $element) { |
729 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
730 | $element->url, | |
731 | $this->getUser()->getId() | |
732 | ); | |
d1fc5902 | 733 | |
72db15ca JB |
734 | $results[$key]['url'] = $element->url; |
735 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
d1fc5902 | 736 | |
72db15ca | 737 | $tags = $element->tags; |
80299ed2 | 738 | |
72db15ca JB |
739 | if (false !== $entry && !(empty($tags))) { |
740 | $tags = explode(',', $tags); | |
741 | foreach ($tags as $label) { | |
742 | $label = trim($label); | |
80299ed2 | 743 | |
72db15ca JB |
744 | $tag = $this->getDoctrine() |
745 | ->getRepository('WallabagCoreBundle:Tag') | |
746 | ->findOneByLabel($label); | |
d1fc5902 | 747 | |
72db15ca JB |
748 | if (false !== $tag) { |
749 | $entry->removeTag($tag); | |
750 | } | |
d1fc5902 | 751 | } |
72db15ca JB |
752 | |
753 | $em = $this->getDoctrine()->getManager(); | |
754 | $em->persist($entry); | |
755 | $em->flush(); | |
d1fc5902 NL |
756 | } |
757 | } | |
758 | ||
72db15ca | 759 | return $this->sendResponse($results); |
d1fc5902 | 760 | } |
80299ed2 NL |
761 | |
762 | /** | |
763 | * Handles an entries list and add tags to them. | |
764 | * | |
765 | * @ApiDoc( | |
766 | * parameters={ | |
767 | * {"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."} | |
768 | * } | |
769 | * ) | |
770 | * | |
771 | * @return JsonResponse | |
772 | */ | |
773 | public function postEntriesTagsListAction(Request $request) | |
774 | { | |
775 | $this->validateAuthentication(); | |
776 | ||
777 | $list = json_decode($request->query->get('list', [])); | |
72db15ca JB |
778 | |
779 | if (empty($list)) { | |
780 | return $this->sendResponse([]); | |
781 | } | |
782 | ||
80299ed2 NL |
783 | $results = []; |
784 | ||
785 | // handle multiple urls | |
72db15ca JB |
786 | foreach ($list as $key => $element) { |
787 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
788 | $element->url, | |
789 | $this->getUser()->getId() | |
790 | ); | |
80299ed2 | 791 | |
72db15ca JB |
792 | $results[$key]['url'] = $element->url; |
793 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
80299ed2 | 794 | |
72db15ca | 795 | $tags = $element->tags; |
80299ed2 | 796 | |
72db15ca | 797 | if (false !== $entry && !(empty($tags))) { |
6bc6fb1f | 798 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
80299ed2 | 799 | |
72db15ca JB |
800 | $em = $this->getDoctrine()->getManager(); |
801 | $em->persist($entry); | |
802 | $em->flush(); | |
80299ed2 NL |
803 | } |
804 | } | |
805 | ||
72db15ca JB |
806 | return $this->sendResponse($results); |
807 | } | |
808 | ||
db0c48af | 809 | /** |
a05b6115 JB |
810 | * Retrieve value from the request. |
811 | * Used for POST & PATCH on a an entry. | |
db0c48af | 812 | * |
db0c48af | 813 | * @param Request $request |
a05b6115 JB |
814 | * |
815 | * @return array | |
db0c48af | 816 | */ |
a05b6115 | 817 | private function retrieveValueFromRequest(Request $request) |
db0c48af | 818 | { |
a05b6115 JB |
819 | return [ |
820 | 'title' => $request->request->get('title'), | |
821 | 'tags' => $request->request->get('tags', []), | |
822 | 'isArchived' => $request->request->get('archive'), | |
823 | 'isStarred' => $request->request->get('starred'), | |
824 | 'isPublic' => $request->request->get('public'), | |
825 | 'content' => $request->request->get('content'), | |
826 | 'language' => $request->request->get('language'), | |
827 | 'picture' => $request->request->get('preview_picture'), | |
828 | 'publishedAt' => $request->request->get('published_at'), | |
829 | 'authors' => $request->request->get('authors', ''), | |
e0ef1a1c | 830 | 'origin_url' => $request->request->get('origin_url', ''), |
a05b6115 | 831 | ]; |
db0c48af | 832 | } |
39ffaba3 JB |
833 | |
834 | /** | |
835 | * Return information about the entry if it exist and depending on the id or not. | |
836 | * | |
837 | * @param Entry|null $entry | |
838 | * @param bool $returnId | |
839 | * | |
840 | * @return bool|int | |
841 | */ | |
842 | private function returnExistInformation($entry, $returnId) | |
843 | { | |
844 | if ($returnId) { | |
845 | return $entry instanceof Entry ? $entry->getId() : null; | |
846 | } | |
847 | ||
331e5b02 | 848 | return $entry instanceof Entry; |
39ffaba3 | 849 | } |
900c8448 | 850 | } |