X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FHelper%2FContentProxy.php;h=4b3e6fbb161fd176ad875b5f3b1d8857037d47df;hb=5d3deafd3efc04df53fc24ee82a49988f72756dd;hp=bbd5db5d1f1bed84a2c103035f49114a3857ad5e;hpb=d3511bbde9743995e7ee7eec74b20b9b7b1b56ca;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index bbd5db5d..4b3e6fbb 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -3,11 +3,9 @@ namespace Wallabag\CoreBundle\Helper; use Graby\Graby; -use Psr\Log\LoggerInterface as Logger; +use Psr\Log\LoggerInterface; use Wallabag\CoreBundle\Entity\Entry; -use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Tools\Utils; -use Wallabag\CoreBundle\Repository\TagRepository; use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; /** @@ -19,16 +17,17 @@ class ContentProxy protected $graby; protected $tagger; protected $logger; - protected $tagRepository; protected $mimeGuesser; + protected $fetchingErrorMessage; + protected $eventDispatcher; - public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger) + public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage) { $this->graby = $graby; $this->tagger = $tagger; $this->logger = $logger; - $this->tagRepository = $tagRepository; $this->mimeGuesser = new MimeTypeExtensionGuesser(); + $this->fetchingErrorMessage = $fetchingErrorMessage; } /** @@ -48,7 +47,13 @@ class ContentProxy { // 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']; @@ -58,7 +63,7 @@ class ContentProxy $html = $content['html']; if (false === $html) { - $html = '

Unable to retrieve readable content.

'; + $html = $this->fetchingErrorMessage; if (isset($content['open_graph']['og_description'])) { $html .= '

But we found a short description:

'; @@ -69,8 +74,22 @@ class ContentProxy $entry->setUrl($content['url'] ?: $url); $entry->setTitle($title); $entry->setContent($html); - $entry->setLanguage($content['language']); - $entry->setMimetype($content['content_type']); + $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); + + if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) { + $entry->setPublishedAt(new \DateTime($content['date'])); + } + + if (!empty($content['authors'])) { + $entry->setPublishedBy($content['authors']); + } + + if (!empty($content['all_headers'])) { + $entry->setHeaders($content['all_headers']); + } + + $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); @@ -78,12 +97,12 @@ class ContentProxy $entry->setDomainName($domainName); } - if (isset($content['open_graph']['og_image'])) { + if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) { $entry->setPreviewPicture($content['open_graph']['og_image']); } // 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']); } @@ -99,54 +118,6 @@ class ContentProxy return $entry; } - /** - * Assign some tags to an entry. - * - * @param Entry $entry - * @param array|string $tags An array of tag or a string coma separated of tag - * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed - * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 - */ - public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) - { - if (!is_array($tags)) { - $tags = explode(',', $tags); - } - - // keeps only Tag entity from the "not yet flushed entities" - $tagsNotYetFlushed = []; - foreach ($entitiesReady as $entity) { - if ($entity instanceof Tag) { - $tagsNotYetFlushed[$entity->getLabel()] = $entity; - } - } - - foreach ($tags as $label) { - $label = trim($label); - - // avoid empty tag - if (0 === strlen($label)) { - continue; - } - - if (isset($tagsNotYetFlushed[$label])) { - $tagEntity = $tagsNotYetFlushed[$label]; - } else { - $tagEntity = $this->tagRepository->findOneByLabel($label); - - if (is_null($tagEntity)) { - $tagEntity = new Tag(); - $tagEntity->setLabel($label); - } - } - - // only add the tag on the entry if the relation doesn't exist - if (false === $entry->getTags()->contains($tagEntity)) { - $entry->addTag($tagEntity); - } - } - } - /** * Validate that the given content as enough value to be used * instead of fetch the content from the url.