3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Helper\ContentProxy
;
9 abstract class BrowserImport
extends AbstractImport
16 abstract public function getName();
21 abstract public function getUrl();
26 abstract public function getDescription();
31 public function import()
34 $this->logger
->error('Wallabag Browser Import: user is not defined');
39 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
40 $this->logger
->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath
]);
45 $data = json_decode(file_get_contents($this->filepath
), true);
51 if ($this->producer
) {
52 $this->parseEntriesForProducer($data);
57 $this->parseEntries($data);
63 * Set file path to the json file.
65 * @param string $filepath
67 public function setFilepath($filepath)
69 $this->filepath
= $filepath;
75 * Parse and insert all given entries.
79 protected function parseEntries($entries)
83 foreach ($entries as $importedEntry) {
84 if ((array) $importedEntry !== $importedEntry) {
88 $entry = $this->parseEntry($importedEntry);
90 if (null === $entry) {
94 // flush every 20 entries
95 if (($i %
20) === 0) {
105 * Parse entries and send them to the queue.
106 * It should just be a simple loop on all item, no call to the database should be done
107 * to speedup queuing.
109 * Faster parse entries for Producer.
110 * We don't care to make check at this time. They'll be done by the consumer.
112 * @param array $entries
114 protected function parseEntriesForProducer(array $entries)
116 foreach ($entries as $importedEntry) {
117 if ((array) $importedEntry !== $importedEntry) {
121 // set userId for the producer (it won't know which user is connected)
122 $importedEntry['userId'] = $this->user
->getId();
124 if ($this->markAsRead
) {
125 $importedEntry = $this->setEntryAsRead($importedEntry);
128 ++
$this->queuedEntries
;
130 $this->producer
->publish(json_encode($importedEntry));
137 public function parseEntry(array $importedEntry)
139 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
140 $this->parseEntries($importedEntry);
145 if (key_exists('children', $importedEntry)) {
146 $this->parseEntries($importedEntry['children']);
151 if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) {
155 $firefox = key_exists('uri', $importedEntry);
157 $existingEntry = $this->em
158 ->getRepository('WallabagCoreBundle:Entry')
159 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user
->getId());
161 if (false !== $existingEntry) {
162 ++
$this->skippedEntries
;
167 $data = $this->prepareEntry($importedEntry);
169 $entry = new Entry($this->user
);
170 $entry->setUrl($data['url']);
171 $entry->setTitle($data['title']);
173 // update entry with content (in case fetching failed, the given entry will be return)
174 $entry = $this->fetchContent($entry, $data['url'], $data);
176 if (array_key_exists('tags', $data)) {
177 $this->contentProxy
->assignTagsToEntry(
183 $entry->setArchived($data['is_archived']);
185 if (!empty($data['created_at'])) {
186 $dt = new \
DateTime();
187 $entry->setCreatedAt($dt->setTimestamp($data['created_at'] / 1000));
190 $this->em
->persist($entry);
191 ++
$this->importedEntries
;
199 protected function prepareEntry($entry = [])
202 'title' => $entry['name'],
204 'url' => $entry['url'],
205 'is_archived' => $this->markAsRead
,
207 // date are in format like "13118829474385693"
208 // and it'll be devided by 1000 in AbstractImport
209 'created_at' => (int) ceil($entry['date_added'] / 10000),
212 if (array_key_exists('tags', $entry) && $entry['tags'] != '') {
213 $data['tags'] = $entry['tags'];
222 protected function setEntryAsRead(array $importedEntry)
224 $importedEntry['is_archived'] = 1;
226 return $importedEntry;