diff --git a/.gitignore b/.gitignore index 54f24c800..f3b908269 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ node_modules/ vendor/ *.zip *.tar.gz +composer.lock diff --git a/features/user-import-csv.feature b/features/user-import-csv.feature index 06af883ba..94d570e0e 100644 --- a/features/user-import-csv.feature +++ b/features/user-import-csv.feature @@ -135,3 +135,45 @@ Feature: Import users from CSV | display_name | roles | | Bob Jones | contributor | | Bill Jones | administrator,author | + + Scenario: Importing users from STDIN + Given a WP install + And a users.csv file: + """ + user_login,user_email,display_name,role + bobjones,bobjones@example.com,Bob Jones,contributor + newuser1,newuser1@example.com,New User,author + admin,admin@example.com,Existing User,administrator + """ + + When I try `wp user import-csv -` + Then STDERR should be: + """ + Error: Unable to read content from STDIN. + """ + + When I run `cat users.csv | wp user import-csv -` + Then STDOUT should be: + """ + Success: bobjones created. + Success: newuser1 created. + Success: admin updated. + """ + And an email should not be sent + + When I run `wp user list --format=count` + Then STDOUT should be: + """ + 3 + """ + + When I run `wp user list --format=json` + Then STDOUT should be JSON containing: + """ + [{ + "user_login":"admin", + "display_name":"Existing User", + "user_email":"admin@example.com", + "roles":"administrator" + }] + """ diff --git a/src/User_Command.php b/src/User_Command.php index 13904c23c..ccb3e4fce 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -836,7 +836,7 @@ public function list_caps( $args, $assoc_args ) { * ## OPTIONS * * - * : The local or remote CSV file of users to import. + * : The local or remote CSV file of users to import. If '-', then reads from STDIN. * * [--send-email] * : Send an email to new users with their account details. @@ -876,7 +876,11 @@ public function import_csv( $args, $assoc_args ) { if ( in_array( $response_code[0], array( 4, 5 ) ) ) { WP_CLI::error( "Couldn't access remote CSV file (HTTP {$response_code} response)." ); } - } else if ( ! file_exists( $filename ) ) { + } elseif ( '-' === $filename ) { + if ( ! WP_CLI\Entity\Utils::has_stdin() ) { + \WP_CLI::error( "Unable to read content from STDIN." ); + } + } elseif ( ! file_exists( $filename ) ) { WP_CLI::error( sprintf( "Missing file: %s", $filename ) ); } @@ -884,7 +888,28 @@ public function import_csv( $args, $assoc_args ) { add_filter( 'send_password_change_email', '__return_false' ); add_filter( 'send_email_change_email', '__return_false' ); - foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $i => $new_user ) { + if ( '-' === $filename && WP_CLI\Entity\Utils::has_stdin() ) { + $file_object = new NoRewindIterator( new SplFileObject( "php://stdin" ) ); + $file_object->setFlags( SplFileObject::READ_CSV ); + $csv_data = array(); + $indexes = array(); + foreach ( $file_object as $line ) { + if ( empty( $line[0] ) ) { + continue; + } elseif ( empty( $indexes ) ) { + $indexes = $line; + continue; + } + foreach ( $indexes as $n => $key ) { + $data[ $key ] = $line[ $n ]; + } + $csv_data[] = $data; + } + } else { + $csv_data = new \WP_CLI\Iterators\CSV( $filename ); + } + + foreach ( $csv_data as $i => $new_user ) { $defaults = array( 'role' => get_option('default_role'), 'user_pass' => wp_generate_password(),