+
+ /**
+ * Update or Insert a new entry.
+ *
+ * @param Entry $entry
+ * @param Request $request
+ * @param bool $disableContentUpdate If we don't want the content to be update by fetching the url (used when patching instead of posting)
+ */
+ private function upsertEntry(Entry $entry, Request $request, $disableContentUpdate = false)
+ {
+ $title = $request->request->get('title');
+ $tags = $request->request->get('tags', []);
+ $isArchived = $request->request->get('archive');
+ $isStarred = $request->request->get('starred');
+ $content = $request->request->get('content');
+ $language = $request->request->get('language');
+ $picture = $request->request->get('preview_picture');
+ $publishedAt = $request->request->get('published_at');
+ $authors = $request->request->get('authors', '');
+
+ try {
+ $this->get('wallabag_core.content_proxy')->updateEntry(
+ $entry,
+ $entry->getUrl(),
+ [
+ 'title' => !empty($title) ? $title : $entry->getTitle(),
+ 'html' => !empty($content) ? $content : $entry->getContent(),
+ 'url' => $entry->getUrl(),
+ 'language' => !empty($language) ? $language : $entry->getLanguage(),
+ 'date' => !empty($publishedAt) ? $publishedAt : $entry->getPublishedAt(),
+ // faking the open graph preview picture
+ 'open_graph' => [
+ 'og_image' => !empty($picture) ? $picture : $entry->getPreviewPicture(),
+ ],
+ 'authors' => is_string($authors) ? explode(',', $authors) : $entry->getPublishedBy(),
+ ],
+ $disableContentUpdate
+ );
+ } catch (\Exception $e) {
+ $this->get('logger')->error('Error while saving an entry', [
+ 'exception' => $e,
+ 'entry' => $entry,
+ ]);
+ }
+
+ if (!is_null($isArchived)) {
+ $entry->setArchived((bool) $isArchived);
+ }
+
+ if (!is_null($isStarred)) {
+ $entry->setStarred((bool) $isStarred);
+ }
+
+ if (!empty($tags)) {
+ $this->get('wallabag_core.tags_assigner')->assignTagsToEntry($entry, $tags);
+ }
+
+ $em = $this->getDoctrine()->getManager();
+ $em->persist($entry);
+ $em->flush();
+
+ // entry saved, dispatch event about it!
+ $this->get('event_dispatcher')->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
+ }