diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-01-22 12:51:14 +0100 |
---|---|---|
committer | Thomas Citharel <tcit@tcit.fr> | 2017-01-22 12:51:14 +0100 |
commit | 8303b037fb4e64b542f6f755828b999fdf6eebb0 (patch) | |
tree | 8348c7e09ad2a59bf86120c85ad26d4b003848b5 /src/Wallabag/CoreBundle | |
parent | afaee1cc0a0e2902e9cd9c0117e9aa6f90fdf662 (diff) | |
download | wallabag-8303b037fb4e64b542f6f755828b999fdf6eebb0.tar.gz wallabag-8303b037fb4e64b542f6f755828b999fdf6eebb0.tar.zst wallabag-8303b037fb4e64b542f6f755828b999fdf6eebb0.zip |
add cli export
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
Diffstat (limited to 'src/Wallabag/CoreBundle')
-rw-r--r-- | src/Wallabag/CoreBundle/Command/ExportCommand.php | 82 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 5 |
2 files changed, 87 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..9c3c3fef --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/ExportCommand.php | |||
@@ -0,0 +1,82 @@ | |||
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\Output\StreamOutput; | ||
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 | 'filename', | ||
27 | InputArgument::OPTIONAL, | ||
28 | 'Path of the exported file' | ||
29 | ) | ||
30 | ; | ||
31 | } | ||
32 | |||
33 | protected function execute(InputInterface $input, OutputInterface $output) | ||
34 | { | ||
35 | try { | ||
36 | $user = $this->getUser($input->getArgument('username')); | ||
37 | } catch (NoResultException $e) { | ||
38 | $output->writeln(sprintf('<error>User "%s" not found.</error>', $input->getArgument('username'))); | ||
39 | return 1; | ||
40 | } | ||
41 | $entries = $this->getDoctrine() | ||
42 | ->getRepository('WallabagCoreBundle:Entry') | ||
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('filename'); | ||
50 | if (!$filePath) { | ||
51 | $filePath = $this->getContainer()->getParameter('kernel.root_dir') . '/../' . sprintf('%s-export', $user->getUsername()); | ||
52 | } | ||
53 | try { | ||
54 | $data = $this->getContainer()->get('wallabag_core.helper.entries_export') | ||
55 | ->setEntries($entries) | ||
56 | ->updateTitle('All') | ||
57 | ->exportJsonData(); | ||
58 | file_put_contents($filePath, $data); | ||
59 | } catch (\InvalidArgumentException $e) { | ||
60 | $output->writeln(sprintf('<error>Error: "%s"</error>', $e->getMessage())); | ||
61 | } | ||
62 | |||
63 | $output->writeln('<info>Done.</info>'); | ||
64 | } | ||
65 | |||
66 | /** | ||
67 | * Fetches a user from its username. | ||
68 | * | ||
69 | * @param string $username | ||
70 | * | ||
71 | * @return \Wallabag\UserBundle\Entity\User | ||
72 | */ | ||
73 | private function getUser($username) | ||
74 | { | ||
75 | return $this->getDoctrine()->getRepository('WallabagUserBundle:User')->findOneByUserName($username); | ||
76 | } | ||
77 | |||
78 | private function getDoctrine() | ||
79 | { | ||
80 | return $this->getContainer()->get('doctrine'); | ||
81 | } | ||
82 | } | ||
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 | * |