3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
8 class ReadabilityImport
extends AbstractImport
12 private $skippedEntries = 0;
13 private $importedEntries = 0;
18 * We define the user in a custom call because on the import command there is no logged in user.
19 * So we can't retrieve user from the `security.token_storage` service.
23 public function setUser(User
$user)
33 public function getName()
41 public function getUrl()
43 return 'import_readability';
49 public function getDescription()
51 return 'import.readability.description';
55 * Set file path to the json file.
57 * @param string $filepath
59 public function setFilepath($filepath)
61 $this->filepath
= $filepath;
67 * Set whether articles must be all marked as read.
69 * @param bool $markAsRead
71 public function setMarkAsRead($markAsRead)
73 $this->markAsRead
= $markAsRead;
79 * Get whether articles must be all marked as read.
81 public function getMarkAsRead()
83 return $this->markAsRead
;
89 public function getSummary()
92 'skipped' => $this->skippedEntries
,
93 'imported' => $this->importedEntries
,
100 public function import()
103 $this->logger
->error('ReadabilityImport: user is not defined');
108 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
109 $this->logger
->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath
]);
114 $data = json_decode(file_get_contents($this->filepath
), true);
116 if (empty($data) || empty($data['bookmarks'])) {
120 $this->parseEntries($data['bookmarks']);
126 * Parse and insert all given entries.
130 protected function parseEntries($entries)
134 foreach ($entries as $importedEntry) {
135 $existingEntry = $this->em
136 ->getRepository('WallabagCoreBundle:Entry')
137 ->findByUrlAndUserId($importedEntry['article__url'], $this->user
->getId());
139 if (false !== $existingEntry) {
140 ++
$this->skippedEntries
;
145 'title' => $importedEntry['article__title'],
146 // 'html' => $importedEntry['article__excerpt'],
147 'url' => $importedEntry['article__url'],
148 'content_type' => '',
150 'is_archived' => $importedEntry['archive'] || $this->markAsRead
,
151 'is_starred' => $importedEntry['favorite'],
154 $entry = $this->fetchContent(
155 new Entry($this->user
),
160 // jump to next entry in case of problem while getting content
161 if (false === $entry) {
162 ++
$this->skippedEntries
;
165 $entry->setArchived($data['is_archived']);
166 $entry->setStarred($data['is_starred']);
168 $this->em
->persist($entry);
169 ++
$this->importedEntries
;
171 // flush every 20 entries
172 if (($i %
20) === 0) {
174 $this->em
->clear($entry);