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