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 private $contentProxy;
19 protected $skippedEntries = 0;
20 protected $importedEntries = 0;
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 '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('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;
126 protected function parseEntries($entries)
130 * Untitled in all languages from v1. This should never have been translated
132 $untitled = array('Untitled', 'Sans titre', 'podle nadpisu', 'Sin título', 'با عنوان', 'per titolo', 'Sem título', 'Без названия', 'po naslovu', 'Без назви');
134 foreach ($entries as $importedEntry) {
135 $existingEntry = $this->em
136 ->getRepository('WallabagCoreBundle:Entry')
137 ->findByUrlAndUserId($importedEntry['url'], $this->user
->getId());
139 if (false !== $existingEntry) {
140 ++
$this->skippedEntries
;
144 // @see ContentProxy->updateEntry
145 $entry = new Entry($this->user
);
146 $entry->setUrl($importedEntry['url']);
147 if (in_array($importedEntry['title'], $untitled)) {
148 $entry = $this->contentProxy
->updateEntry($entry, $entry->getUrl());
150 $entry->setContent($importedEntry['content']);
151 $entry->setTitle($importedEntry['title']);
152 $entry->setReadingTime(Utils
::getReadingTime($importedEntry['content']));
153 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST
));
155 $entry->setArchived($importedEntry['is_read']);
156 $entry->setStarred($importedEntry['is_fav']);
158 $this->em
->persist($entry);
159 ++
$this->importedEntries
;
161 // flush every 20 entries
162 if (($i %
20) === 0) {