Skip to content

Commit 6d4b69d

Browse files
committed
Enable formatter for search command
1 parent bda6405 commit 6d4b69d

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

features/db-search.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,3 +1066,13 @@ Feature: Search through the database
10661066
:aöXYXYX
10671067
"""
10681068
And STDERR should be empty
1069+
1070+
Scenario: Search for a string and output the format as a table
1071+
Given a WP install
1072+
1073+
When I run `wp db search example.com --format=csv`
1074+
Then STDOUT should contain:
1075+
"""
1076+
wp_options,option_value,option_id,14,mail.example.com
1077+
wp_options,option_value,option_id,15,login@example.com
1078+
"""

src/DB_Command.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,10 +1208,10 @@ public function prefix() {
12081208
* : Output the 'table:column' line once before all matching row lines in the table column rather than before each matching row.
12091209
*
12101210
* [--one_line]
1211-
* : Place the 'table:column' output on the same line as the row id and match ('table:column:id:match'). Overrides --table_column_once.
1211+
* : Deprecated: use `--format` insted. Place the 'table:column' output on the same line as the row id and match ('table:column:id:match'). Overrides --table_column_once.
12121212
*
12131213
* [--matches_only]
1214-
* : Only output the string matches (including context). No 'table:column's or row ids are outputted.
1214+
* : Deprecated: use `--format` insted. Only output the string matches (including context). No 'table:column's or row ids are outputted.
12151215
*
12161216
* [--stats]
12171217
* : Output stats on the number of matches found, time taken, tables/columns/rows searched, tables skipped.
@@ -1225,6 +1225,12 @@ public function prefix() {
12251225
* [--match_color=<color_code>]
12261226
* : 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).
12271227
*
1228+
* [--fields=<fields>]
1229+
* : Get a specific subset of the fields.
1230+
*
1231+
* [--format=<format>]
1232+
* : Render output in a particular format.
1233+
*
12281234
* The percent color codes available are:
12291235
*
12301236
* | Code | Color
@@ -1293,6 +1299,21 @@ public function prefix() {
12931299
* # SQL search and delete records from database table 'wp_options' where 'option_name' match 'foo'
12941300
* 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))"
12951301
*
1302+
* # Search for a strings and print the result as a table
1303+
* $ wp db search https://localhost:8889 --format=table
1304+
* +------------+--------------+-----------+----+-----------------------------+
1305+
* | table | column | key | ID | match |
1306+
* +------------+--------------+-----------+----+-----------------------------+
1307+
* | wp_options | option_value | option_id | 1 | https://localhost:8889 |
1308+
* | wp_options | option_value | option_id | 2 | https://localhost:8889 |
1309+
* | wp_posts | guid | ID | 1 | https://localhost:8889/?p=1 |
1310+
* | wp_users | user_url | ID | 1 | https://localhost:8889 |
1311+
* +------------+--------------+-----------+----+-----------------------------+
1312+
*
1313+
* # Search for a strings and get only the IDs (only fors for a single table)
1314+
* $ wp db search https://localhost:8889 wp_options --format=ids
1315+
* 1 2
1316+
*
12961317
* @when after_wp_load
12971318
*/
12981319
public function search( $args, $assoc_args ) {
@@ -1332,6 +1353,8 @@ public function search( $args, $assoc_args ) {
13321353
$one_line = Utils\get_flag_value( $assoc_args, 'one_line', false );
13331354
$matches_only = Utils\get_flag_value( $assoc_args, 'matches_only', false );
13341355
$stats = Utils\get_flag_value( $assoc_args, 'stats', false );
1356+
$fields = Utils\get_flag_value( $assoc_args, 'fields' );
1357+
$format = Utils\get_flag_value( $assoc_args, 'format' );
13351358

13361359
$column_count = 0;
13371360
$row_count = 0;
@@ -1366,6 +1389,8 @@ public function search( $args, $assoc_args ) {
13661389

13671390
$tables = Utils\wp_get_table_names( $args, $assoc_args );
13681391

1392+
$search_results = [];
1393+
13691394
$start_search_time = microtime( true );
13701395

13711396
foreach ( $tables as $table ) {
@@ -1409,7 +1434,7 @@ public function search( $args, $assoc_args ) {
14091434
foreach ( $results as $result ) {
14101435
$col_val = $result->$column;
14111436
if ( preg_match_all( $search_regex, $col_val, $matches, PREG_OFFSET_CAPTURE ) ) {
1412-
if ( ! $matches_only && ( ! $table_column_once || ! $outputted_table_column_once ) && ! $one_line ) {
1437+
if ( ! $format && ! $matches_only && ( ! $table_column_once || ! $outputted_table_column_once ) && ! $one_line ) {
14131438
WP_CLI::log( $table_column_val );
14141439
$outputted_table_column_once = true;
14151440
}
@@ -1457,13 +1482,46 @@ public function search( $args, $assoc_args ) {
14571482
$match_count += $match_cnt;
14581483
$col_val = implode( ' [...] ', $bits );
14591484

1460-
WP_CLI::log( $matches_only ? $col_val : ( $one_line ? "{$table_column_val}:{$pk_val}{$col_val}" : "{$pk_val}{$col_val}" ) );
1485+
if ( $format ) {
1486+
$search_results[] = [
1487+
'table' => $table,
1488+
'column' => $column,
1489+
'key' => $primary_key,
1490+
'ID' => $result->$primary_key,
1491+
// Remove the colors for the format output.
1492+
'match' => str_replace( [ $colors['match'][0], $colors['match'][1] ], [ '', '' ], $col_val ),
1493+
];
1494+
} else {
1495+
WP_CLI::log( $matches_only ? $col_val : ( $one_line ? "{$table_column_val}:{$pk_val}{$col_val}" : "{$pk_val}{$col_val}" ) );
1496+
}
14611497
}
14621498
}
14631499
}
14641500
}
14651501
}
14661502

1503+
if ( $format ) {
1504+
$formatter_args = [
1505+
'format' => $format,
1506+
];
1507+
$formatter_fields = [ 'table', 'column', 'key', 'ID', 'match' ];
1508+
1509+
if ( $fields ) {
1510+
$fields = explode( ',', $assoc_args['fields'] );
1511+
$formatter_fields = array_values( array_intersect( $formatter_fields, $fields ) );
1512+
}
1513+
1514+
if ( in_array( $format, [ 'ids', 'count' ], true ) ) {
1515+
if ( count( $tables ) > 1 ) {
1516+
WP_CLI::error( 'The "ids" format can only be used for a singe table.' );
1517+
}
1518+
$search_results = array_column( $search_results, 'ID' );
1519+
}
1520+
1521+
$formatter = new Formatter( $formatter_args, $formatter_fields );
1522+
$formatter->display_items( $search_results );
1523+
}
1524+
14671525
if ( $stats ) {
14681526
$table_count = count( $tables );
14691527
$skipped_count = count( $skipped );

0 commit comments

Comments
 (0)