]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/BrowserImport.php
Avoid RabbitMQ consumer to loop
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / BrowserImport.php
CommitLineData
ae669126
TC
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
ae669126
TC
5use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Helper\ContentProxy;
8
59201088 9abstract class BrowserImport extends AbstractImport
ae669126 10{
ae669126 11 protected $filepath;
ae669126
TC
12
13 /**
14 * {@inheritdoc}
15 */
59201088 16 abstract public function getName();
ae669126
TC
17
18 /**
19 * {@inheritdoc}
20 */
59201088 21 abstract public function getUrl();
ae669126
TC
22
23 /**
24 * {@inheritdoc}
25 */
59201088 26 abstract public function getDescription();
ae669126
TC
27
28 /**
29 * {@inheritdoc}
30 */
31 public function import()
32 {
33 if (!$this->user) {
64b1229b 34 $this->logger->error('Wallabag Browser Import: user is not defined');
ae669126
TC
35
36 return false;
37 }
38
39 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
64b1229b 40 $this->logger->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath]);
ae669126
TC
41
42 return false;
43 }
44
45 $data = json_decode(file_get_contents($this->filepath), true);
46
47 if (empty($data)) {
c7ea9b41
JB
48 $this->logger->error('Wallabag Browser: no entries in imported file');
49
ae669126
TC
50 return false;
51 }
52
59201088
TC
53 if ($this->producer) {
54 $this->parseEntriesForProducer($data);
55
56 return true;
57 }
58
ae669126 59 $this->parseEntries($data);
ae669126
TC
60
61 return true;
62 }
63
59201088
TC
64 /**
65 * Set file path to the json file.
66 *
67 * @param string $filepath
68 */
69 public function setFilepath($filepath)
70 {
71 $this->filepath = $filepath;
72
73 return $this;
74 }
75
76 /**
77 * Parse and insert all given entries.
78 *
79 * @param $entries
80 */
81 protected function parseEntries($entries)
ae669126 82 {
59201088
TC
83 $i = 1;
84
85 foreach ($entries as $importedEntry) {
86 if ((array) $importedEntry !== $importedEntry) {
87 continue;
88 }
89
90 $entry = $this->parseEntry($importedEntry);
91
92 if (null === $entry) {
93 continue;
94 }
95
96 // flush every 20 entries
97 if (($i % 20) === 0) {
98 $this->em->flush();
59201088
TC
99 }
100 ++$i;
ae669126 101 }
59201088
TC
102
103 $this->em->flush();
ae669126
TC
104 }
105
59201088
TC
106 /**
107 * Parse entries and send them to the queue.
108 * It should just be a simple loop on all item, no call to the database should be done
109 * to speedup queuing.
110 *
111 * Faster parse entries for Producer.
112 * We don't care to make check at this time. They'll be done by the consumer.
113 *
114 * @param array $entries
115 */
116 protected function parseEntriesForProducer(array $entries)
ae669126 117 {
59201088 118 foreach ($entries as $importedEntry) {
59201088
TC
119 if ((array) $importedEntry !== $importedEntry) {
120 continue;
121 }
122
123 // set userId for the producer (it won't know which user is connected)
124 $importedEntry['userId'] = $this->user->getId();
125
126 if ($this->markAsRead) {
127 $importedEntry = $this->setEntryAsRead($importedEntry);
128 }
129
130 ++$this->queuedEntries;
131
132 $this->producer->publish(json_encode($importedEntry));
ae669126 133 }
59201088 134 }
ae669126 135
59201088
TC
136 /**
137 * {@inheritdoc}
138 */
139 public function parseEntry(array $importedEntry)
140 {
12d93e68 141 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
ae669126 142 $this->parseEntries($importedEntry);
2c61db30 143
ae669126
TC
144 return;
145 }
59201088 146
12d93e68 147 if (array_key_exists('children', $importedEntry)) {
ae669126 148 $this->parseEntries($importedEntry['children']);
2c61db30 149
ae669126
TC
150 return;
151 }
ae669126 152
12d93e68 153 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
59201088
TC
154 return;
155 }
ae669126 156
12d93e68 157 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
ae669126 158
59201088
TC
159 $existingEntry = $this->em
160 ->getRepository('WallabagCoreBundle:Entry')
12d93e68 161 ->findByUrlAndUserId($url, $this->user->getId());
ae669126 162
59201088
TC
163 if (false !== $existingEntry) {
164 ++$this->skippedEntries;
ae669126 165
59201088
TC
166 return;
167 }
ae669126 168
59201088 169 $data = $this->prepareEntry($importedEntry);
ae669126 170
59201088
TC
171 $entry = new Entry($this->user);
172 $entry->setUrl($data['url']);
173 $entry->setTitle($data['title']);
ae669126 174
59201088
TC
175 // update entry with content (in case fetching failed, the given entry will be return)
176 $entry = $this->fetchContent($entry, $data['url'], $data);
ae669126 177
59201088
TC
178 if (array_key_exists('tags', $data)) {
179 $this->contentProxy->assignTagsToEntry(
180 $entry,
181 $data['tags']
182 );
ae669126 183 }
ae669126 184
59201088 185 $entry->setArchived($data['is_archived']);
ae669126 186
59201088
TC
187 if (!empty($data['created_at'])) {
188 $dt = new \DateTime();
12d93e68 189 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
59201088 190 }
ae669126 191
59201088
TC
192 $this->em->persist($entry);
193 ++$this->importedEntries;
ae669126 194
59201088 195 return $entry;
ae669126
TC
196 }
197
198 /**
199 * {@inheritdoc}
200 */
59201088 201 protected function setEntryAsRead(array $importedEntry)
ae669126 202 {
59201088
TC
203 $importedEntry['is_archived'] = 1;
204
205 return $importedEntry;
ae669126
TC
206 }
207}