diff --git a/features/search-replace.feature b/features/search-replace.feature index d6feee8a..99736a7f 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -41,6 +41,17 @@ Feature: Do global search/replace | Table | Column | Replacements | Type | | wp_posts | post_content | 0 | SQL | + When I run `wp search-replace foo bar --skip-columns=wp_posts.guid` + Then STDOUT should not contain: + """ + guid + """ + + When I run `wp search-replace foo bar --include-columns=wp_posts.post_content` + Then STDOUT should be a table containing rows: + | Table | Column | Replacements | Type | + | wp_posts | post_content | 0 | SQL | + @require-mysql Scenario: Multisite search/replace Given a WP multisite install diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index bbad1bdf..f08c6d5d 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -184,11 +184,13 @@ class Search_Replace_Command extends WP_CLI_Command { * * [--skip-columns=] * : Do not perform the replacement on specific columns. Use commas to - * specify multiple columns. + * specify multiple columns. Table-qualified column names ("table.column") + * are supported to apply the skip to a specific table only. * * [--include-columns=] * : Perform the replacement on specific columns. Use commas to - * specify multiple columns. + * specify multiple columns. Table-qualified column names ("table.column") + * are supported to apply the inclusion to a specific table only. * * [--precise] * : Force the use of PHP (instead of SQL) for all columns. By default, the command @@ -510,11 +512,11 @@ public function __invoke( $args, $assoc_args ) { } foreach ( $columns as $col ) { - if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) ) { + if ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) && ! in_array( $table . '.' . $col, $this->include_columns, true ) ) { continue; } - if ( in_array( $col, $this->skip_columns, true ) ) { + if ( in_array( $col, $this->skip_columns, true ) || in_array( $table . '.' . $col, $this->skip_columns, true ) ) { continue; } @@ -613,7 +615,12 @@ private function php_export_table( $table, $old, $new ) { $row_fields = array(); foreach ( $all_columns as $col ) { $value = $row->$col; - if ( $value && ! in_array( $col, $primary_keys, true ) && ! in_array( $col, $this->skip_columns, true ) ) { + if ( $value + && ! in_array( $col, $primary_keys, true ) + && ! in_array( $col, $this->skip_columns, true ) + && ! in_array( $table . '.' . $col, $this->skip_columns, true ) + && ( empty( $this->include_columns ) || in_array( $col, $this->include_columns, true ) || in_array( $table . '.' . $col, $this->include_columns, true ) ) + ) { $new_value = $replacer->run( $value ); if ( $new_value !== $value ) { ++$col_counts[ $col ];