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