Skip to content

Commit c52fd0f

Browse files
committed
some fix
1 parent 1751673 commit c52fd0f

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

features/search-replace.feature

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,17 +1062,39 @@ Feature: Do global search/replace
10621062

10631063
When I run `wp search-replace --regex "ap{2}le" "orange" --regex-limit=1 --log`
10641064
Then STDOUT should contain:
1065-
"""
1066-
I have a pen, I have an orange. Pen, pine-apple, apple-pen.
1067-
"""
1068-
1065+
"""
1066+
I have a pen, I have an orange. Pen, pine-apple, apple-pen.
1067+
"""
10691068

10701069
Scenario: Regex search/replace with `--regex-limit=2` option
10711070
Given a WP install
10721071
And I run `wp post create --post_content="I have a pen, I have an apple. Pen, pine-apple, apple-pen."`
10731072

10741073
When I run `wp search-replace --regex "ap{2}le" "orange" --regex-limit=2 --log`
10751074
Then STDOUT should contain:
1076-
"""
1077-
I have a pen, I have an orange. Pen, pine-orange, apple-pen.
1078-
"""
1075+
"""
1076+
I have a pen, I have an orange. Pen, pine-orange, apple-pen.
1077+
"""
1078+
1079+
Scenario: Regex search/replace with incorrect or default `--regex-limit`
1080+
Given a WP install
1081+
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=asdf`
1082+
Then STDERR should be:
1083+
"""
1084+
Error: `--regex-limit` expects a non-zero positive integer or -1.
1085+
"""
1086+
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=0`
1087+
Then STDERR should be:
1088+
"""
1089+
Error: `--regex-limit` expects a non-zero positive integer or -1.
1090+
"""
1091+
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=-2`
1092+
Then STDERR should be:
1093+
"""
1094+
Error: `--regex-limit` expects a non-zero positive integer or -1.
1095+
"""
1096+
When I run `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=-1`
1097+
Then STDOUT should contain:
1098+
"""
1099+
Success:
1100+
"""

src/Search_Replace_Command.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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 the regex in each unserialized data bit per row. Defaults to `-1` (no limit).
112+
* : The maximum possible replacements for the regex per row (or per unserialized data bit per row). Defaults to -1 (no limit).
113113
*
114114
* [--format=<format>]
115115
* : Render output in a particular format.
@@ -183,6 +183,13 @@ public function __invoke( $args, $assoc_args ) {
183183
}
184184
}
185185

186+
if ( null !== ( $regex_limit = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-limit' ) ) ) {
187+
if ( ! preg_match( '/^(?:[0-9]+|-1)$/', $regex_limit ) || 0 === (int) $regex_limit ) {
188+
WP_CLI::error( '`--regex-limit` expects a non-zero positive integer or -1.' );
189+
}
190+
$this->regex_limit = (int) $regex_limit;
191+
}
192+
186193
if ( ! empty( $this->regex ) ) {
187194
if ( '' === $this->regex_delimiter ) {
188195
$this->regex_delimiter = chr( 1 );
@@ -200,12 +207,6 @@ public function __invoke( $args, $assoc_args ) {
200207
}
201208
WP_CLI::error( $msg );
202209
}
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-
}
209210
}
210211

211212
$this->skip_columns = explode( ',', \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-columns' ) );

src/WP_CLI/SearchReplacer.php

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

0 commit comments

Comments
 (0)