protected $logger;
protected $tagRepository;
protected $mimeGuesser;
+ protected $fetchingErrorMessage;
- public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger)
+ public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage)
{
$this->graby = $graby;
$this->tagger = $tagger;
$this->logger = $logger;
$this->tagRepository = $tagRepository;
$this->mimeGuesser = new MimeTypeExtensionGuesser();
+ $this->fetchingErrorMessage = $fetchingErrorMessage;
}
/**
{
// do we have to fetch the content or the provided one is ok?
if (empty($content) || false === $this->validateContent($content)) {
- $content = $this->graby->fetchContent($url);
+ $fetchedContent = $this->graby->fetchContent($url);
+
+ // when content is imported, we have information in $content
+ // in case fetching content goes bad, we'll keep the imported information instead of overriding them
+ if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
+ $content = $fetchedContent;
+ }
}
$title = $content['title'];
$html = $content['html'];
if (false === $html) {
- $html = '<p>Unable to retrieve readable content.</p>';
+ $html = $this->fetchingErrorMessage;
if (isset($content['open_graph']['og_description'])) {
$html .= '<p><i>But we found a short description: </i></p>';
$entry->setContent($html);
$entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
- $entry->setLanguage($content['language']);
- $entry->setMimetype($content['content_type']);
+ $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
+ $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
$entry->setReadingTime(Utils::getReadingTime($html));
$domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
}
// if content is an image define as a preview too
- if (in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
+ if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
$entry->setPreviewPicture($content['url']);
}
class ContentProxyTest extends \PHPUnit_Framework_TestCase
{
+ private $fetchingErrorMessage = 'wallabag can\'t retrieve contents for this article. Please <a href="http://doc.wallabag.org/en/master/user/errors_during_fetching.html#how-can-i-help-to-fix-that">troubleshoot this issue</a>.';
+
public function testWithBadUrl()
{
$tagger = $this->getTaggerMock();
'language' => '',
]);
- $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
+ $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
$entry = $proxy->updateEntry(new Entry(new User()), 'http://user@:80');
$this->assertEquals('http://user@:80', $entry->getUrl());
$this->assertEmpty($entry->getTitle());
- $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
+ $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
$this->assertEmpty($entry->getPreviewPicture());
$this->assertEmpty($entry->getMimetype());
$this->assertEmpty($entry->getLanguage());
'language' => '',
]);
- $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
+ $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
$entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
$this->assertEquals('http://0.0.0.0', $entry->getUrl());
$this->assertEmpty($entry->getTitle());
- $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
+ $this->assertEquals($this->fetchingErrorMessage, $entry->getContent());
$this->assertEmpty($entry->getPreviewPicture());
$this->assertEmpty($entry->getMimetype());
$this->assertEmpty($entry->getLanguage());
],
]);
- $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
+ $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
$entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
$this->assertEquals('http://domain.io', $entry->getUrl());
$this->assertEquals('my title', $entry->getTitle());
- $this->assertEquals('<p>Unable to retrieve readable content.</p><p><i>But we found a short description: </i></p>desc', $entry->getContent());
+ $this->assertEquals($this->fetchingErrorMessage . '<p><i>But we found a short description: </i></p>desc', $entry->getContent());
$this->assertEmpty($entry->getPreviewPicture());
$this->assertEmpty($entry->getLanguage());
$this->assertEmpty($entry->getHttpStatus());
],
]);
- $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
+ $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
$entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
$this->assertEquals('http://1.1.1.1', $entry->getUrl());
$graby = $this->getMockBuilder('Graby\Graby')->getMock();
- $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
+ $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger(), $this->fetchingErrorMessage);
$entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
'html' => str_repeat('this is my content', 325),
'title' => 'this is my title',
->will($this->throwException(new \Exception()));
$tagRepo = $this->getTagRepositoryMock();
- $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $tagger, $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0', [
'html' => str_repeat('this is my content', 325),
->getMock();
$tagRepo = $this->getTagRepositoryMock();
- $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$entry = new Entry(new User());
->getMock();
$tagRepo = $this->getTagRepositoryMock();
- $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$entry = new Entry(new User());
->getMock();
$tagRepo = $this->getTagRepositoryMock();
- $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$entry = new Entry(new User());
->getMock();
$tagRepo = $this->getTagRepositoryMock();
- $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$entry = new Entry(new User());
->getMock();
$tagRepo = $this->getTagRepositoryMock();
- $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$tagEntity = new Tag();
$tagEntity->setLabel('tag1');
$tagRepo->expects($this->never())
->method('__call');
- $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
+ $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger(), $this->fetchingErrorMessage);
$tagEntity = new Tag();
$tagEntity->setLabel('tag1');