]>
Commit | Line | Data |
---|---|---|
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 from a wallabag v1 instance') | |
18 | ->addArgument('userId', InputArgument::REQUIRED, 'User ID to populate') | |
19 | ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file') | |
20 | ->addOption('importer', null, InputArgument::OPTIONAL, 'The importer to use: wallabag v1, v2, firefox or chrome', 'v1') | |
21 | ->addOption('markAsRead', null, InputArgument::OPTIONAL, 'Mark all entries as read', false) | |
22 | ; | |
23 | } | |
24 | ||
25 | protected function execute(InputInterface $input, OutputInterface $output) | |
26 | { | |
27 | $output->writeln('Start : '.(new \DateTime())->format('d-m-Y G:i:s').' ---'); | |
28 | ||
29 | if (!file_exists($input->getArgument('filepath'))) { | |
30 | throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath'))); | |
31 | } | |
32 | ||
33 | $em = $this->getContainer()->get('doctrine')->getManager(); | |
34 | // Turning off doctrine default logs queries for saving memory | |
35 | $em->getConnection()->getConfiguration()->setSQLLogger(null); | |
36 | ||
37 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('userId')); | |
38 | ||
39 | if (!is_object($user)) { | |
40 | throw new Exception(sprintf('User with id "%s" not found', $input->getArgument('userId'))); | |
41 | } | |
42 | ||
43 | switch ($input->getOption('importer')) { | |
44 | case 'v2': | |
45 | $wallabag = $this->getContainer()->get('wallabag_import.wallabag_v2.import'); | |
46 | break; | |
47 | case 'firefox': | |
48 | $wallabag = $this->getContainer()->get('wallabag_import.firefox.import'); | |
49 | break; | |
50 | case 'chrome': | |
51 | $wallabag = $this->getContainer()->get('wallabag_import.chrome.import'); | |
52 | break; | |
53 | case 'v1': | |
54 | default: | |
55 | $wallabag = $this->getContainer()->get('wallabag_import.wallabag_v1.import'); | |
56 | break; | |
57 | } | |
58 | ||
59 | $wallabag->setMarkAsRead($input->getOption('markAsRead')); | |
60 | $wallabag->setUser($user); | |
61 | ||
62 | $res = $wallabag | |
63 | ->setFilepath($input->getArgument('filepath')) | |
64 | ->import(); | |
65 | ||
66 | if (true === $res) { | |
67 | $summary = $wallabag->getSummary(); | |
68 | $output->writeln('<info>'.$summary['imported'].' imported</info>'); | |
69 | $output->writeln('<comment>'.$summary['skipped'].' already saved</comment>'); | |
70 | } | |
71 | ||
72 | $em->clear(); | |
73 | ||
74 | $output->writeln('End : '.(new \DateTime())->format('d-m-Y G:i:s').' ---'); | |
75 | } | |
76 | } |