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
;
12 class WallabagV1Import
implements ImportInterface
17 private $skippedEntries = 0;
18 private $importedEntries = 0;
21 public function __construct(EntityManager
$em)
24 $this->logger
= new NullLogger();
27 public function setLogger(LoggerInterface
$logger)
29 $this->logger
= $logger;
33 * We define the user in a custom call because on the import command there is no logged in user.
34 * So we can't retrieve user from the `security.token_storage` service.
38 public function setUser(User
$user)
48 public function getName()
56 public function getUrl()
58 return 'import_wallabag_v1';
64 public function getDescription()
66 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.';
72 public function import()
75 $this->logger
->error('WallabagV1Import: user is not defined');
80 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
81 $this->logger
->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath
));
86 $data = json_decode(file_get_contents($this->filepath
), true);
92 $this->parseEntries($data);
100 public function getSummary()
103 'skipped' => $this->skippedEntries
,
104 'imported' => $this->importedEntries
,
109 * Set file path to the json file.
111 * @param string $filepath
113 public function setFilepath($filepath)
115 $this->filepath
= $filepath;
123 private function parseEntries($entries)
127 foreach ($entries as $importedEntry) {
128 $existingEntry = $this->em
129 ->getRepository('WallabagCoreBundle:Entry')
130 ->existByUrlAndUserId($importedEntry['url'], $this->user
->getId());
132 if (false !== $existingEntry) {
133 ++
$this->skippedEntries
;
137 // @see ContentProxy->updateEntry
138 $entry = new Entry($this->user
);
139 $entry->setUrl($importedEntry['url']);
140 $entry->setTitle($importedEntry['title']);
141 $entry->setArchived($importedEntry['is_read']);
142 $entry->setStarred($importedEntry['is_fav']);
143 $entry->setContent($importedEntry['content']);
144 $entry->setReadingTime(Utils
::getReadingTime($importedEntry['content']));
145 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST
));
147 $this->em
->persist($entry);
148 ++
$this->importedEntries
;
150 // flush every 20 entries
151 if (($i %
20) === 0) {