]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/ImportBundle/Command/ImportCommand.php
Merge remote-tracking branch 'origin/master' into 2.3
[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\Output\OutputInterface;
10
11 class ImportCommand extends ContainerAwareCommand
12 {
13 protected function configure()
14 {
15 $this
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)
23 ;
24 }
25
26 protected function execute(InputInterface $input, OutputInterface $output)
27 {
28 $output->writeln('Start : '.(new \DateTime())->format('d-m-Y G:i:s').' ---');
29
30 if (!file_exists($input->getArgument('filepath'))) {
31 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
32 }
33
34 $em = $this->getContainer()->get('doctrine')->getManager();
35 // Turning off doctrine default logs queries for saving memory
36 $em->getConnection()->getConfiguration()->setSQLLogger(null);
37
38 if ($input->getOption('useUserId')) {
39 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
40 } else {
41 $user = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
42 }
43
44 if (!is_object($user)) {
45 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
46 }
47
48 switch ($input->getOption('importer')) {
49 case 'v2':
50 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
51 break;
52 case 'firefox':
53 $import = $this->getContainer()->get('wallabag_import.firefox.import');
54 break;
55 case 'chrome':
56 $import = $this->getContainer()->get('wallabag_import.chrome.import');
57 break;
58 case 'readability':
59 $import = $this->getContainer()->get('wallabag_import.readability.import');
60 break;
61 case 'instapaper':
62 $import = $this->getContainer()->get('wallabag_import.instapaper.import');
63 break;
64 case 'pinboard':
65 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
66 break;
67 default:
68 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
69 }
70
71 $import->setMarkAsRead($input->getOption('markAsRead'));
72 $import->setUser($user);
73
74 $res = $import
75 ->setFilepath($input->getArgument('filepath'))
76 ->import();
77
78 if (true === $res) {
79 $summary = $import->getSummary();
80 $output->writeln('<info>'.$summary['imported'].' imported</info>');
81 $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>');
82 }
83
84 $em->clear();
85
86 $output->writeln('End : '.(new \DateTime())->format('d-m-Y G:i:s').' ---');
87 }
88 }