]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
45fd7e09 | 6 | use Psr\Log\LoggerInterface; |
8d7b4f0e | 7 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; |
42f3bb2c | 8 | use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint; |
0d349ea6 JB |
9 | use Symfony\Component\Validator\Constraints\Url as UrlConstraint; |
10 | use Symfony\Component\Validator\Validator\ValidatorInterface; | |
f808b016 JB |
11 | use Wallabag\CoreBundle\Entity\Entry; |
12 | use Wallabag\CoreBundle\Tools\Utils; | |
558d9aab JB |
13 | |
14 | /** | |
15 | * This kind of proxy class take care of getting the content from an url | |
f1e29e69 | 16 | * and update the entry with what it found. |
558d9aab JB |
17 | */ |
18 | class ContentProxy | |
19 | { | |
20 | protected $graby; | |
c3510620 | 21 | protected $tagger; |
be54dfe4 | 22 | protected $validator; |
1c9cd2a7 | 23 | protected $logger; |
8d7b4f0e | 24 | protected $mimeGuesser; |
29dca432 | 25 | protected $fetchingErrorMessage; |
6bc6fb1f | 26 | protected $eventDispatcher; |
558d9aab | 27 | |
0d349ea6 | 28 | public function __construct(Graby $graby, RuleBasedTagger $tagger, ValidatorInterface $validator, LoggerInterface $logger, $fetchingErrorMessage) |
558d9aab | 29 | { |
347fa6be | 30 | $this->graby = $graby; |
c3510620 | 31 | $this->tagger = $tagger; |
0d349ea6 | 32 | $this->validator = $validator; |
1c9cd2a7 | 33 | $this->logger = $logger; |
8d7b4f0e | 34 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); |
29dca432 | 35 | $this->fetchingErrorMessage = $fetchingErrorMessage; |
558d9aab JB |
36 | } |
37 | ||
38 | /** | |
6acadf8e | 39 | * Update entry using either fetched or provided content. |
4d0ec0e7 | 40 | * |
6acadf8e JB |
41 | * @param Entry $entry Entry to update |
42 | * @param string $url Url of the content | |
43 | * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url | |
44 | * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby | |
558d9aab | 45 | */ |
6acadf8e | 46 | public function updateEntry(Entry $entry, $url, array $content = [], $disableContentUpdate = false) |
558d9aab | 47 | { |
6acadf8e JB |
48 | if (!empty($content['html'])) { |
49 | $content['html'] = $this->graby->cleanupHtml($content['html'], $url); | |
d5c2cc54 | 50 | } |
e668a812 | 51 | |
6acadf8e | 52 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { |
ec970721 | 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 | |
6acadf8e | 57 | if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) { |
29dca432 JC |
58 | $content = $fetchedContent; |
59 | } | |
4d0ec0e7 | 60 | } |
558d9aab | 61 | |
6acadf8e JB |
62 | // be sure to keep the url in case of error |
63 | // so we'll be able to refetch it in the future | |
64 | $content['url'] = !empty($content['url']) ? $content['url'] : $url; | |
65 | ||
d0e9b3d6 JC |
66 | $this->stockEntry($entry, $content); |
67 | } | |
68 | ||
c18a2476 JB |
69 | /** |
70 | * Use a Symfony validator to ensure the language is well formatted. | |
71 | * | |
72 | * @param Entry $entry | |
73 | * @param string $value Language to validate and save | |
74 | */ | |
75 | public function updateLanguage(Entry $entry, $value) | |
76 | { | |
77 | // some lang are defined as fr-FR, es-ES. | |
78 | // replacing - by _ might increase language support | |
79 | $value = str_replace('-', '_', $value); | |
80 | ||
81 | $errors = $this->validator->validate( | |
82 | $value, | |
83 | (new LocaleConstraint()) | |
84 | ); | |
85 | ||
86 | if (0 === count($errors)) { | |
87 | $entry->setLanguage($value); | |
88 | ||
89 | return; | |
90 | } | |
91 | ||
92 | $this->logger->warning('Language validation failed. ' . (string) $errors); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Use a Symfony validator to ensure the preview picture is a real url. | |
97 | * | |
98 | * @param Entry $entry | |
99 | * @param string $value URL to validate and save | |
100 | */ | |
101 | public function updatePreviewPicture(Entry $entry, $value) | |
102 | { | |
103 | $errors = $this->validator->validate( | |
104 | $value, | |
105 | (new UrlConstraint()) | |
106 | ); | |
107 | ||
108 | if (0 === count($errors)) { | |
109 | $entry->setPreviewPicture($value); | |
110 | ||
111 | return; | |
112 | } | |
113 | ||
114 | $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors); | |
115 | } | |
116 | ||
117 | /** | |
118 | * Update date. | |
119 | * | |
120 | * @param Entry $entry | |
121 | * @param string $value Date to validate and save | |
122 | */ | |
123 | public function updatePublishedAt(Entry $entry, $value) | |
124 | { | |
ff9f89fd | 125 | $date = $value; |
c18a2476 JB |
126 | |
127 | // is it a timestamp? | |
3ef055ce | 128 | if (false !== filter_var($date, FILTER_VALIDATE_INT)) { |
ff9f89fd | 129 | $date = '@' . $date; |
c18a2476 JB |
130 | } |
131 | ||
132 | try { | |
ff9f89fd JB |
133 | // is it already a DateTime? |
134 | // (it's inside the try/catch in case of fail to be parse time string) | |
135 | if (!$date instanceof \DateTime) { | |
136 | $date = new \DateTime($date); | |
137 | } | |
138 | ||
139 | $entry->setPublishedAt($date); | |
c18a2476 JB |
140 | } catch (\Exception $e) { |
141 | $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]); | |
142 | } | |
143 | } | |
144 | ||
d0e9b3d6 JC |
145 | /** |
146 | * Stock entry with fetched or imported content. | |
147 | * Will fall back to OpenGraph data if available. | |
148 | * | |
d5c2cc54 | 149 | * @param Entry $entry Entry to stock |
ec970721 | 150 | * @param array $content Array with at least title, url & html |
d0e9b3d6 JC |
151 | */ |
152 | private function stockEntry(Entry $entry, array $content) | |
153 | { | |
a05b6115 JB |
154 | $entry->setUrl($content['url']); |
155 | ||
156 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
157 | if (false !== $domainName) { | |
158 | $entry->setDomainName($domainName); | |
159 | } | |
160 | ||
161 | if (!empty($content['title'])) { | |
162 | $entry->setTitle($content['title']); | |
163 | } elseif (!empty($content['open_graph']['og_title'])) { | |
164 | $entry->setTitle($content['open_graph']['og_title']); | |
558d9aab JB |
165 | } |
166 | ||
167 | $html = $content['html']; | |
168 | if (false === $html) { | |
36e6ef52 | 169 | $html = $this->fetchingErrorMessage; |
558d9aab | 170 | |
e668a812 | 171 | if (!empty($content['open_graph']['og_description'])) { |
558d9aab JB |
172 | $html .= '<p><i>But we found a short description: </i></p>'; |
173 | $html .= $content['open_graph']['og_description']; | |
174 | } | |
175 | } | |
176 | ||
48656e0e | 177 | $entry->setContent($html); |
a05b6115 | 178 | $entry->setReadingTime(Utils::getReadingTime($html)); |
f0378b4d | 179 | |
a05b6115 JB |
180 | if (!empty($content['status'])) { |
181 | $entry->setHttpStatus($content['status']); | |
5e9009ce NL |
182 | } |
183 | ||
645291e8 | 184 | if (!empty($content['authors']) && is_array($content['authors'])) { |
7b0b3622 NL |
185 | $entry->setPublishedBy($content['authors']); |
186 | } | |
187 | ||
dda6a6ad NL |
188 | if (!empty($content['all_headers'])) { |
189 | $entry->setHeaders($content['all_headers']); | |
190 | } | |
191 | ||
a05b6115 JB |
192 | if (!empty($content['date'])) { |
193 | $this->updatePublishedAt($entry, $content['date']); | |
194 | } | |
0d349ea6 | 195 | |
a05b6115 JB |
196 | if (!empty($content['language'])) { |
197 | $this->updateLanguage($entry, $content['language']); | |
198 | } | |
199 | ||
200 | if (!empty($content['open_graph']['og_image'])) { | |
201 | $this->updatePreviewPicture($entry, $content['open_graph']['og_image']); | |
202 | } | |
0d349ea6 | 203 | |
be54dfe4 | 204 | // if content is an image, define it as a preview too |
0d349ea6 | 205 | if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
d0ec2ddd | 206 | $this->updatePreviewPicture($entry, $content['url']); |
0d349ea6 JB |
207 | } |
208 | ||
a05b6115 JB |
209 | if (!empty($content['content_type'])) { |
210 | $entry->setMimetype($content['content_type']); | |
4d0ec0e7 | 211 | } |
558d9aab | 212 | |
1c9cd2a7 KG |
213 | try { |
214 | $this->tagger->tag($entry); | |
215 | } catch (\Exception $e) { | |
4094ea47 | 216 | $this->logger->error('Error while trying to automatically tag an entry.', [ |
d0e9b3d6 | 217 | 'entry_url' => $content['url'], |
1c9cd2a7 | 218 | 'error_msg' => $e->getMessage(), |
4094ea47 | 219 | ]); |
1c9cd2a7 | 220 | } |
558d9aab | 221 | } |
c2656f96 | 222 | |
4d0ec0e7 | 223 | /** |
d0e9b3d6 | 224 | * Validate that the given content has at least a title, an html and a url. |
4d0ec0e7 JB |
225 | * |
226 | * @param array $content | |
6acadf8e JB |
227 | * |
228 | * @return bool true if valid otherwise false | |
4d0ec0e7 JB |
229 | */ |
230 | private function validateContent(array $content) | |
231 | { | |
6acadf8e | 232 | return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); |
4d0ec0e7 | 233 | } |
558d9aab | 234 | } |