Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions features/search-replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions src/Search_Replace_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,13 @@ class Search_Replace_Command extends WP_CLI_Command {
*
* [--skip-columns=<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=<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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -613,7 +615,11 @@ 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 ( ! empty( $this->include_columns ) && ! in_array( $col, $this->include_columns, true ) && ! in_array( $table . '.' . $col, $this->include_columns, true ) ) {
$row_fields[ $col ] = $value;
continue;
}
if ( $value && ! in_array( $col, $primary_keys, true ) && ! in_array( $col, $this->skip_columns, true ) && ! in_array( $table . '.' . $col, $this->skip_columns, true ) ) {
Comment on lines +618 to +622
$new_value = $replacer->run( $value );
if ( $new_value !== $value ) {
++$col_counts[ $col ];
Expand Down
Loading