]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/BrowserImport.php
bring chrome and firefox as separate imports
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / BrowserImport.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Import;
4
5 use Psr\Log\LoggerInterface;
6 use Psr\Log\NullLogger;
7 use Doctrine\ORM\EntityManager;
8 use Wallabag\CoreBundle\Entity\Entry;
9 use Wallabag\UserBundle\Entity\User;
10 use Wallabag\CoreBundle\Helper\ContentProxy;
11
12 abstract class BrowserImport extends AbstractImport
13 {
14 protected $filepath;
15
16 /**
17 * {@inheritdoc}
18 */
19 abstract public function getName();
20
21 /**
22 * {@inheritdoc}
23 */
24 abstract public function getUrl();
25
26 /**
27 * {@inheritdoc}
28 */
29 abstract public function getDescription();
30
31 /**
32 * {@inheritdoc}
33 */
34 public function import()
35 {
36 if (!$this->user) {
37 $this->logger->error('WallabagImport: user is not defined');
38
39 return false;
40 }
41
42 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
43 $this->logger->error('WallabagImport: unable to read file', ['filepath' => $this->filepath]);
44
45 return false;
46 }
47
48 $data = json_decode(file_get_contents($this->filepath), true);
49
50 if (empty($data)) {
51 return false;
52 }
53
54 if ($this->producer) {
55 $this->parseEntriesForProducer($data);
56
57 return true;
58 }
59
60 $this->parseEntries($data);
61
62 return true;
63 }
64
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)
83 {
84 $i = 1;
85
86 foreach ($entries as $importedEntry) {
87 if ((array) $importedEntry !== $importedEntry) {
88 continue;
89 }
90
91 $entry = $this->parseEntry($importedEntry);
92
93 if (null === $entry) {
94 continue;
95 }
96
97 // flush every 20 entries
98 if (($i % 20) === 0) {
99 $this->em->flush();
100
101 // clear only affected entities
102 $this->em->clear(Entry::class);
103 $this->em->clear(Tag::class);
104 }
105 ++$i;
106 }
107
108 $this->em->flush();
109 }
110
111 /**
112 * Parse entries and send them to the queue.
113 * It should just be a simple loop on all item, no call to the database should be done
114 * to speedup queuing.
115 *
116 * Faster parse entries for Producer.
117 * We don't care to make check at this time. They'll be done by the consumer.
118 *
119 * @param array $entries
120 */
121 protected function parseEntriesForProducer(array $entries)
122 {
123 foreach ($entries as $importedEntry) {
124
125 if ((array) $importedEntry !== $importedEntry) {
126 continue;
127 }
128
129 // set userId for the producer (it won't know which user is connected)
130 $importedEntry['userId'] = $this->user->getId();
131
132 if ($this->markAsRead) {
133 $importedEntry = $this->setEntryAsRead($importedEntry);
134 }
135
136 ++$this->queuedEntries;
137
138 $this->producer->publish(json_encode($importedEntry));
139 }
140 }
141
142 /**
143 * {@inheritdoc}
144 */
145 public function parseEntry(array $importedEntry)
146 {
147
148 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
149 $this->parseEntries($importedEntry);
150 return;
151 }
152
153 if (key_exists('children', $importedEntry)) {
154 $this->parseEntries($importedEntry['children']);
155 return;
156 }
157
158 if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) {
159 return;
160 }
161
162 $firefox = key_exists('uri', $importedEntry);
163
164 $existingEntry = $this->em
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user->getId());
167
168 if (false !== $existingEntry) {
169 ++$this->skippedEntries;
170
171 return;
172 }
173
174 $data = $this->prepareEntry($importedEntry);
175
176 $entry = new Entry($this->user);
177 $entry->setUrl($data['url']);
178 $entry->setTitle($data['title']);
179
180 // update entry with content (in case fetching failed, the given entry will be return)
181 $entry = $this->fetchContent($entry, $data['url'], $data);
182
183 if (array_key_exists('tags', $data)) {
184 $this->contentProxy->assignTagsToEntry(
185 $entry,
186 $data['tags']
187 );
188 }
189
190 $entry->setArchived($data['is_archived']);
191
192 if (!empty($data['created_at'])) {
193 $dt = new \DateTime();
194 $entry->setCreatedAt($dt->setTimestamp($data['created_at']/1000));
195 }
196
197 $this->em->persist($entry);
198 ++$this->importedEntries;
199
200 return $entry;
201 }
202
203 /**
204 * {@inheritdoc}
205 */
206 protected function setEntryAsRead(array $importedEntry)
207 {
208 $importedEntry['is_archived'] = 1;
209
210 return $importedEntry;
211 }
212 }