diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Import/BrowserImport.php')
-rw-r--r-- | src/Wallabag/ImportBundle/Import/BrowserImport.php | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Import/BrowserImport.php b/src/Wallabag/ImportBundle/Import/BrowserImport.php new file mode 100644 index 00000000..9d75685b --- /dev/null +++ b/src/Wallabag/ImportBundle/Import/BrowserImport.php | |||
@@ -0,0 +1,207 @@ | |||
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 | $this->parseEntries($importedEntry); | ||
143 | |||
144 | return; | ||
145 | } | ||
146 | |||
147 | if (array_key_exists('children', $importedEntry)) { | ||
148 | $this->parseEntries($importedEntry['children']); | ||
149 | |||
150 | return; | ||
151 | } | ||
152 | |||
153 | if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) { | ||
154 | return; | ||
155 | } | ||
156 | |||
157 | $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url']; | ||
158 | |||
159 | $existingEntry = $this->em | ||
160 | ->getRepository('WallabagCoreBundle:Entry') | ||
161 | ->findByUrlAndUserId($url, $this->user->getId()); | ||
162 | |||
163 | if (false !== $existingEntry) { | ||
164 | ++$this->skippedEntries; | ||
165 | |||
166 | return; | ||
167 | } | ||
168 | |||
169 | $data = $this->prepareEntry($importedEntry); | ||
170 | |||
171 | $entry = new Entry($this->user); | ||
172 | $entry->setUrl($data['url']); | ||
173 | $entry->setTitle($data['title']); | ||
174 | |||
175 | // update entry with content (in case fetching failed, the given entry will be return) | ||
176 | $entry = $this->fetchContent($entry, $data['url'], $data); | ||
177 | |||
178 | if (array_key_exists('tags', $data)) { | ||
179 | $this->contentProxy->assignTagsToEntry( | ||
180 | $entry, | ||
181 | $data['tags'] | ||
182 | ); | ||
183 | } | ||
184 | |||
185 | $entry->setArchived($data['is_archived']); | ||
186 | |||
187 | if (!empty($data['created_at'])) { | ||
188 | $dt = new \DateTime(); | ||
189 | $entry->setCreatedAt($dt->setTimestamp($data['created_at'])); | ||
190 | } | ||
191 | |||
192 | $this->em->persist($entry); | ||
193 | ++$this->importedEntries; | ||
194 | |||
195 | return $entry; | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * {@inheritdoc} | ||
200 | */ | ||
201 | protected function setEntryAsRead(array $importedEntry) | ||
202 | { | ||
203 | $importedEntry['is_archived'] = 1; | ||
204 | |||
205 | return $importedEntry; | ||
206 | } | ||
207 | } | ||