diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2017-05-04 15:19:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-04 15:19:16 +0200 |
commit | cebed9c01f20d47cb60259f0c002ea57a80da4d0 (patch) | |
tree | 1a0e848c275ea93943d0b8debe4044346f52efdb | |
parent | 6b76ae3d1f6a061237f983622b2d3708beded368 (diff) | |
parent | d1e5059ea0ccfbf8e224e71f8d233b01ddfbc92d (diff) | |
download | wallabag-cebed9c01f20d47cb60259f0c002ea57a80da4d0.tar.gz wallabag-cebed9c01f20d47cb60259f0c002ea57a80da4d0.tar.zst wallabag-cebed9c01f20d47cb60259f0c002ea57a80da4d0.zip |
Merge pull request #3080 from wallabag/use-username-to-import
Use username to import
-rw-r--r-- | docs/en/user/import.rst | 15 | ||||
-rw-r--r-- | src/Wallabag/ImportBundle/Command/ImportCommand.php | 11 | ||||
-rw-r--r-- | tests/Wallabag/ImportBundle/Command/ImportCommandTest.php | 31 |
3 files changed, 41 insertions, 16 deletions
diff --git a/docs/en/user/import.rst b/docs/en/user/import.rst index 50bb1de3..f6aaa373 100644 --- a/docs/en/user/import.rst +++ b/docs/en/user/import.rst | |||
@@ -77,7 +77,7 @@ From Instapaper | |||
77 | --------------- | 77 | --------------- |
78 | 78 | ||
79 | Export your Instapaper data | 79 | Export your Instapaper data |
80 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 80 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
81 | 81 | ||
82 | On the settings (`https://www.instapaper.com/user <https://www.instapaper.com/user>`_) page, click on "Download .CSV file" in the "Export" section. A CSV file will be downloaded (like ``instapaper-export.csv``). | 82 | On the settings (`https://www.instapaper.com/user <https://www.instapaper.com/user>`_) page, click on "Download .CSV file" in the "Export" section. A CSV file will be downloaded (like ``instapaper-export.csv``). |
83 | 83 | ||
@@ -133,16 +133,21 @@ If you have a CLI access on your web server, you can execute this command to imp | |||
133 | 133 | ||
134 | :: | 134 | :: |
135 | 135 | ||
136 | bin/console wallabag:import 1 ~/Downloads/wallabag-export-1-2016-04-05.json --env=prod | 136 | bin/console wallabag:import username ~/Downloads/wallabag-export-1-2016-04-05.json --env=prod |
137 | 137 | ||
138 | Please replace values: | 138 | Please replace values: |
139 | 139 | ||
140 | * ``1`` is the user identifier in database (The ID of the first user created on wallabag is 1) | 140 | * ``username`` is the user's username |
141 | * ``~/Downloads/wallabag-export-1-2016-04-05.json`` is the path of your wallabag v1 export | 141 | * ``~/Downloads/wallabag-export-1-2016-04-05.json`` is the path of your wallabag v1 export |
142 | 142 | ||
143 | If you want to mark all these entries as read, you can add the ``--markAsRead`` option. | 143 | .. note:: |
144 | If you want to mark all these entries as read, you can add the ``--markAsRead`` option. | ||
144 | 145 | ||
145 | To import a wallabag v2 file, you need to add the option ``--importer=v2``. | 146 | .. note:: |
147 | To import a wallabag v2 file, you need to add the option ``--importer=v2``. | ||
148 | |||
149 | .. note:: | ||
150 | If you want to pass the user id of the user instead of it's username, add the option ``--useUserId=true``. | ||
146 | 151 | ||
147 | You'll have this in return: | 152 | You'll have this in return: |
148 | 153 | ||
diff --git a/src/Wallabag/ImportBundle/Command/ImportCommand.php b/src/Wallabag/ImportBundle/Command/ImportCommand.php index 28d01715..ce72837a 100644 --- a/src/Wallabag/ImportBundle/Command/ImportCommand.php +++ b/src/Wallabag/ImportBundle/Command/ImportCommand.php | |||
@@ -15,10 +15,11 @@ class ImportCommand extends ContainerAwareCommand | |||
15 | $this | 15 | $this |
16 | ->setName('wallabag:import') | 16 | ->setName('wallabag:import') |
17 | ->setDescription('Import entries from a JSON export') | 17 | ->setDescription('Import entries from a JSON export') |
18 | ->addArgument('userId', InputArgument::REQUIRED, 'User ID to populate') | 18 | ->addArgument('username', InputArgument::REQUIRED, 'User to populate') |
19 | ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file') | 19 | ->addArgument('filepath', InputArgument::REQUIRED, 'Path to the JSON file') |
20 | ->addOption('importer', null, InputArgument::OPTIONAL, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1') | 20 | ->addOption('importer', null, InputArgument::OPTIONAL, 'The importer to use: v1, v2, instapaper, pinboard, readability, firefox or chrome', 'v1') |
21 | ->addOption('markAsRead', null, InputArgument::OPTIONAL, 'Mark all entries as read', false) | 21 | ->addOption('markAsRead', null, InputArgument::OPTIONAL, 'Mark all entries as read', false) |
22 | ->addOption('useUserId', null, InputArgument::OPTIONAL, 'Use user id instead of username to find account', false) | ||
22 | ; | 23 | ; |
23 | } | 24 | } |
24 | 25 | ||
@@ -34,10 +35,14 @@ class ImportCommand extends ContainerAwareCommand | |||
34 | // Turning off doctrine default logs queries for saving memory | 35 | // Turning off doctrine default logs queries for saving memory |
35 | $em->getConnection()->getConfiguration()->setSQLLogger(null); | 36 | $em->getConnection()->getConfiguration()->setSQLLogger(null); |
36 | 37 | ||
37 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('userId')); | 38 | if ($input->getOption('useUserId')) { |
39 | $user = $em->getRepository('WallabagUserBundle:User')->findOneById($input->getArgument('username')); | ||
40 | } else { | ||
41 | $user = $em->getRepository('WallabagUserBundle:User')->findOneByUsername($input->getArgument('username')); | ||
42 | } | ||
38 | 43 | ||
39 | if (!is_object($user)) { | 44 | if (!is_object($user)) { |
40 | throw new Exception(sprintf('User with id "%s" not found', $input->getArgument('userId'))); | 45 | throw new Exception(sprintf('User "%s" not found', $input->getArgument('username'))); |
41 | } | 46 | } |
42 | 47 | ||
43 | switch ($input->getOption('importer')) { | 48 | switch ($input->getOption('importer')) { |
diff --git a/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php b/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php index 7be1eb18..7043c345 100644 --- a/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php +++ b/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php | |||
@@ -10,7 +10,7 @@ use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | |||
10 | class ImportCommandTest extends WallabagCoreTestCase | 10 | class ImportCommandTest extends WallabagCoreTestCase |
11 | { | 11 | { |
12 | /** | 12 | /** |
13 | * @expectedException Symfony\Component\Console\Exception\RuntimeException | 13 | * @expectedException \Symfony\Component\Console\Exception\RuntimeException |
14 | * @expectedExceptionMessage Not enough arguments | 14 | * @expectedExceptionMessage Not enough arguments |
15 | */ | 15 | */ |
16 | public function testRunImportCommandWithoutArguments() | 16 | public function testRunImportCommandWithoutArguments() |
@@ -27,7 +27,7 @@ class ImportCommandTest extends WallabagCoreTestCase | |||
27 | } | 27 | } |
28 | 28 | ||
29 | /** | 29 | /** |
30 | * @expectedException Symfony\Component\Config\Definition\Exception\Exception | 30 | * @expectedException \Symfony\Component\Config\Definition\Exception\Exception |
31 | * @expectedExceptionMessage not found | 31 | * @expectedExceptionMessage not found |
32 | */ | 32 | */ |
33 | public function testRunImportCommandWithoutFilepath() | 33 | public function testRunImportCommandWithoutFilepath() |
@@ -40,16 +40,15 @@ class ImportCommandTest extends WallabagCoreTestCase | |||
40 | $tester = new CommandTester($command); | 40 | $tester = new CommandTester($command); |
41 | $tester->execute([ | 41 | $tester->execute([ |
42 | 'command' => $command->getName(), | 42 | 'command' => $command->getName(), |
43 | 'userId' => 1, | 43 | 'username' => 'admin', |
44 | 'filepath' => 1, | 44 | 'filepath' => 1, |
45 | ]); | 45 | ]); |
46 | } | 46 | } |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * @expectedException Symfony\Component\Config\Definition\Exception\Exception | 49 | * @expectedException \Doctrine\ORM\NoResultException |
50 | * @expectedExceptionMessage User with id | ||
51 | */ | 50 | */ |
52 | public function testRunImportCommandWithoutUserId() | 51 | public function testRunImportCommandWithWrongUsername() |
53 | { | 52 | { |
54 | $application = new Application($this->getClient()->getKernel()); | 53 | $application = new Application($this->getClient()->getKernel()); |
55 | $application->add(new ImportCommand()); | 54 | $application->add(new ImportCommand()); |
@@ -59,7 +58,7 @@ class ImportCommandTest extends WallabagCoreTestCase | |||
59 | $tester = new CommandTester($command); | 58 | $tester = new CommandTester($command); |
60 | $tester->execute([ | 59 | $tester->execute([ |
61 | 'command' => $command->getName(), | 60 | 'command' => $command->getName(), |
62 | 'userId' => 0, | 61 | 'username' => 'random', |
63 | 'filepath' => './', | 62 | 'filepath' => './', |
64 | ]); | 63 | ]); |
65 | } | 64 | } |
@@ -74,7 +73,7 @@ class ImportCommandTest extends WallabagCoreTestCase | |||
74 | $tester = new CommandTester($command); | 73 | $tester = new CommandTester($command); |
75 | $tester->execute([ | 74 | $tester->execute([ |
76 | 'command' => $command->getName(), | 75 | 'command' => $command->getName(), |
77 | 'userId' => 1, | 76 | 'username' => 'admin', |
78 | 'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../tests/Wallabag/ImportBundle/fixtures/wallabag-v2-read.json', | 77 | 'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../tests/Wallabag/ImportBundle/fixtures/wallabag-v2-read.json', |
79 | '--importer' => 'v2', | 78 | '--importer' => 'v2', |
80 | ]); | 79 | ]); |
@@ -82,4 +81,20 @@ class ImportCommandTest extends WallabagCoreTestCase | |||
82 | $this->assertContains('imported', $tester->getDisplay()); | 81 | $this->assertContains('imported', $tester->getDisplay()); |
83 | $this->assertContains('already saved', $tester->getDisplay()); | 82 | $this->assertContains('already saved', $tester->getDisplay()); |
84 | } | 83 | } |
84 | |||
85 | public function testRunImportCommandWithUserId() | ||
86 | { | ||
87 | $application = new Application($this->getClient()->getKernel()); | ||
88 | $application->add(new ImportCommand()); | ||
89 | |||
90 | $command = $application->find('wallabag:import'); | ||
91 | |||
92 | $tester = new CommandTester($command); | ||
93 | $tester->execute([ | ||
94 | 'command' => $command->getName(), | ||
95 | 'username' => 1, | ||
96 | 'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../tests/Wallabag/ImportBundle/fixtures/wallabag-v2-read.json', | ||
97 | '--useUserId' => true, | ||
98 | ]); | ||
99 | } | ||
85 | } | 100 | } |