]>
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') |
b1d05721 JB |
17 | ->setDescription('Import entries from a JSON export from a wallabag v1 instance') |
18 | ->addArgument('userId', InputArgument::REQUIRED, 'User ID to populate') | |
19 | ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file') | |
3fad6c74 NL |
20 | ->addOption('importer', null, InputArgument::OPTIONAL, 'The importer to use: v1 or v2', 'v1') |
21 | ->addOption('markAsRead', null, InputArgument::OPTIONAL, 'Mark all entries as read: true/false', 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 NL |
28 | |
29 | $em = $this->getContainer()->get('doctrine')->getManager(); | |
30 | // Turning off doctrine default logs queries for saving memory | |
31 | $em->getConnection()->getConfiguration()->setSQLLogger(null); | |
32 | ||
b1d05721 | 33 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('userId')); |
8c3c77c1 NL |
34 | |
35 | if (!is_object($user)) { | |
b1d05721 | 36 | throw new Exception(sprintf('User with id "%s" not found', $input->getArgument('userId'))); |
8c3c77c1 NL |
37 | } |
38 | ||
b1d05721 | 39 | $wallabag = $this->getContainer()->get('wallabag_import.wallabag_v1.import'); |
3fad6c74 NL |
40 | |
41 | if ('v2' === $input->getOption('importer')) { | |
42 | $wallabag = $this->getContainer()->get('wallabag_import.wallabag_v2.import'); | |
43 | } | |
44 | ||
45 | if ('yes' === $input->getOption('markAsRead')) { | |
46 | $wallabag->setMarkAsRead(true); | |
47 | } | |
48 | ||
b1d05721 JB |
49 | $res = $wallabag |
50 | ->setUser($user) | |
51 | ->setFilepath($input->getArgument('filepath')) | |
52 | ->import(); | |
56ea1de9 | 53 | |
b1d05721 JB |
54 | if (true === $res) { |
55 | $summary = $wallabag->getSummary(); | |
56 | $output->writeln('<info>'.$summary['imported'].' imported</info>'); | |
57 | $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>'); | |
56ea1de9 NL |
58 | } |
59 | ||
56ea1de9 | 60 | $em->clear(); |
56ea1de9 | 61 | |
b1d05721 | 62 | $output->writeln('End : '.(new \DateTime())->format('d-m-Y G:i:s').' ---'); |
56ea1de9 NL |
63 | } |
64 | } |