]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Command/ImportCommand.php
CS
[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;
d0e9b3d6 8use Symfony\Component\Console\Input\InputOption;
56ea1de9
NL
9use Symfony\Component\Console\Input\InputInterface;
10use Symfony\Component\Console\Output\OutputInterface;
56ea1de9
NL
11
12class ImportCommand extends ContainerAwareCommand
13{
14 protected function configure()
15 {
16 $this
3fad6c74 17 ->setName('wallabag:import')
1f66d79e 18 ->setDescription('Import entries from a JSON export')
d1e5059e 19 ->addArgument('username', InputArgument::REQUIRED, 'User to populate')
b1d05721 20 ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file')
9ab024b4 21 ->addOption('importer', null, InputArgument::OPTIONAL, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1')
235026e2 22 ->addOption('markAsRead', null, InputArgument::OPTIONAL, 'Mark all entries as read', false)
d0e9b3d6 23 ->addOption('disableContentUpdate', null, InputOption::VALUE_NONE, 'Disable fetching updated content from URL')
b1d05721 24 ;
56ea1de9
NL
25 }
26
27 protected function execute(InputInterface $input, OutputInterface $output)
28 {
b1d05721 29 $output->writeln('Start : '.(new \DateTime())->format('d-m-Y G:i:s').' ---');
56ea1de9 30
ebf5e508
JB
31 if (!file_exists($input->getArgument('filepath'))) {
32 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
33 }
34
56ea1de9
NL
35 $em = $this->getContainer()->get('doctrine')->getManager();
36 // Turning off doctrine default logs queries for saving memory
37 $em->getConnection()->getConfiguration()->setSQLLogger(null);
38
d1e5059e
TC
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 }
8c3c77c1
NL
44
45 if (!is_object($user)) {
d1e5059e 46 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
8c3c77c1
NL
47 }
48
ae669126
TC
49 switch ($input->getOption('importer')) {
50 case 'v2':
1f66d79e 51 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
ae669126 52 break;
59201088 53 case 'firefox':
1f66d79e 54 $import = $this->getContainer()->get('wallabag_import.firefox.import');
59201088
TC
55 break;
56 case 'chrome':
1f66d79e
JB
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');
ae669126 64 break;
9ab024b4
JB
65 case 'pinboard':
66 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
67 break;
ae669126 68 default:
1f66d79e 69 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
3fad6c74
NL
70 }
71
1f66d79e 72 $import->setMarkAsRead($input->getOption('markAsRead'));
d0e9b3d6 73 $import->setDisableContentUpdate($input->getOption('disableContentUpdate'));
1f66d79e 74 $import->setUser($user);
3fad6c74 75
1f66d79e 76 $res = $import
b1d05721
JB
77 ->setFilepath($input->getArgument('filepath'))
78 ->import();
56ea1de9 79
b1d05721 80 if (true === $res) {
1f66d79e 81 $summary = $import->getSummary();
b1d05721
JB
82 $output->writeln('<info>'.$summary['imported'].' imported</info>');
83 $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>');
56ea1de9
NL
84 }
85
56ea1de9 86 $em->clear();
56ea1de9 87
b1d05721 88 $output->writeln('End : '.(new \DateTime())->format('d-m-Y G:i:s').' ---');
56ea1de9
NL
89 }
90}