From f1db86ba5dfc33f0939a387d318d9e8dc6e90645 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 4 Dec 2026 11:16:45 +0000 Subject: [PATCH 1/2] Quote text primary key values in precise search-replace Integer-looking primary key values were left unquoted in the WHERE clauses used by the PHP (precise) mode. For text columns, MySQL then compares numerically, so `id = 1` also matches '001' or '1abc', and the wrong row's value was read, leaving rows unchanged. Only leave such values unquoted for numeric primary key columns. Fixes #259. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5oWvURkuowLyTEv3vB --- features/search-replace.feature | 29 +++++++++++++++++++++++++++ src/Search_Replace_Command.php | 35 ++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/features/search-replace.feature b/features/search-replace.feature index 47278de2..33a8f10a 100644 --- a/features/search-replace.feature +++ b/features/search-replace.feature @@ -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 diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index 896ce556..a7f1eb3d 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -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 @@ -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 ) ) ) { @@ -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; @@ -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 @@ -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 ) ); } } @@ -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(); @@ -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; @@ -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 ); } /** @@ -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; @@ -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 $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, $numeric = false ) { + $quote = static function ( $v ) use ( $numeric ) { + if ( $numeric && preg_match( '/^[+-]?[0-9]{1,20}$/', $v ) ) { // MySQL BIGINT UNSIGNED max 18446744073709551615 (20 digits). return esc_sql( $v ); } From 342e7cc7fa3079fca0abcce66005283366dac08e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Fri, 4 Dec 2026 11:17:47 +0000 Subject: [PATCH 2/2] Rename parameter to avoid reserved keyword Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01UWTd5oWvURkuowLyTEv3vB --- src/Search_Replace_Command.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Search_Replace_Command.php b/src/Search_Replace_Command.php index a7f1eb3d..da233b02 100644 --- a/src/Search_Replace_Command.php +++ b/src/Search_Replace_Command.php @@ -1164,14 +1164,14 @@ private static function esc_sql_ident( $idents ) { * 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 $numeric Whether the values are compared against a numeric column. + * @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, $numeric = false ) { - $quote = static function ( $v ) use ( $numeric ) { - if ( $numeric && 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 ); }