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