Skip to content

Commit 0ee701b

Browse files
committed
Added support for only showing the number of rows affected
1 parent 8db332d commit 0ee701b

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ change primary key values.
8787
[--regex-flags=<regex-flags>]
8888
Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).
8989

90+
[--format=<format>]
91+
Render output in a particular format.
92+
---
93+
default: table
94+
options:
95+
- table
96+
- count
97+
---
98+
9099
**EXAMPLES**
91100

92101
# Search and replace but skip one column

features/search-replace.feature

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,33 @@ Feature: Do global search/replace
335335
"""
336336
http://BAXAMPLE.com
337337
"""
338+
339+
Scenario: Formatting as count-only
340+
Given a WP install
341+
And I run `wp option set foo 'ALPHA.example.com'`
342+
343+
# --quite should suppress --format=count
344+
When I run `wp search-replace 'ALPHA.example.com' 'BETA.example.com' --quiet --format=count`
345+
Then STDOUT should be empty
346+
347+
# --format=count should suppress --verbose
348+
When I run `wp search-replace 'BETA.example.com' 'ALPHA.example.com' --format=count --verbose`
349+
Then STDOUT should be:
350+
"""
351+
1
352+
"""
353+
354+
# The normal command
355+
When I run `wp search-replace 'ALPHA.example.com' 'BETA.example.com' --format=count`
356+
Then STDOUT should be:
357+
"""
358+
1
359+
"""
360+
361+
# Lets just make sure that zero works, too.
362+
When I run `wp search-replace 'DELTA.example.com' 'ALPHA.example.com' --format=count`
363+
Then STDOUT should be:
364+
"""
365+
0
366+
"""
367+

src/Search_Replace_Command.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Search_Replace_Command extends WP_CLI_Command {
1010
private $regex_flags;
1111
private $skip_columns;
1212
private $include_columns;
13+
private $format;
1314

1415
/**
1516
* Search/replace strings in the database.
@@ -88,6 +89,15 @@ class Search_Replace_Command extends WP_CLI_Command {
8889
* [--regex-flags=<regex-flags>]
8990
* : Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).
9091
*
92+
* [--format=<format>]
93+
* : Render output in a particular format.
94+
* ---
95+
* default: table
96+
* options:
97+
* - table
98+
* - count
99+
* ---
100+
*
91101
* ## EXAMPLES
92102
*
93103
* # Search and replace but skip one column
@@ -125,6 +135,7 @@ public function __invoke( $args, $assoc_args ) {
125135
$this->verbose = \WP_CLI\Utils\get_flag_value( $assoc_args, 'verbose' );
126136
$this->regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex' );
127137
$this->regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags' );
138+
$this->format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' );
128139

129140
$this->skip_columns = explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-columns' ) );
130141
$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 ) {
194205
continue;
195206
}
196207

197-
if ( $this->verbose ) {
208+
if ( $this->verbose && 'count' !== $this->format ) {
198209
$this->start_time = microtime( true );
199210
WP_CLI::log( sprintf( 'Checking: %s.%s', $table, $col ) );
200211
}
@@ -231,6 +242,11 @@ public function __invoke( $args, $assoc_args ) {
231242
return;
232243
}
233244

245+
if ( 'count' === $this->format ) {
246+
WP_CLI::line( $total );
247+
return;
248+
}
249+
234250
$table = new \cli\Table();
235251
$table->setHeaders( array( 'Table', 'Column', 'Replacements', 'Type' ) );
236252
$table->setRows( $report );
@@ -264,7 +280,7 @@ private function php_export_table( $table, $old, $new ) {
264280

265281
$replacer = new \WP_CLI\SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags );
266282
$col_counts = array_fill_keys( $all_columns, 0 );
267-
if ( $this->verbose ) {
283+
if ( $this->verbose && 'table' === $this->format ) {
268284
$this->start_time = microtime( true );
269285
WP_CLI::log( sprintf( 'Checking: %s', $table ) );
270286
}
@@ -297,7 +313,7 @@ private function php_export_table( $table, $old, $new ) {
297313
}
298314
}
299315

300-
if ( $this->verbose ) {
316+
if ( $this->verbose && 'table' === $this->format ) {
301317
$time = round( microtime( true ) - $this->start_time, 3 );
302318
WP_CLI::log( sprintf( '%d columns and %d total rows affected using PHP (in %ss).', $total_cols, $total_rows, $time ) );
303319
}
@@ -314,7 +330,7 @@ private function sql_handle_col( $col, $table, $old, $new ) {
314330
$count = $wpdb->query( $wpdb->prepare( "UPDATE `$table` SET `$col` = REPLACE(`$col`, %s, %s);", $old, $new ) );
315331
}
316332

317-
if ( $this->verbose ) {
333+
if ( $this->verbose && 'table' === $this->format ) {
318334
$time = round( microtime( true ) - $this->start_time, 3 );
319335
WP_CLI::log( sprintf( '%d rows affected using SQL (in %ss).', $count, $time ) );
320336
}
@@ -362,7 +378,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
362378
}
363379
}
364380

365-
if ( $this->verbose ) {
381+
if ( $this->verbose && 'table' === $this->format ) {
366382
$time = round( microtime( true ) - $this->start_time, 3 );
367383
WP_CLI::log( sprintf( '%d rows affected using PHP (in %ss).', $count, $time ) );
368384
}

0 commit comments

Comments
 (0)