@@ -5,11 +5,11 @@ class Search_Replace_Command extends WP_CLI_Command {
5
5
private $ dry_run ;
6
6
private $ export_handle = false ;
7
7
private $ export_insert_size ;
8
- private $ regex_limit ;
9
8
private $ recurse_objects ;
10
9
private $ regex ;
11
10
private $ regex_flags ;
12
11
private $ regex_delimiter ;
12
+ private $ regex_limit = 1 ;
13
13
private $ skip_tables ;
14
14
private $ skip_columns ;
15
15
private $ include_columns ;
@@ -109,7 +109,7 @@ class Search_Replace_Command extends WP_CLI_Command {
109
109
* : The delimiter to use for the regex. It must be escaped if it appears in the search string. The default value is the result of `chr(1)`.
110
110
*
111
111
* [--regex-limit=<regex-limit>]
112
- * : The maximum possible replacements for each pattern in each subject string . Defaults to `-1` (no limit).
112
+ * : The maximum possible replacements for the regex in each unserialized data bit per row . Defaults to `-1` (no limit).
113
113
*
114
114
* [--format=<format>]
115
115
* : Render output in a particular format.
@@ -172,10 +172,6 @@ public function __invoke( $args, $assoc_args ) {
172
172
$ this ->recurse_objects = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'recurse-objects ' , true );
173
173
$ this ->verbose = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'verbose ' );
174
174
$ this ->format = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'format ' );
175
- $ this ->regex_limit = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'regex-limit ' , -1 );
176
- if ( 0 === intval ( $ this ->regex_limit ) ) {
177
- WP_CLI ::error ( '`--regex-limit` expects integer. ' );
178
- }
179
175
180
176
if ( ( $ this ->regex = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'regex ' , false ) ) ) {
181
177
$ this ->regex_flags = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'regex-flags ' , false );
@@ -204,6 +200,12 @@ public function __invoke( $args, $assoc_args ) {
204
200
}
205
201
WP_CLI ::error ( $ msg );
206
202
}
203
+ if ( ( $ regex_limit = (int ) \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'regex-limit ' , - 1 ) ) > - 1 ) {
204
+ if ( ! preg_match ( '/^[0-9]+$/ ' , $ regex_limit ) ) {
205
+ WP_CLI ::error ( '`--regex-limit` expects a positive integer. ' );
206
+ }
207
+ $ this ->regex_limit = $ regex_limit ;
208
+ }
207
209
}
208
210
209
211
$ this ->skip_columns = explode ( ', ' , \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'skip-columns ' ) );
@@ -412,7 +414,7 @@ private function php_export_table( $table, $old, $new ) {
412
414
'chunk_size ' => $ chunk_size
413
415
);
414
416
415
- $ replacer = new \WP_CLI \SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , $ this ->regex_limit );
417
+ $ replacer = new \WP_CLI \SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , false , $ this ->regex_limit );
416
418
$ col_counts = array_fill_keys ( $ all_columns , 0 );
417
419
if ( $ this ->verbose && 'table ' === $ this ->format ) {
418
420
$ this ->start_time = microtime ( true );
@@ -486,7 +488,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
486
488
global $ wpdb ;
487
489
488
490
$ count = 0 ;
489
- $ replacer = new \WP_CLI \SearchReplacer ( $ old , $ new , $ this ->recurse_objects , $ this ->regex , $ this ->regex_flags , $ this ->regex_delimiter , $ this -> regex_limit , null !== $ this ->log_handle );
491
+ $ 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 );
490
492
491
493
$ table_sql = self ::esc_sql_ident ( $ table );
492
494
$ col_sql = self ::esc_sql_ident ( $ col );
@@ -808,16 +810,12 @@ private function log_bits( $search_regex, $old_data, $old_matches, $new ) {
808
810
$ diff += strlen ( $ new ) - strlen ( $ old_matches [0 ][ $ i ][0 ] );
809
811
$ i ++;
810
812
return $ new ;
811
- }, $ old_data , $ this ->regex_limit );
813
+ }, $ old_data , $ this ->regex_limit , $ match_cnt );
812
814
813
815
$ old_bits = $ new_bits = array ();
814
816
$ append_next = false ;
815
817
$ last_old_offset = $ last_new_offset = 0 ;
816
- $ match_cnt = count ( $ old_matches [0 ] );
817
818
for ( $ i = 0 ; $ i < $ match_cnt ; $ i ++ ) {
818
- if ( empty ( $ old_matches [0 ][ $ i ] ) || empty ( $ new_matches [0 ][ $ i ] ) ) {
819
- continue ;
820
- }
821
819
$ old_match = $ old_matches [0 ][ $ i ][0 ];
822
820
$ old_offset = $ old_matches [0 ][ $ i ][1 ];
823
821
$ new_match = $ new_matches [0 ][ $ i ][0 ];
@@ -841,12 +839,10 @@ private function log_bits( $search_regex, $old_data, $old_matches, $new ) {
841
839
$ new_after = \cli \safe_substr ( substr ( $ new_data , $ new_end_offset ), 0 , $ this ->log_after_context , false /*is_width*/ , $ encoding );
842
840
// To lessen context duplication in output, shorten the after context if it overlaps with the next match.
843
841
if ( $ i + 1 < $ match_cnt && $ old_end_offset + strlen ( $ old_after ) > $ old_matches [0 ][ $ i + 1 ][1 ] ) {
844
- if ( ! empty ( $ old_matches [0 ][ $ i + 1 ] ) && ! empty ( $ new_matches [0 ][ $ i + 1 ] ) ) {
845
- $ old_after = substr ( $ old_after , 0 , $ old_matches [0 ][ $ i + 1 ][1 ] - $ old_end_offset );
846
- $ new_after = substr ( $ new_after , 0 , $ new_matches [0 ][ $ i + 1 ][1 ] - $ new_end_offset );
847
- $ after_shortened = true ;
848
- // On the next iteration, will append with no before context.
849
- }
842
+ $ old_after = substr ( $ old_after , 0 , $ old_matches [0 ][ $ i + 1 ][1 ] - $ old_end_offset );
843
+ $ new_after = substr ( $ new_after , 0 , $ new_matches [0 ][ $ i + 1 ][1 ] - $ new_end_offset );
844
+ $ after_shortened = true ;
845
+ // On the next iteration, will append with no before context.
850
846
}
851
847
}
852
848
0 commit comments