]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
CS
[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;
da3d4998 8use Wallabag\CoreBundle\Tools\Utils;
8d7b4f0e 9use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
d0e9b3d6 10use Symfony\Component\Config\Definition\Exception\Exception;
558d9aab
JB
11
12/**
13 * This kind of proxy class take care of getting the content from an url
f1e29e69 14 * and update the entry with what it found.
558d9aab
JB
15 */
16class ContentProxy
17{
18 protected $graby;
c3510620 19 protected $tagger;
1c9cd2a7 20 protected $logger;
8d7b4f0e 21 protected $mimeGuesser;
29dca432 22 protected $fetchingErrorMessage;
6bc6fb1f 23 protected $eventDispatcher;
558d9aab 24
6bc6fb1f 25 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
558d9aab 26 {
347fa6be 27 $this->graby = $graby;
c3510620 28 $this->tagger = $tagger;
1c9cd2a7 29 $this->logger = $logger;
8d7b4f0e 30 $this->mimeGuesser = new MimeTypeExtensionGuesser();
29dca432 31 $this->fetchingErrorMessage = $fetchingErrorMessage;
558d9aab
JB
32 }
33
34 /**
d0e9b3d6 35 * Update existing entry by fetching from URL using Graby.
4d0ec0e7
JB
36 *
37 * @param Entry $entry Entry to update
38 * @param string $url Url to grab content for
558d9aab 39 */
d0e9b3d6 40 public function updateEntry(Entry $entry, $url)
558d9aab 41 {
d0e9b3d6
JC
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);
e668a812 57
d0e9b3d6
JC
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 }
106bdbcd
JB
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
d0e9b3d6 70 if ($fetchedContent['html'] !== $this->fetchingErrorMessage) {
29dca432
JC
71 $content = $fetchedContent;
72 }
4d0ec0e7 73 }
558d9aab 74
d0e9b3d6
JC
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 {
558d9aab 87 $title = $content['title'];
e668a812 88 if (!$title && !empty($content['open_graph']['og_title'])) {
558d9aab
JB
89 $title = $content['open_graph']['og_title'];
90 }
91
92 $html = $content['html'];
93 if (false === $html) {
36e6ef52 94 $html = $this->fetchingErrorMessage;
558d9aab 95
e668a812 96 if (!empty($content['open_graph']['og_description'])) {
558d9aab
JB
97 $html .= '<p><i>But we found a short description: </i></p>';
98 $html .= $content['open_graph']['og_description'];
99 }
100 }
101
d0e9b3d6 102 $entry->setUrl($content['url']);
558d9aab 103 $entry->setTitle($title);
48656e0e 104 $entry->setContent($html);
10b35097 105 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
419214d7 106
e668a812 107 if (!empty($content['date'])) {
f0378b4d
JB
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
e668a812 115 try {
f0378b4d 116 $entry->setPublishedAt(new \DateTime($date));
e668a812 117 } catch (\Exception $e) {
9e349f08 118 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
e668a812 119 }
5e9009ce
NL
120 }
121
7b0b3622
NL
122 if (!empty($content['authors'])) {
123 $entry->setPublishedBy($content['authors']);
124 }
125
dda6a6ad
NL
126 if (!empty($content['all_headers'])) {
127 $entry->setHeaders($content['all_headers']);
128 }
129
e858018f
JC
130 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
131 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 132 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
133
134 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
135 if (false !== $domainName) {
136 $entry->setDomainName($domainName);
137 }
558d9aab 138
e668a812 139 if (!empty($content['open_graph']['og_image'])) {
558d9aab
JB
140 $entry->setPreviewPicture($content['open_graph']['og_image']);
141 }
142
8d7b4f0e 143 // if content is an image define as a preview too
e668a812 144 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
145 $entry->setPreviewPicture($content['url']);
146 }
147
1c9cd2a7
KG
148 try {
149 $this->tagger->tag($entry);
150 } catch (\Exception $e) {
4094ea47 151 $this->logger->error('Error while trying to automatically tag an entry.', [
d0e9b3d6 152 'entry_url' => $content['url'],
1c9cd2a7 153 'error_msg' => $e->getMessage(),
4094ea47 154 ]);
1c9cd2a7 155 }
558d9aab 156 }
c2656f96 157
4d0ec0e7 158 /**
d0e9b3d6 159 * Validate that the given content has at least a title, an html and a url.
4d0ec0e7
JB
160 *
161 * @param array $content
4d0ec0e7
JB
162 */
163 private function validateContent(array $content)
164 {
d0e9b3d6
JC
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 }
4d0ec0e7 176 }
558d9aab 177}