]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Command/ExportCommand.php
php-cs-fixer
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Command / ExportCommand.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Command;
4
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;
10 use Symfony\Component\Console\Style\SymfonyStyle;
11
12 class ExportCommand extends ContainerAwareCommand
13 {
14 protected function configure()
15 {
16 $this
17 ->setName('wallabag:export')
18 ->setDescription('Export all entries for an user')
19 ->setHelp('This command helps you to export all entries for an user')
20 ->addArgument(
21 'username',
22 InputArgument::REQUIRED,
23 'User from which to export entries'
24 )
25 ->addArgument(
26 'filepath',
27 InputArgument::OPTIONAL,
28 'Path of the exported file'
29 )
30 ;
31 }
32
33 protected function execute(InputInterface $input, OutputInterface $output)
34 {
35 $io = new SymfonyStyle($input, $output);
36
37 try {
38 $user = $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($input->getArgument('username'));
39 } catch (NoResultException $e) {
40 $io->error(sprintf('User "%s" not found.', $input->getArgument('username')));
41
42 return 1;
43 }
44
45 $entries = $this->getContainer()->get('wallabag_core.entry_repository')
46 ->getBuilderForAllByUser($user->getId())
47 ->getQuery()
48 ->getResult();
49
50 $io->text(sprintf('Exporting <info>%d</info> entrie(s) for user <info>%s</info>...', \count($entries), $user->getUserName()));
51
52 $filePath = $input->getArgument('filepath');
53
54 if (!$filePath) {
55 $filePath = $this->getContainer()->getParameter('kernel.project_dir') . '/' . sprintf('%s-export.json', $user->getUsername());
56 }
57
58 try {
59 $data = $this->getContainer()->get('wallabag_core.helper.entries_export')
60 ->setEntries($entries)
61 ->updateTitle('All')
62 ->updateAuthor('All')
63 ->exportJsonData();
64 file_put_contents($filePath, $data);
65 } catch (\InvalidArgumentException $e) {
66 $io->error(sprintf('Error: "%s"', $e->getMessage()));
67
68 return 1;
69 }
70
71 $io->success('Done.');
72
73 return 0;
74 }
75 }