]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/ExportCommand.php
Add a real configuration for CS-Fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / ExportCommand.php
CommitLineData
8303b037
TC
1<?php
2
3namespace Wallabag\CoreBundle\Command;
4
5use Doctrine\ORM\NoResultException;
6use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7use Symfony\Component\Console\Input\InputArgument;
8use Symfony\Component\Console\Input\InputInterface;
9use Symfony\Component\Console\Output\OutputInterface;
8303b037
TC
10
11class ExportCommand extends ContainerAwareCommand
12{
13 protected function configure()
14 {
15 $this
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')
19 ->addArgument(
20 'username',
21 InputArgument::REQUIRED,
22 'User from which to export entries'
23 )
24 ->addArgument(
a607b7a9 25 'filepath',
8303b037
TC
26 InputArgument::OPTIONAL,
27 'Path of the exported file'
28 )
29 ;
30 }
31
32 protected function execute(InputInterface $input, OutputInterface $output)
33 {
34 try {
03ce43d4 35 $user = $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($input->getArgument('username'));
8303b037
TC
36 } catch (NoResultException $e) {
37 $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username')));
3b0380f0 38
8303b037
TC
39 return 1;
40 }
3b0380f0 41
03ce43d4 42 $entries = $this->getContainer()->get('wallabag_core.entry_repository')
8303b037
TC
43 ->getBuilderForAllByUser($user->getId())
44 ->getQuery()
45 ->getResult();
46
47 $output->write(sprintf('Exporting %d entrie(s) for user « <comment>%s</comment> »... ', count($entries), $user->getUserName()));
48
a607b7a9 49 $filePath = $input->getArgument('filepath');
3b0380f0 50
8303b037 51 if (!$filePath) {
f808b016 52 $filePath = $this->getContainer()->getParameter('kernel.root_dir') . '/../' . sprintf('%s-export.json', $user->getUsername());
8303b037 53 }
3b0380f0 54
8303b037
TC
55 try {
56 $data = $this->getContainer()->get('wallabag_core.helper.entries_export')
57 ->setEntries($entries)
58 ->updateTitle('All')
59 ->exportJsonData();
60 file_put_contents($filePath, $data);
61 } catch (\InvalidArgumentException $e) {
62 $output->writeln(sprintf('<error>Error: "%s"</error>', $e->getMessage()));
3b0380f0
TC
63
64 return 1;
8303b037
TC
65 }
66
67 $output->writeln('<info>Done.</info>');
8303b037 68
3b0380f0 69 return 0;
8303b037
TC
70 }
71
72 private function getDoctrine()
73 {
74 return $this->getContainer()->get('doctrine');
75 }
76}