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 to skip the fetchContent from the url
44 public function updateEntry(Entry
$entry, $url, array $content = [])
46 // ensure content is a bit cleaned up
47 if (!empty($content['html'])) {
48 $content['html'] = $this->graby
->cleanupHtml($content['html'], $url);
51 // do we have to fetch the content or the provided one is ok?
52 if (empty($content) || false === $this->validateContent($content)) {
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 $title = $content['title'];
63 if (!$title && !empty($content['open_graph']['og_title'])) {
64 $title = $content['open_graph']['og_title'];
67 $html = $content['html'];
68 if (false === $html) {
69 $html = $this->fetchingErrorMessage
;
71 if (!empty($content['open_graph']['og_description'])) {
72 $html .= '<p><i>But we found a short description: </i></p>';
73 $html .= $content['open_graph']['og_description'];
77 $entry->setUrl($content['url'] ?: $url);
78 $entry->setTitle($title);
79 $entry->setContent($html);
80 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
82 if (!empty($content['date'])) {
83 $date = $content['date'];
86 if (filter_var($date, FILTER_VALIDATE_INT
) !== false) {
87 $date = '@'.$content['date'];
91 $entry->setPublishedAt(new \
DateTime($date));
92 } catch (\Exception
$e) {
93 $this->logger
->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
97 if (!empty($content['authors'])) {
98 $entry->setPublishedBy($content['authors']);
101 if (!empty($content['all_headers'])) {
102 $entry->setHeaders($content['all_headers']);
105 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
106 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
107 $entry->setReadingTime(Utils
::getReadingTime($html));
109 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
110 if (false !== $domainName) {
111 $entry->setDomainName($domainName);
114 if (!empty($content['open_graph']['og_image'])) {
115 $entry->setPreviewPicture($content['open_graph']['og_image']);
118 // if content is an image define as a preview too
119 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
120 $entry->setPreviewPicture($content['url']);
124 $this->tagger
->tag($entry);
125 } catch (\Exception
$e) {
126 $this->logger
->error('Error while trying to automatically tag an entry.', [
128 'error_msg' => $e->getMessage(),
134 * Validate that the given content as enough value to be used
135 * instead of fetch the content from the url.
137 * @param array $content
139 * @return bool true if valid otherwise false
141 private function validateContent(array $content)
143 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);