]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
5 | use Psr\Log\LoggerInterface; | |
6 | use Psr\Log\NullLogger; | |
7 | use Doctrine\ORM\EntityManager; | |
8 | use Wallabag\CoreBundle\Helper\ContentProxy; | |
9 | use Wallabag\CoreBundle\Entity\Entry; | |
10 | use Wallabag\CoreBundle\Entity\Tag; | |
11 | use Wallabag\CoreBundle\Helper\TagsAssigner; | |
12 | use Wallabag\UserBundle\Entity\User; | |
13 | use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface; | |
14 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
15 | use Wallabag\CoreBundle\Event\EntrySavedEvent; | |
16 | ||
17 | abstract class AbstractImport implements ImportInterface | |
18 | { | |
19 | protected $em; | |
20 | protected $logger; | |
21 | protected $contentProxy; | |
22 | protected $tagsAssigner; | |
23 | protected $eventDispatcher; | |
24 | protected $producer; | |
25 | protected $user; | |
26 | protected $markAsRead; | |
27 | protected $disableContentUpdate = false; | |
28 | protected $skippedEntries = 0; | |
29 | protected $importedEntries = 0; | |
30 | protected $queuedEntries = 0; | |
31 | ||
32 | public function __construct(EntityManager $em, ContentProxy $contentProxy, TagsAssigner $tagsAssigner, EventDispatcherInterface $eventDispatcher) | |
33 | { | |
34 | $this->em = $em; | |
35 | $this->logger = new NullLogger(); | |
36 | $this->contentProxy = $contentProxy; | |
37 | $this->tagsAssigner = $tagsAssigner; | |
38 | $this->eventDispatcher = $eventDispatcher; | |
39 | } | |
40 | ||
41 | public function setLogger(LoggerInterface $logger) | |
42 | { | |
43 | $this->logger = $logger; | |
44 | } | |
45 | ||
46 | /** | |
47 | * Set RabbitMQ/Redis Producer to send each entry to a queue. | |
48 | * This method should be called when user has enabled RabbitMQ. | |
49 | * | |
50 | * @param ProducerInterface $producer | |
51 | */ | |
52 | public function setProducer(ProducerInterface $producer) | |
53 | { | |
54 | $this->producer = $producer; | |
55 | } | |
56 | ||
57 | /** | |
58 | * Set current user. | |
59 | * Could the current *connected* user or one retrieve by the consumer. | |
60 | * | |
61 | * @param User $user | |
62 | */ | |
63 | public function setUser(User $user) | |
64 | { | |
65 | $this->user = $user; | |
66 | } | |
67 | ||
68 | /** | |
69 | * Set whether articles must be all marked as read. | |
70 | * | |
71 | * @param bool $markAsRead | |
72 | */ | |
73 | public function setMarkAsRead($markAsRead) | |
74 | { | |
75 | $this->markAsRead = $markAsRead; | |
76 | ||
77 | return $this; | |
78 | } | |
79 | ||
80 | /** | |
81 | * Get whether articles must be all marked as read. | |
82 | */ | |
83 | public function getMarkAsRead() | |
84 | { | |
85 | return $this->markAsRead; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Set whether articles should be fetched for updated content. | |
90 | * | |
91 | * @param bool $disableContentUpdate | |
92 | */ | |
93 | public function setDisableContentUpdate($disableContentUpdate) | |
94 | { | |
95 | $this->disableContentUpdate = $disableContentUpdate; | |
96 | ||
97 | return $this; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Fetch content from the ContentProxy (using graby). | |
102 | * If it fails return the given entry to be saved in all case (to avoid user to loose the content). | |
103 | * | |
104 | * @param Entry $entry Entry to update | |
105 | * @param string $url Url to grab content for | |
106 | * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url | |
107 | */ | |
108 | protected function fetchContent(Entry $entry, $url, array $content = []) | |
109 | { | |
110 | try { | |
111 | $this->contentProxy->updateEntry($entry, $url, $content, $this->disableContentUpdate); | |
112 | } catch (\Exception $e) { | |
113 | $this->logger->error('Error trying to import an entry.', [ | |
114 | 'entry_url' => $url, | |
115 | 'error_msg' => $e->getMessage(), | |
116 | ]); | |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
121 | * Parse and insert all given entries. | |
122 | * | |
123 | * @param $entries | |
124 | */ | |
125 | protected function parseEntries($entries) | |
126 | { | |
127 | $i = 1; | |
128 | $entryToBeFlushed = []; | |
129 | ||
130 | foreach ($entries as $importedEntry) { | |
131 | if ($this->markAsRead) { | |
132 | $importedEntry = $this->setEntryAsRead($importedEntry); | |
133 | } | |
134 | ||
135 | $entry = $this->parseEntry($importedEntry); | |
136 | ||
137 | if (null === $entry) { | |
138 | continue; | |
139 | } | |
140 | ||
141 | // store each entry to be flushed so we can trigger the entry.saved event for each of them | |
142 | // entry.saved needs the entry to be persisted in db because it needs it id to generate | |
143 | // images (at least) | |
144 | $entryToBeFlushed[] = $entry; | |
145 | ||
146 | // flush every 20 entries | |
147 | if (($i % 20) === 0) { | |
148 | $this->em->flush(); | |
149 | ||
150 | foreach ($entryToBeFlushed as $entry) { | |
151 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
152 | } | |
153 | ||
154 | $entryToBeFlushed = []; | |
155 | ||
156 | // clear only affected entities | |
157 | $this->em->clear(Entry::class); | |
158 | $this->em->clear(Tag::class); | |
159 | } | |
160 | ++$i; | |
161 | } | |
162 | ||
163 | $this->em->flush(); | |
164 | ||
165 | if (!empty($entryToBeFlushed)) { | |
166 | foreach ($entryToBeFlushed as $entry) { | |
167 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
172 | /** | |
173 | * Parse entries and send them to the queue. | |
174 | * It should just be a simple loop on all item, no call to the database should be done | |
175 | * to speedup queuing. | |
176 | * | |
177 | * Faster parse entries for Producer. | |
178 | * We don't care to make check at this time. They'll be done by the consumer. | |
179 | * | |
180 | * @param array $entries | |
181 | */ | |
182 | protected function parseEntriesForProducer(array $entries) | |
183 | { | |
184 | foreach ($entries as $importedEntry) { | |
185 | // set userId for the producer (it won't know which user is connected) | |
186 | $importedEntry['userId'] = $this->user->getId(); | |
187 | ||
188 | if ($this->markAsRead) { | |
189 | $importedEntry = $this->setEntryAsRead($importedEntry); | |
190 | } | |
191 | ||
192 | ++$this->queuedEntries; | |
193 | ||
194 | $this->producer->publish(json_encode($importedEntry)); | |
195 | } | |
196 | } | |
197 | ||
198 | /** | |
199 | * {@inheritdoc} | |
200 | */ | |
201 | public function getSummary() | |
202 | { | |
203 | return [ | |
204 | 'skipped' => $this->skippedEntries, | |
205 | 'imported' => $this->importedEntries, | |
206 | 'queued' => $this->queuedEntries, | |
207 | ]; | |
208 | } | |
209 | ||
210 | /** | |
211 | * Parse one entry. | |
212 | * | |
213 | * @param array $importedEntry | |
214 | * | |
215 | * @return Entry | |
216 | */ | |
217 | abstract public function parseEntry(array $importedEntry); | |
218 | ||
219 | /** | |
220 | * Set current imported entry to archived / read. | |
221 | * Implementation is different accross all imports. | |
222 | * | |
223 | * @param array $importedEntry | |
224 | * | |
225 | * @return array | |
226 | */ | |
227 | abstract protected function setEntryAsRead(array $importedEntry); | |
228 | } |