]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
8ba77ca921341e60550fb17588980c307a541253
[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 * Fetch content using graby and hydrate given $entry with results information.
35 * In case we couldn't find content, we'll try to use Open Graph data.
36 *
37 * We can also force the content, in case of an import from the v1 for example, so the function won't
38 * fetch the content from the website but rather use information given with the $content parameter.
39 *
40 * @param Entry $entry Entry to update
41 * @param string $url Url to grab content for
42 * @param array $content An array with AT LEAST keys title, html, url to skip the fetchContent from the url
43 *
44 * @return Entry
45 */
46 public function updateEntry(Entry $entry, $url, array $content = [])
47 {
48 // ensure content is a bit cleaned up
49 if (!empty($content['html'])) {
50 $content['html'] = $this->graby->cleanupHtml($content['html'], $url);
51 }
52
53 // do we have to fetch the content or the provided one is ok?
54 if (empty($content) || false === $this->validateContent($content)) {
55 $fetchedContent = $this->graby->fetchContent($url);
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 $title = $content['title'];
65 if (!$title && !empty($content['open_graph']['og_title'])) {
66 $title = $content['open_graph']['og_title'];
67 }
68
69 $html = $content['html'];
70 if (false === $html) {
71 $html = $this->fetchingErrorMessage;
72
73 if (!empty($content['open_graph']['og_description'])) {
74 $html .= '<p><i>But we found a short description: </i></p>';
75 $html .= $content['open_graph']['og_description'];
76 }
77 }
78
79 $entry->setUrl($content['url'] ?: $url);
80 $entry->setTitle($title);
81 $entry->setContent($html);
82 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
83
84 if (!empty($content['date'])) {
85 $date = $content['date'];
86
87 // is it a timestamp?
88 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
89 $date = '@'.$content['date'];
90 }
91
92 try {
93 $entry->setPublishedAt(new \DateTime($date));
94 } catch (\Exception $e) {
95 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
96 }
97 }
98
99 if (!empty($content['authors'])) {
100 $entry->setPublishedBy($content['authors']);
101 }
102
103 if (!empty($content['all_headers'])) {
104 $entry->setHeaders($content['all_headers']);
105 }
106
107 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
108 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
109 $entry->setReadingTime(Utils::getReadingTime($html));
110
111 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
112 if (false !== $domainName) {
113 $entry->setDomainName($domainName);
114 }
115
116 if (!empty($content['open_graph']['og_image'])) {
117 $entry->setPreviewPicture($content['open_graph']['og_image']);
118 }
119
120 // if content is an image define as a preview too
121 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
122 $entry->setPreviewPicture($content['url']);
123 }
124
125 try {
126 $this->tagger->tag($entry);
127 } catch (\Exception $e) {
128 $this->logger->error('Error while trying to automatically tag an entry.', [
129 'entry_url' => $url,
130 'error_msg' => $e->getMessage(),
131 ]);
132 }
133
134 return $entry;
135 }
136
137 /**
138 * Validate that the given content as enough value to be used
139 * instead of fetch the content from the url.
140 *
141 * @param array $content
142 *
143 * @return bool true if valid otherwise false
144 */
145 private function validateContent(array $content)
146 {
147 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
148 }
149 }