3 namespace Wallabag\CoreBundle\Helper
;
6 use Psr\Log\LoggerInterface
;
7 use Symfony\Component\EventDispatcher\EventDispatcher
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\CoreBundle\Tools\Utils
;
10 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser
;
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
21 protected $mimeGuesser;
22 protected $fetchingErrorMessage;
23 protected $eventDispatcher;
25 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, LoggerInterface
$logger, $fetchingErrorMessage)
27 $this->graby
= $graby;
28 $this->tagger
= $tagger;
29 $this->logger
= $logger;
30 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
31 $this->fetchingErrorMessage
= $fetchingErrorMessage;
35 * Fetch content using graby and hydrate given entry with results information.
36 * In case we couldn't find content, we'll try to use Open Graph data.
38 * We can also force the content, in case of an import from the v1 for example, so the function won't
39 * fetch the content from the website but rather use information given with the $content parameter.
41 * @param Entry $entry Entry to update
42 * @param string $url Url to grab content for
43 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
47 public function updateEntry(Entry
$entry, $url, array $content = [])
49 // do we have to fetch the content or the provided one is ok?
50 if (empty($content) || false === $this->validateContent($content)) {
51 $fetchedContent = $this->graby
->fetchContent($url);
53 // when content is imported, we have information in $content
54 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
55 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage
) {
56 $content = $fetchedContent;
60 $title = $content['title'];
61 if (!$title && isset($content['open_graph']['og_title'])) {
62 $title = $content['open_graph']['og_title'];
65 $html = $content['html'];
66 if (false === $html) {
67 $html = $this->fetchingErrorMessage
;
69 if (isset($content['open_graph']['og_description'])) {
70 $html .= '<p><i>But we found a short description: </i></p>';
71 $html .= $content['open_graph']['og_description'];
75 $entry->setUrl($content['url'] ?: $url);
76 $entry->setTitle($title);
77 $entry->setContent($html);
78 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
80 if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) {
81 $entry->setPublishedAt(new \
DateTime($content['date']));
84 if (!empty($content['authors'])) {
85 $entry->setPublishedBy($content['authors']);
88 if (!empty($content['all_headers'])) {
89 $entry->setHeaders($content['all_headers']);
92 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
93 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
94 $entry->setReadingTime(Utils
::getReadingTime($html));
96 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
97 if (false !== $domainName) {
98 $entry->setDomainName($domainName);
101 if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) {
102 $entry->setPreviewPicture($content['open_graph']['og_image']);
105 // if content is an image define as a preview too
106 if (isset($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
107 $entry->setPreviewPicture($content['url']);
111 $this->tagger
->tag($entry);
112 } catch (\Exception
$e) {
113 $this->logger
->error('Error while trying to automatically tag an entry.', [
115 'error_msg' => $e->getMessage(),
123 * Validate that the given content as enough value to be used
124 * instead of fetch the content from the url.
126 * @param array $content
128 * @return bool true if valid otherwise false
130 private function validateContent(array $content)
132 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);