3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Helper\ContentProxy
;
8 use Wallabag\CoreBundle\Event\EntrySavedEvent
;
10 abstract class BrowserImport
extends AbstractImport
17 abstract public function getName();
22 abstract public function getUrl();
27 abstract public function getDescription();
32 public function import()
35 $this->logger
->error('Wallabag Browser Import: user is not defined');
40 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
41 $this->logger
->error('Wallabag Browser Import: unable to read file', ['filepath' => $this->filepath
]);
46 $data = json_decode(file_get_contents($this->filepath
), true);
49 $this->logger
->error('Wallabag Browser: no entries in imported file');
54 if ($this->producer
) {
55 $this->parseEntriesForProducer($data);
60 $this->parseEntries($data);
66 * Set file path to the json file.
68 * @param string $filepath
70 public function setFilepath($filepath)
72 $this->filepath
= $filepath;
78 * Parse and insert all given entries.
82 protected function parseEntries($entries)
85 $entryToBeFlushed = [];
87 foreach ($entries as $importedEntry) {
88 if ((array) $importedEntry !== $importedEntry) {
92 $entry = $this->parseEntry($importedEntry);
94 if (null === $entry) {
98 // @see AbstractImport
99 $entryToBeFlushed[] = $entry;
101 // flush every 20 entries
102 if (($i %
20) === 0) {
105 foreach ($entryToBeFlushed as $entry) {
106 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
109 $entryToBeFlushed = [];
116 if (!empty($entryToBeFlushed)) {
117 foreach ($entryToBeFlushed as $entry) {
118 $this->eventDispatcher
->dispatch(EntrySavedEvent
::NAME
, new EntrySavedEvent($entry));
124 * Parse entries and send them to the queue.
125 * It should just be a simple loop on all item, no call to the database should be done
126 * to speedup queuing.
128 * Faster parse entries for Producer.
129 * We don't care to make check at this time. They'll be done by the consumer.
131 * @param array $entries
133 protected function parseEntriesForProducer(array $entries)
135 foreach ($entries as $importedEntry) {
136 if ((array) $importedEntry !== $importedEntry) {
140 // set userId for the producer (it won't know which user is connected)
141 $importedEntry['userId'] = $this->user
->getId();
143 if ($this->markAsRead
) {
144 $importedEntry = $this->setEntryAsRead($importedEntry);
147 ++
$this->queuedEntries
;
149 $this->producer
->publish(json_encode($importedEntry));
156 public function parseEntry(array $importedEntry)
158 if ((!array_key_exists('guid', $importedEntry) || (!array_key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
159 if ($this->producer
) {
160 $this->parseEntriesForProducer($importedEntry);
165 $this->parseEntries($importedEntry);
170 if (array_key_exists('children', $importedEntry)) {
171 if ($this->producer
) {
172 $this->parseEntriesForProducer($importedEntry['children']);
177 $this->parseEntries($importedEntry['children']);
182 if (!array_key_exists('uri', $importedEntry) && !array_key_exists('url', $importedEntry)) {
186 $url = array_key_exists('uri', $importedEntry) ? $importedEntry['uri'] : $importedEntry['url'];
188 $existingEntry = $this->em
189 ->getRepository('WallabagCoreBundle:Entry')
190 ->findByUrlAndUserId($url, $this->user
->getId());
192 if (false !== $existingEntry) {
193 ++
$this->skippedEntries
;
198 $data = $this->prepareEntry($importedEntry);
200 $entry = new Entry($this->user
);
201 $entry->setUrl($data['url']);
202 $entry->setTitle($data['title']);
204 // update entry with content (in case fetching failed, the given entry will be return)
205 $entry = $this->fetchContent($entry, $data['url'], $data);
207 if (array_key_exists('tags', $data)) {
208 $this->contentProxy
->assignTagsToEntry(
214 $entry->setArchived($data['is_archived']);
216 if (!empty($data['created_at'])) {
217 $dt = new \
DateTime();
218 $entry->setCreatedAt($dt->setTimestamp($data['created_at']));
221 $this->em
->persist($entry);
222 ++
$this->importedEntries
;
230 protected function setEntryAsRead(array $importedEntry)
232 $importedEntry['is_archived'] = 1;
234 return $importedEntry;