Skip to content

Commit dfedb1e

Browse files
Merge pull request #100 from wp-cli/patch-96
`wp user import-csv` to be able to import CSV from STDIN
2 parents 50f907c + 4048954 commit dfedb1e

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_modules/
44
vendor/
55
*.zip
66
*.tar.gz
7+
composer.lock

features/user-import-csv.feature

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,45 @@ Feature: Import users from CSV
135135
| display_name | roles |
136136
| Bob Jones | contributor |
137137
| Bill Jones | administrator,author |
138+
139+
Scenario: Importing users from STDIN
140+
Given a WP install
141+
And a users.csv file:
142+
"""
143+
user_login,user_email,display_name,role
144+
bobjones,[email protected],Bob Jones,contributor
145+
newuser1,[email protected],New User,author
146+
admin,[email protected],Existing User,administrator
147+
"""
148+
149+
When I try `wp user import-csv -`
150+
Then STDERR should be:
151+
"""
152+
Error: Unable to read content from STDIN.
153+
"""
154+
155+
When I run `cat users.csv | wp user import-csv -`
156+
Then STDOUT should be:
157+
"""
158+
Success: bobjones created.
159+
Success: newuser1 created.
160+
Success: admin updated.
161+
"""
162+
And an email should not be sent
163+
164+
When I run `wp user list --format=count`
165+
Then STDOUT should be:
166+
"""
167+
3
168+
"""
169+
170+
When I run `wp user list --format=json`
171+
Then STDOUT should be JSON containing:
172+
"""
173+
[{
174+
"user_login":"admin",
175+
"display_name":"Existing User",
176+
"user_email":"[email protected]",
177+
"roles":"administrator"
178+
}]
179+
"""

src/User_Command.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ public function list_caps( $args, $assoc_args ) {
836836
* ## OPTIONS
837837
*
838838
* <file>
839-
* : The local or remote CSV file of users to import.
839+
* : The local or remote CSV file of users to import. If '-', then reads from STDIN.
840840
*
841841
* [--send-email]
842842
* : Send an email to new users with their account details.
@@ -876,15 +876,40 @@ public function import_csv( $args, $assoc_args ) {
876876
if ( in_array( $response_code[0], array( 4, 5 ) ) ) {
877877
WP_CLI::error( "Couldn't access remote CSV file (HTTP {$response_code} response)." );
878878
}
879-
} else if ( ! file_exists( $filename ) ) {
879+
} elseif ( '-' === $filename ) {
880+
if ( ! WP_CLI\Entity\Utils::has_stdin() ) {
881+
\WP_CLI::error( "Unable to read content from STDIN." );
882+
}
883+
} elseif ( ! file_exists( $filename ) ) {
880884
WP_CLI::error( sprintf( "Missing file: %s", $filename ) );
881885
}
882886

883887
// Don't send core's emails during the creation / update process
884888
add_filter( 'send_password_change_email', '__return_false' );
885889
add_filter( 'send_email_change_email', '__return_false' );
886890

887-
foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $i => $new_user ) {
891+
if ( '-' === $filename && WP_CLI\Entity\Utils::has_stdin() ) {
892+
$file_object = new NoRewindIterator( new SplFileObject( "php://stdin" ) );
893+
$file_object->setFlags( SplFileObject::READ_CSV );
894+
$csv_data = array();
895+
$indexes = array();
896+
foreach ( $file_object as $line ) {
897+
if ( empty( $line[0] ) ) {
898+
continue;
899+
} elseif ( empty( $indexes ) ) {
900+
$indexes = $line;
901+
continue;
902+
}
903+
foreach ( $indexes as $n => $key ) {
904+
$data[ $key ] = $line[ $n ];
905+
}
906+
$csv_data[] = $data;
907+
}
908+
} else {
909+
$csv_data = new \WP_CLI\Iterators\CSV( $filename );
910+
}
911+
912+
foreach ( $csv_data as $i => $new_user ) {
888913
$defaults = array(
889914
'role' => get_option('default_role'),
890915
'user_pass' => wp_generate_password(),

0 commit comments

Comments
 (0)