]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
45fd7e09 | 6 | use Psr\Log\LoggerInterface; |
558d9aab | 7 | use Wallabag\CoreBundle\Entity\Entry; |
da3d4998 | 8 | use Wallabag\CoreBundle\Tools\Utils; |
8d7b4f0e | 9 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; |
d0e9b3d6 | 10 | use Symfony\Component\Config\Definition\Exception\Exception; |
558d9aab JB |
11 | |
12 | /** | |
13 | * This kind of proxy class take care of getting the content from an url | |
f1e29e69 | 14 | * and update the entry with what it found. |
558d9aab JB |
15 | */ |
16 | class ContentProxy | |
17 | { | |
18 | protected $graby; | |
c3510620 | 19 | protected $tagger; |
1c9cd2a7 | 20 | protected $logger; |
8d7b4f0e | 21 | protected $mimeGuesser; |
29dca432 | 22 | protected $fetchingErrorMessage; |
6bc6fb1f | 23 | protected $eventDispatcher; |
558d9aab | 24 | |
6bc6fb1f | 25 | public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage) |
558d9aab | 26 | { |
347fa6be | 27 | $this->graby = $graby; |
c3510620 | 28 | $this->tagger = $tagger; |
1c9cd2a7 | 29 | $this->logger = $logger; |
8d7b4f0e | 30 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); |
29dca432 | 31 | $this->fetchingErrorMessage = $fetchingErrorMessage; |
558d9aab JB |
32 | } |
33 | ||
34 | /** | |
d0e9b3d6 | 35 | * Update existing entry by fetching from URL using Graby. |
4d0ec0e7 | 36 | * |
d5c2cc54 JB |
37 | * @param Entry $entry Entry to update |
38 | * @param string $url Url to grab content for | |
558d9aab | 39 | */ |
d0e9b3d6 | 40 | public function updateEntry(Entry $entry, $url) |
558d9aab | 41 | { |
d0e9b3d6 JC |
42 | $content = $this->graby->fetchContent($url); |
43 | ||
d5c2cc54 JB |
44 | // be sure to keep the url in case of error |
45 | // so we'll be able to refetch it in the future | |
46 | $content['url'] = $content['url'] ?: $url; | |
47 | ||
d0e9b3d6 JC |
48 | $this->stockEntry($entry, $content); |
49 | } | |
50 | ||
51 | /** | |
52 | * Import entry using either fetched or provided content. | |
53 | * | |
843182c7 JB |
54 | * @param Entry $entry Entry to update |
55 | * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url | |
56 | * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby | |
d0e9b3d6 JC |
57 | */ |
58 | public function importEntry(Entry $entry, array $content, $disableContentUpdate = false) | |
59 | { | |
d5c2cc54 JB |
60 | try { |
61 | $this->validateContent($content); | |
62 | } catch (\Exception $e) { | |
63 | // validation failed but do we want to disable updating content? | |
64 | if (true === $disableContentUpdate) { | |
65 | throw $e; | |
66 | } | |
67 | } | |
e668a812 | 68 | |
d0e9b3d6 JC |
69 | if (false === $disableContentUpdate) { |
70 | try { | |
71 | $fetchedContent = $this->graby->fetchContent($content['url']); | |
72 | } catch (\Exception $e) { | |
73 | $this->logger->error('Error while trying to fetch content from URL.', [ | |
74 | 'entry_url' => $content['url'], | |
75 | 'error_msg' => $e->getMessage(), | |
76 | ]); | |
77 | } | |
106bdbcd JB |
78 | |
79 | // when content is imported, we have information in $content | |
80 | // in case fetching content goes bad, we'll keep the imported information instead of overriding them | |
d0e9b3d6 | 81 | if ($fetchedContent['html'] !== $this->fetchingErrorMessage) { |
29dca432 JC |
82 | $content = $fetchedContent; |
83 | } | |
4d0ec0e7 | 84 | } |
558d9aab | 85 | |
d0e9b3d6 JC |
86 | $this->stockEntry($entry, $content); |
87 | } | |
88 | ||
89 | /** | |
90 | * Stock entry with fetched or imported content. | |
91 | * Will fall back to OpenGraph data if available. | |
92 | * | |
d5c2cc54 JB |
93 | * @param Entry $entry Entry to stock |
94 | * @param array $content Array with at least title and URL | |
d0e9b3d6 JC |
95 | */ |
96 | private function stockEntry(Entry $entry, array $content) | |
97 | { | |
558d9aab | 98 | $title = $content['title']; |
e668a812 | 99 | if (!$title && !empty($content['open_graph']['og_title'])) { |
558d9aab JB |
100 | $title = $content['open_graph']['og_title']; |
101 | } | |
102 | ||
103 | $html = $content['html']; | |
104 | if (false === $html) { | |
36e6ef52 | 105 | $html = $this->fetchingErrorMessage; |
558d9aab | 106 | |
e668a812 | 107 | if (!empty($content['open_graph']['og_description'])) { |
558d9aab JB |
108 | $html .= '<p><i>But we found a short description: </i></p>'; |
109 | $html .= $content['open_graph']['og_description']; | |
110 | } | |
111 | } | |
112 | ||
d0e9b3d6 | 113 | $entry->setUrl($content['url']); |
558d9aab | 114 | $entry->setTitle($title); |
48656e0e | 115 | $entry->setContent($html); |
10b35097 | 116 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); |
419214d7 | 117 | |
e668a812 | 118 | if (!empty($content['date'])) { |
f0378b4d JB |
119 | $date = $content['date']; |
120 | ||
121 | // is it a timestamp? | |
122 | if (filter_var($date, FILTER_VALIDATE_INT) !== false) { | |
123 | $date = '@'.$content['date']; | |
124 | } | |
125 | ||
e668a812 | 126 | try { |
f0378b4d | 127 | $entry->setPublishedAt(new \DateTime($date)); |
e668a812 | 128 | } catch (\Exception $e) { |
9e349f08 | 129 | $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]); |
e668a812 | 130 | } |
5e9009ce NL |
131 | } |
132 | ||
7b0b3622 NL |
133 | if (!empty($content['authors'])) { |
134 | $entry->setPublishedBy($content['authors']); | |
135 | } | |
136 | ||
dda6a6ad NL |
137 | if (!empty($content['all_headers'])) { |
138 | $entry->setHeaders($content['all_headers']); | |
139 | } | |
140 | ||
e858018f JC |
141 | $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); |
142 | $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); | |
da3d4998 | 143 | $entry->setReadingTime(Utils::getReadingTime($html)); |
4d0ec0e7 JB |
144 | |
145 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
146 | if (false !== $domainName) { | |
147 | $entry->setDomainName($domainName); | |
148 | } | |
558d9aab | 149 | |
e668a812 | 150 | if (!empty($content['open_graph']['og_image'])) { |
558d9aab JB |
151 | $entry->setPreviewPicture($content['open_graph']['og_image']); |
152 | } | |
153 | ||
8d7b4f0e | 154 | // if content is an image define as a preview too |
e668a812 | 155 | if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
8d7b4f0e JB |
156 | $entry->setPreviewPicture($content['url']); |
157 | } | |
158 | ||
1c9cd2a7 KG |
159 | try { |
160 | $this->tagger->tag($entry); | |
161 | } catch (\Exception $e) { | |
4094ea47 | 162 | $this->logger->error('Error while trying to automatically tag an entry.', [ |
d0e9b3d6 | 163 | 'entry_url' => $content['url'], |
1c9cd2a7 | 164 | 'error_msg' => $e->getMessage(), |
4094ea47 | 165 | ]); |
1c9cd2a7 | 166 | } |
558d9aab | 167 | } |
c2656f96 | 168 | |
4d0ec0e7 | 169 | /** |
d0e9b3d6 | 170 | * Validate that the given content has at least a title, an html and a url. |
4d0ec0e7 JB |
171 | * |
172 | * @param array $content | |
4d0ec0e7 JB |
173 | */ |
174 | private function validateContent(array $content) | |
175 | { | |
d5c2cc54 | 176 | if (empty($content['title'])) { |
d0e9b3d6 JC |
177 | throw new Exception('Missing title from imported entry!'); |
178 | } | |
179 | ||
d5c2cc54 | 180 | if (empty($content['url'])) { |
d0e9b3d6 JC |
181 | throw new Exception('Missing URL from imported entry!'); |
182 | } | |
183 | ||
d5c2cc54 | 184 | if (empty($content['html'])) { |
d0e9b3d6 JC |
185 | throw new Exception('Missing html from imported entry!'); |
186 | } | |
4d0ec0e7 | 187 | } |
558d9aab | 188 | } |