3 namespace Wallabag\CoreBundle\Helper
;
6 use Psr\Log\LoggerInterface
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Entity\Tag
;
9 use Wallabag\CoreBundle\Tools\Utils
;
10 use Wallabag\CoreBundle\Repository\TagRepository
;
11 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser
;
14 * This kind of proxy class take care of getting the content from an url
15 * and update the entry with what it found.
22 protected $tagRepository;
23 protected $mimeGuesser;
24 protected $fetchingErrorMessage;
26 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, TagRepository
$tagRepository, LoggerInterface
$logger, $fetchingErrorMessage)
28 $this->graby
= $graby;
29 $this->tagger
= $tagger;
30 $this->logger
= $logger;
31 $this->tagRepository
= $tagRepository;
32 $this->mimeGuesser
= new MimeTypeExtensionGuesser();
33 $this->fetchingErrorMessage
= $fetchingErrorMessage;
37 * Fetch content using graby and hydrate given entry with results information.
38 * In case we couldn't find content, we'll try to use Open Graph data.
40 * We can also force the content, in case of an import from the v1 for example, so the function won't
41 * fetch the content from the website but rather use information given with the $content parameter.
43 * @param Entry $entry Entry to update
44 * @param string $url Url to grab content for
45 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
49 public function updateEntry(Entry
$entry, $url, array $content = [])
51 // do we have to fetch the content or the provided one is ok?
52 if (empty($content) || false === $this->validateContent($content)) {
53 $fetchedContent = $this->graby
->fetchContent($url);
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;
62 $title = $content['title'];
63 if (!$title && isset($content['open_graph']['og_title'])) {
64 $title = $content['open_graph']['og_title'];
67 $html = $content['html'];
68 if (false === $html) {
69 $html = $this->fetchingErrorMessage
;
71 if (isset($content['open_graph']['og_description'])) {
72 $html .= '<p><i>But we found a short description: </i></p>';
73 $html .= $content['open_graph']['og_description'];
77 $entry->setUrl($content['url'] ?: $url);
78 $entry->setTitle($title);
79 $entry->setContent($html);
80 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
82 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
83 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
84 $entry->setReadingTime(Utils
::getReadingTime($html));
86 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
87 if (false !== $domainName) {
88 $entry->setDomainName($domainName);
91 if (isset($content['open_graph']['og_image'])) {
92 $entry->setPreviewPicture($content['open_graph']['og_image']);
95 // if content is an image define as a preview too
96 if (isset($content['content_type']) && in_array($this->mimeGuesser
->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
97 $entry->setPreviewPicture($content['url']);
101 $this->tagger
->tag($entry);
102 } catch (\Exception
$e) {
103 $this->logger
->error('Error while trying to automatically tag an entry.', [
105 'error_msg' => $e->getMessage(),
113 * Assign some tags to an entry.
115 * @param Entry $entry
116 * @param array|string $tags An array of tag or a string coma separated of tag
117 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
118 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
120 public function assignTagsToEntry(Entry
$entry, $tags, array $entitiesReady = [])
122 if (!is_array($tags)) {
123 $tags = explode(',', $tags);
126 // keeps only Tag entity from the "not yet flushed entities"
127 $tagsNotYetFlushed = [];
128 foreach ($entitiesReady as $entity) {
129 if ($entity instanceof Tag
) {
130 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
134 foreach ($tags as $label) {
135 $label = trim($label);
138 if (0 === strlen($label)) {
142 if (isset($tagsNotYetFlushed[$label])) {
143 $tagEntity = $tagsNotYetFlushed[$label];
145 $tagEntity = $this->tagRepository
->findOneByLabel($label);
147 if (is_null($tagEntity)) {
148 $tagEntity = new Tag();
149 $tagEntity->setLabel($label);
153 // only add the tag on the entry if the relation doesn't exist
154 if (false === $entry->getTags()->contains($tagEntity)) {
155 $entry->addTag($tagEntity);
161 * Validate that the given content as enough value to be used
162 * instead of fetch the content from the url.
164 * @param array $content
166 * @return bool true if valid otherwise false
168 private function validateContent(array $content)
170 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);