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 $skippedEntries = 0;
28 protected $importedEntries = 0;
29 protected $queuedEntries = 0;
31 public function __construct(EntityManager
$em, ContentProxy
$contentProxy, TagsAssigner
$tagsAssigner, EventDispatcherInterface
$eventDispatcher)
34 $this->logger
= new NullLogger();
35 $this->contentProxy
= $contentProxy;
36 $this->tagsAssigner
= $tagsAssigner;
37 $this->eventDispatcher
= $eventDispatcher;
40 public function setLogger(LoggerInterface
$logger)
42 $this->logger
= $logger;
46 * Set RabbitMQ/Redis Producer to send each entry to a queue.
47 * This method should be called when user has enabled RabbitMQ.
49 * @param ProducerInterface $producer
51 public function setProducer(ProducerInterface
$producer)
53 $this->producer
= $producer;
58 * Could the current *connected* user or one retrieve by the consumer.
62 public function setUser(User
$user)
68 * Set whether articles must be all marked as read.
70 * @param bool $markAsRead
72 public function setMarkAsRead($markAsRead)
74 $this->markAsRead
= $markAsRead;
80 * Get whether articles must be all marked as read.
82 public function getMarkAsRead()
84 return $this->markAsRead
;
88 * Fetch content from the ContentProxy (using graby).
89 * If it fails return the given entry to be saved in all case (to avoid user to loose the content).
91 * @param Entry $entry Entry to update
92 * @param string $url Url to grab content for
93 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
95 protected function fetchContent(Entry
$entry, $url, array $content = [])
98 $this->contentProxy
->updateEntry($entry, $url, $content);
99 } catch (\Exception
$e) {
105 * Parse and insert all given entries.
109 protected function parseEntries($entries)
112 $entryToBeFlushed = [];
114 foreach ($entries as $importedEntry) {
115 if ($this->markAsRead
) {
116 $importedEntry = $this->setEntryAsRead($importedEntry);
119 $entry = $this->parseEntry($importedEntry);
121 if (null === $entry) {
125 // store each entry to be flushed so we can trigger the entry.saved event for each of them
126 // entry.saved needs the entry to be persisted in db because it needs it id to generate
128 $entryToBeFlushed[] = $entry;
130 // flush every 20 entries
131 if (($i %
20) === 0) {
134 foreach ($entryToBeFlushed as $entry) {
135 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
138 $entryToBeFlushed = [];
140 // clear only affected entities
141 $this->em
->clear(Entry
::class);
142 $this->em
->clear(Tag
::class);
149 if (!empty($entryToBeFlushed)) {
150 foreach ($entryToBeFlushed as $entry) {
151 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
157 * Parse entries and send them to the queue.
158 * It should just be a simple loop on all item, no call to the database should be done
159 * to speedup queuing.
161 * Faster parse entries for Producer.
162 * We don't care to make check at this time. They'll be done by the consumer.
164 * @param array $entries
166 protected function parseEntriesForProducer(array $entries)
168 foreach ($entries as $importedEntry) {
169 // set userId for the producer (it won't know which user is connected)
170 $importedEntry['userId'] = $this->user
->getId();
172 if ($this->markAsRead
) {
173 $importedEntry = $this->setEntryAsRead($importedEntry);
176 ++
$this->queuedEntries
;
178 $this->producer
->publish(json_encode($importedEntry));
185 public function getSummary()
188 'skipped' => $this->skippedEntries
,
189 'imported' => $this->importedEntries
,
190 'queued' => $this->queuedEntries
,
197 * @param array $importedEntry
201 abstract public function parseEntry(array $importedEntry);
204 * Set current imported entry to archived / read.
205 * Implementation is different accross all imports.
207 * @param array $importedEntry
211 abstract protected function setEntryAsRead(array $importedEntry);