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\UserBundle\Entity\User
;
12 use OldSound\RabbitMqBundle\RabbitMq\ProducerInterface
;
14 abstract class AbstractImport
implements ImportInterface
18 protected $contentProxy;
21 protected $markAsRead;
22 protected $skippedEntries = 0;
23 protected $importedEntries = 0;
24 protected $queuedEntries = 0;
26 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
29 $this->logger
= new NullLogger();
30 $this->contentProxy
= $contentProxy;
33 public function setLogger(LoggerInterface
$logger)
35 $this->logger
= $logger;
39 * Set RabbitMQ/Redis Producer to send each entry to a queue.
40 * This method should be called when user has enabled RabbitMQ.
42 * @param ProducerInterface $producer
44 public function setProducer(ProducerInterface
$producer)
46 $this->producer
= $producer;
51 * Could the current *connected* user or one retrieve by the consumer.
55 public function setUser(User
$user)
61 * Set whether articles must be all marked as read.
63 * @param bool $markAsRead
65 public function setMarkAsRead($markAsRead)
67 $this->markAsRead
= $markAsRead;
73 * Get whether articles must be all marked as read.
75 public function getMarkAsRead()
77 return $this->markAsRead
;
81 * Fetch content from the ContentProxy (using graby).
82 * If it fails return the given entry to be saved in all case (to avoid user to loose the content).
84 * @param Entry $entry Entry to update
85 * @param string $url Url to grab content for
86 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
90 protected function fetchContent(Entry
$entry, $url, array $content = [])
93 return $this->contentProxy
->updateEntry($entry, $url, $content);
94 } catch (\Exception
$e) {
100 * Parse and insert all given entries.
104 protected function parseEntries($entries)
108 foreach ($entries as $importedEntry) {
109 if ($this->markAsRead
) {
110 $importedEntry = $this->setEntryAsRead($importedEntry);
113 $entry = $this->parseEntry($importedEntry);
115 if (null === $entry) {
119 // flush every 20 entries
120 if (($i %
20) === 0) {
123 // clear only affected entities
124 $this->em
->clear(Entry
::class);
125 $this->em
->clear(Tag
::class);
134 * Parse entries and send them to the queue.
135 * It should just be a simple loop on all item, no call to the database should be done
136 * to speedup queuing.
138 * Faster parse entries for Producer.
139 * We don't care to make check at this time. They'll be done by the consumer.
141 * @param array $entries
143 protected function parseEntriesForProducer(array $entries)
145 foreach ($entries as $importedEntry) {
146 // set userId for the producer (it won't know which user is connected)
147 $importedEntry['userId'] = $this->user
->getId();
149 if ($this->markAsRead
) {
150 $importedEntry = $this->setEntryAsRead($importedEntry);
153 ++
$this->queuedEntries
;
155 $this->producer
->publish(json_encode($importedEntry));
162 public function getSummary()
165 'skipped' => $this->skippedEntries
,
166 'imported' => $this->importedEntries
,
167 'queued' => $this->queuedEntries
,
174 * @param array $importedEntry
178 abstract public function parseEntry(array $importedEntry);
181 * Set current imported entry to archived / read.
182 * Implementation is different accross all imports.
184 * @param array $importedEntry
188 abstract protected function setEntryAsRead(array $importedEntry);