aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index bbd5db5d..f222dd88 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -3,7 +3,7 @@
3namespace Wallabag\CoreBundle\Helper; 3namespace Wallabag\CoreBundle\Helper;
4 4
5use Graby\Graby; 5use Graby\Graby;
6use Psr\Log\LoggerInterface as Logger; 6use Psr\Log\LoggerInterface;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag; 8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Tools\Utils; 9use Wallabag\CoreBundle\Tools\Utils;
@@ -21,14 +21,16 @@ class ContentProxy
21 protected $logger; 21 protected $logger;
22 protected $tagRepository; 22 protected $tagRepository;
23 protected $mimeGuesser; 23 protected $mimeGuesser;
24 protected $fetchingErrorMessage;
24 25
25 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger) 26 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage)
26 { 27 {
27 $this->graby = $graby; 28 $this->graby = $graby;
28 $this->tagger = $tagger; 29 $this->tagger = $tagger;
29 $this->logger = $logger; 30 $this->logger = $logger;
30 $this->tagRepository = $tagRepository; 31 $this->tagRepository = $tagRepository;
31 $this->mimeGuesser = new MimeTypeExtensionGuesser(); 32 $this->mimeGuesser = new MimeTypeExtensionGuesser();
33 $this->fetchingErrorMessage = $fetchingErrorMessage;
32 } 34 }
33 35
34 /** 36 /**
@@ -48,7 +50,13 @@ class ContentProxy
48 { 50 {
49 // do we have to fetch the content or the provided one is ok? 51 // do we have to fetch the content or the provided one is ok?
50 if (empty($content) || false === $this->validateContent($content)) { 52 if (empty($content) || false === $this->validateContent($content)) {
51 $content = $this->graby->fetchContent($url); 53 $fetchedContent = $this->graby->fetchContent($url);
54
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;
59 }
52 } 60 }
53 61
54 $title = $content['title']; 62 $title = $content['title'];
@@ -58,7 +66,7 @@ class ContentProxy
58 66
59 $html = $content['html']; 67 $html = $content['html'];
60 if (false === $html) { 68 if (false === $html) {
61 $html = '<p>Unable to retrieve readable content.</p>'; 69 $html = $this->fetchingErrorMessage;
62 70
63 if (isset($content['open_graph']['og_description'])) { 71 if (isset($content['open_graph']['og_description'])) {
64 $html .= '<p><i>But we found a short description: </i></p>'; 72 $html .= '<p><i>But we found a short description: </i></p>';
@@ -69,8 +77,10 @@ class ContentProxy
69 $entry->setUrl($content['url'] ?: $url); 77 $entry->setUrl($content['url'] ?: $url);
70 $entry->setTitle($title); 78 $entry->setTitle($title);
71 $entry->setContent($html); 79 $entry->setContent($html);
72 $entry->setLanguage($content['language']); 80 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
73 $entry->setMimetype($content['content_type']); 81
82 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
83 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
74 $entry->setReadingTime(Utils::getReadingTime($html)); 84 $entry->setReadingTime(Utils::getReadingTime($html));
75 85
76 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); 86 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
@@ -78,12 +88,12 @@ class ContentProxy
78 $entry->setDomainName($domainName); 88 $entry->setDomainName($domainName);
79 } 89 }
80 90
81 if (isset($content['open_graph']['og_image'])) { 91 if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) {
82 $entry->setPreviewPicture($content['open_graph']['og_image']); 92 $entry->setPreviewPicture($content['open_graph']['og_image']);
83 } 93 }
84 94
85 // if content is an image define as a preview too 95 // if content is an image define as a preview too
86 if (in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { 96 if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
87 $entry->setPreviewPicture($content['url']); 97 $entry->setPreviewPicture($content['url']);
88 } 98 }
89 99