]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/AbstractImport.php
Enable Redis async import
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / AbstractImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
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;
13
14 abstract class AbstractImport implements ImportInterface
15 {
16 protected $em;
17 protected $logger;
18 protected $contentProxy;
19 protected $producer;
20 protected $user;
21 protected $markAsRead;
22 protected $skippedEntries = 0;
23 protected $importedEntries = 0;
24
25 public function __construct(EntityManager $em, ContentProxy $contentProxy)
26 {
27 $this->em = $em;
28 $this->logger = new NullLogger();
29 $this->contentProxy = $contentProxy;
30 }
31
32 public function setLogger(LoggerInterface $logger)
33 {
34 $this->logger = $logger;
35 }
36
37 /**
38 * Set RabbitMQ/Redis Producer to send each entry to a queue.
39 * This method should be called when user has enabled RabbitMQ.
40 *
41 * @param ProducerInterface $producer
42 */
43 public function setProducer(ProducerInterface $producer)
44 {
45 $this->producer = $producer;
46 }
47
48 /**
49 * Set current user.
50 * Could the current *connected* user or one retrieve by the consumer.
51 *
52 * @param User $user
53 */
54 public function setUser(User $user)
55 {
56 $this->user = $user;
57 }
58
59 /**
60 * Set whether articles must be all marked as read.
61 *
62 * @param bool $markAsRead
63 */
64 public function setMarkAsRead($markAsRead)
65 {
66 $this->markAsRead = $markAsRead;
67
68 return $this;
69 }
70
71 /**
72 * Get whether articles must be all marked as read.
73 */
74 public function getMarkAsRead()
75 {
76 return $this->markAsRead;
77 }
78
79 /**
80 * Fetch content from the ContentProxy (using graby).
81 * If it fails return false instead of the updated entry.
82 *
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
86 *
87 * @return Entry|false
88 */
89 protected function fetchContent(Entry $entry, $url, array $content = [])
90 {
91 try {
92 return $this->contentProxy->updateEntry($entry, $url, $content);
93 } catch (\Exception $e) {
94 return false;
95 }
96 }
97
98 /**
99 * Parse and insert all given entries.
100 *
101 * @param $entries
102 */
103 protected function parseEntries($entries)
104 {
105 $i = 1;
106
107 foreach ($entries as $importedEntry) {
108 $entry = $this->parseEntry($importedEntry);
109
110 if (null === $entry) {
111 continue;
112 }
113
114 // flush every 20 entries
115 if (($i % 20) === 0) {
116 $this->em->flush();
117
118 // clear only affected entities
119 $this->em->clear(Entry::class);
120 $this->em->clear(Tag::class);
121 }
122 ++$i;
123 }
124
125 $this->em->flush();
126 }
127
128 /**
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.
132 *
133 * Faster parse entries for Producer.
134 * We don't care to make check at this time. They'll be done by the consumer.
135 *
136 * @param array $entries
137 */
138 protected function parseEntriesForProducer(array $entries)
139 {
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();
143
144 if ($this->markAsRead) {
145 $importedEntry = $this->setEntryAsRead($importedEntry);
146 }
147
148 ++$this->importedEntries;
149
150 $this->producer->publish(json_encode($importedEntry));
151 }
152 }
153
154 /**
155 * Parse one entry.
156 *
157 * @param array $importedEntry
158 *
159 * @return Entry
160 */
161 abstract public function parseEntry(array $importedEntry);
162
163 /**
164 * Set current imported entry to archived / read.
165 * Implementation is different accross all imports.
166 *
167 * @param array $importedEntry
168 *
169 * @return array
170 */
171 abstract protected function setEntryAsRead(array $importedEntry);
172 }