Skip to content

Commit f542ed4

Browse files
authored
Merge pull request #70 from alpipego/patch-61
Patch 61
2 parents 4682304 + 8038f09 commit f542ed4

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

src/Search_Replace_Command.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ class Search_Replace_Command extends WP_CLI_Command {
55
private $dry_run;
66
private $export_handle = false;
77
private $export_insert_size;
8-
private $regex_limit;
98
private $recurse_objects;
109
private $regex;
1110
private $regex_flags;
1211
private $regex_delimiter;
12+
private $regex_limit = 1;
1313
private $skip_tables;
1414
private $skip_columns;
1515
private $include_columns;
@@ -109,7 +109,7 @@ class Search_Replace_Command extends WP_CLI_Command {
109109
* : 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)`.
110110
*
111111
* [--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).
113113
*
114114
* [--format=<format>]
115115
* : Render output in a particular format.
@@ -172,10 +172,6 @@ public function __invoke( $args, $assoc_args ) {
172172
$this->recurse_objects = \WP_CLI\Utils\get_flag_value( $assoc_args, 'recurse-objects', true );
173173
$this->verbose = \WP_CLI\Utils\get_flag_value( $assoc_args, 'verbose' );
174174
$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-
}
179175

180176
if ( ( $this->regex = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex', false ) ) ) {
181177
$this->regex_flags = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-flags', false );
@@ -204,6 +200,12 @@ public function __invoke( $args, $assoc_args ) {
204200
}
205201
WP_CLI::error( $msg );
206202
}
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+
}
207209
}
208210

209211
$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 ) {
412414
'chunk_size' => $chunk_size
413415
);
414416

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 );
416418
$col_counts = array_fill_keys( $all_columns, 0 );
417419
if ( $this->verbose && 'table' === $this->format ) {
418420
$this->start_time = microtime( true );
@@ -486,7 +488,7 @@ private function php_handle_col( $col, $primary_keys, $table, $old, $new ) {
486488
global $wpdb;
487489

488490
$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 );
490492

491493
$table_sql = self::esc_sql_ident( $table );
492494
$col_sql = self::esc_sql_ident( $col );
@@ -808,16 +810,12 @@ private function log_bits( $search_regex, $old_data, $old_matches, $new ) {
808810
$diff += strlen( $new ) - strlen( $old_matches[0][ $i ][0] );
809811
$i++;
810812
return $new;
811-
}, $old_data, $this->regex_limit );
813+
}, $old_data, $this->regex_limit, $match_cnt );
812814

813815
$old_bits = $new_bits = array();
814816
$append_next = false;
815817
$last_old_offset = $last_new_offset = 0;
816-
$match_cnt = count( $old_matches[0] );
817818
for ( $i = 0; $i < $match_cnt; $i++ ) {
818-
if ( empty( $old_matches[0][ $i ] ) || empty( $new_matches[0][ $i ] ) ) {
819-
continue;
820-
}
821819
$old_match = $old_matches[0][ $i ][0];
822820
$old_offset = $old_matches[0][ $i ][1];
823821
$new_match = $new_matches[0][ $i ][0];
@@ -841,12 +839,10 @@ private function log_bits( $search_regex, $old_data, $old_matches, $new ) {
841839
$new_after = \cli\safe_substr( substr( $new_data, $new_end_offset ), 0, $this->log_after_context, false /*is_width*/, $encoding );
842840
// To lessen context duplication in output, shorten the after context if it overlaps with the next match.
843841
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.
850846
}
851847
}
852848

src/WP_CLI/SearchReplacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SearchReplacer {
2424
* @param integer $regex_limit The maximum possible replacements for each pattern in each subject string.
2525
* @param bool $logging Whether logging.
2626
*/
27-
function __construct( $from, $to, $recurse_objects = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $regex_limit = -1, $logging = false ) {
27+
function __construct( $from, $to, $recurse_objects = false, $regex = false, $regex_flags = '', $regex_delimiter = '/', $logging = false, $regex_limit = -1 ) {
2828
$this->from = $from;
2929
$this->to = $to;
3030
$this->recurse_objects = $recurse_objects;

0 commit comments

Comments
 (0)