Skip to content

Commit

Permalink
Merge pull request #14 from vendi-advertising/format-count
Browse files Browse the repository at this point in the history
Use `--format=count` to only show number of rows affected
  • Loading branch information
danielbachhuber authored Jun 28, 2017
2 parents 8db332d + 0ee701b commit bb99b41
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ change primary key values.
[--regex-flags=<regex-flags>]
Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).

[--format=<format>]
Render output in a particular format.
---
default: table
options:
- table
- count
---

**EXAMPLES**

# Search and replace but skip one column
Expand Down
30 changes: 30 additions & 0 deletions features/search-replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

26 changes: 21 additions & 5 deletions src/Search_Replace_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -88,6 +89,15 @@ class Search_Replace_Command extends WP_CLI_Command {
* [--regex-flags=<regex-flags>]
* : Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - count
* ---
*
* ## EXAMPLES
*
* # Search and replace but skip one column
Expand Down Expand Up @@ -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' ) ) );
Expand Down Expand Up @@ -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 ) );
}
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 ) );
}
Expand Down Expand Up @@ -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 ) );
}
Expand All @@ -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 ) );
}
Expand Down Expand Up @@ -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 ) );
}
Expand Down

0 comments on commit bb99b41

Please sign in to comment.