]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/BrowserImport.php
bring chrome and firefox as separate imports
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / BrowserImport.php
CommitLineData
ae669126
TC
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
5use Psr\Log\LoggerInterface;
6use Psr\Log\NullLogger;
7use Doctrine\ORM\EntityManager;
8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\UserBundle\Entity\User;
10use Wallabag\CoreBundle\Helper\ContentProxy;
11
59201088 12abstract class BrowserImport extends AbstractImport
ae669126 13{
ae669126 14 protected $filepath;
ae669126
TC
15
16 /**
17 * {@inheritdoc}
18 */
59201088 19 abstract public function getName();
ae669126
TC
20
21 /**
22 * {@inheritdoc}
23 */
59201088 24 abstract public function getUrl();
ae669126
TC
25
26 /**
27 * {@inheritdoc}
28 */
59201088 29 abstract public function getDescription();
ae669126
TC
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
59201088
TC
54 if ($this->producer) {
55 $this->parseEntriesForProducer($data);
56
57 return true;
58 }
59
ae669126 60 $this->parseEntries($data);
ae669126
TC
61
62 return true;
63 }
64
59201088
TC
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)
ae669126 83 {
59201088
TC
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;
ae669126 106 }
59201088
TC
107
108 $this->em->flush();
ae669126
TC
109 }
110
59201088
TC
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)
ae669126 122 {
59201088
TC
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));
ae669126 139 }
59201088 140 }
ae669126 141
59201088
TC
142 /**
143 * {@inheritdoc}
144 */
145 public function parseEntry(array $importedEntry)
146 {
ae669126
TC
147
148 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
149 $this->parseEntries($importedEntry);
ae669126
TC
150 return;
151 }
59201088 152
ae669126
TC
153 if (key_exists('children', $importedEntry)) {
154 $this->parseEntries($importedEntry['children']);
ae669126
TC
155 return;
156 }
ae669126 157
59201088
TC
158 if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) {
159 return;
160 }
ae669126 161
59201088 162 $firefox = key_exists('uri', $importedEntry);
ae669126 163
59201088
TC
164 $existingEntry = $this->em
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user->getId());
ae669126 167
59201088
TC
168 if (false !== $existingEntry) {
169 ++$this->skippedEntries;
ae669126 170
59201088
TC
171 return;
172 }
ae669126 173
59201088 174 $data = $this->prepareEntry($importedEntry);
ae669126 175
59201088
TC
176 $entry = new Entry($this->user);
177 $entry->setUrl($data['url']);
178 $entry->setTitle($data['title']);
ae669126 179
59201088
TC
180 // update entry with content (in case fetching failed, the given entry will be return)
181 $entry = $this->fetchContent($entry, $data['url'], $data);
ae669126 182
59201088
TC
183 if (array_key_exists('tags', $data)) {
184 $this->contentProxy->assignTagsToEntry(
185 $entry,
186 $data['tags']
187 );
ae669126 188 }
ae669126 189
59201088 190 $entry->setArchived($data['is_archived']);
ae669126 191
59201088
TC
192 if (!empty($data['created_at'])) {
193 $dt = new \DateTime();
194 $entry->setCreatedAt($dt->setTimestamp($data['created_at']/1000));
195 }
ae669126 196
59201088
TC
197 $this->em->persist($entry);
198 ++$this->importedEntries;
ae669126 199
59201088 200 return $entry;
ae669126
TC
201 }
202
203 /**
204 * {@inheritdoc}
205 */
59201088 206 protected function setEntryAsRead(array $importedEntry)
ae669126 207 {
59201088
TC
208 $importedEntry['is_archived'] = 1;
209
210 return $importedEntry;
ae669126
TC
211 }
212}