]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ImportBundle/Command/ImportCommand.php
Add a real configuration for CS-Fixer
[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 8use Symfony\Component\Console\Input\InputInterface;
f808b016 9use Symfony\Component\Console\Input\InputOption;
56ea1de9 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')
f5924e95
JB
21 ->addOption('importer', null, InputOption::VALUE_OPTIONAL, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1')
22 ->addOption('markAsRead', null, InputOption::VALUE_OPTIONAL, 'Mark all entries as read', false)
23 ->addOption('useUserId', null, InputOption::VALUE_NONE, 'Use user id instead of username to find account')
d0e9b3d6 24 ->addOption('disableContentUpdate', null, InputOption::VALUE_NONE, 'Disable fetching updated content from URL')
b1d05721 25 ;
56ea1de9
NL
26 }
27
28 protected function execute(InputInterface $input, OutputInterface $output)
29 {
f808b016 30 $output->writeln('Start : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
56ea1de9 31
ebf5e508
JB
32 if (!file_exists($input->getArgument('filepath'))) {
33 throw new Exception(sprintf('File "%s" not found', $input->getArgument('filepath')));
34 }
35
56ea1de9
NL
36 $em = $this->getContainer()->get('doctrine')->getManager();
37 // Turning off doctrine default logs queries for saving memory
38 $em->getConnection()->getConfiguration()->setSQLLogger(null);
39
d1e5059e
TC
40 if ($input->getOption('useUserId')) {
41 $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username'));
42 } else {
43 $user = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username'));
44 }
8c3c77c1
NL
45
46 if (!is_object($user)) {
d1e5059e 47 throw new Exception(sprintf('User "%s" not found', $input->getArgument('username')));
8c3c77c1
NL
48 }
49
ae669126
TC
50 switch ($input->getOption('importer')) {
51 case 'v2':
1f66d79e 52 $import = $this->getContainer()->get('wallabag_import.wallabag_v2.import');
ae669126 53 break;
59201088 54 case 'firefox':
1f66d79e 55 $import = $this->getContainer()->get('wallabag_import.firefox.import');
59201088
TC
56 break;
57 case 'chrome':
1f66d79e
JB
58 $import = $this->getContainer()->get('wallabag_import.chrome.import');
59 break;
60 case 'readability':
61 $import = $this->getContainer()->get('wallabag_import.readability.import');
62 break;
63 case 'instapaper':
64 $import = $this->getContainer()->get('wallabag_import.instapaper.import');
ae669126 65 break;
9ab024b4
JB
66 case 'pinboard':
67 $import = $this->getContainer()->get('wallabag_import.pinboard.import');
68 break;
ae669126 69 default:
1f66d79e 70 $import = $this->getContainer()->get('wallabag_import.wallabag_v1.import');
3fad6c74
NL
71 }
72
1f66d79e 73 $import->setMarkAsRead($input->getOption('markAsRead'));
d0e9b3d6 74 $import->setDisableContentUpdate($input->getOption('disableContentUpdate'));
1f66d79e 75 $import->setUser($user);
3fad6c74 76
1f66d79e 77 $res = $import
b1d05721
JB
78 ->setFilepath($input->getArgument('filepath'))
79 ->import();
56ea1de9 80
b1d05721 81 if (true === $res) {
1f66d79e 82 $summary = $import->getSummary();
f808b016
JB
83 $output->writeln('<info>' . $summary['imported'] . ' imported</info>');
84 $output->writeln('<comment>' . $summary['skipped'] . ' already saved</comment>');
56ea1de9
NL
85 }
86
56ea1de9 87 $em->clear();
56ea1de9 88
f808b016 89 $output->writeln('End : ' . (new \DateTime())->format('d-m-Y G:i:s') . ' ---');
56ea1de9
NL
90 }
91}