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
29 changes: 29 additions & 0 deletions features/search-replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,35 @@ Feature: Do global search/replace
Success: Made 0 replacements.
"""

Scenario: Precise search and replace distinguishes integer-looking text primary keys
Given a WP install
And I run `wp db query "CREATE TABLE wp_sr_key_test ( id VARCHAR(10) NOT NULL PRIMARY KEY, url TEXT NOT NULL );"`
And I run `wp db query "INSERT INTO wp_sr_key_test (id, url) VALUES ('001', 'https://old.example/foo'), ('1', 'https://old.example/bar'), ('1abc', 'https://old.example/baz');"`

When I run `wp search-replace 'https://old.example' 'https://new.example' wp_sr_key_test --all-tables-with-prefix --include-columns=url --precise`
Then STDOUT should contain:
"""
Success: Made 3 replacements.
"""

When I run `wp db query "SELECT url FROM wp_sr_key_test WHERE id = '001';"`
Then STDOUT should contain:
"""
https://new.example/foo
"""

When I run `wp db query "SELECT url FROM wp_sr_key_test WHERE id = '1';"`
Then STDOUT should contain:
"""
https://new.example/bar
"""

When I run `wp db query "SELECT url FROM wp_sr_key_test WHERE id = '1abc';"`
Then STDOUT should contain:
"""
https://new.example/baz
"""

@require-mysql
Scenario: Chunking a regex search and replace works without skipping lines
Given a WP install
Expand Down
35 changes: 23 additions & 12 deletions src/Search_Replace_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public function __invoke( $args, $assoc_args ) {
continue;
}

list( $primary_keys, $columns, $all_columns ) = self::get_columns( $table );
list( $primary_keys, $columns, $all_columns, $numeric_keys ) = self::get_columns( $table );

// since we'll be updating one row at a time,
// we need a primary key to identify the row
Expand Down Expand Up @@ -607,7 +607,7 @@ function ( $col ) use ( $table ) {
if ( $php_only || $this->regex || null !== $serial_row ) {
$type = 'PHP';
// Nothing to replace in a column without matches.
$count = null !== $stats && 0 === $stats['matches'] ? 0 : $this->php_handle_col( $col, $primary_keys, $table, $old, $new );
$count = null !== $stats && 0 === $stats['matches'] ? 0 : $this->php_handle_col( $col, $primary_keys, $table, $old, $new, $numeric_keys );
} else {
$type = 'SQL';
if ( null !== $stats && ( 0 === $stats['matches'] || ( $this->dry_run && ! $this->log_handle ) ) ) {
Expand Down Expand Up @@ -777,7 +777,7 @@ private function sql_handle_col( $col, $primary_keys, $table, $old, $new, $match
return $count;
}

private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
private function php_handle_col( $col, $primary_keys, $table, $old, $new, $numeric_keys = array() ) {
global $wpdb;

$count = 0;
Expand Down Expand Up @@ -819,7 +819,7 @@ static function ( $key ) {
if ( '' !== $where_sql ) {
$where_sql .= ' AND ';
}
$where_sql .= self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v );
$where_sql .= self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v, in_array( $k, $numeric_keys, true ) );
}

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
Expand Down Expand Up @@ -897,9 +897,9 @@ static function ( $key ) {
$v = $last_row->{ $k };

if ( $i < $last_key_index ) {
$next_key_subconditions[] = self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v );
$next_key_subconditions[] = self::esc_sql_ident( $k ) . ' = ' . self::esc_sql_value( $v, in_array( $k, $numeric_keys, true ) );
} else {
$next_key_subconditions[] = self::esc_sql_ident( $k ) . ' > ' . self::esc_sql_value( $v );
$next_key_subconditions[] = self::esc_sql_ident( $k ) . ' > ' . self::esc_sql_value( $v, in_array( $k, $numeric_keys, true ) );
}
}

Expand Down Expand Up @@ -998,6 +998,7 @@ private static function get_columns( $table ) {

$table_sql = self::esc_sql_ident( $table );
$primary_keys = array();
$numeric_keys = array();
$text_columns = array();
$all_columns = array();
$suppress_errors = $wpdb->suppress_errors();
Expand All @@ -1009,6 +1010,9 @@ private static function get_columns( $table ) {
foreach ( $results as $col ) {
if ( 'PRI' === $col->Key ) {
$primary_keys[] = $col->Field;
if ( self::is_numeric_col( $col->Type ) ) {
$numeric_keys[] = $col->Field;
}
}
if ( self::is_text_col( $col->Type ) ) {
$text_columns[] = $col->Field;
Expand All @@ -1017,7 +1021,7 @@ private static function get_columns( $table ) {
}
}
$wpdb->suppress_errors( $suppress_errors );
return array( $primary_keys, $text_columns, $all_columns );
return array( $primary_keys, $text_columns, $all_columns, $numeric_keys );
}

/**
Expand Down Expand Up @@ -1102,6 +1106,10 @@ private static function is_text_col( $type ) {
return false;
}

private static function is_numeric_col( $type ) {
return (bool) preg_match( '/^(tinyint|smallint|mediumint|int|integer|bigint|decimal|numeric|dec|fixed|float|double|real|bit|bool|boolean|year)\b/i', trim( $type ) );
}

private static function esc_like( $old ) {
global $wpdb;

Expand Down Expand Up @@ -1153,14 +1161,17 @@ private static function esc_sql_ident( $idents ) {
/**
* Puts MySQL string values in single quotes, to avoid them being interpreted as column names.
*
* @param string|string[] $values A single value or an array of values.
* Integer values for numeric columns are left unquoted, to avoid MySQL's implicit type conversion.
* They are quoted for text columns, where an unquoted `1` would also match `'001'` or `'1abc'`.
*
* @param string|string[] $values A single value or an array of values.
* @param bool $is_numeric Whether the values are compared against a numeric column.
* @return string|string[] A quoted string if given a string, or an array of quoted strings if given an array of strings.
* @phpstan-return ($values is string ? string : string[])
*/
private static function esc_sql_value( $values ) {
$quote = static function ( $v ) {
// Don't quote integer values to avoid MySQL's implicit type conversion.
if ( preg_match( '/^[+-]?[0-9]{1,20}$/', $v ) ) { // MySQL BIGINT UNSIGNED max 18446744073709551615 (20 digits).
private static function esc_sql_value( $values, $is_numeric = false ) {
$quote = static function ( $v ) use ( $is_numeric ) {
if ( $is_numeric && preg_match( '/^[+-]?[0-9]{1,20}$/', $v ) ) { // MySQL BIGINT UNSIGNED max 18446744073709551615 (20 digits).
return esc_sql( $v );
}

Expand Down