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
;
10 use Symfony\Component\Config\Definition\Exception\Exception
;
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 * Update existing entry by fetching from URL using Graby.
37 * @param Entry $entry Entry to update
38 * @param string $url Url to grab content for
40 public function updateEntry(Entry
$entry, $url)
42 $content = $this->graby
->fetchContent($url);
44 $this->stockEntry($entry, $content);
48 * Import entry using either fetched or provided content.
50 * @param Entry $entry Entry to update
51 * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url
52 * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby
54 public function importEntry(Entry
$entry, array $content, $disableContentUpdate = false)
56 $this->validateContent($content);
58 if (false === $disableContentUpdate) {
60 $fetchedContent = $this->graby
->fetchContent($content['url']);
61 } catch (\Exception
$e) {
62 $this->logger
->error('Error while trying to fetch content from URL.', [
63 'entry_url' => $content['url'],
64 'error_msg' => $e->getMessage(),
68 // when content is imported, we have information in $content
69 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
70 if ($fetchedContent['html'] !== $this->fetchingErrorMessage
) {
71 $content = $fetchedContent;
75 $this->stockEntry($entry, $content);
79 * Stock entry with fetched or imported content.
80 * Will fall back to OpenGraph data if available.
82 * @param Entry $entry Entry to stock
83 * @param array $content Array with at least title and URL
85 private function stockEntry(Entry
$entry, array $content)
87 $title = $content['title'];
88 if (!$title && !empty($content['open_graph']['og_title'])) {
89 $title = $content['open_graph']['og_title'];
92 $html = $content['html'];
93 if (false === $html) {
94 $html = $this->fetchingErrorMessage
;
96 if (!empty($content['open_graph']['og_description'])) {
97 $html .= '<p><i>But we found a short description: </i></p>';
98 $html .= $content['open_graph']['og_description'];
102 $entry->setUrl($content['url']);
103 $entry->setTitle($title);
104 $entry->setContent($html);
105 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
107 if (!empty($content['date'])) {
108 $date = $content['date'];
110 // is it a timestamp?
111 if (filter_var($date, FILTER_VALIDATE_INT
) !== false) {
112 $date = '@'.$content['date'];
116 $entry->setPublishedAt(new \
DateTime($date));
117 } catch (\Exception
$e) {
118 $this->logger
->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
122 if (!empty($content['authors'])) {
123 $entry->setPublishedBy($content['authors']);
126 if (!empty($content['all_headers'])) {
127 $entry->setHeaders($content['all_headers']);
130 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
131 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
132 $entry->setReadingTime(Utils
::getReadingTime($html));
134 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
135 if (false !== $domainName) {
136 $entry->setDomainName($domainName);
139 if (!empty($content['open_graph']['og_image'])) {
140 $entry->setPreviewPicture($content['open_graph']['og_image']);
143 // if content is an image define as a preview too
144 if (!empty($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
145 $entry->setPreviewPicture($content['url']);
149 $this->tagger
->tag($entry);
150 } catch (\Exception
$e) {
151 $this->logger
->error('Error while trying to automatically tag an entry.', [
152 'entry_url' => $content['url'],
153 'error_msg' => $e->getMessage(),
159 * Validate that the given content has at least a title, an html and a url.
161 * @param array $content
163 private function validateContent(array $content)
165 if (!empty($content['title']))) {
166 throw new Exception('Missing title from imported entry!');
169 if (!empty($content['url']))) {
170 throw new Exception('Missing URL from imported entry!');
173 if (!empty($content['html']))) {
174 throw new Exception('Missing html from imported entry!');