diff --git a/features/db-search.feature b/features/db-search.feature index f5e3225e..e6e3d37b 100644 --- a/features/db-search.feature +++ b/features/db-search.feature @@ -982,6 +982,37 @@ Feature: Search through the database And STDOUT should match /\d tables? skipped:.*wp_term_relationships/ And STDERR should be empty + Scenario: Search with exclude revisions option + Given a WP install + And I run `wp post create --post_content="This is the original post content." --post_title="Original Post" --post_status=publish --porcelain` + And save STDOUT as {POST_ID} + And I run `wp post update {POST_ID} --post_content="This is the updated post content."` + + When I run `wp db search "updated post content" wp_posts --format=count` + Then STDOUT should be: + """ + 2 + """ + + When I run `wp db search "updated post content" wp_posts --exclude_revisions --format=ids` + Then STDOUT should be: + """ + {POST_ID} + """ + + When I run `wp db search "updated post content" --exclude_revisions` + Then STDOUT should contain: + """ + {POST_ID}:This is the updated post content. + """ + And STDERR should be empty + + When I run `wp db search "updated post content" wp_posts --exclude_revisions --regex --format=count` + Then STDOUT should be: + """ + 1 + """ + @skip-windows Scenario: Search with custom colors Given a WP install diff --git a/src/DB_Command.php b/src/DB_Command.php index 3b126a6c..5317bf19 100644 --- a/src/DB_Command.php +++ b/src/DB_Command.php @@ -1601,6 +1601,9 @@ public function prefix() { * - count * --- * + * [--exclude_revisions] + * : Exclude revisions from the search. + * * The percent color codes available are: * * | Code | Color @@ -1739,6 +1742,7 @@ public function search( $args, $assoc_args ) { $stats = Utils\get_flag_value( $assoc_args, 'stats', false ); $fields = Utils\get_flag_value( $assoc_args, 'fields' ); $format = Utils\get_flag_value( $assoc_args, 'format' ); + $exclude_revisions = Utils\get_flag_value( $assoc_args, 'exclude_revisions', false ); $column_count = 0; $row_count = 0; @@ -1803,14 +1807,22 @@ public function search( $args, $assoc_args ) { $primary_key_sql = self::esc_sql_ident( $primary_key ) . ', '; } + // Skip revisions in any posts table (including other sites' tables in a multisite network). + $revisions_sql = ''; + if ( $exclude_revisions && preg_match( '/posts$/', $table ) && in_array( 'post_type', $all_columns, true ) ) { + $revisions_sql = ' ' . self::esc_sql_ident( 'post_type' ) . " <> 'revision'"; + } + foreach ( $text_columns as $column ) { $column_sql = self::esc_sql_ident( $column ); if ( $regex ) { + $where_sql = $revisions_sql ? " WHERE{$revisions_sql}" : ''; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like. - $results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}" ); + $results = $wpdb->get_results( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql}{$where_sql}" ); } else { + $where_sql = $revisions_sql ? " AND{$revisions_sql}" : ''; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Escaped through esc_sql_ident/esc_like. - $results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s;", $esc_like_search ) ); + $results = $wpdb->get_results( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s{$where_sql};", $esc_like_search ) ); } if ( $results ) { $row_count += count( $results );