3 namespace Wallabag\ImportBundle\Import
;
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
;
17 abstract class AbstractImport
implements ImportInterface
21 protected $contentProxy;
22 protected $tagsAssigner;
23 protected $eventDispatcher;
26 protected $markAsRead;
27 protected $disableContentUpdate = false;
28 protected $skippedEntries = 0;
29 protected $importedEntries = 0;
30 protected $queuedEntries = 0;
32 public function __construct(EntityManager
$em, ContentProxy
$contentProxy, TagsAssigner
$tagsAssigner, EventDispatcherInterface
$eventDispatcher)
35 $this->logger
= new NullLogger();
36 $this->contentProxy
= $contentProxy;
37 $this->tagsAssigner
= $tagsAssigner;
38 $this->eventDispatcher
= $eventDispatcher;
41 public function setLogger(LoggerInterface
$logger)
43 $this->logger
= $logger;
47 * Set RabbitMQ/Redis Producer to send each entry to a queue.
48 * This method should be called when user has enabled RabbitMQ.
50 * @param ProducerInterface $producer
52 public function setProducer(ProducerInterface
$producer)
54 $this->producer
= $producer;
59 * Could the current *connected* user or one retrieve by the consumer.
63 public function setUser(User
$user)
69 * Set whether articles must be all marked as read.
71 * @param bool $markAsRead
73 public function setMarkAsRead($markAsRead)
75 $this->markAsRead
= $markAsRead;
81 * Get whether articles must be all marked as read.
83 public function getMarkAsRead()
85 return $this->markAsRead
;
89 * Set whether articles should be fetched for updated content.
91 * @param bool $disableContentUpdate
93 public function setDisableContentUpdate($disableContentUpdate)
95 $this->disableContentUpdate
= $disableContentUpdate;
101 * Get whether articles should be fetched for updated content.
103 public function getDisableContentUpdate()
105 return $this->disableContentUpdate
;
109 * Fetch content from the ContentProxy (using graby).
110 * If it fails return the given entry to be saved in all case (to avoid user to loose the content).
112 * @param Entry $entry Entry to update
113 * @param string $url Url to grab content for
114 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
116 protected function fetchContent(Entry
$entry, $url, array $content = [])
119 $this->contentProxy
->updateEntry($entry, $url, $content, $this->disableContentUpdate
);
120 } catch (\Exception
$e) {
121 $this->logger
->error('Error trying to import an entry.', [
123 'error_msg' => $e->getMessage(),
129 * Parse and insert all given entries.
133 protected function parseEntries($entries)
136 $entryToBeFlushed = [];
138 foreach ($entries as $importedEntry) {
139 if ($this->markAsRead
) {
140 $importedEntry = $this->setEntryAsRead($importedEntry);
143 $entry = $this->parseEntry($importedEntry);
145 if (null === $entry) {
149 // store each entry to be flushed so we can trigger the entry.saved event for each of them
150 // entry.saved needs the entry to be persisted in db because it needs it id to generate
152 $entryToBeFlushed[] = $entry;
154 // flush every 20 entries
155 if (($i %
20) === 0) {
158 foreach ($entryToBeFlushed as $entry) {
159 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
162 $entryToBeFlushed = [];
164 // clear only affected entities
165 $this->em
->clear(Entry
::class);
166 $this->em
->clear(Tag
::class);
173 if (!empty($entryToBeFlushed)) {
174 foreach ($entryToBeFlushed as $entry) {
175 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
181 * Parse entries and send them to the queue.
182 * It should just be a simple loop on all item, no call to the database should be done
183 * to speedup queuing.
185 * Faster parse entries for Producer.
186 * We don't care to make check at this time. They'll be done by the consumer.
188 * @param array $entries
190 protected function parseEntriesForProducer(array $entries)
192 foreach ($entries as $importedEntry) {
193 // set userId for the producer (it won't know which user is connected)
194 $importedEntry['userId'] = $this->user
->getId();
196 if ($this->markAsRead
) {
197 $importedEntry = $this->setEntryAsRead($importedEntry);
200 ++
$this->queuedEntries
;
202 $this->producer
->publish(json_encode($importedEntry));
209 public function getSummary()
212 'skipped' => $this->skippedEntries
,
213 'imported' => $this->importedEntries
,
214 'queued' => $this->queuedEntries
,
221 * @param array $importedEntry
225 abstract public function parseEntry(array $importedEntry);
228 * Set current imported entry to archived / read.
229 * Implementation is different accross all imports.
231 * @param array $importedEntry
235 abstract protected function setEntryAsRead(array $importedEntry);