3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
8 class ReadabilityImport
extends AbstractImport
11 private $skippedEntries = 0;
12 private $importedEntries = 0;
17 * We define the user in a custom call because on the import command there is no logged in user.
18 * So we can't retrieve user from the `security.token_storage` service.
22 public function setUser(User
$user)
32 public function getName()
40 public function getUrl()
42 return 'import_readability';
48 public function getDescription()
50 return 'import.readability.description';
54 * Set file path to the json file.
56 * @param string $filepath
58 public function setFilepath($filepath)
60 $this->filepath
= $filepath;
66 * Set whether articles must be all marked as read.
68 * @param bool $markAsRead
70 public function setMarkAsRead($markAsRead)
72 $this->markAsRead
= $markAsRead;
78 * Get whether articles must be all marked as read.
80 public function getMarkAsRead()
82 return $this->markAsRead
;
88 public function getSummary()
91 'skipped' => $this->skippedEntries
,
92 'imported' => $this->importedEntries
,
99 public function import()
102 $this->logger
->error('ReadabilityImport: user is not defined');
107 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
108 $this->logger
->error('ReadabilityImport: unable to read file', ['filepath' => $this->filepath
]);
113 $data = json_decode(file_get_contents($this->filepath
), true);
115 if (empty($data) || empty($data['bookmarks'])) {
119 $this->parseEntries($data['bookmarks']);
125 * Parse and insert all given entries.
129 protected function parseEntries($entries)
133 foreach ($entries as $importedEntry) {
134 $existingEntry = $this->em
135 ->getRepository('WallabagCoreBundle:Entry')
136 ->findByUrlAndUserId($importedEntry['article__url'], $this->user
->getId());
138 if (false !== $existingEntry) {
139 ++
$this->skippedEntries
;
144 'title' => $importedEntry['article__title'],
145 'url' => $importedEntry['article__url'],
146 'content_type' => '',
148 'is_archived' => $importedEntry['archive'] || $this->markAsRead
,
149 'is_starred' => $importedEntry['favorite'],
152 $entry = $this->fetchContent(
153 new Entry($this->user
),
158 // jump to next entry in case of problem while getting content
159 if (false === $entry) {
160 ++
$this->skippedEntries
;
163 $entry->setArchived($data['is_archived']);
164 $entry->setStarred($data['is_starred']);
166 $this->em
->persist($entry);
167 ++
$this->importedEntries
;
169 // flush every 20 entries
170 if (($i %
20) === 0) {
172 $this->em
->clear($entry);