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('WallabagImport: user is not defined');
39 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
40 $this->logger
->error('WallabagImport: 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) {
98 // clear only affected entities
99 $this->em
->clear(Entry
::class);
100 $this->em
->clear(Tag
::class);
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.
113 * Faster parse entries for Producer.
114 * We don't care to make check at this time. They'll be done by the consumer.
116 * @param array $entries
118 protected function parseEntriesForProducer(array $entries)
120 foreach ($entries as $importedEntry) {
121 if ((array) $importedEntry !== $importedEntry) {
125 // set userId for the producer (it won't know which user is connected)
126 $importedEntry['userId'] = $this->user
->getId();
128 if ($this->markAsRead
) {
129 $importedEntry = $this->setEntryAsRead($importedEntry);
132 ++
$this->queuedEntries
;
134 $this->producer
->publish(json_encode($importedEntry));
141 public function parseEntry(array $importedEntry)
143 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
144 $this->parseEntries($importedEntry);
149 if (key_exists('children', $importedEntry)) {
150 $this->parseEntries($importedEntry['children']);
155 if (!key_exists('uri', $importedEntry) && !key_exists('url', $importedEntry)) {
159 $firefox = key_exists('uri', $importedEntry);
161 $existingEntry = $this->em
162 ->getRepository('WallabagCoreBundle:Entry')
163 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user
->getId());
165 if (false !== $existingEntry) {
166 ++
$this->skippedEntries
;
171 $data = $this->prepareEntry($importedEntry);
173 $entry = new Entry($this->user
);
174 $entry->setUrl($data['url']);
175 $entry->setTitle($data['title']);
177 // update entry with content (in case fetching failed, the given entry will be return)
178 $entry = $this->fetchContent($entry, $data['url'], $data);
180 if (array_key_exists('tags', $data)) {
181 $this->contentProxy
->assignTagsToEntry(
187 $entry->setArchived($data['is_archived']);
189 if (!empty($data['created_at'])) {
190 $dt = new \
DateTime();
191 $entry->setCreatedAt($dt->setTimestamp($data['created_at'] / 1000));
194 $this->em
->persist($entry);
195 ++
$this->importedEntries
;
203 protected function setEntryAsRead(array $importedEntry)
205 $importedEntry['is_archived'] = 1;
207 return $importedEntry;