]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Added publication date
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
CommitLineData
558d9aab
JB
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Graby;
45fd7e09 6use Psr\Log\LoggerInterface;
558d9aab 7use Wallabag\CoreBundle\Entity\Entry;
c2656f96 8use Wallabag\CoreBundle\Entity\Tag;
da3d4998 9use Wallabag\CoreBundle\Tools\Utils;
c2656f96 10use Wallabag\CoreBundle\Repository\TagRepository;
8d7b4f0e 11use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
558d9aab
JB
12
13/**
14 * This kind of proxy class take care of getting the content from an url
f1e29e69 15 * and update the entry with what it found.
558d9aab
JB
16 */
17class ContentProxy
18{
19 protected $graby;
c3510620 20 protected $tagger;
1c9cd2a7 21 protected $logger;
c2656f96 22 protected $tagRepository;
8d7b4f0e 23 protected $mimeGuesser;
29dca432 24 protected $fetchingErrorMessage;
558d9aab 25
29dca432 26 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage)
558d9aab 27 {
347fa6be 28 $this->graby = $graby;
c3510620 29 $this->tagger = $tagger;
1c9cd2a7 30 $this->logger = $logger;
c2656f96 31 $this->tagRepository = $tagRepository;
8d7b4f0e 32 $this->mimeGuesser = new MimeTypeExtensionGuesser();
29dca432 33 $this->fetchingErrorMessage = $fetchingErrorMessage;
558d9aab
JB
34 }
35
36 /**
37 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 38 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 39 *
4d0ec0e7
JB
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.
42 *
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
558d9aab
JB
46 *
47 * @return Entry
48 */
4d0ec0e7 49 public function updateEntry(Entry $entry, $url, array $content = [])
558d9aab 50 {
4d0ec0e7
JB
51 // do we have to fetch the content or the provided one is ok?
52 if (empty($content) || false === $this->validateContent($content)) {
29dca432 53 $fetchedContent = $this->graby->fetchContent($url);
106bdbcd
JB
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
29dca432
JC
57 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
58 $content = $fetchedContent;
59 }
4d0ec0e7 60 }
558d9aab
JB
61
62 $title = $content['title'];
63 if (!$title && isset($content['open_graph']['og_title'])) {
64 $title = $content['open_graph']['og_title'];
65 }
66
67 $html = $content['html'];
68 if (false === $html) {
36e6ef52 69 $html = $this->fetchingErrorMessage;
558d9aab
JB
70
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'];
74 }
75 }
76
77 $entry->setUrl($content['url'] ?: $url);
78 $entry->setTitle($title);
48656e0e 79 $entry->setContent($html);
10b35097 80 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
419214d7 81
5e9009ce
NL
82 if (isset($content['date']) && null !== $content['date']) {
83 $entry->setPublishedAt(new \DateTime($content['date']));
84 }
85
e858018f
JC
86 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
87 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 88 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
89
90 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
91 if (false !== $domainName) {
92 $entry->setDomainName($domainName);
93 }
558d9aab 94
3d71d403 95 if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) {
558d9aab
JB
96 $entry->setPreviewPicture($content['open_graph']['og_image']);
97 }
98
8d7b4f0e 99 // if content is an image define as a preview too
e858018f 100 if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
101 $entry->setPreviewPicture($content['url']);
102 }
103
1c9cd2a7
KG
104 try {
105 $this->tagger->tag($entry);
106 } catch (\Exception $e) {
4094ea47 107 $this->logger->error('Error while trying to automatically tag an entry.', [
1c9cd2a7
KG
108 'entry_url' => $url,
109 'error_msg' => $e->getMessage(),
4094ea47 110 ]);
1c9cd2a7 111 }
c3510620 112
558d9aab
JB
113 return $entry;
114 }
c2656f96
JB
115
116 /**
117 * Assign some tags to an entry.
118 *
119 * @param Entry $entry
82fc3290
JB
120 * @param array|string $tags An array of tag or a string coma separated of tag
121 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
122 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
c2656f96 123 */
40113585 124 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
c2656f96
JB
125 {
126 if (!is_array($tags)) {
127 $tags = explode(',', $tags);
128 }
129
40113585
JB
130 // keeps only Tag entity from the "not yet flushed entities"
131 $tagsNotYetFlushed = [];
132 foreach ($entitiesReady as $entity) {
133 if ($entity instanceof Tag) {
134 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
135 }
136 }
137
c2656f96
JB
138 foreach ($tags as $label) {
139 $label = trim($label);
140
141 // avoid empty tag
142 if (0 === strlen($label)) {
143 continue;
144 }
145
40113585
JB
146 if (isset($tagsNotYetFlushed[$label])) {
147 $tagEntity = $tagsNotYetFlushed[$label];
148 } else {
149 $tagEntity = $this->tagRepository->findOneByLabel($label);
c2656f96 150
40113585
JB
151 if (is_null($tagEntity)) {
152 $tagEntity = new Tag();
153 $tagEntity->setLabel($label);
154 }
c2656f96
JB
155 }
156
157 // only add the tag on the entry if the relation doesn't exist
158 if (false === $entry->getTags()->contains($tagEntity)) {
159 $entry->addTag($tagEntity);
160 }
161 }
162 }
4d0ec0e7
JB
163
164 /**
165 * Validate that the given content as enough value to be used
166 * instead of fetch the content from the url.
167 *
168 * @param array $content
169 *
170 * @return bool true if valid otherwise false
171 */
172 private function validateContent(array $content)
173 {
174 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
175 }
558d9aab 176}