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\Input\InputOption
;
10 use Symfony\Component\Console\Output\OutputInterface
;
11 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken
;
13 class ImportCommand
extends ContainerAwareCommand
15 protected function configure()
18 ->setName('wallabag:import')
19 ->setDescription('Import entries from a JSON export')
20 ->addArgument('username', InputArgument
::REQUIRED
, 'User to populate')
21 ->addArgument('filepath', InputArgument
::REQUIRED
, 'Path to the JSON file')
22 ->addOption('importer', null, InputOption
::VALUE_OPTIONAL
, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1')
23 ->addOption('markAsRead', null, InputOption
::VALUE_OPTIONAL
, 'Mark all entries as read', false)
24 ->addOption('useUserId', null, InputOption
::VALUE_NONE
, 'Use user id instead of username to find account')
25 ->addOption('disableContentUpdate', null, InputOption
::VALUE_NONE
, 'Disable fetching updated content from URL')
29 protected function execute(InputInterface
$input, OutputInterface
$output)
31 $output->writeln('Start : ' . (new \
DateTime())->format('d-m-Y G:i:s') . ' ---');
33 if (!file_exists($input->getArgument('filepath'))) {
34 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
37 $em = $this->getContainer()->get('doctrine')->getManager();
38 // Turning off doctrine default logs queries for saving memory
39 $em->getConnection()->getConfiguration()->setSQLLogger(null);
41 if ($input->getOption('useUserId')) {
42 $entityUser = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
44 $entityUser = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
47 if (!\
is_object($entityUser)) {
48 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
51 // Authenticate user for paywalled websites
52 $token = new UsernamePasswordToken(
56 $entityUser->getRoles());
58 $this->getContainer()->get('security.token_storage')->setToken($token);
59 $user = $this->getContainer()->get('security.token_storage')->getToken()->getUser();
61 switch ($input->getOption('importer')) {
63 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
66 $import = $this->getContainer()->get('wallabag_import.firefox.import');
69 $import = $this->getContainer()->get('wallabag_import.chrome.import');
72 $import = $this->getContainer()->get('wallabag_import.readability.import');
75 $import = $this->getContainer()->get('wallabag_import.instapaper.import');
78 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
81 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
84 $import->setMarkAsRead($input->getOption('markAsRead'));
85 $import->setDisableContentUpdate($input->getOption('disableContentUpdate'));
86 $import->setUser($user);
89 ->setFilepath($input->getArgument('filepath'))
93 $summary = $import->getSummary();
94 $output->writeln('<info>' . $summary['imported'] . ' imported</info>');
95 $output->writeln('<comment>' . $summary['skipped'] . ' already saved</comment>');
100 $output->writeln('End : ' . (new \
DateTime())->format('d-m-Y G:i:s') . ' ---');