3 namespace Wallabag\ImportBundle\Import
;
5 use Psr\Log\LoggerInterface
;
6 use Psr\Log\NullLogger
;
7 use Doctrine\ORM\EntityManager
;
8 use Symfony\Component\Translation\TranslatorInterface
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\UserBundle\Entity\User
;
11 use Wallabag\CoreBundle\Tools\Utils
;
13 class WallabagV1Import
implements ImportInterface
18 private $skippedEntries = 0;
19 private $importedEntries = 0;
23 public function __construct(EntityManager
$em, TranslatorInterface
$translator)
26 $this->logger
= new NullLogger();
27 $this->translator
= $translator;
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 $this->translator
->trans('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.');
75 public function import()
78 $this->logger
->error('WallabagV1Import: user is not defined');
83 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
84 $this->logger
->error('WallabagV1Import: 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;
126 private function parseEntries($entries)
130 foreach ($entries as $importedEntry) {
131 $existingEntry = $this->em
132 ->getRepository('WallabagCoreBundle:Entry')
133 ->existByUrlAndUserId($importedEntry['url'], $this->user
->getId());
135 if (false !== $existingEntry) {
136 ++
$this->skippedEntries
;
140 // @see ContentProxy->updateEntry
141 $entry = new Entry($this->user
);
142 $entry->setUrl($importedEntry['url']);
143 $entry->setTitle($importedEntry['title']);
144 $entry->setArchived($importedEntry['is_read']);
145 $entry->setStarred($importedEntry['is_fav']);
146 $entry->setContent($importedEntry['content']);
147 $entry->setReadingTime(Utils
::getReadingTime($importedEntry['content']));
148 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST
));
150 $this->em
->persist($entry);
151 ++
$this->importedEntries
;
153 // flush every 20 entries
154 if (($i %
20) === 0) {