]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Move Tags assigner to a separate file
[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;
6bc6fb1f 7use Symfony\Component\EventDispatcher\EventDispatcher;
558d9aab 8use Wallabag\CoreBundle\Entity\Entry;
da3d4998 9use Wallabag\CoreBundle\Tools\Utils;
8d7b4f0e 10use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
558d9aab
JB
11
12/**
13 * This kind of proxy class take care of getting the content from an url
f1e29e69 14 * and update the entry with what it found.
558d9aab
JB
15 */
16class ContentProxy
17{
18 protected $graby;
c3510620 19 protected $tagger;
1c9cd2a7 20 protected $logger;
8d7b4f0e 21 protected $mimeGuesser;
29dca432 22 protected $fetchingErrorMessage;
6bc6fb1f 23 protected $eventDispatcher;
558d9aab 24
6bc6fb1f 25 public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage)
558d9aab 26 {
347fa6be 27 $this->graby = $graby;
c3510620 28 $this->tagger = $tagger;
1c9cd2a7 29 $this->logger = $logger;
8d7b4f0e 30 $this->mimeGuesser = new MimeTypeExtensionGuesser();
29dca432 31 $this->fetchingErrorMessage = $fetchingErrorMessage;
558d9aab
JB
32 }
33
34 /**
35 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 36 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 37 *
4d0ec0e7
JB
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
558d9aab
JB
44 *
45 * @return Entry
46 */
4d0ec0e7 47 public function updateEntry(Entry $entry, $url, array $content = [])
558d9aab 48 {
4d0ec0e7
JB
49 // do we have to fetch the content or the provided one is ok?
50 if (empty($content) || false === $this->validateContent($content)) {
29dca432 51 $fetchedContent = $this->graby->fetchContent($url);
106bdbcd
JB
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
29dca432
JC
55 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
56 $content = $fetchedContent;
57 }
4d0ec0e7 58 }
558d9aab
JB
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) {
36e6ef52 67 $html = $this->fetchingErrorMessage;
558d9aab
JB
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);
48656e0e 77 $entry->setContent($html);
10b35097 78 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
419214d7 79
7b0b3622 80 if (isset($content['date']) && null !== $content['date'] && '' !== $content['date']) {
5e9009ce
NL
81 $entry->setPublishedAt(new \DateTime($content['date']));
82 }
83
7b0b3622
NL
84 if (!empty($content['authors'])) {
85 $entry->setPublishedBy($content['authors']);
86 }
87
dda6a6ad
NL
88 if (!empty($content['all_headers'])) {
89 $entry->setHeaders($content['all_headers']);
90 }
91
e858018f
JC
92 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
93 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 94 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
95
96 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
97 if (false !== $domainName) {
98 $entry->setDomainName($domainName);
99 }
558d9aab 100
3d71d403 101 if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) {
558d9aab
JB
102 $entry->setPreviewPicture($content['open_graph']['og_image']);
103 }
104
8d7b4f0e 105 // if content is an image define as a preview too
e858018f 106 if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
107 $entry->setPreviewPicture($content['url']);
108 }
109
1c9cd2a7
KG
110 try {
111 $this->tagger->tag($entry);
112 } catch (\Exception $e) {
4094ea47 113 $this->logger->error('Error while trying to automatically tag an entry.', [
1c9cd2a7
KG
114 'entry_url' => $url,
115 'error_msg' => $e->getMessage(),
4094ea47 116 ]);
1c9cd2a7 117 }
c3510620 118
558d9aab
JB
119 return $entry;
120 }
c2656f96 121
4d0ec0e7
JB
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 }
558d9aab 134}