]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Put default fetching error title in global config
[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 /**
9e349f08 34 * Fetch content using graby and hydrate given $entry with results information.
f1e29e69 35 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 36 *
4d0ec0e7
JB
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
9e349f08 42 * @param array $content An array with AT LEAST keys title, html, url to skip the fetchContent from the url
558d9aab 43 */
4d0ec0e7 44 public function updateEntry(Entry $entry, $url, array $content = [])
558d9aab 45 {
e668a812
JB
46 // ensure content is a bit cleaned up
47 if (!empty($content['html'])) {
0d6cfb88 48 $content['html'] = $this->graby->cleanupHtml($content['html'], $url);
e668a812
JB
49 }
50
4d0ec0e7
JB
51 // do we have to fetch the content or the provided one is ok?
52 if (empty($content) || false === $this->validateContent($content)) {
29dca432 53 $fetchedContent = $this->graby->fetchContent($url);
106bdbcd
JB
54
55 // when content is imported, we have information in $content
56 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
29dca432
JC
57 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
58 $content = $fetchedContent;
59 }
4d0ec0e7 60 }
558d9aab
JB
61
62 $title = $content['title'];
e668a812 63 if (!$title && !empty($content['open_graph']['og_title'])) {
558d9aab
JB
64 $title = $content['open_graph']['og_title'];
65 }
66
67 $html = $content['html'];
68 if (false === $html) {
36e6ef52 69 $html = $this->fetchingErrorMessage;
558d9aab 70
e668a812 71 if (!empty($content['open_graph']['og_description'])) {
558d9aab
JB
72 $html .= '<p><i>But we found a short description: </i></p>';
73 $html .= $content['open_graph']['og_description'];
74 }
75 }
76
77 $entry->setUrl($content['url'] ?: $url);
78 $entry->setTitle($title);
48656e0e 79 $entry->setContent($html);
10b35097 80 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
419214d7 81
e668a812 82 if (!empty($content['date'])) {
f0378b4d
JB
83 $date = $content['date'];
84
85 // is it a timestamp?
86 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
87 $date = '@'.$content['date'];
88 }
89
e668a812 90 try {
f0378b4d 91 $entry->setPublishedAt(new \DateTime($date));
e668a812 92 } catch (\Exception $e) {
9e349f08 93 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $url, 'date' => $content['date']]);
e668a812 94 }
5e9009ce
NL
95 }
96
7b0b3622
NL
97 if (!empty($content['authors'])) {
98 $entry->setPublishedBy($content['authors']);
99 }
100
dda6a6ad
NL
101 if (!empty($content['all_headers'])) {
102 $entry->setHeaders($content['all_headers']);
103 }
104
e858018f
JC
105 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
106 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 107 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
108
109 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
110 if (false !== $domainName) {
111 $entry->setDomainName($domainName);
112 }
558d9aab 113
e668a812 114 if (!empty($content['open_graph']['og_image'])) {
558d9aab
JB
115 $entry->setPreviewPicture($content['open_graph']['og_image']);
116 }
117
8d7b4f0e 118 // if content is an image define as a preview too
e668a812 119 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
120 $entry->setPreviewPicture($content['url']);
121 }
122
1c9cd2a7
KG
123 try {
124 $this->tagger->tag($entry);
125 } catch (\Exception $e) {
4094ea47 126 $this->logger->error('Error while trying to automatically tag an entry.', [
1c9cd2a7
KG
127 'entry_url' => $url,
128 'error_msg' => $e->getMessage(),
4094ea47 129 ]);
1c9cd2a7 130 }
558d9aab 131 }
c2656f96 132
4d0ec0e7
JB
133 /**
134 * Validate that the given content as enough value to be used
135 * instead of fetch the content from the url.
136 *
137 * @param array $content
138 *
139 * @return bool true if valid otherwise false
140 */
141 private function validateContent(array $content)
142 {
e668a812 143 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
4d0ec0e7 144 }
558d9aab 145}