]>
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) { | |
34 | $this->logger->error('WallabagImport: user is not defined'); | |
35 | ||
36 | return false; | |
37 | } | |
38 | ||
39 | if (!file_exists($this->filepath) || !is_readable($this->filepath)) { | |
40 | $this->logger->error('WallabagImport: 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 | return false; | |
49 | } | |
50 | ||
59201088 TC |
51 | if ($this->producer) { |
52 | $this->parseEntriesForProducer($data); | |
53 | ||
54 | return true; | |
55 | } | |
56 | ||
ae669126 | 57 | $this->parseEntries($data); |
ae669126 TC |
58 | |
59 | return true; | |
60 | } | |
61 | ||
59201088 TC |
62 | /** |
63 | * Set file path to the json file. | |
64 | * | |
65 | * @param string $filepath | |
66 | */ | |
67 | public function setFilepath($filepath) | |
68 | { | |
69 | $this->filepath = $filepath; | |
70 | ||
71 | return $this; | |
72 | } | |
73 | ||
74 | /** | |
75 | * Parse and insert all given entries. | |
76 | * | |
77 | * @param $entries | |
78 | */ | |
79 | protected function parseEntries($entries) | |
ae669126 | 80 | { |
59201088 TC |
81 | $i = 1; |
82 | ||
83 | foreach ($entries as $importedEntry) { | |
84 | if ((array) $importedEntry !== $importedEntry) { | |
85 | continue; | |
86 | } | |
87 | ||
88 | $entry = $this->parseEntry($importedEntry); | |
89 | ||
90 | if (null === $entry) { | |
91 | continue; | |
92 | } | |
93 | ||
94 | // flush every 20 entries | |
95 | if (($i % 20) === 0) { | |
96 | $this->em->flush(); | |
97 | ||
98 | // clear only affected entities | |
99 | $this->em->clear(Entry::class); | |
100 | $this->em->clear(Tag::class); | |
101 | } | |
102 | ++$i; | |
ae669126 | 103 | } |
59201088 TC |
104 | |
105 | $this->em->flush(); | |
ae669126 TC |
106 | } |
107 | ||
59201088 TC |
108 | /** |
109 | * Parse entries and send them to the queue. | |
110 | * It should just be a simple loop on all item, no call to the database should be done | |
111 | * to speedup queuing. | |
112 | * | |
113 | * Faster parse entries for Producer. | |
114 | * We don't care to make check at this time. They'll be done by the consumer. | |
115 | * | |
116 | * @param array $entries | |
117 | */ | |
118 | protected function parseEntriesForProducer(array $entries) | |
ae669126 | 119 | { |
59201088 | 120 | foreach ($entries as $importedEntry) { |
59201088 TC |
121 | if ((array) $importedEntry !== $importedEntry) { |
122 | continue; | |
123 | } | |
124 | ||
125 | // set userId for the producer (it won't know which user is connected) | |
126 | $importedEntry['userId'] = $this->user->getId(); | |
127 | ||
128 | if ($this->markAsRead) { | |
129 | $importedEntry = $this->setEntryAsRead($importedEntry); | |
130 | } | |
131 | ||
132 | ++$this->queuedEntries; | |
133 | ||
134 | $this->producer->publish(json_encode($importedEntry)); | |
ae669126 | 135 | } |
59201088 | 136 | } |
ae669126 | 137 | |
59201088 TC |
138 | /** |
139 | * {@inheritdoc} | |
140 | */ | |
141 | public function parseEntry(array $importedEntry) | |
142 | { | |
ae669126 TC |
143 | if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) { |
144 | $this->parseEntries($importedEntry); | |
2c61db30 | 145 | |
ae669126 TC |
146 | return; |
147 | } | |
59201088 | 148 | |
ae669126 TC |
149 | if (key_exists('children', $importedEntry)) { |
150 | $this->parseEntries($importedEntry['children']); | |
2c61db30 | 151 | |
ae669126 TC |
152 | return; |
153 | } | |
ae669126 | 154 | |
59201088 TC |
155 | if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) { |
156 | return; | |
157 | } | |
ae669126 | 158 | |
59201088 | 159 | $firefox = key_exists('uri', $importedEntry); |
ae669126 | 160 | |
59201088 TC |
161 | $existingEntry = $this->em |
162 | ->getRepository('WallabagCoreBundle:Entry') | |
163 | ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user->getId()); | |
ae669126 | 164 | |
59201088 TC |
165 | if (false !== $existingEntry) { |
166 | ++$this->skippedEntries; | |
ae669126 | 167 | |
59201088 TC |
168 | return; |
169 | } | |
ae669126 | 170 | |
59201088 | 171 | $data = $this->prepareEntry($importedEntry); |
ae669126 | 172 | |
59201088 TC |
173 | $entry = new Entry($this->user); |
174 | $entry->setUrl($data['url']); | |
175 | $entry->setTitle($data['title']); | |
ae669126 | 176 | |
59201088 TC |
177 | // update entry with content (in case fetching failed, the given entry will be return) |
178 | $entry = $this->fetchContent($entry, $data['url'], $data); | |
ae669126 | 179 | |
59201088 TC |
180 | if (array_key_exists('tags', $data)) { |
181 | $this->contentProxy->assignTagsToEntry( | |
182 | $entry, | |
183 | $data['tags'] | |
184 | ); | |
ae669126 | 185 | } |
ae669126 | 186 | |
59201088 | 187 | $entry->setArchived($data['is_archived']); |
ae669126 | 188 | |
59201088 TC |
189 | if (!empty($data['created_at'])) { |
190 | $dt = new \DateTime(); | |
2c61db30 | 191 | $entry->setCreatedAt($dt->setTimestamp($data['created_at'] / 1000)); |
59201088 | 192 | } |
ae669126 | 193 | |
59201088 TC |
194 | $this->em->persist($entry); |
195 | ++$this->importedEntries; | |
ae669126 | 196 | |
59201088 | 197 | return $entry; |
ae669126 TC |
198 | } |
199 | ||
200 | /** | |
201 | * {@inheritdoc} | |
202 | */ | |
59201088 | 203 | protected function setEntryAsRead(array $importedEntry) |
ae669126 | 204 | { |
59201088 TC |
205 | $importedEntry['is_archived'] = 1; |
206 | ||
207 | return $importedEntry; | |
ae669126 TC |
208 | } |
209 | } |