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\Helper\ContentProxy
;
12 class BrowserImport
implements ImportInterface
17 protected $contentProxy;
18 protected $skippedEntries = 0;
19 protected $importedEntries = 0;
20 protected $totalEntries = 0;
22 protected $markAsRead;
25 public function __construct(EntityManager
$em, ContentProxy
$contentProxy)
28 $this->logger
= new NullLogger();
29 $this->contentProxy
= $contentProxy;
32 public function setLogger(LoggerInterface
$logger)
34 $this->logger
= $logger;
38 * We define the user in a custom call because on the import command there is no logged in user.
39 * So we can't retrieve user from the `security.token_storage` service.
45 public function setUser(User
$user)
55 public function getName()
57 return 'Firefox & Google Chrome';
63 public function getUrl()
65 return 'import_browser';
71 public function getDescription()
73 return 'import.browser.description';
79 public function import()
82 $this->logger
->error('WallabagImport: user is not defined');
87 if (!file_exists($this->filepath
) || !is_readable($this->filepath
)) {
88 $this->logger
->error('WallabagImport: unable to read file', ['filepath' => $this->filepath
]);
93 $data = json_decode(file_get_contents($this->filepath
), true);
100 $this->parseEntries($data);
106 private function parseEntries($data)
108 foreach ($data as $importedEntry) {
109 $this->parseEntry($importedEntry);
111 $this->totalEntries +
= count($data);
114 private function parseEntry($importedEntry)
116 if (!is_array($importedEntry)) {
120 /* Firefox uses guid while Chrome uses id */
122 if ((!key_exists('guid', $importedEntry) || (!key_exists('id', $importedEntry))) && is_array(reset($importedEntry))) {
123 $this->parseEntries($importedEntry);
127 if (key_exists('children', $importedEntry)) {
128 $this->parseEntries($importedEntry['children']);
132 if (key_exists('uri', $importedEntry) || key_exists('url', $importedEntry)) {
134 /* Firefox uses uri while Chrome uses url */
136 $firefox = key_exists('uri', $importedEntry);
138 $existingEntry = $this->em
139 ->getRepository('WallabagCoreBundle:Entry')
140 ->findByUrlAndUserId(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], $this->user
->getId());
142 if (false !== $existingEntry) {
143 ++
$this->skippedEntries
;
148 if (false === parse_url(($firefox) ? $importedEntry['uri'] : $importedEntry['url']) || false === filter_var(($firefox) ? $importedEntry['uri'] : $importedEntry['url'], FILTER_VALIDATE_URL
)) {
149 $this->logger
->warning('Imported URL '.($firefox) ? $importedEntry['uri'] : $importedEntry['url'].' is not valid');
150 ++
$this->skippedEntries
;
156 $entry = $this->contentProxy
->updateEntry(
157 new Entry($this->user
),
158 ($firefox) ? $importedEntry['uri'] : $importedEntry['url']
160 } catch (\Exception
$e) {
161 $this->logger
->warning('Error while saving '.($firefox) ? $importedEntry['uri'] : $importedEntry['url']);
162 ++
$this->skippedEntries
;
167 $entry->setArchived($this->markAsRead
);
169 $this->em
->persist($entry);
170 ++
$this->importedEntries
;
172 // flush every 20 entries
173 if (($this->nbEntries %
20) === 0) {
175 $this->em
->clear($entry);
181 Maybe not useful. Delete at will.
185 $this->logger
->info($this->nbEntries
.' / '.$this->totalEntries
);
190 * Set whether articles must be all marked as read.
192 * @param bool $markAsRead
196 public function setMarkAsRead($markAsRead)
198 $this->markAsRead
= $markAsRead;
204 * Set file path to the json file.
206 * @param string $filepath
210 public function setFilepath($filepath)
212 $this->filepath
= $filepath;
220 public function getSummary()
223 'skipped' => $this->skippedEntries
,
224 'imported' => $this->importedEntries
,