3 namespace Wallabag\CoreBundle\Helper
;
6 use Psr\Log\LoggerInterface
;
7 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser
;
8 use Symfony\Component\Validator\Constraints\Locale
as LocaleConstraint
;
9 use Symfony\Component\Validator\Constraints\Url
as UrlConstraint
;
10 use Symfony\Component\Validator\Validator\ValidatorInterface
;
11 use Wallabag\CoreBundle\Entity\Entry
;
12 use Wallabag\CoreBundle\Tools\Utils
;
15 * This kind of proxy class take care of getting the content from an url
16 * and update the entry with what it found.
24 protected $mimeGuesser;
25 protected $fetchingErrorMessage;
26 protected $eventDispatcher;
28 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, ValidatorInterface
$validator, LoggerInterface
$logger, $fetchingErrorMessage)
30 $this->graby
= $graby;
31 $this->tagger
= $tagger;
32 $this->validator
= $validator;
33 $this->logger
= $logger;
34 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
35 $this->fetchingErrorMessage
= $fetchingErrorMessage;
39 * Update entry using either fetched or provided content.
41 * @param Entry $entry Entry to update
42 * @param string $url Url of the content
43 * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url
44 * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby
46 public function updateEntry(Entry
$entry, $url, array $content = [], $disableContentUpdate = false)
48 if (!empty($content['html'])) {
49 $content['html'] = $this->graby
->cleanupHtml($content['html'], $url);
52 if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) {
53 $fetchedContent = $this->graby
->fetchContent($url);
55 // when content is imported, we have information in $content
56 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
57 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage
) {
58 $content = $fetchedContent;
62 // be sure to keep the url in case of error
63 // so we'll be able to refetch it in the future
64 $content['url'] = !empty($content['url']) ? $content['url'] : $url;
66 $this->stockEntry($entry, $content);
70 * Stock entry with fetched or imported content.
71 * Will fall back to OpenGraph data if available.
73 * @param Entry $entry Entry to stock
74 * @param array $content Array with at least title, url & html
76 private function stockEntry(Entry
$entry, array $content)
78 $entry->setUrl($content['url']);
80 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
81 if (false !== $domainName) {
82 $entry->setDomainName($domainName);
85 if (!empty($content['title'])) {
86 $entry->setTitle($content['title']);
87 } elseif (!empty($content['open_graph']['og_title'])) {
88 $entry->setTitle($content['open_graph']['og_title']);
91 $html = $content['html'];
92 if (false === $html) {
93 $html = $this->fetchingErrorMessage
;
95 if (!empty($content['open_graph']['og_description'])) {
96 $html .= '<p><i>But we found a short description: </i></p>';
97 $html .= $content['open_graph']['og_description'];
101 $entry->setContent($html);
102 $entry->setReadingTime(Utils
::getReadingTime($html));
104 if (!empty($content['status'])) {
105 $entry->setHttpStatus($content['status']);
108 if (!empty($content['authors']) && is_array($content['authors'])) {
109 $entry->setPublishedBy($content['authors']);
112 if (!empty($content['all_headers'])) {
113 $entry->setHeaders($content['all_headers']);
116 if (!empty($content['date'])) {
117 $this->updatePublishedAt($entry, $content['date']);
120 if (!empty($content['language'])) {
121 $this->updateLanguage($entry, $content['language']);
124 if (!empty($content['open_graph']['og_image'])) {
125 $this->updatePreviewPicture($entry, $content['open_graph']['og_image']);
128 // if content is an image, define it as a preview too
129 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
130 $this->updatePreviewPicture($entry, $content['url']);
133 if (!empty($content['content_type'])) {
134 $entry->setMimetype($content['content_type']);
138 $this->tagger
->tag($entry);
139 } catch (\Exception
$e) {
140 $this->logger
->error('Error while trying to automatically tag an entry.', [
141 'entry_url' => $content['url'],
142 'error_msg' => $e->getMessage(),
148 * Validate that the given content has at least a title, an html and a url.
150 * @param array $content
152 * @return bool true if valid otherwise false
154 private function validateContent(array $content)
156 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
160 * Use a Symfony validator to ensure the language is well formatted.
162 * @param Entry $entry
163 * @param string $value Language to validate and save
165 public function updateLanguage(Entry
$entry, $value)
167 // some lang are defined as fr-FR, es-ES.
168 // replacing - by _ might increase language support
169 $value = str_replace('-', '_', $value);
171 $errors = $this->validator
->validate(
173 (new LocaleConstraint())
176 if (0 === count($errors)) {
177 $entry->setLanguage($value);
182 $this->logger
->warning('Language validation failed. ' . (string) $errors);
186 * Use a Symfony validator to ensure the preview picture is a real url.
188 * @param Entry $entry
189 * @param string $value URL to validate and save
191 public function updatePreviewPicture(Entry
$entry, $value)
193 $errors = $this->validator
->validate(
195 (new UrlConstraint())
198 if (0 === count($errors)) {
199 $entry->setPreviewPicture($value);
204 $this->logger
->warning('PreviewPicture validation failed. ' . (string) $errors);
210 * @param Entry $entry
211 * @param string $value Date to validate and save
213 public function updatePublishedAt(Entry
$entry, $value)
217 // is it a timestamp?
218 if (filter_var($date, FILTER_VALIDATE_INT
) !== false) {
223 $entry->setPublishedAt(new \
DateTime($date));
224 } catch (\Exception
$e) {
225 $this->logger
->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]);