]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
6 | use Psr\Log\LoggerInterface; | |
7 | use Wallabag\CoreBundle\Entity\Entry; | |
8 | use Wallabag\CoreBundle\Tools\Utils; | |
9 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; | |
10 | ||
11 | /** | |
12 | * This kind of proxy class take care of getting the content from an url | |
13 | * and update the entry with what it found. | |
14 | */ | |
15 | class ContentProxy | |
16 | { | |
17 | protected $graby; | |
18 | protected $tagger; | |
19 | protected $logger; | |
20 | protected $mimeGuesser; | |
21 | protected $fetchingErrorMessage; | |
22 | protected $eventDispatcher; | |
23 | ||
24 | public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage) | |
25 | { | |
26 | $this->graby = $graby; | |
27 | $this->tagger = $tagger; | |
28 | $this->logger = $logger; | |
29 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); | |
30 | $this->fetchingErrorMessage = $fetchingErrorMessage; | |
31 | } | |
32 | ||
33 | /** | |
34 | * Update entry using either fetched or provided content. | |
35 | * | |
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 | |
40 | */ | |
41 | public function updateEntry(Entry $entry, $url, array $content = [], $disableContentUpdate = false) | |
42 | { | |
43 | if (!empty($content['html'])) { | |
44 | $content['html'] = $this->graby->cleanupHtml($content['html'], $url); | |
45 | } | |
46 | ||
47 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { | |
48 | $fetchedContent = $this->graby->fetchContent($url); | |
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 | |
52 | if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) { | |
53 | $content = $fetchedContent; | |
54 | } | |
55 | } | |
56 | ||
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 | ||
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 | * | |
68 | * @param Entry $entry Entry to stock | |
69 | * @param array $content Array with at least title, url & html | |
70 | */ | |
71 | private function stockEntry(Entry $entry, array $content) | |
72 | { | |
73 | $title = $content['title']; | |
74 | if (!$title && !empty($content['open_graph']['og_title'])) { | |
75 | $title = $content['open_graph']['og_title']; | |
76 | } | |
77 | ||
78 | $html = $content['html']; | |
79 | if (false === $html) { | |
80 | $html = $this->fetchingErrorMessage; | |
81 | ||
82 | if (!empty($content['open_graph']['og_description'])) { | |
83 | $html .= '<p><i>But we found a short description: </i></p>'; | |
84 | $html .= $content['open_graph']['og_description']; | |
85 | } | |
86 | } | |
87 | ||
88 | $entry->setUrl($content['url']); | |
89 | $entry->setTitle($title); | |
90 | $entry->setContent($html); | |
91 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); | |
92 | ||
93 | if (!empty($content['date'])) { | |
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 | ||
101 | try { | |
102 | $entry->setPublishedAt(new \DateTime($date)); | |
103 | } catch (\Exception $e) { | |
104 | $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]); | |
105 | } | |
106 | } | |
107 | ||
108 | if (!empty($content['authors'])) { | |
109 | $entry->setPublishedBy($content['authors']); | |
110 | } | |
111 | ||
112 | if (!empty($content['all_headers'])) { | |
113 | $entry->setHeaders($content['all_headers']); | |
114 | } | |
115 | ||
116 | $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); | |
117 | $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); | |
118 | $entry->setReadingTime(Utils::getReadingTime($html)); | |
119 | ||
120 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
121 | if (false !== $domainName) { | |
122 | $entry->setDomainName($domainName); | |
123 | } | |
124 | ||
125 | if (!empty($content['open_graph']['og_image'])) { | |
126 | $entry->setPreviewPicture($content['open_graph']['og_image']); | |
127 | } | |
128 | ||
129 | // if content is an image define as a preview too | |
130 | if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { | |
131 | $entry->setPreviewPicture($content['url']); | |
132 | } | |
133 | ||
134 | try { | |
135 | $this->tagger->tag($entry); | |
136 | } catch (\Exception $e) { | |
137 | $this->logger->error('Error while trying to automatically tag an entry.', [ | |
138 | 'entry_url' => $content['url'], | |
139 | 'error_msg' => $e->getMessage(), | |
140 | ]); | |
141 | } | |
142 | } | |
143 | ||
144 | /** | |
145 | * Validate that the given content has at least a title, an html and a url. | |
146 | * | |
147 | * @param array $content | |
148 | * | |
149 | * @return bool true if valid otherwise false | |
150 | */ | |
151 | private function validateContent(array $content) | |
152 | { | |
153 | return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); | |
154 | } | |
155 | } |