]>
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 | ||
9 | abstract class BrowserImport extends AbstractImport | |
10 | { | |
11 | protected $filepath; | |
12 | ||
13 | /** | |
14 | * {@inheritdoc} | |
15 | */ | |
16 | abstract public function getName(); | |
17 | ||
18 | /** | |
19 | * {@inheritdoc} | |
20 | */ | |
21 | abstract public function getUrl(); | |
22 | ||
23 | /** | |
24 | * {@inheritdoc} | |
25 | */ | |
26 | abstract public function getDescription(); | |
27 | ||
28 | /** | |
29 | * {@inheritdoc} | |
30 | */ | |
31 | public function import() | |
32 | { | |
33 | if (!$this->user) { | |
34 | $this->logger->error('Wallabag Browser Import: user is not defined'); | |
35 | ||
36 | return false; | |
37 | } | |
38 | ||
39 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
40 | $this->logger->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath]); | |
41 | ||
42 | return false; | |
43 | } | |
44 | ||
45 | $data = json_decode(file_get_contents($this->filepath), true); | |
46 | ||
47 | if (empty($data)) { | |
48 | $this->logger->error('Wallabag Browser: no entries in imported file'); | |
49 | ||
50 | return false; | |
51 | } | |
52 | ||
53 | if ($this->producer) { | |
54 | $this->parseEntriesForProducer($data); | |
55 | ||
56 | return true; | |
57 | } | |
58 | ||
59 | $this->parseEntries($data); | |
60 | ||
61 | return true; | |
62 | } | |
63 | ||
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) | |
82 | { | |
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(); | |
99 | } | |
100 | ++$i; | |
101 | } | |
102 | ||
103 | $this->em->flush(); | |
104 | } | |
105 | ||
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) | |
117 | { | |
118 | foreach ($entries as $importedEntry) { | |
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)); | |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * {@inheritdoc} | |
138 | */ | |
139 | public function parseEntry(array $importedEntry) | |
140 | { | |
141 | if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) { | |
142 | if ($this->producer) { | |
143 | $this->parseEntriesForProducer($importedEntry); | |
144 | ||
145 | return; | |
146 | } | |
147 | ||
148 | $this->parseEntries($importedEntry); | |
149 | ||
150 | return; | |
151 | } | |
152 | ||
153 | if (array_key_exists('children', $importedEntry)) { | |
154 | if ($this->producer) { | |
155 | $this->parseEntriesForProducer($importedEntry['children']); | |
156 | ||
157 | return; | |
158 | } | |
159 | ||
160 | $this->parseEntries($importedEntry['children']); | |
161 | ||
162 | return; | |
163 | } | |
164 | ||
165 | if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) { | |
166 | return; | |
167 | } | |
168 | ||
169 | $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url']; | |
170 | ||
171 | $existingEntry = $this->em | |
172 | ->getRepository('WallabagCoreBundle:Entry') | |
173 | ->findByUrlAndUserId($url, $this->user->getId()); | |
174 | ||
175 | if (false !== $existingEntry) { | |
176 | ++$this->skippedEntries; | |
177 | ||
178 | return; | |
179 | } | |
180 | ||
181 | $data = $this->prepareEntry($importedEntry); | |
182 | ||
183 | $entry = new Entry($this->user); | |
184 | $entry->setUrl($data['url']); | |
185 | $entry->setTitle($data['title']); | |
186 | ||
187 | // update entry with content (in case fetching failed, the given entry will be return) | |
188 | $entry = $this->fetchContent($entry, $data['url'], $data); | |
189 | ||
190 | if (array_key_exists('tags', $data)) { | |
191 | $this->contentProxy->assignTagsToEntry( | |
192 | $entry, | |
193 | $data['tags'] | |
194 | ); | |
195 | } | |
196 | ||
197 | $entry->setArchived($data['is_archived']); | |
198 | ||
199 | if (!empty($data['created_at'])) { | |
200 | $dt = new \DateTime(); | |
201 | $entry->setCreatedAt($dt->setTimestamp($data['created_at'])); | |
202 | } | |
203 | ||
204 | $this->em->persist($entry); | |
205 | ++$this->importedEntries; | |
206 | ||
207 | return $entry; | |
208 | } | |
209 | ||
210 | /** | |
211 | * {@inheritdoc} | |
212 | */ | |
213 | protected function setEntryAsRead(array $importedEntry) | |
214 | { | |
215 | $importedEntry['is_archived'] = 1; | |
216 | ||
217 | return $importedEntry; | |
218 | } | |
219 | } |