]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Command/ImportCommand.php
Merge pull request #4438 from wallabag/dependabot/composer/scheb/two-factor-bundle...
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Command / ImportCommand.php
CommitLineData
56ea1de9
NL
1<?php
2
77a7752a 3namespace Wallabag\ImportBundle\Command;
56ea1de9
NL
4
5use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8c3c77c1 6use Symfony\Component\Config\Definition\Exception\Exception;
a1bb1b3c 7use Symfony\Component\Console\Input\InputArgument;
56ea1de9 8use Symfony\Component\Console\Input\InputInterface;
f808b016 9use Symfony\Component\Console\Input\InputOption;
56ea1de9 10use Symfony\Component\Console\Output\OutputInterface;
607e1a1a 11use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
56ea1de9
NL
12
13class ImportCommand extends ContainerAwareCommand
14{
15 protected function configure()
16 {
17 $this
3fad6c74 18 ->setName('wallabag:import')
1f66d79e 19 ->setDescription('Import entries from a JSON export')
d1e5059e 20 ->addArgument('username', InputArgument::REQUIRED, 'User to populate')
b1d05721 21 ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file')
f5924e95
JB
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')
d0e9b3d6 25 ->addOption('disableContentUpdate', null, InputOption::VALUE_NONE, 'Disable fetching updated content from URL')
b1d05721 26 ;
56ea1de9
NL
27 }
28
29 protected function execute(InputInterface $input, OutputInterface $output)
30 {
f808b016 31 $output->writeln('Start : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
56ea1de9 32
ebf5e508
JB
33 if (!file_exists($input->getArgument('filepath'))) {
34 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
35 }
36
56ea1de9
NL
37 $em = $this->getContainer()->get('doctrine')->getManager();
38 // Turning off doctrine default logs queries for saving memory
39 $em->getConnection()->getConfiguration()->setSQLLogger(null);
40
d1e5059e 41 if ($input->getOption('useUserId')) {
607e1a1a 42 $entityUser = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
d1e5059e 43 } else {
607e1a1a 44 $entityUser = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
d1e5059e 45 }
8c3c77c1 46
607e1a1a 47 if (!\is_object($entityUser)) {
d1e5059e 48 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
8c3c77c1
NL
49 }
50
607e1a1a
NL
51 // Authenticate user for paywalled websites
52 $token = new UsernamePasswordToken(
53 $entityUser,
54 null,
55 'main',
56 $entityUser->getRoles());
57
58 $this->getContainer()->get('security.token_storage')->setToken($token);
59 $user = $this->getContainer()->get('security.token_storage')->getToken()->getUser();
60
ae669126
TC
61 switch ($input->getOption('importer')) {
62 case 'v2':
1f66d79e 63 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
ae669126 64 break;
59201088 65 case 'firefox':
1f66d79e 66 $import = $this->getContainer()->get('wallabag_import.firefox.import');
59201088
TC
67 break;
68 case 'chrome':
1f66d79e
JB
69 $import = $this->getContainer()->get('wallabag_import.chrome.import');
70 break;
71 case 'readability':
72 $import = $this->getContainer()->get('wallabag_import.readability.import');
73 break;
74 case 'instapaper':
75 $import = $this->getContainer()->get('wallabag_import.instapaper.import');
ae669126 76 break;
9ab024b4
JB
77 case 'pinboard':
78 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
79 break;
ae669126 80 default:
1f66d79e 81 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
3fad6c74
NL
82 }
83
1f66d79e 84 $import->setMarkAsRead($input->getOption('markAsRead'));
d0e9b3d6 85 $import->setDisableContentUpdate($input->getOption('disableContentUpdate'));
1f66d79e 86 $import->setUser($user);
3fad6c74 87
1f66d79e 88 $res = $import
b1d05721
JB
89 ->setFilepath($input->getArgument('filepath'))
90 ->import();
56ea1de9 91
b1d05721 92 if (true === $res) {
1f66d79e 93 $summary = $import->getSummary();
f808b016
JB
94 $output->writeln('<info>' . $summary['imported'] . ' imported</info>');
95 $output->writeln('<comment>' . $summary['skipped'] . ' already saved</comment>');
56ea1de9
NL
96 }
97
56ea1de9 98 $em->clear();
56ea1de9 99
f808b016 100 $output->writeln('End : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
56ea1de9
NL
101 }
102}