aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Citharel <tcit@tcit.fr>2017-01-22 12:51:14 +0100
committerThomas Citharel <tcit@tcit.fr>2017-01-22 12:51:14 +0100
commit8303b037fb4e64b542f6f755828b999fdf6eebb0 (patch)
tree8348c7e09ad2a59bf86120c85ad26d4b003848b5
parentafaee1cc0a0e2902e9cd9c0117e9aa6f90fdf662 (diff)
downloadwallabag-8303b037fb4e64b542f6f755828b999fdf6eebb0.tar.gz
wallabag-8303b037fb4e64b542f6f755828b999fdf6eebb0.tar.zst
wallabag-8303b037fb4e64b542f6f755828b999fdf6eebb0.zip
add cli export
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
-rw-r--r--src/Wallabag/CoreBundle/Command/ExportCommand.php82
-rw-r--r--src/Wallabag/CoreBundle/Helper/EntriesExport.php5
-rw-r--r--tests/Wallabag/CoreBundle/Command/ExportCommandTest.php60
3 files changed, 147 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
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;
10use Symfony\Component\Console\Output\StreamOutput;
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(
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 *
diff --git a/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php
new file mode 100644
index 00000000..41491838
--- /dev/null
+++ b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php
@@ -0,0 +1,60 @@
1<?php
2
3namespace Tests\Wallabag\CoreBundle\Command;
4
5use Symfony\Bundle\FrameworkBundle\Console\Application;
6use Symfony\Component\Console\Tester\CommandTester;
7use Wallabag\CoreBundle\Command\ExportCommand;
8use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
9
10class ExportCommandTest extends WallabagCoreTestCase
11{
12 /**
13 * @expectedException Symfony\Component\Console\Exception\RuntimeException
14 * @expectedExceptionMessage Not enough arguments (missing: "username")
15 */
16 public function testExportCommandWithoutUsername()
17 {
18 $application = new Application($this->getClient()->getKernel());
19 $application->add(new ExportCommand());
20
21 $command = $application->find('wallabag:export');
22
23 $tester = new CommandTester($command);
24 $tester->execute([
25 'command' => $command->getName(),
26 ]);
27 }
28
29 public function testExportCommandWithBadUsername()
30 {
31 $application = new Application($this->getClient()->getKernel());
32 $application->add(new ExportCommand());
33
34 $command = $application->find('wallabag:export');
35
36 $tester = new CommandTester($command);
37 $tester->execute([
38 'command' => $command->getName(),
39 'username' => 'unknown',
40 ]);
41
42 $this->assertContains('User "unknown" not found', $tester->getDisplay());
43 }
44
45 public function testExportCommand()
46 {
47 $application = new Application($this->getClient()->getKernel());
48 $application->add(new ExportCommand());
49
50 $command = $application->find('wallabag:export');
51
52 $tester = new CommandTester($command);
53 $tester->execute([
54 'command' => $command->getName(),
55 'username' => 'admin',
56 ]);
57
58 $this->assertContains('Exporting 6 entrie(s) for user « admin »... Done', $tester->getDisplay());
59 }
60}