]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Revert switch to KernelTestCase for ContentProxyTest
[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;
c2656f96 8use Wallabag\CoreBundle\Entity\Tag;
da3d4998 9use Wallabag\CoreBundle\Tools\Utils;
c2656f96 10use Wallabag\CoreBundle\Repository\TagRepository;
8d7b4f0e 11use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser;
558d9aab
JB
12
13/**
14 * This kind of proxy class take care of getting the content from an url
f1e29e69 15 * and update the entry with what it found.
558d9aab
JB
16 */
17class ContentProxy
18{
19 protected $graby;
c3510620 20 protected $tagger;
1c9cd2a7 21 protected $logger;
c2656f96 22 protected $tagRepository;
8d7b4f0e 23 protected $mimeGuesser;
29dca432 24 protected $fetchingErrorMessage;
558d9aab 25
29dca432 26 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage)
558d9aab 27 {
347fa6be 28 $this->graby = $graby;
c3510620 29 $this->tagger = $tagger;
1c9cd2a7 30 $this->logger = $logger;
c2656f96 31 $this->tagRepository = $tagRepository;
8d7b4f0e 32 $this->mimeGuesser = new MimeTypeExtensionGuesser();
29dca432 33 $this->fetchingErrorMessage = $fetchingErrorMessage;
558d9aab
JB
34 }
35
36 /**
37 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 38 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 39 *
4d0ec0e7
JB
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
558d9aab
JB
46 *
47 * @return Entry
48 */
4d0ec0e7 49 public function updateEntry(Entry $entry, $url, array $content = [])
558d9aab 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
JC
53 $fetchedContent = $this->graby->fetchContent($url);
54 if (empty($content) || $fetchedContent['html'] !== $this->fetchingErrorMessage) {
55 $content = $fetchedContent;
56 }
4d0ec0e7 57 }
558d9aab
JB
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) {
36e6ef52 66 $html = $this->fetchingErrorMessage;
558d9aab
JB
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);
48656e0e 76 $entry->setContent($html);
10b35097 77 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : '');
419214d7 78
e858018f
JC
79 $entry->setLanguage(isset($content['language']) ? $content['language'] : '');
80 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : '');
da3d4998 81 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
82
83 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
84 if (false !== $domainName) {
85 $entry->setDomainName($domainName);
86 }
558d9aab
JB
87
88 if (isset($content['open_graph']['og_image'])) {
89 $entry->setPreviewPicture($content['open_graph']['og_image']);
90 }
91
8d7b4f0e 92 // if content is an image define as a preview too
e858018f 93 if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
8d7b4f0e
JB
94 $entry->setPreviewPicture($content['url']);
95 }
96
1c9cd2a7
KG
97 try {
98 $this->tagger->tag($entry);
99 } catch (\Exception $e) {
4094ea47 100 $this->logger->error('Error while trying to automatically tag an entry.', [
1c9cd2a7
KG
101 'entry_url' => $url,
102 'error_msg' => $e->getMessage(),
4094ea47 103 ]);
1c9cd2a7 104 }
c3510620 105
558d9aab
JB
106 return $entry;
107 }
c2656f96
JB
108
109 /**
110 * Assign some tags to an entry.
111 *
112 * @param Entry $entry
82fc3290
JB
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
c2656f96 116 */
40113585 117 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
c2656f96
JB
118 {
119 if (!is_array($tags)) {
120 $tags = explode(',', $tags);
121 }
122
40113585
JB
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
c2656f96
JB
131 foreach ($tags as $label) {
132 $label = trim($label);
133
134 // avoid empty tag
135 if (0 === strlen($label)) {
136 continue;
137 }
138
40113585
JB
139 if (isset($tagsNotYetFlushed[$label])) {
140 $tagEntity = $tagsNotYetFlushed[$label];
141 } else {
142 $tagEntity = $this->tagRepository->findOneByLabel($label);
c2656f96 143
40113585
JB
144 if (is_null($tagEntity)) {
145 $tagEntity = new Tag();
146 $tagEntity->setLabel($label);
147 }
c2656f96
JB
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 }
4d0ec0e7
JB
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 }
558d9aab 169}