]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Command/ExportCommand.php
php-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;
e1b33efb 10use Symfony\Component\Console\Style\SymfonyStyle;
8303b037
TC
11
12class 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(
a607b7a9 26 'filepath',
8303b037
TC
27 InputArgument::OPTIONAL,
28 'Path of the exported file'
29 )
30 ;
31 }
32
33 protected function execute(InputInterface $input, OutputInterface $output)
34 {
e1b33efb
NH
35 $io = new SymfonyStyle($input, $output);
36
8303b037 37 try {
03ce43d4 38 $user = $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($input->getArgument('username'));
8303b037 39 } catch (NoResultException $e) {
e1b33efb 40 $io->error(sprintf('User "%s" not found.', $input->getArgument('username')));
3b0380f0 41
8303b037
TC
42 return 1;
43 }
3b0380f0 44
03ce43d4 45 $entries = $this->getContainer()->get('wallabag_core.entry_repository')
8303b037
TC
46 ->getBuilderForAllByUser($user->getId())
47 ->getQuery()
48 ->getResult();
49
2a1ceb67 50 $io->text(sprintf('Exporting <info>%d</info> entrie(s) for user <info>%s</info>...', \count($entries), $user->getUserName()));
8303b037 51
a607b7a9 52 $filePath = $input->getArgument('filepath');
3b0380f0 53
8303b037 54 if (!$filePath) {
9ca069a6 55 $filePath = $this->getContainer()->getParameter('kernel.project_dir') . '/' . sprintf('%s-export.json', $user->getUsername());
8303b037 56 }
3b0380f0 57
8303b037
TC
58 try {
59 $data = $this->getContainer()->get('wallabag_core.helper.entries_export')
60 ->setEntries($entries)
61 ->updateTitle('All')
07320a2b 62 ->updateAuthor('All')
8303b037
TC
63 ->exportJsonData();
64 file_put_contents($filePath, $data);
65 } catch (\InvalidArgumentException $e) {
e1b33efb 66 $io->error(sprintf('Error: "%s"', $e->getMessage()));
3b0380f0
TC
67
68 return 1;
8303b037
TC
69 }
70
e1b33efb 71 $io->success('Done.');
8303b037 72
3b0380f0 73 return 0;
8303b037 74 }
8303b037 75}