3 namespace Wallabag\CoreBundle\Command
;
5 use Doctrine\ORM\NoResultException
;
6 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand
;
7 use Symfony\Component\Console\Input\InputArgument
;
8 use Symfony\Component\Console\Input\InputInterface
;
9 use Symfony\Component\Console\Output\OutputInterface
;
11 class ExportCommand
extends ContainerAwareCommand
13 protected function configure()
16 ->setName('wallabag:export')
17 ->setDescription('Export all entries for an user')
18 ->setHelp('This command helps you to export all entries for an user')
21 InputArgument
::REQUIRED
,
22 'User from which to export entries'
26 InputArgument
::OPTIONAL
,
27 'Path of the exported file'
32 protected function execute(InputInterface
$input, OutputInterface
$output)
35 $user = $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($input->getArgument('username'));
36 } catch (NoResultException
$e) {
37 $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username')));
42 $entries = $this->getDoctrine()
43 ->getRepository('WallabagCoreBundle:Entry')
44 ->getBuilderForAllByUser($user->getId())
48 $output->write(sprintf('Exporting %d entrie(s) for user « <comment>%s</comment> »... ', count($entries), $user->getUserName()));
50 $filePath = $input->getArgument('filepath');
53 $filePath = $this->getContainer()->getParameter('kernel.root_dir').'/../'.sprintf('%s-export.json', $user->getUsername());
57 $data = $this->getContainer()->get('wallabag_core.helper.entries_export')
58 ->setEntries($entries)
61 file_put_contents($filePath, $data);
62 } catch (\InvalidArgumentException
$e) {
63 $output->writeln(sprintf('<error>Error: "%s"</error>', $e->getMessage()));
68 $output->writeln('<info>Done.</info>');
73 private function getDoctrine()
75 return $this->getContainer()->get('doctrine');