]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Add disableContentUpdate import option
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
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 use Symfony\Component\Config\Definition\Exception\Exception;
11
12 /**
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
15 */
16 class ContentProxy
17 {
18 protected $graby;
19 protected $tagger;
20 protected $logger;
21 protected $mimeGuesser;
22 protected $fetchingErrorMessage;
23 protected $eventDispatcher;
24
25 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
26 {
27 $this->graby = $graby;
28 $this->tagger = $tagger;
29 $this->logger = $logger;
30 $this->mimeGuesser = new MimeTypeExtensionGuesser();
31 $this->fetchingErrorMessage = $fetchingErrorMessage;
32 }
33
34 /**
35 * Update existing entry by fetching from URL using Graby.
36 *
37 * @param Entry $entry Entry to update
38 * @param string $url Url to grab content for
39 */
40 public function updateEntry(Entry $entry, $url)
41 {
42 $content = $this->graby->fetchContent($url);
43
44 $this->stockEntry($entry, $content);
45 }
46
47 /**
48 * Import entry using either fetched or provided content.
49 *
50 * @param Entry $entry Entry to update
51 * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url
52 * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby
53 */
54 public function importEntry(Entry $entry, array $content, $disableContentUpdate = false)
55 {
56 $this->validateContent($content);
57
58 if (false === $disableContentUpdate) {
59 try {
60 $fetchedContent = $this->graby->fetchContent($content['url']);
61 } catch (\Exception $e) {
62 $this->logger->error('Error while trying to fetch content from URL.', [
63 'entry_url' => $content['url'],
64 'error_msg' => $e->getMessage(),
65 ]);
66 }
67
68 // when content is imported, we have information in $content
69 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
70 if ($fetchedContent['html'] !== $this->fetchingErrorMessage) {
71 $content = $fetchedContent;
72 }
73 }
74
75 $this->stockEntry($entry, $content);
76 }
77
78 /**
79 * Stock entry with fetched or imported content.
80 * Will fall back to OpenGraph data if available.
81 *
82 * @param Entry $entry Entry to stock
83 * @param array $content Array with at least title and URL
84 */
85 private function stockEntry(Entry $entry, array $content)
86 {
87 $title = $content['title'];
88 if (!$title && !empty($content['open_graph']['og_title'])) {
89 $title = $content['open_graph']['og_title'];
90 }
91
92 $html = $content['html'];
93 if (false === $html) {
94 $html = $this->fetchingErrorMessage;
95
96 if (!empty($content['open_graph']['og_description'])) {
97 $html .= '<p><i>But we found a short description: </i></p>';
98 $html .= $content['open_graph']['og_description'];
99 }
100 }
101
102 $entry->setUrl($content['url']);
103 $entry->setTitle($title);
104 $entry->setContent($html);
105 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
106
107 if (!empty($content['date'])) {
108 $date = $content['date'];
109
110 // is it a timestamp?
111 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
112 $date = '@'.$content['date'];
113 }
114
115 try {
116 $entry->setPublishedAt(new \DateTime($date));
117 } catch (\Exception $e) {
118 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
119 }
120 }
121
122 if (!empty($content['authors'])) {
123 $entry->setPublishedBy($content['authors']);
124 }
125
126 if (!empty($content['all_headers'])) {
127 $entry->setHeaders($content['all_headers']);
128 }
129
130 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
131 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
132 $entry->setReadingTime(Utils::getReadingTime($html));
133
134 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
135 if (false !== $domainName) {
136 $entry->setDomainName($domainName);
137 }
138
139 if (!empty($content['open_graph']['og_image'])) {
140 $entry->setPreviewPicture($content['open_graph']['og_image']);
141 }
142
143 // if content is an image define as a preview too
144 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
145 $entry->setPreviewPicture($content['url']);
146 }
147
148 try {
149 $this->tagger->tag($entry);
150 } catch (\Exception $e) {
151 $this->logger->error('Error while trying to automatically tag an entry.', [
152 'entry_url' => $content['url'],
153 'error_msg' => $e->getMessage(),
154 ]);
155 }
156 }
157
158 /**
159 * Validate that the given content has at least a title, an html and a url.
160 *
161 * @param array $content
162 */
163 private function validateContent(array $content)
164 {
165 if (!empty($content['title']))) {
166 throw new Exception('Missing title from imported entry!');
167 }
168
169 if (!empty($content['url']))) {
170 throw new Exception('Missing URL from imported entry!');
171 }
172
173 if (!empty($content['html']))) {
174 throw new Exception('Missing html from imported entry!');
175 }
176 }
177 }