]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Move related event things in Event folder
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
CommitLineData
558d9aab
JB
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Graby;
1c9cd2a7 6use Psr\Log\LoggerInterface as Logger;
558d9aab 7use Wallabag\CoreBundle\Entity\Entry;
c2656f96 8use Wallabag\CoreBundle\Entity\Tag;
da3d4998 9use Wallabag\CoreBundle\Tools\Utils;
c2656f96 10use Wallabag\CoreBundle\Repository\TagRepository;
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;
c2656f96 21 protected $tagRepository;
558d9aab 22
c2656f96 23 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger)
558d9aab 24 {
347fa6be 25 $this->graby = $graby;
c3510620 26 $this->tagger = $tagger;
1c9cd2a7 27 $this->logger = $logger;
c2656f96 28 $this->tagRepository = $tagRepository;
558d9aab
JB
29 }
30
31 /**
32 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 33 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 34 *
4d0ec0e7
JB
35 * We can also force the content, in case of an import from the v1 for example, so the function won't
36 * fetch the content from the website but rather use information given with the $content parameter.
37 *
38 * @param Entry $entry Entry to update
39 * @param string $url Url to grab content for
40 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
558d9aab
JB
41 *
42 * @return Entry
43 */
4d0ec0e7 44 public function updateEntry(Entry $entry, $url, array $content = [])
558d9aab 45 {
4d0ec0e7
JB
46 // do we have to fetch the content or the provided one is ok?
47 if (empty($content) || false === $this->validateContent($content)) {
48 $content = $this->graby->fetchContent($url);
49 }
558d9aab
JB
50
51 $title = $content['title'];
52 if (!$title && isset($content['open_graph']['og_title'])) {
53 $title = $content['open_graph']['og_title'];
54 }
55
56 $html = $content['html'];
57 if (false === $html) {
58 $html = '<p>Unable to retrieve readable content.</p>';
59
60 if (isset($content['open_graph']['og_description'])) {
61 $html .= '<p><i>But we found a short description: </i></p>';
62 $html .= $content['open_graph']['og_description'];
63 }
64 }
65
66 $entry->setUrl($content['url'] ?: $url);
67 $entry->setTitle($title);
419214d7 68
98f0929f 69 $entry->setLanguage($content['language']);
558d9aab 70 $entry->setMimetype($content['content_type']);
da3d4998 71 $entry->setReadingTime(Utils::getReadingTime($html));
4d0ec0e7
JB
72
73 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
74 if (false !== $domainName) {
75 $entry->setDomainName($domainName);
76 }
558d9aab 77
419214d7 78 if (true) {
156bf627 79 $this->logger->log('debug', 'Starting to download images');
419214d7
TC
80 $downloadImages = new DownloadImages($html, $url, $this->logger);
81 $html = $downloadImages->process();
82 }
83
84 $entry->setContent($html);
85
558d9aab
JB
86 if (isset($content['open_graph']['og_image'])) {
87 $entry->setPreviewPicture($content['open_graph']['og_image']);
88 }
89
1c9cd2a7
KG
90 try {
91 $this->tagger->tag($entry);
92 } catch (\Exception $e) {
4094ea47 93 $this->logger->error('Error while trying to automatically tag an entry.', [
1c9cd2a7
KG
94 'entry_url' => $url,
95 'error_msg' => $e->getMessage(),
4094ea47 96 ]);
1c9cd2a7 97 }
c3510620 98
558d9aab
JB
99 return $entry;
100 }
c2656f96
JB
101
102 /**
103 * Assign some tags to an entry.
104 *
105 * @param Entry $entry
82fc3290
JB
106 * @param array|string $tags An array of tag or a string coma separated of tag
107 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
108 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
c2656f96 109 */
40113585 110 public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = [])
c2656f96
JB
111 {
112 if (!is_array($tags)) {
113 $tags = explode(',', $tags);
114 }
115
40113585
JB
116 // keeps only Tag entity from the "not yet flushed entities"
117 $tagsNotYetFlushed = [];
118 foreach ($entitiesReady as $entity) {
119 if ($entity instanceof Tag) {
120 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
121 }
122 }
123
c2656f96
JB
124 foreach ($tags as $label) {
125 $label = trim($label);
126
127 // avoid empty tag
128 if (0 === strlen($label)) {
129 continue;
130 }
131
40113585
JB
132 if (isset($tagsNotYetFlushed[$label])) {
133 $tagEntity = $tagsNotYetFlushed[$label];
134 } else {
135 $tagEntity = $this->tagRepository->findOneByLabel($label);
c2656f96 136
40113585
JB
137 if (is_null($tagEntity)) {
138 $tagEntity = new Tag();
139 $tagEntity->setLabel($label);
140 }
c2656f96
JB
141 }
142
143 // only add the tag on the entry if the relation doesn't exist
144 if (false === $entry->getTags()->contains($tagEntity)) {
145 $entry->addTag($tagEntity);
146 }
147 }
148 }
4d0ec0e7
JB
149
150 /**
151 * Validate that the given content as enough value to be used
152 * instead of fetch the content from the url.
153 *
154 * @param array $content
155 *
156 * @return bool true if valid otherwise false
157 */
158 private function validateContent(array $content)
159 {
160 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);
161 }
558d9aab 162}