diff options
-rw-r--r-- | app/DoctrineMigrations/Version20161214094402.php | 75 | ||||
-rw-r--r-- | composer.json | 1 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Command/ExportCommand.php | 77 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 5 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Command/ExportCommandTest.php | 78 |
5 files changed, 236 insertions, 0 deletions
diff --git a/app/DoctrineMigrations/Version20161214094402.php b/app/DoctrineMigrations/Version20161214094402.php new file mode 100644 index 00000000..db125f76 --- /dev/null +++ b/app/DoctrineMigrations/Version20161214094402.php | |||
@@ -0,0 +1,75 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Application\Migrations; | ||
4 | |||
5 | use Doctrine\DBAL\Migrations\AbstractMigration; | ||
6 | use Doctrine\DBAL\Schema\Schema; | ||
7 | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
8 | use Symfony\Component\DependencyInjection\ContainerInterface; | ||
9 | |||
10 | /** | ||
11 | * Renamed uuid to uid in entry table | ||
12 | */ | ||
13 | class Version20161214094402 extends AbstractMigration implements ContainerAwareInterface | ||
14 | { | ||
15 | /** | ||
16 | * @var ContainerInterface | ||
17 | */ | ||
18 | private $container; | ||
19 | |||
20 | public function setContainer(ContainerInterface $container = null) | ||
21 | { | ||
22 | $this->container = $container; | ||
23 | } | ||
24 | |||
25 | private function getTable($tableName) | ||
26 | { | ||
27 | return $this->container->getParameter('database_table_prefix').$tableName; | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * @param Schema $schema | ||
32 | */ | ||
33 | public function up(Schema $schema) | ||
34 | { | ||
35 | $entryTable = $schema->getTable($this->getTable('entry')); | ||
36 | |||
37 | $this->skipIf($entryTable->hasColumn('uid'), 'It seems that you already played this migration.'); | ||
38 | |||
39 | switch ($this->connection->getDatabasePlatform()->getName()) { | ||
40 | case 'sqlite': | ||
41 | $this->addSql('CREATE TEMPORARY TABLE __temp__wallabag_entry AS SELECT id, user_id, uuid, title, url, is_archived, is_starred, content, created_at, updated_at, mimetype, language, reading_time, domain_name, preview_picture, is_public FROM '.$this->getTable('entry')); | ||
42 | $this->addSql('DROP TABLE '.$this->getTable('entry')); | ||
43 | $this->addSql('CREATE TABLE '.$this->getTable('entry').' (id INTEGER NOT NULL, user_id INTEGER DEFAULT NULL, uid CLOB DEFAULT NULL COLLATE BINARY, title CLOB DEFAULT NULL COLLATE BINARY, url CLOB DEFAULT NULL COLLATE BINARY, is_archived BOOLEAN NOT NULL, is_starred BOOLEAN NOT NULL, content CLOB DEFAULT NULL COLLATE BINARY, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, mimetype CLOB DEFAULT NULL COLLATE BINARY, language CLOB DEFAULT NULL COLLATE BINARY, reading_time INTEGER DEFAULT NULL, domain_name CLOB DEFAULT NULL COLLATE BINARY, preview_picture CLOB DEFAULT NULL COLLATE BINARY, is_public BOOLEAN DEFAULT "0", PRIMARY KEY(id));'); | ||
44 | $this->addSql('INSERT INTO '.$this->getTable('entry').' (id, user_id, uid, title, url, is_archived, is_starred, content, created_at, updated_at, mimetype, language, reading_time, domain_name, preview_picture, is_public) SELECT id, user_id, uuid, title, url, is_archived, is_starred, content, created_at, updated_at, mimetype, language, reading_time, domain_name, preview_picture, is_public FROM __temp__wallabag_entry;'); | ||
45 | $this->addSql('DROP TABLE __temp__wallabag_entry'); | ||
46 | break; | ||
47 | case 'mysql': | ||
48 | $this->addSql('ALTER TABLE '.$this->getTable('entry').' CHANGE uuid uid VARCHAR(23)'); | ||
49 | break; | ||
50 | case 'postgresql': | ||
51 | $this->addSql('ALTER TABLE '.$this->getTable('entry').' RENAME uuid TO uid'); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * @param Schema $schema | ||
57 | */ | ||
58 | public function down(Schema $schema) | ||
59 | { | ||
60 | $entryTable = $schema->getTable($this->getTable('entry')); | ||
61 | |||
62 | $this->skipIf($entryTable->hasColumn('uuid'), 'It seems that you already played this migration.'); | ||
63 | |||
64 | switch ($this->connection->getDatabasePlatform()->getName()) { | ||
65 | case 'sqlite': | ||
66 | throw new SkipMigrationException('Too complex ...'); | ||
67 | break; | ||
68 | case 'mysql': | ||
69 | $this->addSql('ALTER TABLE '.$this->getTable('entry').' CHANGE uid uuid VARCHAR(23)'); | ||
70 | break; | ||
71 | case 'postgresql': | ||
72 | $this->addSql('ALTER TABLE '.$this->getTable('entry').' RENAME uid TO uuid'); | ||
73 | } | ||
74 | } | ||
75 | } | ||
diff --git a/composer.json b/composer.json index b21c9538..767d9c82 100644 --- a/composer.json +++ b/composer.json | |||
@@ -72,6 +72,7 @@ | |||
72 | "grandt/phpepub": "~4.0", | 72 | "grandt/phpepub": "~4.0", |
73 | "wallabag/php-mobi": "~1.0.0", | 73 | "wallabag/php-mobi": "~1.0.0", |
74 | "kphoen/rulerz-bundle": "~0.10", | 74 | "kphoen/rulerz-bundle": "~0.10", |
75 | "kphoen/rulerz": "0.19.1", | ||
75 | "guzzlehttp/guzzle": "^5.3.1", | 76 | "guzzlehttp/guzzle": "^5.3.1", |
76 | "doctrine/doctrine-migrations-bundle": "^1.0", | 77 | "doctrine/doctrine-migrations-bundle": "^1.0", |
77 | "paragonie/random_compat": "~1.0", | 78 | "paragonie/random_compat": "~1.0", |
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 | * |
diff --git a/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php new file mode 100644 index 00000000..6798c5d7 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php | |||
@@ -0,0 +1,78 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\CoreBundle\Command; | ||
4 | |||
5 | use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
6 | use Symfony\Component\Console\Tester\CommandTester; | ||
7 | use Wallabag\CoreBundle\Command\ExportCommand; | ||
8 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
9 | |||
10 | class 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 | $this->assertFileExists('admin-export.json'); | ||
60 | } | ||
61 | |||
62 | public function testExportCommandWithSpecialPath() | ||
63 | { | ||
64 | $application = new Application($this->getClient()->getKernel()); | ||
65 | $application->add(new ExportCommand()); | ||
66 | |||
67 | $command = $application->find('wallabag:export'); | ||
68 | |||
69 | $tester = new CommandTester($command); | ||
70 | $tester->execute([ | ||
71 | 'command' => $command->getName(), | ||
72 | 'username' => 'admin', | ||
73 | 'filepath' => 'specialexport.json' | ||
74 | ]); | ||
75 | |||
76 | $this->assertFileExists('specialexport.json'); | ||
77 | } | ||
78 | } | ||