3 namespace Wallabag\ImportBundle\Import
;
5 use Psr\Log\LoggerInterface
;
6 use Psr\Log\NullLogger
;
7 use Doctrine\ORM\EntityManager
;
8 use Wallabag\CoreBundle\Entity\Entry
;
9 use Wallabag\UserBundle\Entity\User
;
10 use Wallabag\CoreBundle\Helper\ContentProxy
;
12 abstract class WallabagImport
implements ImportInterface
17 protected $contentProxy;
18 protected $skippedEntries = 0;
19 protected $importedEntries = 0;
21 protected $markAsRead;
22 // untitled in all languages from v1
23 protected $untitled = [
38 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
41 $this->logger
= new NullLogger();
42 $this->contentProxy
= $contentProxy;
45 public function setLogger(LoggerInterface
$logger)
47 $this->logger
= $logger;
51 * We define the user in a custom call because on the import command there is no logged in user.
52 * So we can't retrieve user from the `security.token_storage` service.
56 public function setUser(User
$user)
66 abstract public function getName();
71 abstract public function getUrl();
76 abstract public function getDescription();
81 public function import()
84 $this->logger
->error('WallabagImport: user is not defined');
89 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
90 $this->logger
->error('WallabagImport: unable to read file', ['filepath' => $this->filepath
]);
95 $data = json_decode(file_get_contents($this->filepath
), true);
101 $this->parseEntries($data);
109 public function getSummary()
112 'skipped' => $this->skippedEntries
,
113 'imported' => $this->importedEntries
,
118 * Set file path to the json file.
120 * @param string $filepath
122 public function setFilepath($filepath)
124 $this->filepath
= $filepath;
130 * Set whether articles must be all marked as read.
132 * @param bool $markAsRead
134 public function setMarkAsRead($markAsRead)
136 $this->markAsRead
= $markAsRead;
142 * Parse and insert all given entries.
146 protected function parseEntries($entries)
150 foreach ($entries as $importedEntry) {
151 $existingEntry = $this->em
152 ->getRepository('WallabagCoreBundle:Entry')
153 ->findByUrlAndUserId($importedEntry['url'], $this->user
->getId());
155 if (false !== $existingEntry) {
156 ++
$this->skippedEntries
;
160 $data = $this->prepareEntry($importedEntry, $this->markAsRead
);
162 $entry = $this->contentProxy
->updateEntry(
163 new Entry($this->user
),
164 $importedEntry['url'],
168 if (array_key_exists('tags', $data)) {
169 $this->contentProxy
->assignTagsToEntry(
175 if (isset($importedEntry['preview_picture'])) {
176 $entry->setPreviewPicture($importedEntry['preview_picture']);
179 $entry->setArchived($data['is_archived']);
180 $entry->setStarred($data['is_starred']);
182 $this->em
->persist($entry);
183 ++
$this->importedEntries
;
185 // flush every 20 entries
186 if (($i %
20) === 0) {
188 $this->em
->clear($entry);
197 * This should return a cleaned array for a given entry to be given to `updateEntry`.
199 * @param array $entry Data from the imported file
200 * @param bool $markAsRead Should we mark as read content?
204 abstract protected function prepareEntry($entry = [], $markAsRead = false);