]>
Commit | Line | Data |
---|---|---|
ae669126 TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
ae669126 | 5 | use Wallabag\CoreBundle\Entity\Entry; |
7816eb62 | 6 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
ae669126 | 7 | |
59201088 | 8 | abstract class BrowserImport extends AbstractImport |
ae669126 | 9 | { |
ae669126 | 10 | protected $filepath; |
ae669126 TC |
11 | |
12 | /** | |
13 | * {@inheritdoc} | |
14 | */ | |
59201088 | 15 | abstract public function getName(); |
ae669126 TC |
16 | |
17 | /** | |
18 | * {@inheritdoc} | |
19 | */ | |
59201088 | 20 | abstract public function getUrl(); |
ae669126 TC |
21 | |
22 | /** | |
23 | * {@inheritdoc} | |
24 | */ | |
59201088 | 25 | abstract public function getDescription(); |
ae669126 TC |
26 | |
27 | /** | |
28 | * {@inheritdoc} | |
29 | */ | |
30 | public function import() | |
31 | { | |
32 | if (!$this->user) { | |
64b1229b | 33 | $this->logger->error('Wallabag Browser Import: user is not defined'); |
ae669126 TC |
34 | |
35 | return false; | |
36 | } | |
37 | ||
38 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
64b1229b | 39 | $this->logger->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath]); |
ae669126 TC |
40 | |
41 | return false; | |
42 | } | |
43 | ||
44 | $data = json_decode(file_get_contents($this->filepath), true); | |
45 | ||
46 | if (empty($data)) { | |
c7ea9b41 JB |
47 | $this->logger->error('Wallabag Browser: no entries in imported file'); |
48 | ||
ae669126 TC |
49 | return false; |
50 | } | |
51 | ||
59201088 TC |
52 | if ($this->producer) { |
53 | $this->parseEntriesForProducer($data); | |
54 | ||
55 | return true; | |
56 | } | |
57 | ||
ae669126 | 58 | $this->parseEntries($data); |
ae669126 TC |
59 | |
60 | return true; | |
61 | } | |
62 | ||
59201088 TC |
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 | ||
f808b016 JB |
75 | /** |
76 | * {@inheritdoc} | |
77 | */ | |
78 | public function parseEntry(array $importedEntry) | |
79 | { | |
2a1ceb67 | 80 | if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && \is_array(reset($importedEntry))) { |
f808b016 JB |
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 | ||
7975395d | 136 | $entry->updateArchived($data['is_archived']); |
f808b016 JB |
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 | ||
59201088 TC |
149 | /** |
150 | * Parse and insert all given entries. | |
151 | * | |
9f8f188d | 152 | * @param array $entries |
59201088 | 153 | */ |
9f8f188d | 154 | protected function parseEntries(array $entries) |
ae669126 | 155 | { |
59201088 | 156 | $i = 1; |
7816eb62 | 157 | $entryToBeFlushed = []; |
59201088 TC |
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 | ||
7816eb62 JB |
170 | // @see AbstractImport |
171 | $entryToBeFlushed[] = $entry; | |
172 | ||
59201088 | 173 | // flush every 20 entries |
3ef055ce | 174 | if (0 === ($i % 20)) { |
59201088 | 175 | $this->em->flush(); |
7816eb62 JB |
176 | |
177 | foreach ($entryToBeFlushed as $entry) { | |
178 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
179 | } | |
180 | ||
181 | $entryToBeFlushed = []; | |
59201088 TC |
182 | } |
183 | ++$i; | |
ae669126 | 184 | } |
59201088 TC |
185 | |
186 | $this->em->flush(); | |
7816eb62 JB |
187 | |
188 | if (!empty($entryToBeFlushed)) { | |
189 | foreach ($entryToBeFlushed as $entry) { | |
190 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
191 | } | |
192 | } | |
ae669126 TC |
193 | } |
194 | ||
59201088 TC |
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) | |
ae669126 | 206 | { |
59201088 | 207 | foreach ($entries as $importedEntry) { |
59201088 TC |
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)); | |
ae669126 | 222 | } |
59201088 | 223 | } |
ae669126 | 224 | |
ae669126 TC |
225 | /** |
226 | * {@inheritdoc} | |
227 | */ | |
59201088 | 228 | protected function setEntryAsRead(array $importedEntry) |
ae669126 | 229 | { |
59201088 TC |
230 | $importedEntry['is_archived'] = 1; |
231 | ||
232 | return $importedEntry; | |
ae669126 | 233 | } |
fe312015 NH |
234 | |
235 | abstract protected function prepareEntry(array $entry = []); | |
ae669126 | 236 | } |