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 * Fetch content using graby and hydrate given entry with results information.
35 * In case we couldn't find content, we'll try to use Open Graph data.
37 * We can also force the content, in case of an import from the v1 for example, so the function won't
38 * fetch the content from the website but rather use information given with the $content parameter.
40 * @param Entry $entry Entry to update
41 * @param string $url Url to grab content for
42 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
46 public function updateEntry(Entry
$entry, $url, array $content = [])
48 // ensure content is a bit cleaned up
49 if (!empty($content['html'])) {
50 $extractor = $this->graby
->getExtractor();
51 $contentExtracted = $extractor->process($content['html'], $url);
53 if ($contentExtracted) {
54 $contentBlock = $extractor->getContent();
55 $contentBlock->normalize();
57 $content['html'] = trim($contentBlock->innerHTML
);
60 $content['html'] = htmLawed($content['html'], [
62 // which means: do not remove iframe elements
63 'elements' => '*+iframe',
64 'deny_attribute' => 'style',
70 // do we have to fetch the content or the provided one is ok?
71 if (empty($content) || false === $this->validateContent($content)) {
72 $fetchedContent = $this->graby
->fetchContent($url);
74 // when content is imported, we have information in $content
75 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
76 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage
) {
77 $content = $fetchedContent;
81 $title = $content['title'];
82 if (!$title && !empty($content['open_graph']['og_title'])) {
83 $title = $content['open_graph']['og_title'];
86 $html = $content['html'];
87 if (false === $html) {
88 $html = $this->fetchingErrorMessage
;
90 if (!empty($content['open_graph']['og_description'])) {
91 $html .= '<p><i>But we found a short description: </i></p>';
92 $html .= $content['open_graph']['og_description'];
96 $entry->setUrl($content['url'] ?: $url);
97 $entry->setTitle($title);
98 $entry->setContent($html);
99 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
101 if (!empty($content['date'])) {
103 $entry->setPublishedAt(new \
DateTime($content['date']));
104 } catch (\Exception
$e) {
105 $this->logger
->warn('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
109 if (!empty($content['authors'])) {
110 $entry->setPublishedBy($content['authors']);
113 if (!empty($content['all_headers'])) {
114 $entry->setHeaders($content['all_headers']);
117 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
118 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
119 $entry->setReadingTime(Utils
::getReadingTime($html));
121 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
122 if (false !== $domainName) {
123 $entry->setDomainName($domainName);
126 if (!empty($content['open_graph']['og_image'])) {
127 $entry->setPreviewPicture($content['open_graph']['og_image']);
130 // if content is an image define as a preview too
131 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
132 $entry->setPreviewPicture($content['url']);
136 $this->tagger
->tag($entry);
137 } catch (\Exception
$e) {
138 $this->logger
->error('Error while trying to automatically tag an entry.', [
140 'error_msg' => $e->getMessage(),
148 * Validate that the given content as enough value to be used
149 * instead of fetch the content from the url.
151 * @param array $content
153 * @return bool true if valid otherwise false
155 private function validateContent(array $content)
157 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);