]>
Commit | Line | Data |
---|---|---|
ae669126 TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\ImportBundle\Import; | |
4 | ||
ae669126 TC |
5 | use Wallabag\CoreBundle\Entity\Entry; |
6 | use Wallabag\UserBundle\Entity\User; | |
7816eb62 | 7 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
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 | ||
76 | /** | |
77 | * Parse and insert all given entries. | |
78 | * | |
79 | * @param $entries | |
80 | */ | |
81 | protected function parseEntries($entries) | |
ae669126 | 82 | { |
59201088 | 83 | $i = 1; |
7816eb62 | 84 | $entryToBeFlushed = []; |
59201088 TC |
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 | ||
7816eb62 JB |
97 | // @see AbstractImport |
98 | $entryToBeFlushed[] = $entry; | |
99 | ||
59201088 TC |
100 | // flush every 20 entries |
101 | if (($i % 20) === 0) { | |
102 | $this->em->flush(); | |
7816eb62 JB |
103 | |
104 | foreach ($entryToBeFlushed as $entry) { | |
105 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
106 | } | |
107 | ||
108 | $entryToBeFlushed = []; | |
59201088 TC |
109 | } |
110 | ++$i; | |
ae669126 | 111 | } |
59201088 TC |
112 | |
113 | $this->em->flush(); | |
7816eb62 JB |
114 | |
115 | if (!empty($entryToBeFlushed)) { | |
116 | foreach ($entryToBeFlushed as $entry) { | |
117 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
118 | } | |
119 | } | |
ae669126 TC |
120 | } |
121 | ||
59201088 TC |
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) | |
ae669126 | 133 | { |
59201088 | 134 | foreach ($entries as $importedEntry) { |
59201088 TC |
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)); | |
ae669126 | 149 | } |
59201088 | 150 | } |
ae669126 | 151 | |
59201088 TC |
152 | /** |
153 | * {@inheritdoc} | |
154 | */ | |
155 | public function parseEntry(array $importedEntry) | |
156 | { | |
12d93e68 | 157 | if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) { |
54535004 JB |
158 | if ($this->producer) { |
159 | $this->parseEntriesForProducer($importedEntry); | |
160 | ||
161 | return; | |
162 | } | |
163 | ||
ae669126 | 164 | $this->parseEntries($importedEntry); |
2c61db30 | 165 | |
ae669126 TC |
166 | return; |
167 | } | |
59201088 | 168 | |
12d93e68 | 169 | if (array_key_exists('children', $importedEntry)) { |
54535004 JB |
170 | if ($this->producer) { |
171 | $this->parseEntriesForProducer($importedEntry['children']); | |
172 | ||
173 | return; | |
174 | } | |
175 | ||
ae669126 | 176 | $this->parseEntries($importedEntry['children']); |
2c61db30 | 177 | |
ae669126 TC |
178 | return; |
179 | } | |
ae669126 | 180 | |
12d93e68 | 181 | if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) { |
59201088 TC |
182 | return; |
183 | } | |
ae669126 | 184 | |
12d93e68 | 185 | $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url']; |
ae669126 | 186 | |
59201088 TC |
187 | $existingEntry = $this->em |
188 | ->getRepository('WallabagCoreBundle:Entry') | |
12d93e68 | 189 | ->findByUrlAndUserId($url, $this->user->getId()); |
ae669126 | 190 | |
59201088 TC |
191 | if (false !== $existingEntry) { |
192 | ++$this->skippedEntries; | |
ae669126 | 193 | |
59201088 TC |
194 | return; |
195 | } | |
ae669126 | 196 | |
59201088 | 197 | $data = $this->prepareEntry($importedEntry); |
ae669126 | 198 | |
59201088 TC |
199 | $entry = new Entry($this->user); |
200 | $entry->setUrl($data['url']); | |
201 | $entry->setTitle($data['title']); | |
ae669126 | 202 | |
59201088 TC |
203 | // update entry with content (in case fetching failed, the given entry will be return) |
204 | $entry = $this->fetchContent($entry, $data['url'], $data); | |
ae669126 | 205 | |
59201088 | 206 | if (array_key_exists('tags', $data)) { |
6bc6fb1f | 207 | $this->tagsAssigner->assignTagsToEntry( |
59201088 TC |
208 | $entry, |
209 | $data['tags'] | |
210 | ); | |
ae669126 | 211 | } |
ae669126 | 212 | |
59201088 | 213 | $entry->setArchived($data['is_archived']); |
ae669126 | 214 | |
59201088 TC |
215 | if (!empty($data['created_at'])) { |
216 | $dt = new \DateTime(); | |
12d93e68 | 217 | $entry->setCreatedAt($dt->setTimestamp($data['created_at'])); |
59201088 | 218 | } |
ae669126 | 219 | |
59201088 TC |
220 | $this->em->persist($entry); |
221 | ++$this->importedEntries; | |
ae669126 | 222 | |
59201088 | 223 | return $entry; |
ae669126 TC |
224 | } |
225 | ||
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 | } |