3 namespace Wallabag\CoreBundle\Helper
;
6 use Psr\Log\LoggerInterface
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Tools\Utils
;
9 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser
;
10 use Symfony\Component\Config\Definition\Exception\Exception
;
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
21 protected $mimeGuesser;
22 protected $fetchingErrorMessage;
23 protected $eventDispatcher;
25 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, LoggerInterface
$logger, $fetchingErrorMessage)
27 $this->graby
= $graby;
28 $this->tagger
= $tagger;
29 $this->logger
= $logger;
30 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
31 $this->fetchingErrorMessage
= $fetchingErrorMessage;
35 * Update existing entry by fetching from URL using Graby.
37 * @param Entry $entry Entry to update
38 * @param string $url Url to grab content for
40 public function updateEntry(Entry
$entry, $url)
42 $content = $this->graby
->fetchContent($url);
44 // be sure to keep the url in case of error
45 // so we'll be able to refetch it in the future
46 $content['url'] = $content['url'] ?: $url;
48 $this->stockEntry($entry, $content);
52 * Import entry using either fetched or provided content.
54 * @param Entry $entry Entry to update
55 * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url
56 * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby
58 public function importEntry(Entry
$entry, array $content, $disableContentUpdate = false)
61 $this->validateContent($content);
62 } catch (\Exception
$e) {
63 // validation failed but do we want to disable updating content?
64 if (true === $disableContentUpdate) {
69 if (false === $disableContentUpdate) {
71 $fetchedContent = $this->graby
->fetchContent($content['url']);
72 } catch (\Exception
$e) {
73 $this->logger
->error('Error while trying to fetch content from URL.', [
74 'entry_url' => $content['url'],
75 'error_msg' => $e->getMessage(),
79 // when content is imported, we have information in $content
80 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
81 if ($fetchedContent['html'] !== $this->fetchingErrorMessage
) {
82 $content = $fetchedContent;
86 $this->stockEntry($entry, $content);
90 * Stock entry with fetched or imported content.
91 * Will fall back to OpenGraph data if available.
93 * @param Entry $entry Entry to stock
94 * @param array $content Array with at least title and URL
96 private function stockEntry(Entry
$entry, array $content)
98 $title = $content['title'];
99 if (!$title && !empty($content['open_graph']['og_title'])) {
100 $title = $content['open_graph']['og_title'];
103 $html = $content['html'];
104 if (false === $html) {
105 $html = $this->fetchingErrorMessage
;
107 if (!empty($content['open_graph']['og_description'])) {
108 $html .= '<p><i>But we found a short description: </i></p>';
109 $html .= $content['open_graph']['og_description'];
113 $entry->setUrl($content['url']);
114 $entry->setTitle($title);
115 $entry->setContent($html);
116 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
118 if (!empty($content['date'])) {
119 $date = $content['date'];
121 // is it a timestamp?
122 if (filter_var($date, FILTER_VALIDATE_INT
) !== false) {
123 $date = '@'.$content['date'];
127 $entry->setPublishedAt(new \
DateTime($date));
128 } catch (\Exception
$e) {
129 $this->logger
->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
133 if (!empty($content['authors'])) {
134 $entry->setPublishedBy($content['authors']);
137 if (!empty($content['all_headers'])) {
138 $entry->setHeaders($content['all_headers']);
141 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
142 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
143 $entry->setReadingTime(Utils
::getReadingTime($html));
145 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
146 if (false !== $domainName) {
147 $entry->setDomainName($domainName);
150 if (!empty($content['open_graph']['og_image'])) {
151 $entry->setPreviewPicture($content['open_graph']['og_image']);
154 // if content is an image define as a preview too
155 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
156 $entry->setPreviewPicture($content['url']);
160 $this->tagger
->tag($entry);
161 } catch (\Exception
$e) {
162 $this->logger
->error('Error while trying to automatically tag an entry.', [
163 'entry_url' => $content['url'],
164 'error_msg' => $e->getMessage(),
170 * Validate that the given content has at least a title, an html and a url.
172 * @param array $content
174 private function validateContent(array $content)
176 if (empty($content['title'])) {
177 throw new Exception('Missing title from imported entry!');
180 if (empty($content['url'])) {
181 throw new Exception('Missing URL from imported entry!');
184 if (empty($content['html'])) {
185 throw new Exception('Missing html from imported entry!');