]>
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; | |
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | |
7816eb62 | 8 | use Wallabag\CoreBundle\Event\EntrySavedEvent; |
ae669126 | 9 | |
59201088 | 10 | abstract class BrowserImport extends AbstractImport |
ae669126 | 11 | { |
ae669126 | 12 | protected $filepath; |
ae669126 TC |
13 | |
14 | /** | |
15 | * {@inheritdoc} | |
16 | */ | |
59201088 | 17 | abstract public function getName(); |
ae669126 TC |
18 | |
19 | /** | |
20 | * {@inheritdoc} | |
21 | */ | |
59201088 | 22 | abstract public function getUrl(); |
ae669126 TC |
23 | |
24 | /** | |
25 | * {@inheritdoc} | |
26 | */ | |
59201088 | 27 | abstract public function getDescription(); |
ae669126 TC |
28 | |
29 | /** | |
30 | * {@inheritdoc} | |
31 | */ | |
32 | public function import() | |
33 | { | |
34 | if (!$this->user) { | |
64b1229b | 35 | $this->logger->error('Wallabag Browser Import: user is not defined'); |
ae669126 TC |
36 | |
37 | return false; | |
38 | } | |
39 | ||
40 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
64b1229b | 41 | $this->logger->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath]); |
ae669126 TC |
42 | |
43 | return false; | |
44 | } | |
45 | ||
46 | $data = json_decode(file_get_contents($this->filepath), true); | |
47 | ||
48 | if (empty($data)) { | |
c7ea9b41 JB |
49 | $this->logger->error('Wallabag Browser: no entries in imported file'); |
50 | ||
ae669126 TC |
51 | return false; |
52 | } | |
53 | ||
59201088 TC |
54 | if ($this->producer) { |
55 | $this->parseEntriesForProducer($data); | |
56 | ||
57 | return true; | |
58 | } | |
59 | ||
ae669126 | 60 | $this->parseEntries($data); |
ae669126 TC |
61 | |
62 | return true; | |
63 | } | |
64 | ||
59201088 TC |
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) | |
ae669126 | 83 | { |
59201088 | 84 | $i = 1; |
7816eb62 | 85 | $entryToBeFlushed = []; |
59201088 TC |
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 | ||
7816eb62 JB |
98 | // @see AbstractImport |
99 | $entryToBeFlushed[] = $entry; | |
100 | ||
59201088 TC |
101 | // flush every 20 entries |
102 | if (($i % 20) === 0) { | |
103 | $this->em->flush(); | |
7816eb62 JB |
104 | |
105 | foreach ($entryToBeFlushed as $entry) { | |
106 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
107 | } | |
108 | ||
109 | $entryToBeFlushed = []; | |
59201088 TC |
110 | } |
111 | ++$i; | |
ae669126 | 112 | } |
59201088 TC |
113 | |
114 | $this->em->flush(); | |
7816eb62 JB |
115 | |
116 | if (!empty($entryToBeFlushed)) { | |
117 | foreach ($entryToBeFlushed as $entry) { | |
118 | $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry)); | |
119 | } | |
120 | } | |
ae669126 TC |
121 | } |
122 | ||
59201088 TC |
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) | |
ae669126 | 134 | { |
59201088 | 135 | foreach ($entries as $importedEntry) { |
59201088 TC |
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)); | |
ae669126 | 150 | } |
59201088 | 151 | } |
ae669126 | 152 | |
59201088 TC |
153 | /** |
154 | * {@inheritdoc} | |
155 | */ | |
156 | public function parseEntry(array $importedEntry) | |
157 | { | |
12d93e68 | 158 | if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) { |
54535004 JB |
159 | if ($this->producer) { |
160 | $this->parseEntriesForProducer($importedEntry); | |
161 | ||
162 | return; | |
163 | } | |
164 | ||
ae669126 | 165 | $this->parseEntries($importedEntry); |
2c61db30 | 166 | |
ae669126 TC |
167 | return; |
168 | } | |
59201088 | 169 | |
12d93e68 | 170 | if (array_key_exists('children', $importedEntry)) { |
54535004 JB |
171 | if ($this->producer) { |
172 | $this->parseEntriesForProducer($importedEntry['children']); | |
173 | ||
174 | return; | |
175 | } | |
176 | ||
ae669126 | 177 | $this->parseEntries($importedEntry['children']); |
2c61db30 | 178 | |
ae669126 TC |
179 | return; |
180 | } | |
ae669126 | 181 | |
12d93e68 | 182 | if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) { |
59201088 TC |
183 | return; |
184 | } | |
ae669126 | 185 | |
12d93e68 | 186 | $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url']; |
ae669126 | 187 | |
59201088 TC |
188 | $existingEntry = $this->em |
189 | ->getRepository('WallabagCoreBundle:Entry') | |
12d93e68 | 190 | ->findByUrlAndUserId($url, $this->user->getId()); |
ae669126 | 191 | |
59201088 TC |
192 | if (false !== $existingEntry) { |
193 | ++$this->skippedEntries; | |
ae669126 | 194 | |
59201088 TC |
195 | return; |
196 | } | |
ae669126 | 197 | |
59201088 | 198 | $data = $this->prepareEntry($importedEntry); |
ae669126 | 199 | |
59201088 TC |
200 | $entry = new Entry($this->user); |
201 | $entry->setUrl($data['url']); | |
202 | $entry->setTitle($data['title']); | |
ae669126 | 203 | |
59201088 TC |
204 | // update entry with content (in case fetching failed, the given entry will be return) |
205 | $entry = $this->fetchContent($entry, $data['url'], $data); | |
ae669126 | 206 | |
59201088 TC |
207 | if (array_key_exists('tags', $data)) { |
208 | $this->contentProxy->assignTagsToEntry( | |
209 | $entry, | |
210 | $data['tags'] | |
211 | ); | |
ae669126 | 212 | } |
ae669126 | 213 | |
59201088 | 214 | $entry->setArchived($data['is_archived']); |
ae669126 | 215 | |
59201088 TC |
216 | if (!empty($data['created_at'])) { |
217 | $dt = new \DateTime(); | |
12d93e68 | 218 | $entry->setCreatedAt($dt->setTimestamp($data['created_at'])); |
59201088 | 219 | } |
ae669126 | 220 | |
59201088 TC |
221 | $this->em->persist($entry); |
222 | ++$this->importedEntries; | |
ae669126 | 223 | |
59201088 | 224 | return $entry; |
ae669126 TC |
225 | } |
226 | ||
227 | /** | |
228 | * {@inheritdoc} | |
229 | */ | |
59201088 | 230 | protected function setEntryAsRead(array $importedEntry) |
ae669126 | 231 | { |
59201088 TC |
232 | $importedEntry['is_archived'] = 1; |
233 | ||
234 | return $importedEntry; | |
ae669126 TC |
235 | } |
236 | } |