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