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