3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
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)
84 $entryToBeFlushed = [];
86 foreach ($entries as $importedEntry) {
87 if ((array) $importedEntry !== $importedEntry) {
91 $entry = $this->parseEntry($importedEntry);
93 if (null === $entry) {
97 // @see AbstractImport
98 $entryToBeFlushed[] = $entry;
100 // flush every 20 entries
101 if (($i %
20) === 0) {
104 foreach ($entryToBeFlushed as $entry) {
105 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
108 $entryToBeFlushed = [];
115 if (!empty($entryToBeFlushed)) {
116 foreach ($entryToBeFlushed as $entry) {
117 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
123 * Parse entries and send them to the queue.
124 * It should just be a simple loop on all item, no call to the database should be done
125 * to speedup queuing.
127 * Faster parse entries for Producer.
128 * We don't care to make check at this time. They'll be done by the consumer.
130 * @param array $entries
132 protected function parseEntriesForProducer(array $entries)
134 foreach ($entries as $importedEntry) {
135 if ((array) $importedEntry !== $importedEntry) {
139 // set userId for the producer (it won't know which user is connected)
140 $importedEntry['userId'] = $this->user
->getId();
142 if ($this->markAsRead
) {
143 $importedEntry = $this->setEntryAsRead($importedEntry);
146 ++
$this->queuedEntries
;
148 $this->producer
->publish(json_encode($importedEntry));
155 public function parseEntry(array $importedEntry)
157 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
158 if ($this->producer
) {
159 $this->parseEntriesForProducer($importedEntry);
164 $this->parseEntries($importedEntry);
169 if (array_key_exists('children', $importedEntry)) {
170 if ($this->producer
) {
171 $this->parseEntriesForProducer($importedEntry['children']);
176 $this->parseEntries($importedEntry['children']);
181 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
185 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
187 $existingEntry = $this->em
188 ->getRepository('WallabagCoreBundle:Entry')
189 ->findByUrlAndUserId($url, $this->user
->getId());
191 if (false !== $existingEntry) {
192 ++
$this->skippedEntries
;
197 $data = $this->prepareEntry($importedEntry);
199 $entry = new Entry($this->user
);
200 $entry->setUrl($data['url']);
201 $entry->setTitle($data['title']);
203 // update entry with content (in case fetching failed, the given entry will be return)
204 $entry = $this->fetchContent($entry, $data['url'], $data);
206 if (array_key_exists('tags', $data)) {
207 $this->tagsAssigner
->assignTagsToEntry(
213 $entry->setArchived($data['is_archived']);
215 if (!empty($data['created_at'])) {
216 $dt = new \
DateTime();
217 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
220 $this->em
->persist($entry);
221 ++
$this->importedEntries
;
229 protected function setEntryAsRead(array $importedEntry)
231 $importedEntry['is_archived'] = 1;
233 return $importedEntry;