]>
Commit | Line | Data |
---|---|---|
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 | ||
11 | class 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( | |
25 | 'filepath', | |
26 | InputArgument::OPTIONAL, | |
27 | 'Path of the exported file' | |
28 | ) | |
29 | ; | |
30 | } | |
31 | ||
32 | protected function execute(InputInterface $input, OutputInterface $output) | |
33 | { | |
34 | try { | |
35 | $user = $this->getContainer()->get('wallabag_user.user_repository')->findOneByUserName($input->getArgument('username')); | |
36 | } catch (NoResultException $e) { | |
37 | $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username'))); | |
38 | ||
39 | return 1; | |
40 | } | |
41 | ||
42 | $entries = $this->getContainer()->get('wallabag_core.entry_repository') | |
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 | ||
49 | $filePath = $input->getArgument('filepath'); | |
50 | ||
51 | if (!$filePath) { | |
52 | $filePath = $this->getContainer()->getParameter('kernel.root_dir') . '/../' . sprintf('%s-export.json', $user->getUsername()); | |
53 | } | |
54 | ||
55 | try { | |
56 | $data = $this->getContainer()->get('wallabag_core.helper.entries_export') | |
57 | ->setEntries($entries) | |
58 | ->updateTitle('All') | |
59 | ->updateAuthor('All') | |
60 | ->exportJsonData(); | |
61 | file_put_contents($filePath, $data); | |
62 | } catch (\InvalidArgumentException $e) { | |
63 | $output->writeln(sprintf('<error>Error: "%s"</error>', $e->getMessage())); | |
64 | ||
65 | return 1; | |
66 | } | |
67 | ||
68 | $output->writeln('<info>Done.</info>'); | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | private function getDoctrine() | |
74 | { | |
75 | return $this->getContainer()->get('doctrine'); | |
76 | } | |
77 | } |