3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
7 use Wallabag\UserBundle\Entity\User
;
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;
79 public function parseEntry(array $importedEntry)
81 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
82 if ($this->producer
) {
83 $this->parseEntriesForProducer($importedEntry);
88 $this->parseEntries($importedEntry);
93 if (array_key_exists('children', $importedEntry)) {
94 if ($this->producer
) {
95 $this->parseEntriesForProducer($importedEntry['children']);
100 $this->parseEntries($importedEntry['children']);
105 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
109 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
111 $existingEntry = $this->em
112 ->getRepository('WallabagCoreBundle:Entry')
113 ->findByUrlAndUserId($url, $this->user
->getId());
115 if (false !== $existingEntry) {
116 ++
$this->skippedEntries
;
121 $data = $this->prepareEntry($importedEntry);
123 $entry = new Entry($this->user
);
124 $entry->setUrl($data['url']);
125 $entry->setTitle($data['title']);
127 // update entry with content (in case fetching failed, the given entry will be return)
128 $this->fetchContent($entry, $data['url'], $data);
130 if (array_key_exists('tags', $data)) {
131 $this->tagsAssigner
->assignTagsToEntry(
137 $entry->setArchived($data['is_archived']);
139 if (!empty($data['created_at'])) {
140 $dt = new \
DateTime();
141 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
144 $this->em
->persist($entry);
145 ++
$this->importedEntries
;
151 * Parse and insert all given entries.
155 protected function parseEntries($entries)
158 $entryToBeFlushed = [];
160 foreach ($entries as $importedEntry) {
161 if ((array) $importedEntry !== $importedEntry) {
165 $entry = $this->parseEntry($importedEntry);
167 if (null === $entry) {
171 // @see AbstractImport
172 $entryToBeFlushed[] = $entry;
174 // flush every 20 entries
175 if (($i %
20) === 0) {
178 foreach ($entryToBeFlushed as $entry) {
179 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
182 $entryToBeFlushed = [];
189 if (!empty($entryToBeFlushed)) {
190 foreach ($entryToBeFlushed as $entry) {
191 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
197 * Parse entries and send them to the queue.
198 * It should just be a simple loop on all item, no call to the database should be done
199 * to speedup queuing.
201 * Faster parse entries for Producer.
202 * We don't care to make check at this time. They'll be done by the consumer.
204 * @param array $entries
206 protected function parseEntriesForProducer(array $entries)
208 foreach ($entries as $importedEntry) {
209 if ((array) $importedEntry !== $importedEntry) {
213 // set userId for the producer (it won't know which user is connected)
214 $importedEntry['userId'] = $this->user
->getId();
216 if ($this->markAsRead
) {
217 $importedEntry = $this->setEntryAsRead($importedEntry);
220 ++
$this->queuedEntries
;
222 $this->producer
->publish(json_encode($importedEntry));
229 protected function setEntryAsRead(array $importedEntry)
231 $importedEntry['is_archived'] = 1;
233 return $importedEntry;