]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | /** | |
13 | * This kind of proxy class take care of getting the content from an url | |
14 | * and update the entry with what it found. | |
15 | */ | |
16 | class ContentProxy | |
17 | { | |
18 | protected $graby; | |
19 | protected $tagger; | |
20 | protected $logger; | |
21 | protected $tagRepository; | |
22 | ||
23 | public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger) | |
24 | { | |
25 | $this->graby = $graby; | |
26 | $this->tagger = $tagger; | |
27 | $this->logger = $logger; | |
28 | $this->tagRepository = $tagRepository; | |
29 | } | |
30 | ||
31 | /** | |
32 | * Fetch content using graby and hydrate given entry with results information. | |
33 | * In case we couldn't find content, we'll try to use Open Graph data. | |
34 | * | |
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 | |
41 | * | |
42 | * @return Entry | |
43 | */ | |
44 | public function updateEntry(Entry $entry, $url, array $content = []) | |
45 | { | |
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 | } | |
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); | |
68 | $entry->setContent($html); | |
69 | ||
70 | $entry->setLanguage($content['language']); | |
71 | $entry->setMimetype($content['content_type']); | |
72 | $entry->setReadingTime(Utils::getReadingTime($html)); | |
73 | ||
74 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | |
75 | if (false !== $domainName) { | |
76 | $entry->setDomainName($domainName); | |
77 | } | |
78 | ||
79 | if (isset($content['open_graph']['og_image'])) { | |
80 | $entry->setPreviewPicture($content['open_graph']['og_image']); | |
81 | } | |
82 | ||
83 | try { | |
84 | $this->tagger->tag($entry); | |
85 | } catch (\Exception $e) { | |
86 | $this->logger->error('Error while trying to automatically tag an entry.', [ | |
87 | 'entry_url' => $url, | |
88 | 'error_msg' => $e->getMessage(), | |
89 | ]); | |
90 | } | |
91 | ||
92 | return $entry; | |
93 | } | |
94 | ||
95 | /** | |
96 | * Assign some tags to an entry. | |
97 | * | |
98 | * @param Entry $entry | |
99 | * @param array|string $tags An array of tag or a string coma separated of tag | |
100 | * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed | |
101 | * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 | |
102 | */ | |
103 | public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) | |
104 | { | |
105 | if (!is_array($tags)) { | |
106 | $tags = explode(',', $tags); | |
107 | } | |
108 | ||
109 | // keeps only Tag entity from the "not yet flushed entities" | |
110 | $tagsNotYetFlushed = []; | |
111 | foreach ($entitiesReady as $entity) { | |
112 | if ($entity instanceof Tag) { | |
113 | $tagsNotYetFlushed[$entity->getLabel()] = $entity; | |
114 | } | |
115 | } | |
116 | ||
117 | foreach ($tags as $label) { | |
118 | $label = trim($label); | |
119 | ||
120 | // avoid empty tag | |
121 | if (0 === strlen($label)) { | |
122 | continue; | |
123 | } | |
124 | ||
125 | if (isset($tagsNotYetFlushed[$label])) { | |
126 | $tagEntity = $tagsNotYetFlushed[$label]; | |
127 | } else { | |
128 | $tagEntity = $this->tagRepository->findOneByLabel($label); | |
129 | ||
130 | if (is_null($tagEntity)) { | |
131 | $tagEntity = new Tag(); | |
132 | $tagEntity->setLabel($label); | |
133 | } | |
134 | } | |
135 | ||
136 | // only add the tag on the entry if the relation doesn't exist | |
137 | if (false === $entry->getTags()->contains($tagEntity)) { | |
138 | $entry->addTag($tagEntity); | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
143 | /** | |
144 | * Validate that the given content as enough value to be used | |
145 | * instead of fetch the content from the url. | |
146 | * | |
147 | * @param array $content | |
148 | * | |
149 | * @return bool true if valid otherwise false | |
150 | */ | |
151 | private function validateContent(array $content) | |
152 | { | |
153 | return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']); | |
154 | } | |
155 | } |