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\Producer
;
14 abstract class AbstractImport
implements ImportInterface
18 protected $contentProxy;
21 protected $markAsRead;
22 protected $skippedEntries = 0;
23 protected $importedEntries = 0;
25 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
28 $this->logger
= new NullLogger();
29 $this->contentProxy
= $contentProxy;
32 public function setLogger(LoggerInterface
$logger)
34 $this->logger
= $logger;
38 * Set RabbitMQ Producer to send each entry to a queue.
39 * This method should be called when user has enabled RabbitMQ.
41 * @param Producer $producer
43 public function setRabbitmqProducer(Producer
$producer)
45 $this->producer
= $producer;
50 * Could the current *connected* user or one retrieve by the consumer.
54 public function setUser(User
$user)
60 * Set whether articles must be all marked as read.
62 * @param bool $markAsRead
64 public function setMarkAsRead($markAsRead)
66 $this->markAsRead
= $markAsRead;
72 * Get whether articles must be all marked as read.
74 public function getMarkAsRead()
76 return $this->markAsRead
;
80 * Fetch content from the ContentProxy (using graby).
81 * If it fails return false instead of the updated entry.
83 * @param Entry $entry Entry to update
84 * @param string $url Url to grab content for
85 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
89 protected function fetchContent(Entry
$entry, $url, array $content = [])
92 return $this->contentProxy
->updateEntry($entry, $url, $content);
93 } catch (\Exception
$e) {
99 * Parse and insert all given entries.
103 protected function parseEntries($entries)
107 foreach ($entries as $importedEntry) {
108 $entry = $this->parseEntry($importedEntry);
110 if (null === $entry) {
114 // flush every 20 entries
115 if (($i %
20) === 0) {
118 // clear only affected entities
119 $this->em
->clear(Entry
::class);
120 $this->em
->clear(Tag
::class);
129 * Parse entries and send them to the queue.
130 * It should just be a simple loop on all item, no call to the database should be done
131 * to speedup queuing.
133 * Faster parse entries for Producer.
134 * We don't care to make check at this time. They'll be done by the consumer.
136 * @param array $entries
138 protected function parseEntriesForProducer(array $entries)
140 foreach ($entries as $importedEntry) {
141 // set userId for the producer (it won't know which user is connected)
142 $importedEntry['userId'] = $this->user
->getId();
144 if ($this->markAsRead
) {
145 $importedEntry = $this->setEntryAsRead($importedEntry);
148 ++
$this->importedEntries
;
150 $this->producer
->publish(json_encode($importedEntry));
157 * @param array $importedEntry
161 abstract public function parseEntry(array $importedEntry);
164 * Set current imported entry to archived / read.
165 * Implementation is different accross all imports.
167 * @param array $importedEntry
171 abstract protected function setEntryAsRead(array $importedEntry);