]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/AbstractImport.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[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 48 * This method should be called when user has enabled RabbitMQ.
c98db1b6 49 */
b3437d58 50 public function setProducer(ProducerInterface $producer)
c98db1b6
JB
51 {
52 $this->producer = $producer;
53 }
54
55 /**
56 * Set current user.
57 * Could the current *connected* user or one retrieve by the consumer.
c98db1b6 58 */
3849a9f3 59 public function setUser(User $user)
c98db1b6
JB
60 {
61 $this->user = $user;
62 }
63
64 /**
65 * Set whether articles must be all marked as read.
66 *
67 * @param bool $markAsRead
68 */
69 public function setMarkAsRead($markAsRead)
70 {
71 $this->markAsRead = $markAsRead;
72
73 return $this;
74 }
75
76 /**
77 * Get whether articles must be all marked as read.
78 */
79 public function getMarkAsRead()
80 {
81 return $this->markAsRead;
82 }
83
d0e9b3d6
JC
84 /**
85 * Set whether articles should be fetched for updated content.
86 *
432a24f5 87 * @param bool $disableContentUpdate
d0e9b3d6
JC
88 */
89 public function setDisableContentUpdate($disableContentUpdate)
90 {
91 $this->disableContentUpdate = $disableContentUpdate;
92
93 return $this;
94 }
95
f808b016
JB
96 /**
97 * {@inheritdoc}
98 */
99 public function getSummary()
100 {
101 return [
102 'skipped' => $this->skippedEntries,
103 'imported' => $this->importedEntries,
104 'queued' => $this->queuedEntries,
105 ];
106 }
107
108 /**
109 * Parse one entry.
110 *
f808b016
JB
111 * @return Entry
112 */
113 abstract public function parseEntry(array $importedEntry);
114
9f8f188d
JB
115 /**
116 * Validate that an entry is valid (like has some required keys, etc.).
117 *
9f8f188d
JB
118 * @return bool
119 */
120 abstract public function validateEntry(array $importedEntry);
121
19d9efab
JB
122 /**
123 * Fetch content from the ContentProxy (using graby).
59b97fae 124 * If it fails return the given entry to be saved in all case (to avoid user to loose the content).
19d9efab
JB
125 *
126 * @param Entry $entry Entry to update
127 * @param string $url Url to grab content for
128 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
19d9efab
JB
129 */
130 protected function fetchContent(Entry $entry, $url, array $content = [])
131 {
132 try {
6acadf8e 133 $this->contentProxy->updateEntry($entry, $url, $content, $this->disableContentUpdate);
19d9efab 134 } catch (\Exception $e) {
d0e9b3d6 135 $this->logger->error('Error trying to import an entry.', [
6acadf8e 136 'entry_url' => $url,
d0e9b3d6
JC
137 'error_msg' => $e->getMessage(),
138 ]);
19d9efab
JB
139 }
140 }
c98db1b6
JB
141
142 /**
143 * Parse and insert all given entries.
c98db1b6 144 */
9f8f188d 145 protected function parseEntries(array $entries)
c98db1b6
JB
146 {
147 $i = 1;
7816eb62 148 $entryToBeFlushed = [];
c98db1b6
JB
149
150 foreach ($entries as $importedEntry) {
ff1a5362
JB
151 if ($this->markAsRead) {
152 $importedEntry = $this->setEntryAsRead($importedEntry);
153 }
154
9f8f188d
JB
155 if (false === $this->validateEntry($importedEntry)) {
156 continue;
157 }
158
c98db1b6
JB
159 $entry = $this->parseEntry($importedEntry);
160
161 if (null === $entry) {
162 continue;
163 }
164
7816eb62
JB
165 // store each entry to be flushed so we can trigger the entry.saved event for each of them
166 // entry.saved needs the entry to be persisted in db because it needs it id to generate
167 // images (at least)
168 $entryToBeFlushed[] = $entry;
169
c98db1b6 170 // flush every 20 entries
3ef055ce 171 if (0 === ($i % 20)) {
c98db1b6 172 $this->em->flush();
8664069e 173
7816eb62
JB
174 foreach ($entryToBeFlushed as $entry) {
175 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
176 }
177
178 $entryToBeFlushed = [];
179
8664069e
JB
180 // clear only affected entities
181 $this->em->clear(Entry::class);
182 $this->em->clear(Tag::class);
c98db1b6
JB
183 }
184 ++$i;
185 }
186
187 $this->em->flush();
7816eb62
JB
188
189 if (!empty($entryToBeFlushed)) {
190 foreach ($entryToBeFlushed as $entry) {
191 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
192 }
193 }
c98db1b6
JB
194 }
195
3849a9f3
JB
196 /**
197 * Parse entries and send them to the queue.
198 * It should just be a simple loop on all item, no call to the database should be done
199 * to speedup queuing.
200 *
201 * Faster parse entries for Producer.
202 * We don't care to make check at this time. They'll be done by the consumer.
3849a9f3
JB
203 */
204 protected function parseEntriesForProducer(array $entries)
205 {
206 foreach ($entries as $importedEntry) {
207 // set userId for the producer (it won't know which user is connected)
208 $importedEntry['userId'] = $this->user->getId();
209
210 if ($this->markAsRead) {
211 $importedEntry = $this->setEntryAsRead($importedEntry);
212 }
213
c80cc01a 214 ++$this->queuedEntries;
3849a9f3
JB
215
216 $this->producer->publish(json_encode($importedEntry));
217 }
218 }
219
3849a9f3
JB
220 /**
221 * Set current imported entry to archived / read.
222 * Implementation is different accross all imports.
223 *
3849a9f3
JB
224 * @return array
225 */
226 abstract protected function setEntryAsRead(array $importedEntry);
19d9efab 227}