-
Notifications
You must be signed in to change notification settings - Fork 67
Enable --format=<format> for db search
#247
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
Merged
danielbachhuber
merged 5 commits into
wp-cli:main
from
2ndkauboy:fix/158-enable-formatter
Apr 27, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fd762a7
Enable formatter for search command
2ndkauboy 1c9b2e8
Use `value` instead of `ID` for the primary key column
2ndkauboy 531b897
Rename and reorder the columns
2ndkauboy 1e72bb1
Update docs
danielbachhuber 8211354
Less brittle test
danielbachhuber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1225,6 +1225,12 @@ public function prefix() { | |
| * [--match_color=<color_code>] | ||
| * : Percent color code to use for the match (unless both before and after context are 0, when no color code is used). For a list of available percent color codes, see below. Default '%3%k' (black on a mustard background). | ||
| * | ||
| * [--fields=<fields>] | ||
| * : Get a specific subset of the fields. | ||
| * | ||
| * [--format=<format>] | ||
| * : Render output in a particular format. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should document supported |
||
| * | ||
| * The percent color codes available are: | ||
| * | ||
| * | Code | Color | ||
|
|
@@ -1293,6 +1299,21 @@ public function prefix() { | |
| * # SQL search and delete records from database table 'wp_options' where 'option_name' match 'foo' | ||
| * wp db query "DELETE from wp_options where option_id in ($(wp db query "SELECT GROUP_CONCAT(option_id SEPARATOR ',') from wp_options where option_name like '%foo%';" --silent --skip-column-names))" | ||
| * | ||
| * # Search for a string and print the result as a table | ||
| * $ wp db search https://localhost:8889 --format=table --fields=table,column,match | ||
| * +------------+--------------+-----------------------------+ | ||
| * | table | column | match | | ||
| * +------------+--------------+-----------------------------+ | ||
| * | wp_options | option_value | https://localhost:8889 | | ||
| * | wp_options | option_value | https://localhost:8889 | | ||
| * | wp_posts | guid | https://localhost:8889/?p=1 | | ||
| * | wp_users | user_url | https://localhost:8889 | | ||
| * +------------+--------------+-----------------------------+ | ||
| * | ||
| * # Search for a string and get only the IDs (only works for a single table) | ||
| * $ wp db search https://localhost:8889 wp_options --format=ids | ||
| * 1 2 | ||
| * | ||
| * @when after_wp_load | ||
| */ | ||
| public function search( $args, $assoc_args ) { | ||
|
|
@@ -1332,6 +1353,8 @@ public function search( $args, $assoc_args ) { | |
| $one_line = Utils\get_flag_value( $assoc_args, 'one_line', false ); | ||
| $matches_only = Utils\get_flag_value( $assoc_args, 'matches_only', false ); | ||
| $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' ); | ||
|
|
||
| $column_count = 0; | ||
| $row_count = 0; | ||
|
|
@@ -1366,6 +1389,8 @@ public function search( $args, $assoc_args ) { | |
|
|
||
| $tables = Utils\wp_get_table_names( $args, $assoc_args ); | ||
|
|
||
| $search_results = []; | ||
|
|
||
| $start_search_time = microtime( true ); | ||
|
|
||
| foreach ( $tables as $table ) { | ||
|
|
@@ -1409,7 +1434,7 @@ public function search( $args, $assoc_args ) { | |
| foreach ( $results as $result ) { | ||
| $col_val = $result->$column; | ||
| if ( preg_match_all( $search_regex, $col_val, $matches, PREG_OFFSET_CAPTURE ) ) { | ||
| if ( ! $matches_only && ( ! $table_column_once || ! $outputted_table_column_once ) && ! $one_line ) { | ||
| if ( ! $format && ! $matches_only && ( ! $table_column_once || ! $outputted_table_column_once ) && ! $one_line ) { | ||
| WP_CLI::log( $table_column_val ); | ||
| $outputted_table_column_once = true; | ||
| } | ||
|
|
@@ -1457,13 +1482,50 @@ public function search( $args, $assoc_args ) { | |
| $match_count += $match_cnt; | ||
| $col_val = implode( ' [...] ', $bits ); | ||
|
|
||
| WP_CLI::log( $matches_only ? $col_val : ( $one_line ? "{$table_column_val}:{$pk_val}{$col_val}" : "{$pk_val}{$col_val}" ) ); | ||
| if ( $format ) { | ||
| $search_results[] = [ | ||
| 'table' => $table, | ||
| 'column' => $column, | ||
| // Remove the colors for the format output. | ||
| 'match' => str_replace( | ||
| [ $colors['match'][0], $colors['match'][1] ], | ||
| [ '','' ], | ||
| $col_val | ||
| ), | ||
| 'primary_key_name' => $primary_key, | ||
| 'primary_key_value' => $result->$primary_key, | ||
| ]; | ||
| } else { | ||
| WP_CLI::log( $matches_only ? $col_val : ( $one_line ? "{$table_column_val}:{$pk_val}{$col_val}" : "{$pk_val}{$col_val}" ) ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ( $format ) { | ||
| $formatter_args = [ | ||
| 'format' => $format, | ||
| ]; | ||
| $formatter_fields = [ 'table', 'column', 'match', 'primary_key_name', 'primary_key_value' ]; | ||
|
|
||
| if ( $fields ) { | ||
| $fields = explode( ',', $assoc_args['fields'] ); | ||
| $formatter_fields = array_values( array_intersect( $formatter_fields, $fields ) ); | ||
| } | ||
|
|
||
| if ( in_array( $format, [ 'ids', 'count' ], true ) ) { | ||
| if ( count( $tables ) > 1 ) { | ||
| WP_CLI::error( 'The "ids" format can only be used for a single table.' ); | ||
| } | ||
| $search_results = array_column( $search_results, 'primary_key_value' ); | ||
| } | ||
|
|
||
| $formatter = new Formatter( $formatter_args, $formatter_fields ); | ||
| $formatter->display_items( $search_results ); | ||
| } | ||
|
|
||
| if ( $stats ) { | ||
| $table_count = count( $tables ); | ||
| $skipped_count = count( $skipped ); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What fields are supported here?