]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Import/BrowserImport.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Import / BrowserImport.php
CommitLineData
ae669126
TC
1<?php
2
3namespace Wallabag\ImportBundle\Import;
4
ae669126 5use Wallabag\CoreBundle\Entity\Entry;
7816eb62 6use Wallabag\CoreBundle\Event\EntrySavedEvent;
ae669126 7
59201088 8abstract class BrowserImport extends AbstractImport
ae669126 9{
ae669126 10 protected $filepath;
ae669126
TC
11
12 /**
13 * {@inheritdoc}
14 */
59201088 15 abstract public function getName();
ae669126
TC
16
17 /**
18 * {@inheritdoc}
19 */
59201088 20 abstract public function getUrl();
ae669126
TC
21
22 /**
23 * {@inheritdoc}
24 */
59201088 25 abstract public function getDescription();
ae669126
TC
26
27 /**
28 * {@inheritdoc}
29 */
30 public function import()
31 {
32 if (!$this->user) {
64b1229b 33 $this->logger->error('Wallabag Browser Import: user is not defined');
ae669126
TC
34
35 return false;
36 }
37
38 if (!file_exists($this->filepath) || !is_readable($this->filepath)) {
64b1229b 39 $this->logger->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath]);
ae669126
TC
40
41 return false;
42 }
43
44 $data = json_decode(file_get_contents($this->filepath), true);
45
46 if (empty($data)) {
c7ea9b41
JB
47 $this->logger->error('Wallabag Browser: no entries in imported file');
48
ae669126
TC
49 return false;
50 }
51
59201088
TC
52 if ($this->producer) {
53 $this->parseEntriesForProducer($data);
54
55 return true;
56 }
57
ae669126 58 $this->parseEntries($data);
ae669126
TC
59
60 return true;
61 }
62
59201088
TC
63 /**
64 * Set file path to the json file.
65 *
66 * @param string $filepath
67 */
68 public function setFilepath($filepath)
69 {
70 $this->filepath = $filepath;
71
72 return $this;
73 }
74
f808b016
JB
75 /**
76 * {@inheritdoc}
77 */
78 public function parseEntry(array $importedEntry)
79 {
ea925bb1 80 if ((!\array_key_exists('guid', $importedEntry) || (!\array_key_exists('id', $importedEntry))) && \is_array(reset($importedEntry))) {
f808b016
JB
81 if ($this->producer) {
82 $this->parseEntriesForProducer($importedEntry);
83
84 return;
85 }
86
87 $this->parseEntries($importedEntry);
88
89 return;
90 }
91
ea925bb1 92 if (\array_key_exists('children', $importedEntry)) {
f808b016
JB
93 if ($this->producer) {
94 $this->parseEntriesForProducer($importedEntry['children']);
95
96 return;
97 }
98
99 $this->parseEntries($importedEntry['children']);
100
101 return;
102 }
103
ea925bb1 104 if (!\array_key_exists('uri', $importedEntry) && !\array_key_exists('url', $importedEntry)) {
f808b016
JB
105 return;
106 }
107
ea925bb1 108 $url = \array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
f808b016
JB
109
110 $existingEntry = $this->em
111 ->getRepository('WallabagCoreBundle:Entry')
112 ->findByUrlAndUserId($url, $this->user->getId());
113
114 if (false !== $existingEntry) {
115 ++$this->skippedEntries;
116
117 return;
118 }
119
120 $data = $this->prepareEntry($importedEntry);
121
122 $entry = new Entry($this->user);
123 $entry->setUrl($data['url']);
124 $entry->setTitle($data['title']);
125
126 // update entry with content (in case fetching failed, the given entry will be return)
127 $this->fetchContent($entry, $data['url'], $data);
128
ea925bb1 129 if (\array_key_exists('tags', $data)) {
f808b016
JB
130 $this->tagsAssigner->assignTagsToEntry(
131 $entry,
132 $data['tags']
133 );
134 }
135
7975395d 136 $entry->updateArchived($data['is_archived']);
f808b016
JB
137
138 if (!empty($data['created_at'])) {
139 $dt = new \DateTime();
140 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
141 }
142
143 $this->em->persist($entry);
144 ++$this->importedEntries;
145
146 return $entry;
147 }
148
59201088
TC
149 /**
150 * Parse and insert all given entries.
59201088 151 */
9f8f188d 152 protected function parseEntries(array $entries)
ae669126 153 {
59201088 154 $i = 1;
7816eb62 155 $entryToBeFlushed = [];
59201088
TC
156
157 foreach ($entries as $importedEntry) {
158 if ((array) $importedEntry !== $importedEntry) {
8c0ba953 159 continue;
59201088
TC
160 }
161
162 $entry = $this->parseEntry($importedEntry);
163
164 if (null === $entry) {
8c0ba953 165 continue;
59201088
TC
166 }
167
7816eb62
JB
168 // @see AbstractImport
169 $entryToBeFlushed[] = $entry;
170
59201088 171 // flush every 20 entries
3ef055ce 172 if (0 === ($i % 20)) {
59201088 173 $this->em->flush();
7816eb62
JB
174
175 foreach ($entryToBeFlushed as $entry) {
176 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
177 }
178
179 $entryToBeFlushed = [];
59201088
TC
180 }
181 ++$i;
ae669126 182 }
59201088
TC
183
184 $this->em->flush();
7816eb62
JB
185
186 if (!empty($entryToBeFlushed)) {
187 foreach ($entryToBeFlushed as $entry) {
188 $this->eventDispatcher->dispatch(EntrySavedEvent::NAME, new EntrySavedEvent($entry));
189 }
190 }
ae669126
TC
191 }
192
59201088
TC
193 /**
194 * Parse entries and send them to the queue.
195 * It should just be a simple loop on all item, no call to the database should be done
196 * to speedup queuing.
197 *
198 * Faster parse entries for Producer.
199 * We don't care to make check at this time. They'll be done by the consumer.
59201088
TC
200 */
201 protected function parseEntriesForProducer(array $entries)
ae669126 202 {
59201088 203 foreach ($entries as $importedEntry) {
59201088 204 if ((array) $importedEntry !== $importedEntry) {
8c0ba953 205 continue;
59201088
TC
206 }
207
208 // set userId for the producer (it won't know which user is connected)
209 $importedEntry['userId'] = $this->user->getId();
210
211 if ($this->markAsRead) {
212 $importedEntry = $this->setEntryAsRead($importedEntry);
213 }
214
215 ++$this->queuedEntries;
216
217 $this->producer->publish(json_encode($importedEntry));
ae669126 218 }
59201088 219 }
ae669126 220
ae669126
TC
221 /**
222 * {@inheritdoc}
223 */
59201088 224 protected function setEntryAsRead(array $importedEntry)
ae669126 225 {
59201088
TC
226 $importedEntry['is_archived'] = 1;
227
228 return $importedEntry;
ae669126 229 }
fe312015
NH
230
231 abstract protected function prepareEntry(array $entry = []);
ae669126 232}