1
1
<?php
2
2
3
+ use cli \Colors ;
4
+ use cli \Table ;
5
+ use WP_CLI \Iterators ;
6
+ use WP_CLI \SearchReplacer ;
7
+ use WP_CLI \Utils ;
8
+ use function cli \safe_substr ;
9
+
3
10
class Search_Replace_Command extends WP_CLI_Command {
4
11
5
12
private $ dry_run ;
@@ -15,8 +22,9 @@ class Search_Replace_Command extends WP_CLI_Command {
15
22
private $ include_columns ;
16
23
private $ format ;
17
24
private $ report ;
18
- private $ report_changed_only ;
25
+ private $ verbose ;
19
26
27
+ private $ report_changed_only ;
20
28
private $ log_handle = null ;
21
29
private $ log_before_context = 40 ;
22
30
private $ log_after_context = 40 ;
@@ -167,24 +175,24 @@ public function __invoke( $args, $assoc_args ) {
167
175
$ new = array_shift ( $ args );
168
176
$ total = 0 ;
169
177
$ report = array ();
170
- $ this ->dry_run = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'dry-run ' );
171
- $ php_only = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'precise ' );
172
- $ this ->recurse_objects = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'recurse-objects ' , true );
173
- $ this ->verbose = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'verbose ' );
174
- $ this ->format = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'format ' );
175
- $ this ->regex = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'regex ' , false );
178
+ $ this ->dry_run = Utils \get_flag_value ( $ assoc_args , 'dry-run ' );
179
+ $ php_only = Utils \get_flag_value ( $ assoc_args , 'precise ' );
180
+ $ this ->recurse_objects = Utils \get_flag_value ( $ assoc_args , 'recurse-objects ' , true );
181
+ $ this ->verbose = Utils \get_flag_value ( $ assoc_args , 'verbose ' );
182
+ $ this ->format = Utils \get_flag_value ( $ assoc_args , 'format ' );
183
+ $ this ->regex = Utils \get_flag_value ( $ assoc_args , 'regex ' , false );
176
184
177
185
if ( null !== $ this ->regex ) {
178
186
$ default_regex_delimiter = false ;
179
- $ this ->regex_flags = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'regex-flags ' , false );
180
- $ this ->regex_delimiter = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'regex-delimiter ' , '' );
187
+ $ this ->regex_flags = Utils \get_flag_value ( $ assoc_args , 'regex-flags ' , false );
188
+ $ this ->regex_delimiter = Utils \get_flag_value ( $ assoc_args , 'regex-delimiter ' , '' );
181
189
if ( '' === $ this ->regex_delimiter ) {
182
190
$ this ->regex_delimiter = chr ( 1 );
183
191
$ default_regex_delimiter = true ;
184
192
}
185
193
}
186
194
187
- $ regex_limit = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'regex-limit ' );
195
+ $ regex_limit = Utils \get_flag_value ( $ assoc_args , 'regex-limit ' );
188
196
if ( null !== $ regex_limit ) {
189
197
if ( ! preg_match ( '/^(?:[0-9]+|-1)$/ ' , $ regex_limit ) || 0 === (int ) $ regex_limit ) {
190
198
WP_CLI ::error ( '`--regex-limit` expects a non-zero positive integer or -1. ' );
@@ -215,16 +223,16 @@ public function __invoke( $args, $assoc_args ) {
215
223
}
216
224
}
217
225
218
- $ this ->skip_columns = explode ( ', ' , \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'skip-columns ' ) );
219
- $ this ->skip_tables = explode ( ', ' , \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'skip-tables ' ) );
220
- $ this ->include_columns = array_filter ( explode ( ', ' , \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'include-columns ' ) ) );
226
+ $ this ->skip_columns = explode ( ', ' , Utils \get_flag_value ( $ assoc_args , 'skip-columns ' ) );
227
+ $ this ->skip_tables = explode ( ', ' , Utils \get_flag_value ( $ assoc_args , 'skip-tables ' ) );
228
+ $ this ->include_columns = array_filter ( explode ( ', ' , Utils \get_flag_value ( $ assoc_args , 'include-columns ' ) ) );
221
229
222
230
if ( $ old === $ new && ! $ this ->regex ) {
223
231
WP_CLI ::warning ( "Replacement value ' {$ old }' is identical to search value ' {$ new }'. Skipping operation. " );
224
232
exit ;
225
233
}
226
234
227
- $ export = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'export ' );
235
+ $ export = Utils \get_flag_value ( $ assoc_args , 'export ' );
228
236
if ( null !== $ export ) {
229
237
if ( $ this ->dry_run ) {
230
238
WP_CLI ::error ( 'You cannot supply --dry-run and --export at the same time. ' );
@@ -239,15 +247,15 @@ public function __invoke( $args, $assoc_args ) {
239
247
WP_CLI ::error ( sprintf ( 'Unable to open export file "%s" for writing: %s. ' , $ assoc_args ['export ' ], $ error ['message ' ] ) );
240
248
}
241
249
}
242
- $ export_insert_size = WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'export_insert_size ' , 50 );
250
+ $ export_insert_size = Utils \get_flag_value ( $ assoc_args , 'export_insert_size ' , 50 );
243
251
// phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison -- See the code, this is deliberate.
244
252
if ( (int ) $ export_insert_size == $ export_insert_size && $ export_insert_size > 0 ) {
245
253
$ this ->export_insert_size = $ export_insert_size ;
246
254
}
247
255
$ php_only = true ;
248
256
}
249
257
250
- $ log = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'log ' );
258
+ $ log = Utils \get_flag_value ( $ assoc_args , 'log ' );
251
259
if ( null !== $ log ) {
252
260
if ( true === $ log || '- ' === $ log ) {
253
261
$ this ->log_handle = STDOUT ;
@@ -259,12 +267,12 @@ public function __invoke( $args, $assoc_args ) {
259
267
}
260
268
}
261
269
if ( $ this ->log_handle ) {
262
- $ before_context = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'before_context ' );
270
+ $ before_context = Utils \get_flag_value ( $ assoc_args , 'before_context ' );
263
271
if ( null !== $ before_context && preg_match ( '/^[0-9]+$/ ' , $ before_context ) ) {
264
272
$ this ->log_before_context = (int ) $ before_context ;
265
273
}
266
274
267
- $ after_context = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'after_context ' );
275
+ $ after_context = Utils \get_flag_value ( $ assoc_args , 'after_context ' );
268
276
if ( null !== $ after_context && preg_match ( '/^[0-9]+$/ ' , $ after_context ) ) {
269
277
$ this ->log_after_context = (int ) $ after_context ;
270
278
}
@@ -297,14 +305,14 @@ public function __invoke( $args, $assoc_args ) {
297
305
);
298
306
}
299
307
300
- $ this ->log_colors = self :: get_colors ( $ assoc_args , $ default_log_colors );
308
+ $ this ->log_colors = $ this -> get_colors ( $ assoc_args , $ default_log_colors );
301
309
$ this ->log_encoding = 0 === strpos ( $ wpdb ->charset , 'utf8 ' ) ? 'UTF-8 ' : false ;
302
310
}
303
311
}
304
312
305
- $ this ->report = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'report ' , true );
313
+ $ this ->report = Utils \get_flag_value ( $ assoc_args , 'report ' , true );
306
314
// Defaults to true if logging, else defaults to false.
307
- $ this ->report_changed_only = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , 'report-changed-only ' , null !== $ this ->log_handle );
315
+ $ this ->report_changed_only = Utils \get_flag_value ( $ assoc_args , 'report-changed-only ' , null !== $ this ->log_handle );
308
316
309
317
if ( $ this ->regex_flags ) {
310
318
$ php_only = true ;
@@ -314,7 +322,7 @@ public function __invoke( $args, $assoc_args ) {
314
322
$ this ->skip_columns [] = 'user_pass ' ;
315
323
316
324
// Get table names based on leftover $args or supplied $assoc_args
317
- $ tables = \ WP_CLI \ Utils \wp_get_table_names ( $ args , $ assoc_args );
325
+ $ tables = Utils \wp_get_table_names ( $ args , $ assoc_args );
318
326
319
327
foreach ( $ tables as $ table ) {
320
328
@@ -418,7 +426,7 @@ public function __invoke( $args, $assoc_args ) {
418
426
}
419
427
420
428
if ( $ this ->report && ! empty ( $ report ) ) {
421
- $ table = new \ cli \ Table ();
429
+ $ table = new Table ();
422
430
$ table ->setHeaders ( array ( 'Table ' , 'Column ' , 'Replacements ' , 'Type ' ) );
423
431
$ table ->setRows ( $ report );
424
432
$ table ->display ();
@@ -429,7 +437,7 @@ public function __invoke( $args, $assoc_args ) {
429
437
$ success_message = 1 === $ total ? "Made 1 replacement and exported to {$ assoc_args ['export ' ]}. " : "Made {$ total } replacements and exported to {$ assoc_args ['export ' ]}. " ;
430
438
} else {
431
439
$ success_message = 1 === $ total ? 'Made 1 replacement. ' : "Made $ total replacements. " ;
432
- if ( $ total && 'Default ' !== WP_CLI \ Utils \wp_get_cache_type () ) {
440
+ if ( $ total && 'Default ' !== Utils \wp_get_cache_type () ) {
433
441
$ success_message .= ' Please remember to flush your persistent object cache with `wp cache flush`. ' ;
434
442
}
435
443
}
@@ -450,15 +458,15 @@ private function php_export_table( $table, $old, $new ) {
450
458
'chunk_size ' => $ chunk_size ,
451
459
);
452
460
453
- $ replacer = new \ WP_CLI \ SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , false , $ this ->regex_limit );
461
+ $ replacer = new SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , false , $ this ->regex_limit );
454
462
$ col_counts = array_fill_keys ( $ all_columns , 0 );
455
463
if ( $ this ->verbose && 'table ' === $ this ->format ) {
456
464
$ this ->start_time = microtime ( true );
457
465
WP_CLI ::log ( sprintf ( 'Checking: %s ' , $ table ) );
458
466
}
459
467
460
468
$ rows = array ();
461
- foreach ( new \ WP_CLI \ Iterators \Table ( $ args ) as $ i => $ row ) {
469
+ foreach ( new Iterators \Table ( $ args ) as $ i => $ row ) {
462
470
$ row_fields = array ();
463
471
foreach ( $ all_columns as $ col ) {
464
472
$ value = $ row ->$ col ;
@@ -527,15 +535,15 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
527
535
global $ wpdb ;
528
536
529
537
$ count = 0 ;
530
- $ replacer = new \ WP_CLI \ SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , null !== $ this ->log_handle , $ this ->regex_limit );
538
+ $ replacer = new SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , null !== $ this ->log_handle , $ this ->regex_limit );
531
539
532
540
$ table_sql = self ::esc_sql_ident ( $ table );
533
541
$ col_sql = self ::esc_sql_ident ( $ col );
534
542
$ where = $ this ->regex ? '' : " WHERE $ col_sql " . $ wpdb ->prepare ( ' LIKE BINARY %s ' , '% ' . self ::esc_like ( $ old ) . '% ' );
535
543
$ escaped_primary_keys = self ::esc_sql_ident ( $ primary_keys );
536
544
$ primary_keys_sql = implode ( ', ' , $ escaped_primary_keys );
537
545
$ order_by_keys = array_map (
538
- function ( $ key ) {
546
+ static function ( $ key ) {
539
547
return "{$ key } ASC " ;
540
548
},
541
549
$ escaped_primary_keys
@@ -552,7 +560,7 @@ function( $key ) {
552
560
foreach ( $ rows as $ keys ) {
553
561
$ where_sql = '' ;
554
562
foreach ( (array ) $ keys as $ k => $ v ) {
555
- if ( strlen ( $ where_sql ) ) {
563
+ if ( $ where_sql !== '' ) {
556
564
$ where_sql .= ' AND ' ;
557
565
}
558
566
$ where_sql .= self ::esc_sql_ident ( $ k ) . ' = ' . self ::esc_sql_value ( $ v );
@@ -776,14 +784,14 @@ private function get_colors( $assoc_args, $colors ) {
776
784
return substr ( $ v , 1 );
777
785
};
778
786
779
- $ color_codes = array_keys ( \ cli \ Colors::getColors () );
787
+ $ color_codes = array_keys ( Colors::getColors () );
780
788
$ color_codes = array_map ( $ color_code_callback , $ color_codes );
781
789
$ color_codes = implode ( '' , $ color_codes );
782
790
783
791
$ color_codes_regex = '/^(?:%[ ' . $ color_codes . '])*$/ ' ;
784
792
785
793
foreach ( array_keys ( $ colors ) as $ color_col ) {
786
- $ col_color_flag = \ WP_CLI \ Utils \get_flag_value ( $ assoc_args , $ color_col . '_color ' );
794
+ $ col_color_flag = Utils \get_flag_value ( $ assoc_args , $ color_col . '_color ' );
787
795
if ( null !== $ col_color_flag ) {
788
796
if ( ! preg_match ( $ color_codes_regex , $ col_color_flag , $ matches ) ) {
789
797
WP_CLI ::warning ( "Unrecognized percent color code ' $ col_color_flag' for ' {$ color_col }_color'. " );
@@ -939,14 +947,14 @@ static function ( $m ) use ( $matches ) {
939
947
940
948
// Offsets are in bytes, so need to use `strlen()` and `substr()` before using `safe_substr()`.
941
949
if ( $ this ->log_before_context && $ old_offset && ! $ append_next ) {
942
- $ old_before = \ cli \ safe_substr ( substr ( $ old_data , $ last_old_offset , $ old_offset - $ last_old_offset ), -$ this ->log_before_context , null /*length*/ , false /*is_width*/ , $ encoding );
943
- $ new_before = \ cli \ safe_substr ( substr ( $ new_data , $ last_new_offset , $ new_offset - $ last_new_offset ), -$ this ->log_before_context , null /*length*/ , false /*is_width*/ , $ encoding );
950
+ $ old_before = safe_substr ( substr ( $ old_data , $ last_old_offset , $ old_offset - $ last_old_offset ), -$ this ->log_before_context , null /*length*/ , false /*is_width*/ , $ encoding );
951
+ $ new_before = safe_substr ( substr ( $ new_data , $ last_new_offset , $ new_offset - $ last_new_offset ), -$ this ->log_before_context , null /*length*/ , false /*is_width*/ , $ encoding );
944
952
}
945
953
if ( $ this ->log_after_context ) {
946
954
$ old_end_offset = $ old_offset + strlen ( $ old_match );
947
955
$ new_end_offset = $ new_offset + strlen ( $ new_match );
948
- $ old_after = \ cli \ safe_substr ( substr ( $ old_data , $ old_end_offset ), 0 , $ this ->log_after_context , false /*is_width*/ , $ encoding );
949
- $ new_after = \ cli \ safe_substr ( substr ( $ new_data , $ new_end_offset ), 0 , $ this ->log_after_context , false /*is_width*/ , $ encoding );
956
+ $ old_after = safe_substr ( substr ( $ old_data , $ old_end_offset ), 0 , $ this ->log_after_context , false /*is_width*/ , $ encoding );
957
+ $ new_after = safe_substr ( substr ( $ new_data , $ new_end_offset ), 0 , $ this ->log_after_context , false /*is_width*/ , $ encoding );
950
958
// To lessen context duplication in output, shorten the after context if it overlaps with the next match.
951
959
if ( $ i + 1 < $ match_cnt && $ old_end_offset + strlen ( $ old_after ) > $ old_matches [0 ][ $ i + 1 ][1 ] ) {
952
960
$ old_after = substr ( $ old_after , 0 , $ old_matches [0 ][ $ i + 1 ][1 ] - $ old_end_offset );
0 commit comments