]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Rewrote code & fix tests
[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
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 try {
49 $fetchedContent = $this->graby->fetchContent($url);
50 } catch (\Exception $e) {
51 $this->logger->error('Error while trying to fetch content from URL.', [
52 'entry_url' => $url,
53 'error_msg' => $e->getMessage(),
54 ]);
55 }
56
57 // when content is imported, we have information in $content
58 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
59 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
60 $content = $fetchedContent;
61 }
62 }
63
64 // be sure to keep the url in case of error
65 // so we'll be able to refetch it in the future
66 $content['url'] = !empty($content['url']) ? $content['url'] : $url;
67
68 $this->stockEntry($entry, $content);
69 }
70
71 /**
72 * Stock entry with fetched or imported content.
73 * Will fall back to OpenGraph data if available.
74 *
75 * @param Entry $entry Entry to stock
76 * @param array $content Array with at least title and URL
77 */
78 private function stockEntry(Entry $entry, array $content)
79 {
80 $title = $content['title'];
81 if (!$title && !empty($content['open_graph']['og_title'])) {
82 $title = $content['open_graph']['og_title'];
83 }
84
85 $html = $content['html'];
86 if (false === $html) {
87 $html = $this->fetchingErrorMessage;
88
89 if (!empty($content['open_graph']['og_description'])) {
90 $html .= '<p><i>But we found a short description: </i></p>';
91 $html .= $content['open_graph']['og_description'];
92 }
93 }
94
95 $entry->setUrl($content['url']);
96 $entry->setTitle($title);
97 $entry->setContent($html);
98 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
99
100 if (!empty($content['date'])) {
101 $date = $content['date'];
102
103 // is it a timestamp?
104 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
105 $date = '@'.$content['date'];
106 }
107
108 try {
109 $entry->setPublishedAt(new \DateTime($date));
110 } catch (\Exception $e) {
111 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]);
112 }
113 }
114
115 if (!empty($content['authors'])) {
116 $entry->setPublishedBy($content['authors']);
117 }
118
119 if (!empty($content['all_headers'])) {
120 $entry->setHeaders($content['all_headers']);
121 }
122
123 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
124 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
125 $entry->setReadingTime(Utils::getReadingTime($html));
126
127 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
128 if (false !== $domainName) {
129 $entry->setDomainName($domainName);
130 }
131
132 if (!empty($content['open_graph']['og_image'])) {
133 $entry->setPreviewPicture($content['open_graph']['og_image']);
134 }
135
136 // if content is an image define as a preview too
137 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
138 $entry->setPreviewPicture($content['url']);
139 }
140
141 try {
142 $this->tagger->tag($entry);
143 } catch (\Exception $e) {
144 $this->logger->error('Error while trying to automatically tag an entry.', [
145 'entry_url' => $content['url'],
146 'error_msg' => $e->getMessage(),
147 ]);
148 }
149 }
150
151 /**
152 * Validate that the given content has at least a title, an html and a url.
153 *
154 * @param array $content
155 *
156 * @return bool true if valid otherwise false
157 */
158 private function validateContent(array $content)
159 {
160 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
161 }
162 }