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 $title = $content['title'];
79 if (!$title && !empty($content['open_graph']['og_title'])) {
80 $title = $content['open_graph']['og_title'];
83 $html = $content['html'];
84 if (false === $html) {
85 $html = $this->fetchingErrorMessage
;
87 if (!empty($content['open_graph']['og_description'])) {
88 $html .= '<p><i>But we found a short description: </i></p>';
89 $html .= $content['open_graph']['og_description'];
93 $entry->setUrl($content['url']);
94 $entry->setTitle($title);
95 $entry->setContent($html);
96 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
98 if (!empty($content['date'])) {
99 $date = $content['date'];
101 // is it a timestamp?
102 if (filter_var($date, FILTER_VALIDATE_INT
) !== false) {
103 $date = '@' . $content['date'];
107 $entry->setPublishedAt(new \
DateTime($date));
108 } catch (\Exception
$e) {
109 $this->logger
->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]);
113 if (!empty($content['authors']) && is_array($content['authors'])) {
114 $entry->setPublishedBy($content['authors']);
117 if (!empty($content['all_headers'])) {
118 $entry->setHeaders($content['all_headers']);
121 $this->validateAndSetLanguage(
123 isset($content['language']) ? $content['language'] : null
126 $this->validateAndSetPreviewPicture(
128 isset($content['open_graph']['og_image']) ? $content['open_graph']['og_image'] : null
131 // if content is an image, define it as a preview too
132 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
133 $this->validateAndSetPreviewPicture(
139 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
140 $entry->setReadingTime(Utils
::getReadingTime($html));
142 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
143 if (false !== $domainName) {
144 $entry->setDomainName($domainName);
148 $this->tagger
->tag($entry);
149 } catch (\Exception
$e) {
150 $this->logger
->error('Error while trying to automatically tag an entry.', [
151 'entry_url' => $content['url'],
152 'error_msg' => $e->getMessage(),
158 * Validate that the given content has at least a title, an html and a url.
160 * @param array $content
162 * @return bool true if valid otherwise false
164 private function validateContent(array $content)
166 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
170 * Use a Symfony validator to ensure the language is well formatted.
172 * @param Entry $entry
173 * @param string $value Language to validate
175 private function validateAndSetLanguage($entry, $value)
177 // some lang are defined as fr-FR, es-ES.
178 // replacing - by _ might increase language support
179 $value = str_replace('-', '_', $value);
181 $errors = $this->validator
->validate(
183 (new LocaleConstraint())
186 if (0 === count($errors)) {
187 $entry->setLanguage($value);
192 $this->logger
->warning('Language validation failed. ' . (string) $errors);
196 * Use a Symfony validator to ensure the preview picture is a real url.
198 * @param Entry $entry
199 * @param string $value URL to validate
201 private function validateAndSetPreviewPicture($entry, $value)
203 $errors = $this->validator
->validate(
205 (new UrlConstraint())
208 if (0 === count($errors)) {
209 $entry->setPreviewPicture($value);
214 $this->logger
->warning('PreviewPicture validation failed. ' . (string) $errors);