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
6 changes: 2 additions & 4 deletions features/search-replace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ Feature: Do global search/replace
"""

# See https://github.com/wp-cli/search-replace-command/issues/190
@skip-sqlite
Scenario: Regex search/replace
Given a WP install
When I run `wp search-replace '(Hello)\s(world)' '$2, $1' --regex`
Expand Down Expand Up @@ -962,6 +961,8 @@ Feature: Do global search/replace
And STDERR should be empty

# See https://github.com/wp-cli/search-replace-command/issues/190
# SQLite: regex backreference replacement produces different results due to
# LIKE case-sensitivity differences. Requires further investigation.
@skip-sqlite
Scenario: Logging with regex replace
Given a WP install
Expand Down Expand Up @@ -1326,7 +1327,6 @@ Feature: Do global search/replace
"""

# See https://github.com/wp-cli/search-replace-command/issues/190
@skip-sqlite
Scenario: Regex search/replace with `--regex-limit=1` option
Given a WP install
And I run `wp post create --post_content="I have a pen, I have an apple. Pen, pine-apple, apple-pen."`
Expand All @@ -1338,7 +1338,6 @@ Feature: Do global search/replace
"""

# See https://github.com/wp-cli/search-replace-command/issues/190
@skip-sqlite
Scenario: Regex search/replace with `--regex-limit=2` option
Given a WP install
And I run `wp post create --post_content="I have a pen, I have an apple. Pen, pine-apple, apple-pen."`
Expand All @@ -1350,7 +1349,6 @@ Feature: Do global search/replace
"""

# See https://github.com/wp-cli/search-replace-command/issues/190
@skip-sqlite
Scenario: Regex search/replace with incorrect or default `--regex-limit`
Given a WP install
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=asdf`
Expand Down
29 changes: 20 additions & 9 deletions src/Search_Replace_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,16 @@ public function __invoke( $args, $assoc_args ) {
$tables = Utils\wp_get_table_names( $args, $assoc_args );

// Identify views so they can be skipped; views are dynamic and cannot be directly modified.
$views_args = $assoc_args;
$views_args['views-only'] = true;
$views = Utils\wp_get_table_names( [], $views_args );
$view_set = array_flip( array_intersect( $views, $tables ) );
// The SQLite integration drop-in does not support SHOW FULL TABLES and WordPress
// on SQLite does not use views, so skip the views query entirely.
if ( 'sqlite' === Utils\get_db_type() ) {
$view_set = array();
} else {
$views_args = $assoc_args;
$views_args['views-only'] = true;
$views = Utils\wp_get_table_names( [], $views_args );
$view_set = array_flip( array_intersect( $views, $tables ) );
}

foreach ( $tables as $table ) {
foreach ( $this->skip_tables as $skip_table ) {
Expand Down Expand Up @@ -555,12 +561,17 @@ public function __invoke( $args, $assoc_args ) {
$col_sql = self::esc_sql_ident( $col );
$wpdb->last_error = '';

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
$serial_row = $wpdb->get_row( "SELECT * FROM $table_sql WHERE $col_sql REGEXP '^[aiO]:[1-9]' LIMIT 1" );

// When the regex triggers an error, we should fall back to PHP
if ( false !== strpos( $wpdb->last_error, 'ERROR 1139' ) ) {
if ( 'sqlite' === Utils\get_db_type() ) {
// SQLite does not support REGEXP by default, fall back to PHP processing.
$serial_row = true;
} else {
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- escaped through self::esc_sql_ident
$serial_row = $wpdb->get_row( "SELECT * FROM $table_sql WHERE $col_sql REGEXP '^[aiO]:[1-9]' LIMIT 1" );

// When the regex triggers an error, we should fall back to PHP
if ( false !== strpos( $wpdb->last_error, 'ERROR 1139' ) ) {
$serial_row = true;
}
}
}

Expand Down
Loading