]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
45fd7e09 | 6 | use Psr\Log\LoggerInterface; |
558d9aab | 7 | use Wallabag\CoreBundle\Entity\Entry; |
da3d4998 | 8 | use Wallabag\CoreBundle\Tools\Utils; |
8d7b4f0e | 9 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; |
558d9aab JB |
10 | |
11 | /** | |
12 | * This kind of proxy class take care of getting the content from an url | |
f1e29e69 | 13 | * and update the entry with what it found. |
558d9aab JB |
14 | */ |
15 | class ContentProxy | |
16 | { | |
17 | protected $graby; | |
c3510620 | 18 | protected $tagger; |
1c9cd2a7 | 19 | protected $logger; |
8d7b4f0e | 20 | protected $mimeGuesser; |
29dca432 | 21 | protected $fetchingErrorMessage; |
6bc6fb1f | 22 | protected $eventDispatcher; |
558d9aab | 23 | |
6bc6fb1f | 24 | public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage) |
558d9aab | 25 | { |
347fa6be | 26 | $this->graby = $graby; |
c3510620 | 27 | $this->tagger = $tagger; |
1c9cd2a7 | 28 | $this->logger = $logger; |
8d7b4f0e | 29 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); |
29dca432 | 30 | $this->fetchingErrorMessage = $fetchingErrorMessage; |
558d9aab JB |
31 | } |
32 | ||
33 | /** | |
6acadf8e | 34 | * Update entry using either fetched or provided content. |
4d0ec0e7 | 35 | * |
6acadf8e JB |
36 | * @param Entry $entry Entry to update |
37 | * @param string $url Url of the content | |
38 | * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url | |
39 | * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby | |
558d9aab | 40 | */ |
6acadf8e | 41 | public function updateEntry(Entry $entry, $url, array $content = [], $disableContentUpdate = false) |
558d9aab | 42 | { |
6acadf8e JB |
43 | if (!empty($content['html'])) { |
44 | $content['html'] = $this->graby->cleanupHtml($content['html'], $url); | |
d5c2cc54 | 45 | } |
e668a812 | 46 | |
6acadf8e | 47 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { |
ec970721 | 48 | $fetchedContent = $this->graby->fetchContent($url); |
106bdbcd JB |
49 | |
50 | // when content is imported, we have information in $content | |
51 | // in case fetching content goes bad, we'll keep the imported information instead of overriding them | |
6acadf8e | 52 | if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) { |
29dca432 JC |
53 | $content = $fetchedContent; |
54 | } | |
4d0ec0e7 | 55 | } |
558d9aab | 56 | |
6acadf8e JB |
57 | // be sure to keep the url in case of error |
58 | // so we'll be able to refetch it in the future | |
59 | $content['url'] = !empty($content['url']) ? $content['url'] : $url; | |
60 | ||
d0e9b3d6 JC |
61 | $this->stockEntry($entry, $content); |
62 | } | |
63 | ||
64 | /** | |
65 | * Stock entry with fetched or imported content. | |
66 | * Will fall back to OpenGraph data if available. | |
67 | * | |
d5c2cc54 | 68 | * @param Entry $entry Entry to stock |
ec970721 | 69 | * @param array $content Array with at least title, url & html |
d0e9b3d6 JC |
70 | */ |
71 | private function stockEntry(Entry $entry, array $content) | |
72 | { | |
558d9aab | 73 | $title = $content['title']; |
e668a812 | 74 | if (!$title && !empty($content['open_graph']['og_title'])) { |
558d9aab JB |
75 | $title = $content['open_graph']['og_title']; |
76 | } | |
77 | ||
78 | $html = $content['html']; | |
79 | if (false === $html) { | |
36e6ef52 | 80 | $html = $this->fetchingErrorMessage; |
558d9aab | 81 | |
e668a812 | 82 | if (!empty($content['open_graph']['og_description'])) { |
558d9aab JB |
83 | $html .= '<p><i>But we found a short description: </i></p>'; |
84 | $html .= $content['open_graph']['og_description']; | |
85 | } | |
86 | } | |
87 | ||
d0e9b3d6 | 88 | $entry->setUrl($content['url']); |
558d9aab | 89 | $entry->setTitle($title); |
48656e0e | 90 | $entry->setContent($html); |
10b35097 | 91 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); |
419214d7 | 92 | |
e668a812 | 93 | if (!empty($content['date'])) { |
f0378b4d JB |
94 | $date = $content['date']; |
95 | ||
96 | // is it a timestamp? | |
97 | if (filter_var($date, FILTER_VALIDATE_INT) !== false) { | |
98 | $date = '@'.$content['date']; | |
99 | } | |
100 | ||
e668a812 | 101 | try { |
f0378b4d | 102 | $entry->setPublishedAt(new \DateTime($date)); |
e668a812 | 103 | } catch (\Exception $e) { |
6acadf8e | 104 | $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]); |
e668a812 | 105 | } |
5e9009ce NL |
106 | } |
107 | ||
645291e8 | 108 | if (!empty($content['authors']) && is_array($content['authors'])) { |
7b0b3622 NL |
109 | $entry->setPublishedBy($content['authors']); |
110 | } | |
111 | ||
dda6a6ad NL |
112 | if (!empty($content['all_headers'])) { |
113 | $entry->setHeaders($content['all_headers']); | |
114 | } | |
115 | ||
e858018f JC |
116 | $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); |
117 | $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); | |
da3d4998 | 118 | $entry->setReadingTime(Utils::getReadingTime($html)); |
4d0ec0e7 JB |
119 | |
120 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
121 | if (false !== $domainName) { | |
122 | $entry->setDomainName($domainName); | |
123 | } | |
558d9aab | 124 | |
e668a812 | 125 | if (!empty($content['open_graph']['og_image'])) { |
558d9aab JB |
126 | $entry->setPreviewPicture($content['open_graph']['og_image']); |
127 | } | |
128 | ||
8d7b4f0e | 129 | // if content is an image define as a preview too |
e668a812 | 130 | if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
8d7b4f0e JB |
131 | $entry->setPreviewPicture($content['url']); |
132 | } | |
133 | ||
1c9cd2a7 KG |
134 | try { |
135 | $this->tagger->tag($entry); | |
136 | } catch (\Exception $e) { | |
4094ea47 | 137 | $this->logger->error('Error while trying to automatically tag an entry.', [ |
d0e9b3d6 | 138 | 'entry_url' => $content['url'], |
1c9cd2a7 | 139 | 'error_msg' => $e->getMessage(), |
4094ea47 | 140 | ]); |
1c9cd2a7 | 141 | } |
558d9aab | 142 | } |
c2656f96 | 143 | |
4d0ec0e7 | 144 | /** |
d0e9b3d6 | 145 | * Validate that the given content has at least a title, an html and a url. |
4d0ec0e7 JB |
146 | * |
147 | * @param array $content | |
6acadf8e JB |
148 | * |
149 | * @return bool true if valid otherwise false | |
4d0ec0e7 JB |
150 | */ |
151 | private function validateContent(array $content) | |
152 | { | |
6acadf8e | 153 | return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); |
4d0ec0e7 | 154 | } |
558d9aab | 155 | } |