3 namespace Wallabag\ImportBundle\Import
;
5 use Wallabag\CoreBundle\Entity\Entry
;
6 use Wallabag\UserBundle\Entity\User
;
8 abstract class WallabagImport
extends AbstractImport
11 protected $skippedEntries = 0;
12 protected $importedEntries = 0;
14 protected $markAsRead;
15 // untitled in all languages from v1
16 protected $untitled = [
32 * We define the user in a custom call because on the import command there is no logged in user.
33 * So we can't retrieve user from the `security.token_storage` service.
37 public function setUser(User
$user)
47 abstract public function getName();
52 abstract public function getUrl();
57 abstract public function getDescription();
62 public function import()
65 $this->logger
->error('WallabagImport: user is not defined');
70 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
71 $this->logger
->error('WallabagImport: unable to read file', ['filepath' => $this->filepath
]);
76 $data = json_decode(file_get_contents($this->filepath
), true);
82 $this->parseEntries($data);
90 public function getSummary()
93 'skipped' => $this->skippedEntries
,
94 'imported' => $this->importedEntries
,
99 * Set file path to the json file.
101 * @param string $filepath
103 public function setFilepath($filepath)
105 $this->filepath
= $filepath;
111 * Set whether articles must be all marked as read.
113 * @param bool $markAsRead
115 public function setMarkAsRead($markAsRead)
117 $this->markAsRead
= $markAsRead;
123 * Parse and insert all given entries.
127 protected function parseEntries($entries)
131 foreach ($entries as $importedEntry) {
132 $existingEntry = $this->em
133 ->getRepository('WallabagCoreBundle:Entry')
134 ->findByUrlAndUserId($importedEntry['url'], $this->user
->getId());
136 if (false !== $existingEntry) {
137 ++
$this->skippedEntries
;
141 $data = $this->prepareEntry($importedEntry, $this->markAsRead
);
143 $entry = $this->fetchContent(
144 new Entry($this->user
),
145 $importedEntry['url'],
149 // jump to next entry in case of problem while getting content
150 if (false === $entry) {
151 ++
$this->skippedEntries
;
155 if (array_key_exists('tags', $data)) {
156 $this->contentProxy
->assignTagsToEntry(
162 if (isset($importedEntry['preview_picture'])) {
163 $entry->setPreviewPicture($importedEntry['preview_picture']);
166 $entry->setArchived($data['is_archived']);
167 $entry->setStarred($data['is_starred']);
169 $this->em
->persist($entry);
170 ++
$this->importedEntries
;
172 // flush every 20 entries
173 if (($i %
20) === 0) {
175 $this->em
->clear($entry);
184 * This should return a cleaned array for a given entry to be given to `updateEntry`.
186 * @param array $entry Data from the imported file
187 * @param bool $markAsRead Should we mark as read content?
191 abstract protected function prepareEntry($entry = [], $markAsRead = false);