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 getDescription()
58 return 'This importer will import all your wallabag v1 articles.';
64 public function import()
67 $this->logger
->error('WallabagV1Import: user is not defined');
72 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
73 $this->logger
->error('WallabagV1Import: unable to read file', array('filepath' => $this->filepath
));
78 $this->parseEntries(json_decode(file_get_contents($this->filepath
), true));
86 public function getSummary()
89 'skipped' => $this->skippedEntries
,
90 'imported' => $this->importedEntries
,
95 * Set file path to the json file.
97 * @param string $filepath
99 public function setFilepath($filepath)
101 $this->filepath
= $filepath;
109 private function parseEntries($entries)
111 foreach ($entries as $importedEntry) {
112 $existingEntry = $this->em
113 ->getRepository('WallabagCoreBundle:Entry')
114 ->existByUrlAndUserId($importedEntry['url'], $this->user
->getId());
116 if (false !== $existingEntry) {
117 ++
$this->skippedEntries
;
121 // @see ContentProxy->updateEntry
122 $entry = new Entry($this->user
);
123 $entry->setUrl($importedEntry['url']);
124 $entry->setTitle($importedEntry['title']);
125 $entry->setArchived($importedEntry['is_read']);
126 $entry->setStarred($importedEntry['is_fav']);
127 $entry->setContent($importedEntry['content']);
128 $entry->setReadingTime(Utils
::getReadingTime($importedEntry['content']));
129 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST
));
131 $this->em
->persist($entry);
132 ++
$this->importedEntries
;