diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-01-24 21:07:49 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-24 21:07:49 +0100 |
commit | b8805fad03ce199cfc2d7fe910e8bfe38be840b2 (patch) | |
tree | abab8726f1c71ddc8a0d1d35f1123dd219d1245d /src/Wallabag | |
parent | afaee1cc0a0e2902e9cd9c0117e9aa6f90fdf662 (diff) | |
parent | 3b0380f049d408f0721e28218170c562153a56e5 (diff) | |
download | wallabag-b8805fad03ce199cfc2d7fe910e8bfe38be840b2.tar.gz wallabag-b8805fad03ce199cfc2d7fe910e8bfe38be840b2.tar.zst wallabag-b8805fad03ce199cfc2d7fe910e8bfe38be840b2.zip |
Merge pull request #2774 from wallabag/cli-export
add cli export
Diffstat (limited to 'src/Wallabag')
-rw-r--r-- | src/Wallabag/CoreBundle/Command/ExportCommand.php | 77 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 5 |
2 files changed, 82 insertions, 0 deletions
diff --git a/src/Wallabag/CoreBundle/Command/ExportCommand.php b/src/Wallabag/CoreBundle/Command/ExportCommand.php new file mode 100644 index 00000000..e3d3b399 --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/ExportCommand.php | |||
@@ -0,0 +1,77 @@ | |||
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->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'))); | ||
38 | |||
39 | return 1; | ||
40 | } | ||
41 | |||
42 | $entries = $this->getDoctrine() | ||
43 | ->getRepository('WallabagCoreBundle:Entry') | ||
44 | ->getBuilderForAllByUser($user->getId()) | ||
45 | ->getQuery() | ||
46 | ->getResult(); | ||
47 | |||
48 | $output->write(sprintf('Exporting %d entrie(s) for user « <comment>%s</comment> »... ', count($entries), $user->getUserName())); | ||
49 | |||
50 | $filePath = $input->getArgument('filepath'); | ||
51 | |||
52 | if (!$filePath) { | ||
53 | $filePath = $this->getContainer()->getParameter('kernel.root_dir').'/../'.sprintf('%s-export.json', $user->getUsername()); | ||
54 | } | ||
55 | |||
56 | try { | ||
57 | $data = $this->getContainer()->get('wallabag_core.helper.entries_export') | ||
58 | ->setEntries($entries) | ||
59 | ->updateTitle('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 | } | ||
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 4bf292a4..93c01fcb 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php | |||
@@ -89,6 +89,11 @@ class EntriesExport | |||
89 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); | 89 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); |
90 | } | 90 | } |
91 | 91 | ||
92 | public function exportJsonData() | ||
93 | { | ||
94 | return $this->prepareSerializingContent('json'); | ||
95 | } | ||
96 | |||
92 | /** | 97 | /** |
93 | * Use PHPePub to dump a .epub file. | 98 | * Use PHPePub to dump a .epub file. |
94 | * | 99 | * |