Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement exclude revision flag to remove revision results from the search #255

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions features/db-search.feature
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,29 @@ Feature: Search through the database
"""
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"`
# Create a revision
And I run `wp post update 1 --post_content="This is the updated post content."`

When I run `wp db search "updated post content"`
Then STDOUT should contain:
"""
wp_posts:post_content
1:This is the updated post content.
wp_posts:post_content
5:This is the updated post content.
"""

When I run `wp db search "updated post content" --exclude_revisions`
Then STDOUT should contain:
"""
wp_posts:post_content
1:This is the updated post content.
"""
And STDERR should be empty

Scenario: Search with custom colors
Given a WP install

Expand Down
17 changes: 15 additions & 2 deletions src/DB_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,9 @@ public function prefix() {
* [--format=<format>]
* : Render output in a particular format.
*
* [--exclude_revisions]
* : Exclude revisions from the search.
*
* The percent color codes available are:
*
* | Code | Color
Expand Down Expand Up @@ -1388,6 +1391,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;
Expand Down Expand Up @@ -1452,10 +1456,19 @@ public function search( $args, $assoc_args ) {
}

foreach ( $text_columns as $column ) {
$column_sql = self::esc_sql_ident( $column );
$column_sql = self::esc_sql_ident( $column );
$post_type_sql = self::esc_sql_ident( 'post_type' );
if ( $regex ) {
if ( $exclude_revisions && 'wp_posts' === $table ) {
// 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} WHERE {$post_type_sql} NOT IN ( 'revision' )" );
} else {
// 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}" );
}
} elseif ( $exclude_revisions && 'wp_posts' === $table ) {
// 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( $wpdb->prepare( "SELECT {$primary_key_sql}{$column_sql} FROM {$table_sql} WHERE {$column_sql} LIKE %s AND {$post_type_sql} NOT IN ( 'revision' )", $esc_like_search ) );
} else {
// 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 ) );
Expand Down
Loading