]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/BrowserImport.php
Update deps
[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->updateArchived($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 protected function parseEntries(array $entries)
153 {
154 $i = 1;
155 $entryToBeFlushed = [];
156
157 foreach ($entries as $importedEntry) {
158 if ((array) $importedEntry !== $importedEntry) {
159 continue;
160 }
161
162 $entry = $this->parseEntry($importedEntry);
163
164 if (null === $entry) {
165 continue;
166 }
167
168 // @see AbstractImport
169 $entryToBeFlushed[] = $entry;
170
171 // flush every 20 entries
172 if (0 === ($i % 20)) {
173 $this->em->flush();
174
175 foreach ($entryToBeFlushed as $entry) {
176 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
177 }
178
179 $entryToBeFlushed = [];
180 }
181 ++$i;
182 }
183
184 $this->em->flush();
185
186 if (!empty($entryToBeFlushed)) {
187 foreach ($entryToBeFlushed as $entry) {
188 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
189 }
190 }
191 }
192
193 /**
194 * Parse entries and send them to the queue.
195 * It should just be a simple loop on all item, no call to the database should be done
196 * to speedup queuing.
197 *
198 * Faster parse entries for Producer.
199 * We don't care to make check at this time. They'll be done by the consumer.
200 */
201 protected function parseEntriesForProducer(array $entries)
202 {
203 foreach ($entries as $importedEntry) {
204 if ((array) $importedEntry !== $importedEntry) {
205 continue;
206 }
207
208 // set userId for the producer (it won't know which user is connected)
209 $importedEntry['userId'] = $this->user->getId();
210
211 if ($this->markAsRead) {
212 $importedEntry = $this->setEntryAsRead($importedEntry);
213 }
214
215 ++$this->queuedEntries;
216
217 $this->producer->publish(json_encode($importedEntry));
218 }
219 }
220
221 /**
222 * {@inheritdoc}
223 */
224 protected function setEntryAsRead(array $importedEntry)
225 {
226 $importedEntry['is_archived'] = 1;
227
228 return $importedEntry;
229 }
230
231 abstract protected function prepareEntry(array $entry = []);
232 }