3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
8 abstract class BrowserImport
extends AbstractImport
15 abstract public function getName();
20 abstract public function getUrl();
25 abstract public function getDescription();
30 public function import()
33 $this->logger
->error('Wallabag Browser Import: user is not defined');
38 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
39 $this->logger
->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath
]);
44 $data = json_decode(file_get_contents($this->filepath
), true);
47 $this->logger
->error('Wallabag Browser: no entries in imported file');
52 if ($this->producer
) {
53 $this->parseEntriesForProducer($data);
58 $this->parseEntries($data);
64 * Set file path to the json file.
66 * @param string $filepath
68 public function setFilepath($filepath)
70 $this->filepath
= $filepath;
78 public function parseEntry(array $importedEntry)
80 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
81 if ($this->producer
) {
82 $this->parseEntriesForProducer($importedEntry);
87 $this->parseEntries($importedEntry);
92 if (array_key_exists('children', $importedEntry)) {
93 if ($this->producer
) {
94 $this->parseEntriesForProducer($importedEntry['children']);
99 $this->parseEntries($importedEntry['children']);
104 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
108 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
110 $existingEntry = $this->em
111 ->getRepository('WallabagCoreBundle:Entry')
112 ->findByUrlAndUserId($url, $this->user
->getId());
114 if (false !== $existingEntry) {
115 ++
$this->skippedEntries
;
120 $data = $this->prepareEntry($importedEntry);
122 $entry = new Entry($this->user
);
123 $entry->setUrl($data['url']);
124 $entry->setTitle($data['title']);
126 // update entry with content (in case fetching failed, the given entry will be return)
127 $this->fetchContent($entry, $data['url'], $data);
129 if (array_key_exists('tags', $data)) {
130 $this->tagsAssigner
->assignTagsToEntry(
136 $entry->setArchived($data['is_archived']);
138 if (!empty($data['created_at'])) {
139 $dt = new \
DateTime();
140 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
143 $this->em
->persist($entry);
144 ++
$this->importedEntries
;
150 * Parse and insert all given entries.
154 protected function parseEntries($entries)
157 $entryToBeFlushed = [];
159 foreach ($entries as $importedEntry) {
160 if ((array) $importedEntry !== $importedEntry) {
164 $entry = $this->parseEntry($importedEntry);
166 if (null === $entry) {
170 // @see AbstractImport
171 $entryToBeFlushed[] = $entry;
173 // flush every 20 entries
174 if (0 === ($i %
20)) {
177 foreach ($entryToBeFlushed as $entry) {
178 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
181 $entryToBeFlushed = [];
188 if (!empty($entryToBeFlushed)) {
189 foreach ($entryToBeFlushed as $entry) {
190 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
196 * Parse entries and send them to the queue.
197 * It should just be a simple loop on all item, no call to the database should be done
198 * to speedup queuing.
200 * Faster parse entries for Producer.
201 * We don't care to make check at this time. They'll be done by the consumer.
203 * @param array $entries
205 protected function parseEntriesForProducer(array $entries)
207 foreach ($entries as $importedEntry) {
208 if ((array) $importedEntry !== $importedEntry) {
212 // set userId for the producer (it won't know which user is connected)
213 $importedEntry['userId'] = $this->user
->getId();
215 if ($this->markAsRead
) {
216 $importedEntry = $this->setEntryAsRead($importedEntry);
219 ++
$this->queuedEntries
;
221 $this->producer
->publish(json_encode($importedEntry));
228 protected function setEntryAsRead(array $importedEntry)
230 $importedEntry['is_archived'] = 1;
232 return $importedEntry;
235 abstract protected function prepareEntry(array $entry = []);