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