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