]>
Commit | Line | Data |
---|---|---|
56ea1de9 NL |
1 | <?php |
2 | ||
77a7752a | 3 | namespace Wallabag\ImportBundle\Command; |
56ea1de9 NL |
4 | |
5 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
8c3c77c1 | 6 | use Symfony\Component\Config\Definition\Exception\Exception; |
a1bb1b3c | 7 | use Symfony\Component\Console\Input\InputArgument; |
56ea1de9 NL |
8 | use Symfony\Component\Console\Input\InputInterface; |
9 | use Symfony\Component\Console\Output\OutputInterface; | |
56ea1de9 NL |
10 | |
11 | class ImportCommand extends ContainerAwareCommand | |
12 | { | |
13 | protected function configure() | |
14 | { | |
15 | $this | |
3fad6c74 | 16 | ->setName('wallabag:import') |
1f66d79e | 17 | ->setDescription('Import entries from a JSON export') |
b1d05721 JB |
18 | ->addArgument('userId', InputArgument::REQUIRED, 'User ID to populate') |
19 | ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file') | |
1f66d79e | 20 | ->addOption('importer', null, InputArgument::OPTIONAL, 'The importer to use: v1, v2, instapaper, readability, firefox or chrome', 'v1') |
235026e2 | 21 | ->addOption('markAsRead', null, InputArgument::OPTIONAL, 'Mark all entries as read', false) |
b1d05721 | 22 | ; |
56ea1de9 NL |
23 | } |
24 | ||
25 | protected function execute(InputInterface $input, OutputInterface $output) | |
26 | { | |
b1d05721 | 27 | $output->writeln('Start : '.(new \DateTime())->format('d-m-Y G:i:s').' ---'); |
56ea1de9 | 28 | |
ebf5e508 JB |
29 | if (!file_exists($input->getArgument('filepath'))) { |
30 | throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath'))); | |
31 | } | |
32 | ||
56ea1de9 NL |
33 | $em = $this->getContainer()->get('doctrine')->getManager(); |
34 | // Turning off doctrine default logs queries for saving memory | |
35 | $em->getConnection()->getConfiguration()->setSQLLogger(null); | |
36 | ||
b1d05721 | 37 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('userId')); |
8c3c77c1 NL |
38 | |
39 | if (!is_object($user)) { | |
b1d05721 | 40 | throw new Exception(sprintf('User with id "%s" not found', $input->getArgument('userId'))); |
8c3c77c1 NL |
41 | } |
42 | ||
ae669126 TC |
43 | switch ($input->getOption('importer')) { |
44 | case 'v2': | |
1f66d79e | 45 | $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import'); |
ae669126 | 46 | break; |
59201088 | 47 | case 'firefox': |
1f66d79e | 48 | $import = $this->getContainer()->get('wallabag_import.firefox.import'); |
59201088 TC |
49 | break; |
50 | case 'chrome': | |
1f66d79e JB |
51 | $import = $this->getContainer()->get('wallabag_import.chrome.import'); |
52 | break; | |
53 | case 'readability': | |
54 | $import = $this->getContainer()->get('wallabag_import.readability.import'); | |
55 | break; | |
56 | case 'instapaper': | |
57 | $import = $this->getContainer()->get('wallabag_import.instapaper.import'); | |
ae669126 TC |
58 | break; |
59 | default: | |
1f66d79e | 60 | $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import'); |
3fad6c74 NL |
61 | } |
62 | ||
1f66d79e JB |
63 | $import->setMarkAsRead($input->getOption('markAsRead')); |
64 | $import->setUser($user); | |
3fad6c74 | 65 | |
1f66d79e | 66 | $res = $import |
b1d05721 JB |
67 | ->setFilepath($input->getArgument('filepath')) |
68 | ->import(); | |
56ea1de9 | 69 | |
b1d05721 | 70 | if (true === $res) { |
1f66d79e | 71 | $summary = $import->getSummary(); |
b1d05721 JB |
72 | $output->writeln('<info>'.$summary['imported'].' imported</info>'); |
73 | $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>'); | |
56ea1de9 NL |
74 | } |
75 | ||
56ea1de9 | 76 | $em->clear(); |
56ea1de9 | 77 | |
b1d05721 | 78 | $output->writeln('End : '.(new \DateTime())->format('d-m-Y G:i:s').' ---'); |
56ea1de9 NL |
79 | } |
80 | } |