]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Move Tags assigner to a separate file
[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 Symfony\Component\EventDispatcher\EventDispatcher;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\CoreBundle\Tools\Utils;
10 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
11
12 /**
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
15 */
16 class ContentProxy
17 {
18 protected $graby;
19 protected $tagger;
20 protected $logger;
21 protected $mimeGuesser;
22 protected $fetchingErrorMessage;
23 protected $eventDispatcher;
24
25 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
26 {
27 $this->graby = $graby;
28 $this->tagger = $tagger;
29 $this->logger = $logger;
30 $this->mimeGuesser = new MimeTypeExtensionGuesser();
31 $this->fetchingErrorMessage = $fetchingErrorMessage;
32 }
33
34 /**
35 * Fetch content using graby and hydrate given entry with results information.
36 * In case we couldn't find content, we'll try to use Open Graph data.
37 *
38 * We can also force the content, in case of an import from the v1 for example, so the function won't
39 * fetch the content from the website but rather use information given with the $content parameter.
40 *
41 * @param Entry $entry Entry to update
42 * @param string $url Url to grab content for
43 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
44 *
45 * @return Entry
46 */
47 public function updateEntry(Entry $entry, $url, array $content = [])
48 {
49 // do we have to fetch the content or the provided one is ok?
50 if (empty($content) || false === $this->validateContent($content)) {
51 $fetchedContent = $this->graby->fetchContent($url);
52
53 // when content is imported, we have information in $content
54 // in case fetching content goes bad, we'll keep the imported information instead of overriding them
55 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
56 $content = $fetchedContent;
57 }
58 }
59
60 $title = $content['title'];
61 if (!$title && isset($content['open_graph']['og_title'])) {
62 $title = $content['open_graph']['og_title'];
63 }
64
65 $html = $content['html'];
66 if (false === $html) {
67 $html = $this->fetchingErrorMessage;
68
69 if (isset($content['open_graph']['og_description'])) {
70 $html .= '<p><i>But we found a short description: </i></p>';
71 $html .= $content['open_graph']['og_description'];
72 }
73 }
74
75 $entry->setUrl($content['url'] ?: $url);
76 $entry->setTitle($title);
77 $entry->setContent($html);
78 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
79
80 if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) {
81 $entry->setPublishedAt(new \DateTime($content['date']));
82 }
83
84 if (!empty($content['authors'])) {
85 $entry->setPublishedBy($content['authors']);
86 }
87
88 if (!empty($content['all_headers'])) {
89 $entry->setHeaders($content['all_headers']);
90 }
91
92 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
93 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
94 $entry->setReadingTime(Utils::getReadingTime($html));
95
96 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
97 if (false !== $domainName) {
98 $entry->setDomainName($domainName);
99 }
100
101 if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) {
102 $entry->setPreviewPicture($content['open_graph']['og_image']);
103 }
104
105 // if content is an image define as a preview too
106 if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
107 $entry->setPreviewPicture($content['url']);
108 }
109
110 try {
111 $this->tagger->tag($entry);
112 } catch (\Exception $e) {
113 $this->logger->error('Error while trying to automatically tag an entry.', [
114 'entry_url' => $url,
115 'error_msg' => $e->getMessage(),
116 ]);
117 }
118
119 return $entry;
120 }
121
122 /**
123 * Validate that the given content as enough value to be used
124 * instead of fetch the content from the url.
125 *
126 * @param array $content
127 *
128 * @return bool true if valid otherwise false
129 */
130 private function validateContent(array $content)
131 {
132 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
133 }
134 }