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 class WallabagV1Import
implements ImportInterface
17 protected $contentProxy;
18 protected $skippedEntries = 0;
19 protected $importedEntries = 0;
21 protected $markAsRead;
23 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
26 $this->logger
= new NullLogger();
27 $this->contentProxy
= $contentProxy;
30 public function setLogger(LoggerInterface
$logger)
32 $this->logger
= $logger;
36 * We define the user in a custom call because on the import command there is no logged in user.
37 * So we can't retrieve user from the `security.token_storage` service.
41 public function setUser(User
$user)
51 public function getName()
59 public function getUrl()
61 return 'import_wallabag_v1';
67 public function getDescription()
69 return 'import.wallabag_v1.description';
75 public function import()
78 $this->logger
->error('WallabagImport: user is not defined');
83 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
84 $this->logger
->error('WallabagImport: unable to read file', array('filepath' => $this->filepath
));
89 $data = json_decode(file_get_contents($this->filepath
), true);
95 $this->parseEntries($data);
103 public function getSummary()
106 'skipped' => $this->skippedEntries
,
107 'imported' => $this->importedEntries
,
112 * Set file path to the json file.
114 * @param string $filepath
116 public function setFilepath($filepath)
118 $this->filepath
= $filepath;
124 * Set whether articles must be all marked as read.
126 * @param bool $markAsRead
128 public function setMarkAsRead($markAsRead)
130 $this->markAsRead
= $markAsRead;
138 protected function parseEntries($entries)
142 //Untitled in all languages from v1. This should never have been translated
143 $untitled = array('Untitled', 'Sans titre', 'podle nadpisu', 'Sin título', 'با عنوان', 'per titolo', 'Sem título', 'Без названия', 'po naslovu', 'Без назви', 'No title found', '');
145 foreach ($entries as $importedEntry) {
146 $existingEntry = $this->em
147 ->getRepository('WallabagCoreBundle:Entry')
148 ->findByUrlAndUserId($importedEntry['url'], $this->user
->getId());
150 if (false !== $existingEntry) {
151 ++
$this->skippedEntries
;
156 'title' => $importedEntry['title'],
157 'html' => $importedEntry['content'],
158 'url' => $importedEntry['url'],
159 'content_type' => '',
163 // force content to be refreshed in case on bad fetch in the v1 installation
164 if (in_array($importedEntry['title'], $untitled)) {
168 $entry = $this->contentProxy
->updateEntry(
169 new Entry($this->user
),
170 $importedEntry['url'],
174 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') {
175 $this->contentProxy
->assignTagsToEntry(
177 $importedEntry['tags']
181 $entry->setArchived($importedEntry['is_read'] || $this->markAsRead
);
182 $entry->setStarred($importedEntry['is_fav']);
184 $this->em
->persist($entry);
185 ++
$this->importedEntries
;
187 // flush every 20 entries
188 if (($i %
20) === 0) {