diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index bc257ffb..c6fa0d98 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -12,8 +12,8 @@ use Wallabag\CoreBundle\Entity\Entry; | |||
12 | use Wallabag\CoreBundle\Tools\Utils; | 12 | use Wallabag\CoreBundle\Tools\Utils; |
13 | 13 | ||
14 | /** | 14 | /** |
15 | * This kind of proxy class take care of getting the content from an url | 15 | * This kind of proxy class takes care of getting the content from an url |
16 | * and update the entry with what it found. | 16 | * and updates the entry with what it found. |
17 | */ | 17 | */ |
18 | class ContentProxy | 18 | class ContentProxy |
19 | { | 19 | { |
@@ -54,7 +54,11 @@ class ContentProxy | |||
54 | 54 | ||
55 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { | 55 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { |
56 | $fetchedContent = $this->graby->fetchContent($url); | 56 | $fetchedContent = $this->graby->fetchContent($url); |
57 | $fetchedContent['title'] = $this->sanitizeContentTitle($fetchedContent['title'], $fetchedContent['content_type']); | 57 | |
58 | $fetchedContent['title'] = $this->sanitizeContentTitle( | ||
59 | $fetchedContent['title'], | ||
60 | isset($fetchedContent['headers']['content-type']) ? $fetchedContent['headers']['content-type'] : '' | ||
61 | ); | ||
58 | 62 | ||
59 | // when content is imported, we have information in $content | 63 | // when content is imported, we have information in $content |
60 | // in case fetching content goes bad, we'll keep the imported information instead of overriding them | 64 | // in case fetching content goes bad, we'll keep the imported information instead of overriding them |
@@ -188,8 +192,8 @@ class ContentProxy | |||
188 | /** | 192 | /** |
189 | * Try to sanitize the title of the fetched content from wrong character encodings and invalid UTF-8 character. | 193 | * Try to sanitize the title of the fetched content from wrong character encodings and invalid UTF-8 character. |
190 | * | 194 | * |
191 | * @param $title | 195 | * @param string $title |
192 | * @param $contentType | 196 | * @param string $contentType |
193 | * | 197 | * |
194 | * @return string | 198 | * @return string |
195 | */ | 199 | */ |
@@ -253,16 +257,14 @@ class ContentProxy | |||
253 | 257 | ||
254 | if (!empty($content['title'])) { | 258 | if (!empty($content['title'])) { |
255 | $entry->setTitle($content['title']); | 259 | $entry->setTitle($content['title']); |
256 | } elseif (!empty($content['open_graph']['og_title'])) { | ||
257 | $entry->setTitle($content['open_graph']['og_title']); | ||
258 | } | 260 | } |
259 | 261 | ||
260 | if (empty($content['html'])) { | 262 | if (empty($content['html'])) { |
261 | $content['html'] = $this->fetchingErrorMessage; | 263 | $content['html'] = $this->fetchingErrorMessage; |
262 | 264 | ||
263 | if (!empty($content['open_graph']['og_description'])) { | 265 | if (!empty($content['description'])) { |
264 | $content['html'] .= '<p><i>But we found a short description: </i></p>'; | 266 | $content['html'] .= '<p><i>But we found a short description: </i></p>'; |
265 | $content['html'] .= $content['open_graph']['og_description']; | 267 | $content['html'] .= $content['description']; |
266 | } | 268 | } |
267 | } | 269 | } |
268 | 270 | ||
@@ -277,8 +279,8 @@ class ContentProxy | |||
277 | $entry->setPublishedBy($content['authors']); | 279 | $entry->setPublishedBy($content['authors']); |
278 | } | 280 | } |
279 | 281 | ||
280 | if (!empty($content['all_headers']) && $this->storeArticleHeaders) { | 282 | if (!empty($content['headers'])) { |
281 | $entry->setHeaders($content['all_headers']); | 283 | $entry->setHeaders($content['headers']); |
282 | } | 284 | } |
283 | 285 | ||
284 | if (!empty($content['date'])) { | 286 | if (!empty($content['date'])) { |
@@ -289,17 +291,30 @@ class ContentProxy | |||
289 | $this->updateLanguage($entry, $content['language']); | 291 | $this->updateLanguage($entry, $content['language']); |
290 | } | 292 | } |
291 | 293 | ||
292 | if (!empty($content['open_graph']['og_image'])) { | 294 | $previewPictureUrl = ''; |
293 | $this->updatePreviewPicture($entry, $content['open_graph']['og_image']); | 295 | if (!empty($content['image'])) { |
296 | $previewPictureUrl = $content['image']; | ||
294 | } | 297 | } |
295 | 298 | ||
296 | // if content is an image, define it as a preview too | 299 | // if content is an image, define it as a preview too |
297 | if (!empty($content['content_type']) && \in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { | 300 | if (!empty($content['headers']['content-type']) && \in_array($this->mimeGuesser->guess($content['headers']['content-type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
298 | $this->updatePreviewPicture($entry, $content['url']); | 301 | $previewPictureUrl = $content['url']; |
302 | } elseif (empty($previewPictureUrl)) { | ||
303 | $this->logger->debug('Extracting images from content to provide a default preview picture'); | ||
304 | $imagesUrls = DownloadImages::extractImagesUrlsFromHtml($content['html']); | ||
305 | $this->logger->debug(\count($imagesUrls) . ' pictures found'); | ||
306 | |||
307 | if (!empty($imagesUrls)) { | ||
308 | $previewPictureUrl = $imagesUrls[0]; | ||
309 | } | ||
310 | } | ||
311 | |||
312 | if (!empty($content['headers']['content-type'])) { | ||
313 | $entry->setMimetype($content['headers']['content-type']); | ||
299 | } | 314 | } |
300 | 315 | ||
301 | if (!empty($content['content_type'])) { | 316 | if (!empty($previewPictureUrl)) { |
302 | $entry->setMimetype($content['content_type']); | 317 | $this->updatePreviewPicture($entry, $previewPictureUrl); |
303 | } | 318 | } |
304 | 319 | ||
305 | try { | 320 | try { |