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
;
12 * This kind of proxy class take care of getting the content from an url
13 * and update the entry with what it found.
20 protected $mimeGuesser;
21 protected $fetchingErrorMessage;
22 protected $eventDispatcher;
24 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, LoggerInterface
$logger, $fetchingErrorMessage)
26 $this->graby
= $graby;
27 $this->tagger
= $tagger;
28 $this->logger
= $logger;
29 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
30 $this->fetchingErrorMessage
= $fetchingErrorMessage;
34 * Update entry using either fetched or provided content.
36 * @param Entry $entry Entry to update
37 * @param string $url Url of the content
38 * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url
39 * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby
41 public function updateEntry(Entry
$entry, $url, array $content = [], $disableContentUpdate = false)
43 if (!empty($content['html'])) {
44 $content['html'] = $this->graby
->cleanupHtml($content['html'], $url);
47 if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) {
49 $fetchedContent = $this->graby
->fetchContent($url);
50 } catch (\Exception
$e) {
51 $this->logger
->error('Error while trying to fetch content from URL.', [
53 'error_msg' => $e->getMessage(),
57 // when content is imported, we have information in $content
58 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
59 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage
) {
60 $content = $fetchedContent;
64 // be sure to keep the url in case of error
65 // so we'll be able to refetch it in the future
66 $content['url'] = !empty($content['url']) ? $content['url'] : $url;
68 $this->stockEntry($entry, $content);
72 * Stock entry with fetched or imported content.
73 * Will fall back to OpenGraph data if available.
75 * @param Entry $entry Entry to stock
76 * @param array $content Array with at least title and URL
78 private function stockEntry(Entry
$entry, array $content)
80 $title = $content['title'];
81 if (!$title && !empty($content['open_graph']['og_title'])) {
82 $title = $content['open_graph']['og_title'];
85 $html = $content['html'];
86 if (false === $html) {
87 $html = $this->fetchingErrorMessage
;
89 if (!empty($content['open_graph']['og_description'])) {
90 $html .= '<p><i>But we found a short description: </i></p>';
91 $html .= $content['open_graph']['og_description'];
95 $entry->setUrl($content['url']);
96 $entry->setTitle($title);
97 $entry->setContent($html);
98 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
100 if (!empty($content['date'])) {
101 $date = $content['date'];
103 // is it a timestamp?
104 if (filter_var($date, FILTER_VALIDATE_INT
) !== false) {
105 $date = '@'.$content['date'];
109 $entry->setPublishedAt(new \
DateTime($date));
110 } catch (\Exception
$e) {
111 $this->logger
->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]);
115 if (!empty($content['authors'])) {
116 $entry->setPublishedBy($content['authors']);
119 if (!empty($content['all_headers'])) {
120 $entry->setHeaders($content['all_headers']);
123 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
124 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
125 $entry->setReadingTime(Utils
::getReadingTime($html));
127 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
128 if (false !== $domainName) {
129 $entry->setDomainName($domainName);
132 if (!empty($content['open_graph']['og_image'])) {
133 $entry->setPreviewPicture($content['open_graph']['og_image']);
136 // if content is an image define as a preview too
137 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
138 $entry->setPreviewPicture($content['url']);
142 $this->tagger
->tag($entry);
143 } catch (\Exception
$e) {
144 $this->logger
->error('Error while trying to automatically tag an entry.', [
145 'entry_url' => $content['url'],
146 'error_msg' => $e->getMessage(),
152 * Validate that the given content has at least a title, an html and a url.
154 * @param array $content
156 * @return bool true if valid otherwise false
158 private function validateContent(array $content)
160 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);