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