]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Import/BrowserImport.php
fix tests
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / BrowserImport.php
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 return false;
49 }
50
51 if ($this->producer) {
52 $this->parseEntriesForProducer($data);
53
54 return true;
55 }
56
57 $this->parseEntries($data);
58
59 return true;
60 }
61
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)
80 {
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;
103 }
104
105 $this->em->flush();
106 }
107
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)
119 {
120 foreach ($entries as $importedEntry) {
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));
135 }
136 }
137
138 /**
139 * {@inheritdoc}
140 */
141 public function parseEntry(array $importedEntry)
142 {
143 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
144 $this->parseEntries($importedEntry);
145
146 return;
147 }
148
149 if (key_exists('children', $importedEntry)) {
150 $this->parseEntries($importedEntry['children']);
151
152 return;
153 }
154
155 if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) {
156 return;
157 }
158
159 $firefox = key_exists('uri', $importedEntry);
160
161 $existingEntry = $this->em
162 ->getRepository('WallabagCoreBundle:Entry')
163 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user->getId());
164
165 if (false !== $existingEntry) {
166 ++$this->skippedEntries;
167
168 return;
169 }
170
171 $data = $this->prepareEntry($importedEntry);
172
173 $entry = new Entry($this->user);
174 $entry->setUrl($data['url']);
175 $entry->setTitle($data['title']);
176
177 // update entry with content (in case fetching failed, the given entry will be return)
178 $entry = $this->fetchContent($entry, $data['url'], $data);
179
180 if (array_key_exists('tags', $data)) {
181 $this->contentProxy->assignTagsToEntry(
182 $entry,
183 $data['tags']
184 );
185 }
186
187 $entry->setArchived($data['is_archived']);
188
189 if (!empty($data['created_at'])) {
190 $dt = new \DateTime();
191 $entry->setCreatedAt($dt->setTimestamp($data['created_at'] / 1000));
192 }
193
194 $this->em->persist($entry);
195 ++$this->importedEntries;
196
197 return $entry;
198 }
199
200 /**
201 * {@inheritdoc}
202 */
203 protected function setEntryAsRead(array $importedEntry)
204 {
205 $importedEntry['is_archived'] = 1;
206
207 return $importedEntry;
208 }
209 }