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