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);
48 $this->logger
->error('Wallabag Browser: no entries in imported file');
53 if ($this->producer
) {
54 $this->parseEntriesForProducer($data);
59 $this->parseEntries($data);
65 * Set file path to the json file.
67 * @param string $filepath
69 public function setFilepath($filepath)
71 $this->filepath
= $filepath;
77 * Parse and insert all given entries.
81 protected function parseEntries($entries)
85 foreach ($entries as $importedEntry) {
86 if ((array) $importedEntry !== $importedEntry) {
90 $entry = $this->parseEntry($importedEntry);
92 if (null === $entry) {
96 // flush every 20 entries
97 if (($i %
20) === 0) {
107 * Parse entries and send them to the queue.
108 * It should just be a simple loop on all item, no call to the database should be done
109 * to speedup queuing.
111 * Faster parse entries for Producer.
112 * We don't care to make check at this time. They'll be done by the consumer.
114 * @param array $entries
116 protected function parseEntriesForProducer(array $entries)
118 foreach ($entries as $importedEntry) {
119 if ((array) $importedEntry !== $importedEntry) {
123 // set userId for the producer (it won't know which user is connected)
124 $importedEntry['userId'] = $this->user
->getId();
126 if ($this->markAsRead
) {
127 $importedEntry = $this->setEntryAsRead($importedEntry);
130 ++
$this->queuedEntries
;
132 $this->producer
->publish(json_encode($importedEntry));
139 public function parseEntry(array $importedEntry)
141 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
142 $this->parseEntries($importedEntry);
147 if (array_key_exists('children', $importedEntry)) {
148 $this->parseEntries($importedEntry['children']);
153 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
157 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
159 $existingEntry = $this->em
160 ->getRepository('WallabagCoreBundle:Entry')
161 ->findByUrlAndUserId($url, $this->user
->getId());
163 if (false !== $existingEntry) {
164 ++
$this->skippedEntries
;
169 $data = $this->prepareEntry($importedEntry);
171 $entry = new Entry($this->user
);
172 $entry->setUrl($data['url']);
173 $entry->setTitle($data['title']);
175 // update entry with content (in case fetching failed, the given entry will be return)
176 $entry = $this->fetchContent($entry, $data['url'], $data);
178 if (array_key_exists('tags', $data)) {
179 $this->contentProxy
->assignTagsToEntry(
185 $entry->setArchived($data['is_archived']);
187 if (!empty($data['created_at'])) {
188 $dt = new \
DateTime();
189 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
192 $this->em
->persist($entry);
193 ++
$this->importedEntries
;
201 protected function setEntryAsRead(array $importedEntry)
203 $importedEntry['is_archived'] = 1;
205 return $importedEntry;