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