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