3 namespace Wallabag\ImportBundle\Command
;
5 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
6 use Symfony\Component\Config\Definition\Exception\Exception
;
7 use Symfony\Component\Console\Input\InputArgument
;
8 use Symfony\Component\Console\Input\InputInterface
;
9 use Symfony\Component\Console\Output\OutputInterface
;
11 class ImportCommand
extends ContainerAwareCommand
13 protected function configure()
16 ->setName('wallabag:import')
17 ->setDescription('Import entries from a JSON export')
18 ->addArgument('username', InputArgument
::REQUIRED
, 'User to populate')
19 ->addArgument('filepath', InputArgument
::REQUIRED
, 'Path to the JSON file')
20 ->addOption('importer', null, InputArgument
::OPTIONAL
, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1')
21 ->addOption('markAsRead', null, InputArgument
::OPTIONAL
, 'Mark all entries as read', false)
22 ->addOption('useUserId', null, InputArgument
::OPTIONAL
, 'Use user id instead of username to find account', false)
26 protected function execute(InputInterface
$input, OutputInterface
$output)
28 $output->writeln('Start : '.(new \
DateTime())->format('d-m-Y G:i:s').' ---');
30 if (!file_exists($input->getArgument('filepath'))) {
31 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
34 $em = $this->getContainer()->get('doctrine')->getManager();
35 // Turning off doctrine default logs queries for saving memory
36 $em->getConnection()->getConfiguration()->setSQLLogger(null);
38 if ($input->getOption('useUserId')) {
39 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
41 $user = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
44 if (!is_object($user)) {
45 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
48 switch ($input->getOption('importer')) {
50 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
53 $import = $this->getContainer()->get('wallabag_import.firefox.import');
56 $import = $this->getContainer()->get('wallabag_import.chrome.import');
59 $import = $this->getContainer()->get('wallabag_import.readability.import');
62 $import = $this->getContainer()->get('wallabag_import.instapaper.import');
65 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
68 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
71 $import->setMarkAsRead($input->getOption('markAsRead'));
72 $import->setUser($user);
75 ->setFilepath($input->getArgument('filepath'))
79 $summary = $import->getSummary();
80 $output->writeln('<info>'.$summary['imported'].' imported</info>');
81 $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>');
86 $output->writeln('End : '.(new \
DateTime())->format('d-m-Y G:i:s').' ---');