]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Merge pull request #3104 from wallabag/migration-username-length
[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
7b0b3622 82 if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) {
5e9009ce
NL
83 $entry->setPublishedAt(new \DateTime($content['date']));
84 }
85
7b0b3622
NL
86 if (!empty($content['authors'])) {
87 $entry->setPublishedBy($content['authors']);
88 }
89
e858018f
JC
90 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
91 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 92 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
93
94 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
95 if (false !== $domainName) {
96 $entry->setDomainName($domainName);
97 }
558d9aab 98
3d71d403 99 if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) {
558d9aab
JB
100 $entry->setPreviewPicture($content['open_graph']['og_image']);
101 }
102
8d7b4f0e 103 // if content is an image define as a preview too
e858018f 104 if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
105 $entry->setPreviewPicture($content['url']);
106 }
107
1c9cd2a7
KG
108 try {
109 $this->tagger->tag($entry);
110 } catch (\Exception $e) {
4094ea47 111 $this->logger->error('Error while trying to automatically tag an entry.', [
1c9cd2a7
KG
112 'entry_url' => $url,
113 'error_msg' => $e->getMessage(),
4094ea47 114 ]);
1c9cd2a7 115 }
c3510620 116
558d9aab
JB
117 return $entry;
118 }
c2656f96
JB
119
120 /**
121 * Assign some tags to an entry.
122 *
123 * @param Entry $entry
82fc3290
JB
124 * @param array|string $tags An array of tag or a string coma separated of tag
125 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
126 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
c2656f96 127 */
40113585 128 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
c2656f96
JB
129 {
130 if (!is_array($tags)) {
131 $tags = explode(',', $tags);
132 }
133
40113585
JB
134 // keeps only Tag entity from the "not yet flushed entities"
135 $tagsNotYetFlushed = [];
136 foreach ($entitiesReady as $entity) {
137 if ($entity instanceof Tag) {
138 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
139 }
140 }
141
c2656f96
JB
142 foreach ($tags as $label) {
143 $label = trim($label);
144
145 // avoid empty tag
146 if (0 === strlen($label)) {
147 continue;
148 }
149
40113585
JB
150 if (isset($tagsNotYetFlushed[$label])) {
151 $tagEntity = $tagsNotYetFlushed[$label];
152 } else {
153 $tagEntity = $this->tagRepository->findOneByLabel($label);
c2656f96 154
40113585
JB
155 if (is_null($tagEntity)) {
156 $tagEntity = new Tag();
157 $tagEntity->setLabel($label);
158 }
c2656f96
JB
159 }
160
161 // only add the tag on the entry if the relation doesn't exist
162 if (false === $entry->getTags()->contains($tagEntity)) {
163 $entry->addTag($tagEntity);
164 }
165 }
166 }
4d0ec0e7
JB
167
168 /**
169 * Validate that the given content as enough value to be used
170 * instead of fetch the content from the url.
171 *
172 * @param array $content
173 *
174 * @return bool true if valid otherwise false
175 */
176 private function validateContent(array $content)
177 {
178 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
179 }
558d9aab 180}