]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Imported entries which fail to fetch get standard error body
[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 Wallabag\CoreBundle\Entity\Entry;
8 use Wallabag\CoreBundle\Entity\Tag;
9 use Wallabag\CoreBundle\Tools\Utils;
10 use Wallabag\CoreBundle\Repository\TagRepository;
11 use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
12
13 /**
14 * This kind of proxy class take care of getting the content from an url
15 * and update the entry with what it found.
16 */
17 class ContentProxy
18 {
19 protected $graby;
20 protected $tagger;
21 protected $logger;
22 protected $tagRepository;
23 protected $mimeGuesser;
24 protected $fetchingErrorMessage;
25
26 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage)
27 {
28 $this->graby = $graby;
29 $this->tagger = $tagger;
30 $this->logger = $logger;
31 $this->tagRepository = $tagRepository;
32 $this->mimeGuesser = new MimeTypeExtensionGuesser();
33 $this->fetchingErrorMessage = $fetchingErrorMessage;
34 }
35
36 /**
37 * Fetch content using graby and hydrate given entry with results information.
38 * In case we couldn't find content, we'll try to use Open Graph data.
39 *
40 * We can also force the content, in case of an import from the v1 for example, so the function won't
41 * fetch the content from the website but rather use information given with the $content parameter.
42 *
43 * @param Entry $entry Entry to update
44 * @param string $url Url to grab content for
45 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
46 *
47 * @return Entry
48 */
49 public function updateEntry(Entry $entry, $url, array $content = [])
50 {
51 // do we have to fetch the content or the provided one is ok?
52 if (empty($content) || false === $this->validateContent($content)) {
53 $fetchedContent = $this->graby->fetchContent($url);
54 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
55 $content = $fetchedContent;
56 }
57 }
58
59 $title = $content['title'];
60 if (!$title && isset($content['open_graph']['og_title'])) {
61 $title = $content['open_graph']['og_title'];
62 }
63
64 $html = $content['html'];
65 if (false === $html) {
66 $html = $this->fetchingErrorMessage;
67
68 if (isset($content['open_graph']['og_description'])) {
69 $html .= '<p><i>But we found a short description: </i></p>';
70 $html .= $content['open_graph']['og_description'];
71 }
72 }
73
74 $entry->setUrl($content['url'] ?: $url);
75 $entry->setTitle($title);
76 $entry->setContent($html);
77 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
78
79 $entry->setLanguage($content['language']);
80 $entry->setMimetype($content['content_type']);
81 $entry->setReadingTime(Utils::getReadingTime($html));
82
83 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
84 if (false !== $domainName) {
85 $entry->setDomainName($domainName);
86 }
87
88 if (isset($content['open_graph']['og_image'])) {
89 $entry->setPreviewPicture($content['open_graph']['og_image']);
90 }
91
92 // if content is an image define as a preview too
93 if (in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
94 $entry->setPreviewPicture($content['url']);
95 }
96
97 try {
98 $this->tagger->tag($entry);
99 } catch (\Exception $e) {
100 $this->logger->error('Error while trying to automatically tag an entry.', [
101 'entry_url' => $url,
102 'error_msg' => $e->getMessage(),
103 ]);
104 }
105
106 return $entry;
107 }
108
109 /**
110 * Assign some tags to an entry.
111 *
112 * @param Entry $entry
113 * @param array|string $tags An array of tag or a string coma separated of tag
114 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
115 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
116 */
117 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
118 {
119 if (!is_array($tags)) {
120 $tags = explode(',', $tags);
121 }
122
123 // keeps only Tag entity from the "not yet flushed entities"
124 $tagsNotYetFlushed = [];
125 foreach ($entitiesReady as $entity) {
126 if ($entity instanceof Tag) {
127 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
128 }
129 }
130
131 foreach ($tags as $label) {
132 $label = trim($label);
133
134 // avoid empty tag
135 if (0 === strlen($label)) {
136 continue;
137 }
138
139 if (isset($tagsNotYetFlushed[$label])) {
140 $tagEntity = $tagsNotYetFlushed[$label];
141 } else {
142 $tagEntity = $this->tagRepository->findOneByLabel($label);
143
144 if (is_null($tagEntity)) {
145 $tagEntity = new Tag();
146 $tagEntity->setLabel($label);
147 }
148 }
149
150 // only add the tag on the entry if the relation doesn't exist
151 if (false === $entry->getTags()->contains($tagEntity)) {
152 $entry->addTag($tagEntity);
153 }
154 }
155 }
156
157 /**
158 * Validate that the given content as enough value to be used
159 * instead of fetch the content from the url.
160 *
161 * @param array $content
162 *
163 * @return bool true if valid otherwise false
164 */
165 private function validateContent(array $content)
166 {
167 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
168 }
169 }