diff --git a/README.md b/README.md index eafdc83b1..cc5a5a84c 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,15 @@ change primary key values. [--regex-flags=] Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity). + [--format=] + Render output in a particular format. + --- + default: table + options: + - table + - count + --- + **EXAMPLES** # Search and replace but skip one column diff --git a/features/search-replace.feature b/features/search-replace.feature index e23581d38..a60ffbbeb 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -335,3 +335,33 @@ Feature: Do global search/replace """ http://BAXAMPLE.com """ + + Scenario: Formatting as count-only + Given a WP install + And I run `wp option set foo 'ALPHA.example.com'` + + # --quite should suppress --format=count + When I run `wp search-replace 'ALPHA.example.com' 'BETA.example.com' --quiet --format=count` + Then STDOUT should be empty + + # --format=count should suppress --verbose + When I run `wp search-replace 'BETA.example.com' 'ALPHA.example.com' --format=count --verbose` + Then STDOUT should be: + """ + 1 + """ + + # The normal command + When I run `wp search-replace 'ALPHA.example.com' 'BETA.example.com' --format=count` + Then STDOUT should be: + """ + 1 + """ + + # Lets just make sure that zero works, too. + When I run `wp search-replace 'DELTA.example.com' 'ALPHA.example.com' --format=count` + Then STDOUT should be: + """ + 0 + """ + diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index b269afda9..bd82a7351 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -10,6 +10,7 @@ class Search_Replace_Command extends WP_CLI_Command { private $regex_flags; private $skip_columns; private $include_columns; + private $format; /** * Search/replace strings in the database. @@ -88,6 +89,15 @@ class Search_Replace_Command extends WP_CLI_Command { * [--regex-flags=] * : Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity). * + * [--format=] + * : Render output in a particular format. + * --- + * default: table + * options: + * - table + * - count + * --- + * * ## EXAMPLES * * # Search and replace but skip one column @@ -125,6 +135,7 @@ public function __invoke( $args, $assoc_args ) { $this->verbose = \WP_CLI\Utils\get_flag_value( $assoc_args, 'verbose' ); $this->regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex' ); $this->regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags' ); + $this->format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); $this->skip_columns = explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-columns' ) ); $this->include_columns = array_filter( explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'include-columns' ) ) ); @@ -194,7 +205,7 @@ public function __invoke( $args, $assoc_args ) { continue; } - if ( $this->verbose ) { + if ( $this->verbose && 'count' !== $this->format ) { $this->start_time = microtime( true ); WP_CLI::log( sprintf( 'Checking: %s.%s', $table, $col ) ); } @@ -231,6 +242,11 @@ public function __invoke( $args, $assoc_args ) { return; } + if ( 'count' === $this->format ) { + WP_CLI::line( $total ); + return; + } + $table = new \cli\Table(); $table->setHeaders( array( 'Table', 'Column', 'Replacements', 'Type' ) ); $table->setRows( $report ); @@ -264,7 +280,7 @@ private function php_export_table( $table, $old, $new ) { $replacer = new \WP_CLI\SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags ); $col_counts = array_fill_keys( $all_columns, 0 ); - if ( $this->verbose ) { + if ( $this->verbose && 'table' === $this->format ) { $this->start_time = microtime( true ); WP_CLI::log( sprintf( 'Checking: %s', $table ) ); } @@ -297,7 +313,7 @@ private function php_export_table( $table, $old, $new ) { } } - if ( $this->verbose ) { + if ( $this->verbose && 'table' === $this->format ) { $time = round( microtime( true ) - $this->start_time, 3 ); WP_CLI::log( sprintf( '%d columns and %d total rows affected using PHP (in %ss).', $total_cols, $total_rows, $time ) ); } @@ -314,7 +330,7 @@ private function sql_handle_col( $col, $table, $old, $new ) { $count = $wpdb->query( $wpdb->prepare( "UPDATE `$table` SET `$col` = REPLACE(`$col`, %s, %s);", $old, $new ) ); } - if ( $this->verbose ) { + if ( $this->verbose && 'table' === $this->format ) { $time = round( microtime( true ) - $this->start_time, 3 ); WP_CLI::log( sprintf( '%d rows affected using SQL (in %ss).', $count, $time ) ); } @@ -362,7 +378,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) { } } - if ( $this->verbose ) { + if ( $this->verbose && 'table' === $this->format ) { $time = round( microtime( true ) - $this->start_time, 3 ); WP_CLI::log( sprintf( '%d rows affected using PHP (in %ss).', $count, $time ) ); }