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\Tools\Utils
;
11 use Wallabag\CoreBundle\Helper\ContentProxy
;
13 class WallabagV1Import
implements ImportInterface
18 protected $contentProxy;
19 protected $skippedEntries = 0;
20 protected $importedEntries = 0;
22 protected $markAsRead;
24 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
27 $this->logger
= new NullLogger();
28 $this->contentProxy
= $contentProxy;
31 public function setLogger(LoggerInterface
$logger)
33 $this->logger
= $logger;
37 * We define the user in a custom call because on the import command there is no logged in user.
38 * So we can't retrieve user from the `security.token_storage` service.
42 public function setUser(User
$user)
52 public function getName()
60 public function getUrl()
62 return 'import_wallabag_v1';
68 public function getDescription()
70 return 'This importer will import all your wallabag v1 articles. On your config page, click on "JSON export" in the "Export your wallabag data" section. You will have a "wallabag-export-1-xxxx-xx-xx.json" file.';
76 public function import()
79 $this->logger
->error('WallabagImport: user is not defined');
84 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
85 $this->logger
->error('WallabagImport: unable to read file', array('filepath' => $this->filepath
));
90 $data = json_decode(file_get_contents($this->filepath
), true);
96 $this->parseEntries($data);
104 public function getSummary()
107 'skipped' => $this->skippedEntries
,
108 'imported' => $this->importedEntries
,
113 * Set file path to the json file.
115 * @param string $filepath
117 public function setFilepath($filepath)
119 $this->filepath
= $filepath;
125 * Set whether articles must be all marked as read.
127 * @param bool $markAsRead
129 public function setMarkAsRead($markAsRead)
131 var_dump($markAsRead);
132 $this->markAsRead
= $markAsRead;
140 protected function parseEntries($entries)
144 //Untitled in all languages from v1. This should never have been translated
145 $untitled = array('Untitled', 'Sans titre', 'podle nadpisu', 'Sin título', 'با عنوان', 'per titolo', 'Sem título', 'Без названия', 'po naslovu', 'Без назви', 'No title found', '');
147 foreach ($entries as $importedEntry) {
148 $existingEntry = $this->em
149 ->getRepository('WallabagCoreBundle:Entry')
150 ->findByUrlAndUserId($importedEntry['url'], $this->user
->getId());
152 if (false !== $existingEntry) {
153 ++
$this->skippedEntries
;
157 // @see ContentProxy->updateEntry
158 $entry = new Entry($this->user
);
159 $entry->setUrl($importedEntry['url']);
161 if (in_array($importedEntry['title'], $untitled)) {
162 $entry = $this->contentProxy
->updateEntry($entry, $importedEntry['url']);
164 $entry->setContent($importedEntry['content']);
165 $entry->setTitle($importedEntry['title']);
166 $entry->setReadingTime(Utils
::getReadingTime($importedEntry['content']));
167 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST
));
170 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') {
171 $this->contentProxy
->assignTagsToEntry(
173 $importedEntry['tags']
177 $entry->setArchived($importedEntry['is_read'] || $this->markAsRead
);
178 $entry->setStarred($importedEntry['is_fav']);
180 $this->em
->persist($entry);
181 ++
$this->importedEntries
;
183 // flush every 20 entries
184 if (($i %
20) === 0) {