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
97 protected function fetchContent(Entry
$entry, $url, array $content = [])
100 return $this->contentProxy
->updateEntry($entry, $url, $content);
101 } catch (\Exception
$e) {
107 * Parse and insert all given entries.
111 protected function parseEntries($entries)
114 $entryToBeFlushed = [];
116 foreach ($entries as $importedEntry) {
117 if ($this->markAsRead
) {
118 $importedEntry = $this->setEntryAsRead($importedEntry);
121 $entry = $this->parseEntry($importedEntry);
123 if (null === $entry) {
127 // store each entry to be flushed so we can trigger the entry.saved event for each of them
128 // entry.saved needs the entry to be persisted in db because it needs it id to generate
130 $entryToBeFlushed[] = $entry;
132 // flush every 20 entries
133 if (($i %
20) === 0) {
136 foreach ($entryToBeFlushed as $entry) {
137 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
140 $entryToBeFlushed = [];
142 // clear only affected entities
143 $this->em
->clear(Entry
::class);
144 $this->em
->clear(Tag
::class);
151 if (!empty($entryToBeFlushed)) {
152 foreach ($entryToBeFlushed as $entry) {
153 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
159 * Parse entries and send them to the queue.
160 * It should just be a simple loop on all item, no call to the database should be done
161 * to speedup queuing.
163 * Faster parse entries for Producer.
164 * We don't care to make check at this time. They'll be done by the consumer.
166 * @param array $entries
168 protected function parseEntriesForProducer(array $entries)
170 foreach ($entries as $importedEntry) {
171 // set userId for the producer (it won't know which user is connected)
172 $importedEntry['userId'] = $this->user
->getId();
174 if ($this->markAsRead
) {
175 $importedEntry = $this->setEntryAsRead($importedEntry);
178 ++
$this->queuedEntries
;
180 $this->producer
->publish(json_encode($importedEntry));
187 public function getSummary()
190 'skipped' => $this->skippedEntries
,
191 'imported' => $this->importedEntries
,
192 'queued' => $this->queuedEntries
,
199 * @param array $importedEntry
203 abstract public function parseEntry(array $importedEntry);
206 * Set current imported entry to archived / read.
207 * Implementation is different accross all imports.
209 * @param array $importedEntry
213 abstract protected function setEntryAsRead(array $importedEntry);