]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Rewrote code & fix tests
[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;
558d9aab
JB
10
11/**
12 * This kind of proxy class take care of getting the content from an url
f1e29e69 13 * and update the entry with what it found.
558d9aab
JB
14 */
15class ContentProxy
16{
17 protected $graby;
c3510620 18 protected $tagger;
1c9cd2a7 19 protected $logger;
8d7b4f0e 20 protected $mimeGuesser;
29dca432 21 protected $fetchingErrorMessage;
6bc6fb1f 22 protected $eventDispatcher;
558d9aab 23
6bc6fb1f 24 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
558d9aab 25 {
347fa6be 26 $this->graby = $graby;
c3510620 27 $this->tagger = $tagger;
1c9cd2a7 28 $this->logger = $logger;
8d7b4f0e 29 $this->mimeGuesser = new MimeTypeExtensionGuesser();
29dca432 30 $this->fetchingErrorMessage = $fetchingErrorMessage;
558d9aab
JB
31 }
32
33 /**
6acadf8e 34 * Update entry using either fetched or provided content.
4d0ec0e7 35 *
6acadf8e
JB
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
558d9aab 40 */
6acadf8e 41 public function updateEntry(Entry $entry, $url, array $content = [], $disableContentUpdate = false)
558d9aab 42 {
6acadf8e
JB
43 if (!empty($content['html'])) {
44 $content['html'] = $this->graby->cleanupHtml($content['html'], $url);
d5c2cc54 45 }
e668a812 46
6acadf8e 47 if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) {
d0e9b3d6 48 try {
6acadf8e 49 $fetchedContent = $this->graby->fetchContent($url);
d0e9b3d6
JC
50 } catch (\Exception $e) {
51 $this->logger->error('Error while trying to fetch content from URL.', [
6acadf8e 52 'entry_url' => $url,
d0e9b3d6
JC
53 'error_msg' => $e->getMessage(),
54 ]);
55 }
106bdbcd
JB
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
6acadf8e 59 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
29dca432
JC
60 $content = $fetchedContent;
61 }
4d0ec0e7 62 }
558d9aab 63
6acadf8e
JB
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
d0e9b3d6
JC
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 *
d5c2cc54
JB
75 * @param Entry $entry Entry to stock
76 * @param array $content Array with at least title and URL
d0e9b3d6
JC
77 */
78 private function stockEntry(Entry $entry, array $content)
79 {
558d9aab 80 $title = $content['title'];
e668a812 81 if (!$title && !empty($content['open_graph']['og_title'])) {
558d9aab
JB
82 $title = $content['open_graph']['og_title'];
83 }
84
85 $html = $content['html'];
86 if (false === $html) {
36e6ef52 87 $html = $this->fetchingErrorMessage;
558d9aab 88
e668a812 89 if (!empty($content['open_graph']['og_description'])) {
558d9aab
JB
90 $html .= '<p><i>But we found a short description: </i></p>';
91 $html .= $content['open_graph']['og_description'];
92 }
93 }
94
d0e9b3d6 95 $entry->setUrl($content['url']);
558d9aab 96 $entry->setTitle($title);
48656e0e 97 $entry->setContent($html);
10b35097 98 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
419214d7 99
e668a812 100 if (!empty($content['date'])) {
f0378b4d
JB
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
e668a812 108 try {
f0378b4d 109 $entry->setPublishedAt(new \DateTime($date));
e668a812 110 } catch (\Exception $e) {
6acadf8e 111 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]);
e668a812 112 }
5e9009ce
NL
113 }
114
7b0b3622
NL
115 if (!empty($content['authors'])) {
116 $entry->setPublishedBy($content['authors']);
117 }
118
dda6a6ad
NL
119 if (!empty($content['all_headers'])) {
120 $entry->setHeaders($content['all_headers']);
121 }
122
e858018f
JC
123 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
124 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 125 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
126
127 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
128 if (false !== $domainName) {
129 $entry->setDomainName($domainName);
130 }
558d9aab 131
e668a812 132 if (!empty($content['open_graph']['og_image'])) {
558d9aab
JB
133 $entry->setPreviewPicture($content['open_graph']['og_image']);
134 }
135
8d7b4f0e 136 // if content is an image define as a preview too
e668a812 137 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
138 $entry->setPreviewPicture($content['url']);
139 }
140
1c9cd2a7
KG
141 try {
142 $this->tagger->tag($entry);
143 } catch (\Exception $e) {
4094ea47 144 $this->logger->error('Error while trying to automatically tag an entry.', [
d0e9b3d6 145 'entry_url' => $content['url'],
1c9cd2a7 146 'error_msg' => $e->getMessage(),
4094ea47 147 ]);
1c9cd2a7 148 }
558d9aab 149 }
c2656f96 150
4d0ec0e7 151 /**
d0e9b3d6 152 * Validate that the given content has at least a title, an html and a url.
4d0ec0e7
JB
153 *
154 * @param array $content
6acadf8e
JB
155 *
156 * @return bool true if valid otherwise false
4d0ec0e7
JB
157 */
158 private function validateContent(array $content)
159 {
6acadf8e 160 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
4d0ec0e7 161 }
558d9aab 162}