]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
45fd7e09 | 6 | use Psr\Log\LoggerInterface; |
558d9aab | 7 | use Wallabag\CoreBundle\Entity\Entry; |
c2656f96 | 8 | use Wallabag\CoreBundle\Entity\Tag; |
da3d4998 | 9 | use Wallabag\CoreBundle\Tools\Utils; |
c2656f96 | 10 | use Wallabag\CoreBundle\Repository\TagRepository; |
8d7b4f0e | 11 | use 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 | */ |
17 | class ContentProxy | |
18 | { | |
19 | protected $graby; | |
c3510620 | 20 | protected $tagger; |
1c9cd2a7 | 21 | protected $logger; |
c2656f96 | 22 | protected $tagRepository; |
8d7b4f0e | 23 | protected $mimeGuesser; |
558d9aab | 24 | |
45fd7e09 | 25 | public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger) |
558d9aab | 26 | { |
347fa6be | 27 | $this->graby = $graby; |
c3510620 | 28 | $this->tagger = $tagger; |
1c9cd2a7 | 29 | $this->logger = $logger; |
c2656f96 | 30 | $this->tagRepository = $tagRepository; |
8d7b4f0e | 31 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); |
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)) { | |
51 | $content = $this->graby->fetchContent($url); | |
52 | } | |
558d9aab JB |
53 | |
54 | $title = $content['title']; | |
55 | if (!$title && isset($content['open_graph']['og_title'])) { | |
56 | $title = $content['open_graph']['og_title']; | |
57 | } | |
58 | ||
59 | $html = $content['html']; | |
60 | if (false === $html) { | |
61 | $html = '<p>Unable to retrieve readable content.</p>'; | |
62 | ||
63 | if (isset($content['open_graph']['og_description'])) { | |
64 | $html .= '<p><i>But we found a short description: </i></p>'; | |
65 | $html .= $content['open_graph']['og_description']; | |
66 | } | |
67 | } | |
68 | ||
69 | $entry->setUrl($content['url'] ?: $url); | |
70 | $entry->setTitle($title); | |
48656e0e | 71 | $entry->setContent($html); |
10b35097 | 72 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); |
419214d7 | 73 | |
98f0929f | 74 | $entry->setLanguage($content['language']); |
558d9aab | 75 | $entry->setMimetype($content['content_type']); |
da3d4998 | 76 | $entry->setReadingTime(Utils::getReadingTime($html)); |
4d0ec0e7 JB |
77 | |
78 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
79 | if (false !== $domainName) { | |
80 | $entry->setDomainName($domainName); | |
81 | } | |
558d9aab JB |
82 | |
83 | if (isset($content['open_graph']['og_image'])) { | |
84 | $entry->setPreviewPicture($content['open_graph']['og_image']); | |
85 | } | |
86 | ||
8d7b4f0e JB |
87 | // if content is an image define as a preview too |
88 | if (in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { | |
89 | $entry->setPreviewPicture($content['url']); | |
90 | } | |
91 | ||
1c9cd2a7 KG |
92 | try { |
93 | $this->tagger->tag($entry); | |
94 | } catch (\Exception $e) { | |
4094ea47 | 95 | $this->logger->error('Error while trying to automatically tag an entry.', [ |
1c9cd2a7 KG |
96 | 'entry_url' => $url, |
97 | 'error_msg' => $e->getMessage(), | |
4094ea47 | 98 | ]); |
1c9cd2a7 | 99 | } |
c3510620 | 100 | |
558d9aab JB |
101 | return $entry; |
102 | } | |
c2656f96 JB |
103 | |
104 | /** | |
105 | * Assign some tags to an entry. | |
106 | * | |
107 | * @param Entry $entry | |
82fc3290 JB |
108 | * @param array|string $tags An array of tag or a string coma separated of tag |
109 | * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed | |
110 | * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 | |
c2656f96 | 111 | */ |
40113585 | 112 | public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) |
c2656f96 JB |
113 | { |
114 | if (!is_array($tags)) { | |
115 | $tags = explode(',', $tags); | |
116 | } | |
117 | ||
40113585 JB |
118 | // keeps only Tag entity from the "not yet flushed entities" |
119 | $tagsNotYetFlushed = []; | |
120 | foreach ($entitiesReady as $entity) { | |
121 | if ($entity instanceof Tag) { | |
122 | $tagsNotYetFlushed[$entity->getLabel()] = $entity; | |
123 | } | |
124 | } | |
125 | ||
c2656f96 JB |
126 | foreach ($tags as $label) { |
127 | $label = trim($label); | |
128 | ||
129 | // avoid empty tag | |
130 | if (0 === strlen($label)) { | |
131 | continue; | |
132 | } | |
133 | ||
40113585 JB |
134 | if (isset($tagsNotYetFlushed[$label])) { |
135 | $tagEntity = $tagsNotYetFlushed[$label]; | |
136 | } else { | |
137 | $tagEntity = $this->tagRepository->findOneByLabel($label); | |
c2656f96 | 138 | |
40113585 JB |
139 | if (is_null($tagEntity)) { |
140 | $tagEntity = new Tag(); | |
141 | $tagEntity->setLabel($label); | |
142 | } | |
c2656f96 JB |
143 | } |
144 | ||
145 | // only add the tag on the entry if the relation doesn't exist | |
146 | if (false === $entry->getTags()->contains($tagEntity)) { | |
147 | $entry->addTag($tagEntity); | |
148 | } | |
149 | } | |
150 | } | |
4d0ec0e7 JB |
151 | |
152 | /** | |
153 | * Validate that the given content as enough value to be used | |
154 | * instead of fetch the content from the url. | |
155 | * | |
156 | * @param array $content | |
157 | * | |
158 | * @return bool true if valid otherwise false | |
159 | */ | |
160 | private function validateContent(array $content) | |
161 | { | |
162 | return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']); | |
163 | } | |
558d9aab | 164 | } |