]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Command/ImportCommand.php
php-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / ImportBundle / Command / ImportCommand.php
1 <?php
2
3 namespace Wallabag\ImportBundle\Command;
4
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
12 class ImportCommand extends ContainerAwareCommand
13 {
14 protected function configure()
15 {
16 $this
17 ->setName('wallabag:import')
18 ->setDescription('Import entries from a JSON export')
19 ->addArgument('username', InputArgument::REQUIRED, 'User to populate')
20 ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file')
21 ->addOption('importer', null, InputOption::VALUE_OPTIONAL, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1')
22 ->addOption('markAsRead', null, InputOption::VALUE_OPTIONAL, 'Mark all entries as read', false)
23 ->addOption('useUserId', null, InputOption::VALUE_NONE, 'Use user id instead of username to find account')
24 ->addOption('disableContentUpdate', null, InputOption::VALUE_NONE, 'Disable fetching updated content from URL')
25 ;
26 }
27
28 protected function execute(InputInterface $input, OutputInterface $output)
29 {
30 $output->writeln('Start : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
31
32 if (!file_exists($input->getArgument('filepath'))) {
33 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
34 }
35
36 $em = $this->getContainer()->get('doctrine')->getManager();
37 // Turning off doctrine default logs queries for saving memory
38 $em->getConnection()->getConfiguration()->setSQLLogger(null);
39
40 if ($input->getOption('useUserId')) {
41 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
42 } else {
43 $user = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
44 }
45
46 if (!\is_object($user)) {
47 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
48 }
49
50 switch ($input->getOption('importer')) {
51 case 'v2':
52 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
53 break;
54 case 'firefox':
55 $import = $this->getContainer()->get('wallabag_import.firefox.import');
56 break;
57 case 'chrome':
58 $import = $this->getContainer()->get('wallabag_import.chrome.import');
59 break;
60 case 'readability':
61 $import = $this->getContainer()->get('wallabag_import.readability.import');
62 break;
63 case 'instapaper':
64 $import = $this->getContainer()->get('wallabag_import.instapaper.import');
65 break;
66 case 'pinboard':
67 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
68 break;
69 default:
70 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
71 }
72
73 $import->setMarkAsRead($input->getOption('markAsRead'));
74 $import->setDisableContentUpdate($input->getOption('disableContentUpdate'));
75 $import->setUser($user);
76
77 $res = $import
78 ->setFilepath($input->getArgument('filepath'))
79 ->import();
80
81 if (true === $res) {
82 $summary = $import->getSummary();
83 $output->writeln('<info>' . $summary['imported'] . ' imported</info>');
84 $output->writeln('<comment>' . $summary['skipped'] . ' already saved</comment>');
85 }
86
87 $em->clear();
88
89 $output->writeln('End : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
90 }
91 }