]>
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; | |
72db15ca | 8 | use Symfony\Component\HttpKernel\Exception\HttpException; |
900c8448 NL |
9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpFoundation\JsonResponse; | |
11 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
12 | use Wallabag\CoreBundle\Entity\Entry; | |
13 | use Wallabag\CoreBundle\Entity\Tag; | |
5a619812 JB |
14 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
15 | use Wallabag\CoreBundle\Event\EntryDeletedEvent; | |
900c8448 NL |
16 | |
17 | class EntryRestController extends WallabagRestController | |
18 | { | |
19 | /** | |
20 | * Check if an entry exist by url. | |
21 | * | |
22 | * @ApiDoc( | |
23 | * parameters={ | |
24 | * {"name"="url", "dataType"="string", "required"=true, "format"="An url", "description"="Url to check if it exists"}, | |
25 | * {"name"="urls", "dataType"="string", "required"=false, "format"="An array of urls (?urls[]=http...&urls[]=http...)", "description"="Urls (as an array) to check if it exists"} | |
26 | * } | |
27 | * ) | |
28 | * | |
29 | * @return JsonResponse | |
30 | */ | |
31 | public function getEntriesExistsAction(Request $request) | |
32 | { | |
33 | $this->validateAuthentication(); | |
34 | ||
35 | $urls = $request->query->get('urls', []); | |
36 | ||
37 | // handle multiple urls first | |
38 | if (!empty($urls)) { | |
39 | $results = []; | |
40 | foreach ($urls as $url) { | |
41 | $res = $this->getDoctrine() | |
42 | ->getRepository('WallabagCoreBundle:Entry') | |
43 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
44 | ||
ca9a83ee | 45 | $results[$url] = $res instanceof Entry ? $res->getId() : false; |
900c8448 NL |
46 | } |
47 | ||
72db15ca | 48 | return $this->sendResponse($results); |
900c8448 NL |
49 | } |
50 | ||
51 | // let's see if it is a simple url? | |
52 | $url = $request->query->get('url', ''); | |
53 | ||
54 | if (empty($url)) { | |
55 | throw $this->createAccessDeniedException('URL is empty?, logged user id: '.$this->getUser()->getId()); | |
56 | } | |
57 | ||
58 | $res = $this->getDoctrine() | |
59 | ->getRepository('WallabagCoreBundle:Entry') | |
60 | ->findByUrlAndUserId($url, $this->getUser()->getId()); | |
61 | ||
ca9a83ee | 62 | $exists = $res instanceof Entry ? $res->getId() : false; |
900c8448 | 63 | |
72db15ca | 64 | return $this->sendResponse(['exists' => $exists]); |
900c8448 NL |
65 | } |
66 | ||
67 | /** | |
68 | * Retrieve all entries. It could be filtered by many options. | |
69 | * | |
70 | * @ApiDoc( | |
71 | * parameters={ | |
72 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by archived status."}, | |
73 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by starred status."}, | |
74 | * {"name"="sort", "dataType"="string", "required"=false, "format"="'created' or 'updated', default 'created'", "description"="sort entries by date."}, | |
75 | * {"name"="order", "dataType"="string", "required"=false, "format"="'asc' or 'desc', default 'desc'", "description"="order of sort."}, | |
76 | * {"name"="page", "dataType"="integer", "required"=false, "format"="default '1'", "description"="what page you want."}, | |
77 | * {"name"="perPage", "dataType"="integer", "required"=false, "format"="default'30'", "description"="results per page."}, | |
78 | * {"name"="tags", "dataType"="string", "required"=false, "format"="api,rest", "description"="a list of tags url encoded. Will returns entries that matches ALL tags."}, | |
79 | * {"name"="since", "dataType"="integer", "required"=false, "format"="default '0'", "description"="The timestamp since when you want entries updated."}, | |
1112e547 | 80 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0, all entries by default", "description"="filter by entries with a public link"}, |
900c8448 NL |
81 | * } |
82 | * ) | |
83 | * | |
84 | * @return JsonResponse | |
85 | */ | |
86 | public function getEntriesAction(Request $request) | |
87 | { | |
88 | $this->validateAuthentication(); | |
89 | ||
90 | $isArchived = (null === $request->query->get('archive')) ? null : (bool) $request->query->get('archive'); | |
91 | $isStarred = (null === $request->query->get('starred')) ? null : (bool) $request->query->get('starred'); | |
1112e547 | 92 | $isPublic = (null === $request->query->get('public')) ? null : (bool) $request->query->get('public'); |
900c8448 NL |
93 | $sort = $request->query->get('sort', 'created'); |
94 | $order = $request->query->get('order', 'desc'); | |
95 | $page = (int) $request->query->get('page', 1); | |
96 | $perPage = (int) $request->query->get('perPage', 30); | |
97 | $tags = $request->query->get('tags', ''); | |
98 | $since = $request->query->get('since', 0); | |
99 | ||
b60a666d | 100 | /** @var \Pagerfanta\Pagerfanta $pager */ |
1112e547 JB |
101 | $pager = $this->get('wallabag_core.entry_repository')->findEntries( |
102 | $this->getUser()->getId(), | |
103 | $isArchived, | |
104 | $isStarred, | |
105 | $isPublic, | |
106 | $sort, | |
107 | $order, | |
108 | $since, | |
109 | $tags | |
110 | ); | |
900c8448 | 111 | |
900c8448 | 112 | $pager->setMaxPerPage($perPage); |
b60a666d | 113 | $pager->setCurrentPage($page); |
900c8448 NL |
114 | |
115 | $pagerfantaFactory = new PagerfantaFactory('page', 'perPage'); | |
116 | $paginatedCollection = $pagerfantaFactory->createRepresentation( | |
117 | $pager, | |
118 | new Route( | |
119 | 'api_get_entries', | |
120 | [ | |
121 | 'archive' => $isArchived, | |
122 | 'starred' => $isStarred, | |
1112e547 | 123 | 'public' => $isPublic, |
900c8448 NL |
124 | 'sort' => $sort, |
125 | 'order' => $order, | |
126 | 'page' => $page, | |
127 | 'perPage' => $perPage, | |
128 | 'tags' => $tags, | |
129 | 'since' => $since, | |
130 | ], | |
131 | UrlGeneratorInterface::ABSOLUTE_URL | |
132 | ) | |
133 | ); | |
134 | ||
72db15ca | 135 | return $this->sendResponse($paginatedCollection); |
900c8448 NL |
136 | } |
137 | ||
138 | /** | |
139 | * Retrieve a single entry. | |
140 | * | |
141 | * @ApiDoc( | |
142 | * requirements={ | |
143 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
144 | * } | |
145 | * ) | |
146 | * | |
147 | * @return JsonResponse | |
148 | */ | |
149 | public function getEntryAction(Entry $entry) | |
150 | { | |
151 | $this->validateAuthentication(); | |
152 | $this->validateUserAccess($entry->getUser()->getId()); | |
153 | ||
72db15ca | 154 | return $this->sendResponse($entry); |
900c8448 NL |
155 | } |
156 | ||
864c1dd2 JB |
157 | /** |
158 | * Retrieve a single entry as a predefined format. | |
159 | * | |
160 | * @ApiDoc( | |
161 | * requirements={ | |
162 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
163 | * } | |
164 | * ) | |
165 | * | |
166 | * @return Response | |
167 | */ | |
168 | public function getEntryExportAction(Entry $entry, Request $request) | |
169 | { | |
170 | $this->validateAuthentication(); | |
171 | $this->validateUserAccess($entry->getUser()->getId()); | |
172 | ||
173 | return $this->get('wallabag_core.helper.entries_export') | |
174 | ->setEntries($entry) | |
175 | ->updateTitle('entry') | |
176 | ->exportAs($request->attributes->get('_format')); | |
177 | } | |
178 | ||
1eca7831 | 179 | /** |
a7abcc7b | 180 | * Handles an entries list and delete URL. |
1eca7831 NL |
181 | * |
182 | * @ApiDoc( | |
183 | * parameters={ | |
a7abcc7b | 184 | * {"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 |
185 | * } |
186 | * ) | |
187 | * | |
188 | * @return JsonResponse | |
189 | */ | |
a7abcc7b | 190 | public function deleteEntriesListAction(Request $request) |
1eca7831 NL |
191 | { |
192 | $this->validateAuthentication(); | |
193 | ||
a7abcc7b | 194 | $urls = json_decode($request->query->get('urls', [])); |
72db15ca JB |
195 | |
196 | if (empty($urls)) { | |
197 | return $this->sendResponse([]); | |
198 | } | |
199 | ||
1eca7831 NL |
200 | $results = []; |
201 | ||
202 | // handle multiple urls | |
72db15ca JB |
203 | foreach ($urls as $key => $url) { |
204 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
205 | $url, | |
206 | $this->getUser()->getId() | |
207 | ); | |
a7abcc7b | 208 | |
72db15ca | 209 | $results[$key]['url'] = $url; |
a7abcc7b | 210 | |
72db15ca JB |
211 | if (false !== $entry) { |
212 | $em = $this->getDoctrine()->getManager(); | |
213 | $em->remove($entry); | |
214 | $em->flush(); | |
1eca7831 | 215 | |
72db15ca JB |
216 | // entry deleted, dispatch event about it! |
217 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
a7abcc7b | 218 | } |
1eca7831 | 219 | |
72db15ca JB |
220 | $results[$key]['entry'] = $entry instanceof Entry ? true : false; |
221 | } | |
1eca7831 | 222 | |
72db15ca | 223 | return $this->sendResponse($results); |
a7abcc7b | 224 | } |
1eca7831 | 225 | |
a7abcc7b NL |
226 | /** |
227 | * Handles an entries list and create URL. | |
228 | * | |
229 | * @ApiDoc( | |
230 | * parameters={ | |
231 | * {"name"="urls", "dataType"="string", "required"=true, "format"="A JSON array of urls [{'url': 'http://...'}, {'url': 'http://...'}]", "description"="Urls (as an array) to create."} | |
232 | * } | |
233 | * ) | |
234 | * | |
235 | * @return JsonResponse | |
efd351c9 | 236 | * |
72db15ca | 237 | * @throws HttpException When limit is reached |
a7abcc7b NL |
238 | */ |
239 | public function postEntriesListAction(Request $request) | |
240 | { | |
241 | $this->validateAuthentication(); | |
1eca7831 | 242 | |
a7abcc7b | 243 | $urls = json_decode($request->query->get('urls', [])); |
1eca7831 | 244 | |
efd351c9 NL |
245 | $limit = $this->container->getParameter('wallabag_core.api_limit_mass_actions'); |
246 | ||
247 | if (count($urls) > $limit) { | |
72db15ca | 248 | throw new HttpException(400, 'API limit reached'); |
efd351c9 NL |
249 | } |
250 | ||
d5c2cc54 JB |
251 | $results = []; |
252 | if (empty($urls)) { | |
253 | return $this->sendResponse($results); | |
254 | } | |
255 | ||
a7abcc7b | 256 | // handle multiple urls |
d5c2cc54 JB |
257 | foreach ($urls as $key => $url) { |
258 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
259 | $url, | |
260 | $this->getUser()->getId() | |
261 | ); | |
1eca7831 | 262 | |
d5c2cc54 | 263 | $results[$key]['url'] = $url; |
1eca7831 | 264 | |
d5c2cc54 JB |
265 | if (false === $entry) { |
266 | $entry = new Entry($this->getUser()); | |
a7abcc7b | 267 | |
d5c2cc54 JB |
268 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $url); |
269 | } | |
a7abcc7b | 270 | |
d5c2cc54 JB |
271 | $em = $this->getDoctrine()->getManager(); |
272 | $em->persist($entry); | |
273 | $em->flush(); | |
a7abcc7b | 274 | |
d5c2cc54 JB |
275 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; |
276 | ||
277 | // entry saved, dispatch event about it! | |
278 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
1eca7831 NL |
279 | } |
280 | ||
72db15ca | 281 | return $this->sendResponse($results); |
1eca7831 NL |
282 | } |
283 | ||
900c8448 NL |
284 | /** |
285 | * Create an entry. | |
286 | * | |
9e349f08 JB |
287 | * 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**. |
288 | * Otherwise, content will be fetched as normal from the url and values will be overwritten. | |
289 | * | |
900c8448 NL |
290 | * @ApiDoc( |
291 | * parameters={ | |
292 | * {"name"="url", "dataType"="string", "required"=true, "format"="http://www.test.com/article.html", "description"="Url for the entry."}, | |
293 | * {"name"="title", "dataType"="string", "required"=false, "description"="Optional, we'll get the title from the page."}, | |
294 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
295 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already starred"}, | |
296 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="entry already archived"}, | |
e668a812 JB |
297 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, |
298 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
299 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
f0378b4d | 300 | * {"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 | 301 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, |
1112e547 | 302 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, |
900c8448 NL |
303 | * } |
304 | * ) | |
305 | * | |
306 | * @return JsonResponse | |
307 | */ | |
308 | public function postEntriesAction(Request $request) | |
309 | { | |
310 | $this->validateAuthentication(); | |
311 | ||
312 | $url = $request->request->get('url'); | |
900c8448 | 313 | |
db0c48af JB |
314 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( |
315 | $url, | |
316 | $this->getUser()->getId() | |
317 | ); | |
900c8448 NL |
318 | |
319 | if (false === $entry) { | |
08f29ae7 | 320 | $entry = new Entry($this->getUser()); |
e668a812 | 321 | $entry->setUrl($url); |
900c8448 NL |
322 | } |
323 | ||
db0c48af | 324 | $this->upsertEntry($entry, $request); |
5a619812 | 325 | |
72db15ca | 326 | return $this->sendResponse($entry); |
900c8448 NL |
327 | } |
328 | ||
329 | /** | |
330 | * Change several properties of an entry. | |
331 | * | |
332 | * @ApiDoc( | |
333 | * requirements={ | |
334 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
335 | * }, | |
336 | * parameters={ | |
337 | * {"name"="title", "dataType"="string", "required"=false}, | |
338 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
339 | * {"name"="archive", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="archived the entry."}, | |
340 | * {"name"="starred", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="starred the entry."}, | |
645291e8 JB |
341 | * {"name"="content", "dataType"="string", "required"=false, "description"="Content of the entry"}, |
342 | * {"name"="language", "dataType"="string", "required"=false, "description"="Language of the entry"}, | |
343 | * {"name"="preview_picture", "dataType"="string", "required"=false, "description"="Preview picture of the entry"}, | |
344 | * {"name"="published_at", "dataType"="datetime|integer", "format"="YYYY-MM-DDTHH:II:SS+TZ or a timestamp", "required"=false, "description"="Published date of the entry"}, | |
345 | * {"name"="authors", "dataType"="string", "format"="Name Firstname,author2,author3", "required"=false, "description"="Authors of the entry"}, | |
1112e547 | 346 | * {"name"="public", "dataType"="integer", "required"=false, "format"="1 or 0", "description"="will generate a public link for the entry"}, |
900c8448 NL |
347 | * } |
348 | * ) | |
349 | * | |
350 | * @return JsonResponse | |
351 | */ | |
352 | public function patchEntriesAction(Entry $entry, Request $request) | |
353 | { | |
354 | $this->validateAuthentication(); | |
355 | $this->validateUserAccess($entry->getUser()->getId()); | |
356 | ||
db0c48af | 357 | $this->upsertEntry($entry, $request, true); |
900c8448 | 358 | |
72db15ca | 359 | return $this->sendResponse($entry); |
900c8448 NL |
360 | } |
361 | ||
0a6f4568 JB |
362 | /** |
363 | * Reload an entry. | |
5cd0857e | 364 | * 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 |
365 | * |
366 | * @ApiDoc( | |
367 | * requirements={ | |
368 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
369 | * } | |
370 | * ) | |
371 | * | |
372 | * @return JsonResponse | |
373 | */ | |
374 | public function patchEntriesReloadAction(Entry $entry) | |
375 | { | |
376 | $this->validateAuthentication(); | |
377 | $this->validateUserAccess($entry->getUser()->getId()); | |
378 | ||
0a6f4568 | 379 | try { |
7aba665e | 380 | $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl()); |
0a6f4568 JB |
381 | } catch (\Exception $e) { |
382 | $this->get('logger')->error('Error while saving an entry', [ | |
383 | 'exception' => $e, | |
384 | 'entry' => $entry, | |
385 | ]); | |
386 | ||
5cd0857e | 387 | return new JsonResponse([], 304); |
0a6f4568 JB |
388 | } |
389 | ||
390 | // if refreshing entry failed, don't save it | |
391 | if ($this->getParameter('wallabag_core.fetching_error_message') === $entry->getContent()) { | |
5cd0857e | 392 | return new JsonResponse([], 304); |
0a6f4568 JB |
393 | } |
394 | ||
395 | $em = $this->getDoctrine()->getManager(); | |
396 | $em->persist($entry); | |
397 | $em->flush(); | |
398 | ||
399 | // entry saved, dispatch event about it! | |
400 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
401 | ||
72db15ca | 402 | return $this->sendResponse($entry); |
0a6f4568 JB |
403 | } |
404 | ||
900c8448 NL |
405 | /** |
406 | * Delete **permanently** an entry. | |
407 | * | |
408 | * @ApiDoc( | |
409 | * requirements={ | |
410 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
411 | * } | |
412 | * ) | |
413 | * | |
414 | * @return JsonResponse | |
415 | */ | |
416 | public function deleteEntriesAction(Entry $entry) | |
417 | { | |
418 | $this->validateAuthentication(); | |
419 | $this->validateUserAccess($entry->getUser()->getId()); | |
420 | ||
421 | $em = $this->getDoctrine()->getManager(); | |
422 | $em->remove($entry); | |
423 | $em->flush(); | |
424 | ||
5a619812 JB |
425 | // entry deleted, dispatch event about it! |
426 | $this->get('event_dispatcher')->dispatch(EntryDeletedEvent::NAME, new EntryDeletedEvent($entry)); | |
427 | ||
72db15ca | 428 | return $this->sendResponse($entry); |
900c8448 NL |
429 | } |
430 | ||
431 | /** | |
432 | * Retrieve all tags for an entry. | |
433 | * | |
434 | * @ApiDoc( | |
435 | * requirements={ | |
436 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
437 | * } | |
438 | * ) | |
439 | * | |
440 | * @return JsonResponse | |
441 | */ | |
442 | public function getEntriesTagsAction(Entry $entry) | |
443 | { | |
444 | $this->validateAuthentication(); | |
445 | $this->validateUserAccess($entry->getUser()->getId()); | |
446 | ||
72db15ca | 447 | return $this->sendResponse($entry->getTags()); |
900c8448 NL |
448 | } |
449 | ||
450 | /** | |
451 | * Add one or more tags to an entry. | |
452 | * | |
453 | * @ApiDoc( | |
454 | * requirements={ | |
455 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
456 | * }, | |
457 | * parameters={ | |
458 | * {"name"="tags", "dataType"="string", "required"=false, "format"="tag1,tag2,tag3", "description"="a comma-separated list of tags."}, | |
459 | * } | |
460 | * ) | |
461 | * | |
462 | * @return JsonResponse | |
463 | */ | |
464 | public function postEntriesTagsAction(Request $request, Entry $entry) | |
465 | { | |
466 | $this->validateAuthentication(); | |
467 | $this->validateUserAccess($entry->getUser()->getId()); | |
468 | ||
469 | $tags = $request->request->get('tags', ''); | |
470 | if (!empty($tags)) { | |
6bc6fb1f | 471 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
900c8448 NL |
472 | } |
473 | ||
474 | $em = $this->getDoctrine()->getManager(); | |
475 | $em->persist($entry); | |
476 | $em->flush(); | |
477 | ||
72db15ca | 478 | return $this->sendResponse($entry); |
900c8448 NL |
479 | } |
480 | ||
481 | /** | |
482 | * Permanently remove one tag for an entry. | |
483 | * | |
484 | * @ApiDoc( | |
485 | * requirements={ | |
486 | * {"name"="tag", "dataType"="integer", "requirement"="\w+", "description"="The tag ID"}, | |
487 | * {"name"="entry", "dataType"="integer", "requirement"="\w+", "description"="The entry ID"} | |
488 | * } | |
489 | * ) | |
490 | * | |
491 | * @return JsonResponse | |
492 | */ | |
493 | public function deleteEntriesTagsAction(Entry $entry, Tag $tag) | |
494 | { | |
495 | $this->validateAuthentication(); | |
496 | $this->validateUserAccess($entry->getUser()->getId()); | |
497 | ||
498 | $entry->removeTag($tag); | |
499 | $em = $this->getDoctrine()->getManager(); | |
500 | $em->persist($entry); | |
501 | $em->flush(); | |
502 | ||
72db15ca | 503 | return $this->sendResponse($entry); |
900c8448 | 504 | } |
d1fc5902 NL |
505 | |
506 | /** | |
80299ed2 | 507 | * Handles an entries list delete tags from them. |
d1fc5902 NL |
508 | * |
509 | * @ApiDoc( | |
510 | * parameters={ | |
80299ed2 | 511 | * {"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 |
512 | * } |
513 | * ) | |
514 | * | |
515 | * @return JsonResponse | |
516 | */ | |
80299ed2 | 517 | public function deleteEntriesTagsListAction(Request $request) |
d1fc5902 NL |
518 | { |
519 | $this->validateAuthentication(); | |
520 | ||
521 | $list = json_decode($request->query->get('list', [])); | |
72db15ca JB |
522 | |
523 | if (empty($list)) { | |
524 | return $this->sendResponse([]); | |
525 | } | |
d1fc5902 NL |
526 | |
527 | // handle multiple urls | |
72db15ca | 528 | $results = []; |
d1fc5902 | 529 | |
72db15ca JB |
530 | foreach ($list as $key => $element) { |
531 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
532 | $element->url, | |
533 | $this->getUser()->getId() | |
534 | ); | |
d1fc5902 | 535 | |
72db15ca JB |
536 | $results[$key]['url'] = $element->url; |
537 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
d1fc5902 | 538 | |
72db15ca | 539 | $tags = $element->tags; |
80299ed2 | 540 | |
72db15ca JB |
541 | if (false !== $entry && !(empty($tags))) { |
542 | $tags = explode(',', $tags); | |
543 | foreach ($tags as $label) { | |
544 | $label = trim($label); | |
80299ed2 | 545 | |
72db15ca JB |
546 | $tag = $this->getDoctrine() |
547 | ->getRepository('WallabagCoreBundle:Tag') | |
548 | ->findOneByLabel($label); | |
d1fc5902 | 549 | |
72db15ca JB |
550 | if (false !== $tag) { |
551 | $entry->removeTag($tag); | |
552 | } | |
d1fc5902 | 553 | } |
72db15ca JB |
554 | |
555 | $em = $this->getDoctrine()->getManager(); | |
556 | $em->persist($entry); | |
557 | $em->flush(); | |
d1fc5902 NL |
558 | } |
559 | } | |
560 | ||
72db15ca | 561 | return $this->sendResponse($results); |
d1fc5902 | 562 | } |
80299ed2 NL |
563 | |
564 | /** | |
565 | * Handles an entries list and add tags to them. | |
566 | * | |
567 | * @ApiDoc( | |
568 | * parameters={ | |
569 | * {"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."} | |
570 | * } | |
571 | * ) | |
572 | * | |
573 | * @return JsonResponse | |
574 | */ | |
575 | public function postEntriesTagsListAction(Request $request) | |
576 | { | |
577 | $this->validateAuthentication(); | |
578 | ||
579 | $list = json_decode($request->query->get('list', [])); | |
72db15ca JB |
580 | |
581 | if (empty($list)) { | |
582 | return $this->sendResponse([]); | |
583 | } | |
584 | ||
80299ed2 NL |
585 | $results = []; |
586 | ||
587 | // handle multiple urls | |
72db15ca JB |
588 | foreach ($list as $key => $element) { |
589 | $entry = $this->get('wallabag_core.entry_repository')->findByUrlAndUserId( | |
590 | $element->url, | |
591 | $this->getUser()->getId() | |
592 | ); | |
80299ed2 | 593 | |
72db15ca JB |
594 | $results[$key]['url'] = $element->url; |
595 | $results[$key]['entry'] = $entry instanceof Entry ? $entry->getId() : false; | |
80299ed2 | 596 | |
72db15ca | 597 | $tags = $element->tags; |
80299ed2 | 598 | |
72db15ca | 599 | if (false !== $entry && !(empty($tags))) { |
6bc6fb1f | 600 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); |
80299ed2 | 601 | |
72db15ca JB |
602 | $em = $this->getDoctrine()->getManager(); |
603 | $em->persist($entry); | |
604 | $em->flush(); | |
80299ed2 NL |
605 | } |
606 | } | |
607 | ||
72db15ca JB |
608 | return $this->sendResponse($results); |
609 | } | |
610 | ||
611 | /** | |
612 | * Shortcut to send data serialized in json. | |
613 | * | |
614 | * @param mixed $data | |
615 | * | |
616 | * @return JsonResponse | |
617 | */ | |
618 | private function sendResponse($data) | |
619 | { | |
620 | $json = $this->get('serializer')->serialize($data, 'json'); | |
80299ed2 NL |
621 | |
622 | return (new JsonResponse())->setJson($json); | |
623 | } | |
db0c48af JB |
624 | |
625 | /** | |
626 | * Update or Insert a new entry. | |
627 | * | |
628 | * @param Entry $entry | |
629 | * @param Request $request | |
630 | * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting) | |
631 | */ | |
632 | private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false) | |
633 | { | |
634 | $title = $request->request->get('title'); | |
635 | $tags = $request->request->get('tags', []); | |
636 | $isArchived = $request->request->get('archive'); | |
637 | $isStarred = $request->request->get('starred'); | |
1112e547 | 638 | $isPublic = $request->request->get('public'); |
db0c48af JB |
639 | $content = $request->request->get('content'); |
640 | $language = $request->request->get('language'); | |
641 | $picture = $request->request->get('preview_picture'); | |
642 | $publishedAt = $request->request->get('published_at'); | |
643 | $authors = $request->request->get('authors', ''); | |
644 | ||
645 | try { | |
646 | $this->get('wallabag_core.content_proxy')->updateEntry( | |
647 | $entry, | |
648 | $entry->getUrl(), | |
649 | [ | |
650 | 'title' => !empty($title) ? $title : $entry->getTitle(), | |
651 | 'html' => !empty($content) ? $content : $entry->getContent(), | |
652 | 'url' => $entry->getUrl(), | |
653 | 'language' => !empty($language) ? $language : $entry->getLanguage(), | |
654 | 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(), | |
655 | // faking the open graph preview picture | |
656 | 'open_graph' => [ | |
657 | 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(), | |
658 | ], | |
659 | 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(), | |
660 | ], | |
661 | $disableContentUpdate | |
662 | ); | |
663 | } catch (\Exception $e) { | |
664 | $this->get('logger')->error('Error while saving an entry', [ | |
665 | 'exception' => $e, | |
666 | 'entry' => $entry, | |
667 | ]); | |
668 | } | |
669 | ||
670 | if (!is_null($isArchived)) { | |
671 | $entry->setArchived((bool) $isArchived); | |
672 | } | |
673 | ||
674 | if (!is_null($isStarred)) { | |
675 | $entry->setStarred((bool) $isStarred); | |
676 | } | |
677 | ||
678 | if (!empty($tags)) { | |
679 | $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags); | |
680 | } | |
681 | ||
1112e547 | 682 | if (!is_null($isPublic)) { |
1112e547 JB |
683 | if (true === (bool) $isPublic && null === $entry->getUid()) { |
684 | $entry->generateUid(); | |
d9da186f | 685 | } elseif (false === (bool) $isPublic) { |
a9c6577f | 686 | $entry->cleanUid(); |
1112e547 JB |
687 | } |
688 | } | |
689 | ||
db0c48af JB |
690 | $em = $this->getDoctrine()->getManager(); |
691 | $em->persist($entry); | |
692 | $em->flush(); | |
693 | ||
694 | // entry saved, dispatch event about it! | |
695 | $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
696 | } | |
900c8448 | 697 | } |