-
Notifications
You must be signed in to change notification settings - Fork 44
Add --regex-limit option. #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| private $log_prefixes = array( '< ', '> ' ); | ||
| private $log_colors; | ||
| private $log_encoding; | ||
| private $log_run_data = array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is an unused variable.
features/search-replace.feature
Outdated
| """ | ||
| world, Hello | ||
| """ | ||
| When I run `wp search-replace 'o' 'O' --regex --regex-limit=1` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think --regex-limit deserves its own scenario(s) with targetted data rather than piggy-backing on existing ones.
features/search-replace.feature
Outdated
| kppr | ||
| """ | ||
| And the return code should be 1 | ||
| When I run `wp search-replace 'o' 'O' --regex --regex-limit=1` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
features/search-replace.feature
Outdated
| Content_ab\1z__baz_1234567890_eb\1z__bez_1234567890_ib\1z__biz_1234567890_ob\1z__boz_1234567890_ub\1z__buz_ | ||
| """ | ||
|
|
||
| When I run `wp search-replace '_{2}' '-' wp_posts --regex --regex-limit=2 --log --before_context=11 --after_context=11` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above.
src/Search_Replace_Command.php
Outdated
| * : 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)`. | ||
| * | ||
| * [--regex-limit=<regex-limit>] | ||
| * : The maximum possible replacements for each pattern in each subject string. Defaults to `-1` (no limit). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you copied this from http://php.net/manual/en/function.preg-replace.php but it needs adapting here, eg there's only one subject string. Maybe The maximum possible replacements for the regex per row (or per unserialized data bit per row). Defaults... or something (need to mention the special serialized data case I think).
| $report = array(); | ||
| $this->dry_run = \WP_CLI\Utils\get_flag_value( $assoc_args, 'dry-run' ); | ||
| $php_only = \WP_CLI\Utils\get_flag_value( $assoc_args, 'precise' ); | ||
| $php_only = \WP_CLI\Utils\get_flag_value( $assoc_args, 'precise' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a fan of manually aligning assignments that happen to follow each other (it's so awkward to maintain and fundamentally random - when do you start/stop?!) so personally would just remove but ignore me.
src/Search_Replace_Command.php
Outdated
| $old_bits = $new_bits = array(); | ||
| $append_next = false; | ||
| $last_old_offset = $last_new_offset = 0; | ||
| $match_cnt = count( $old_matches[0] ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the easiest thing is just to do
$match_cnt = count( $new_matches[0] ); // `$new_matches[0]` may be limited by `regex_limit` so use instead of `$old_matches[0]`.
here which means the empty() checks following aren't needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gitlost why not use the $count preg_replace_callback provides?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Er, well one could, but it's not important and I don't think reviewing a review is going to work to be honest! If you'd like to contribute please create an idea or an issue or a PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't think reviewing a review is going to work
Of course you're right. The context is, that I am going through this with @miya0001 at my side at WordCamp Bangkok Contributer Day with the goal of fixing his PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see! Sorry, I thought the PR was being hi-jacked! Ignore my previous comments!
src/Search_Replace_Command.php
Outdated
| $last_old_offset = $last_new_offset = 0; | ||
| $match_cnt = count( $old_matches[0] ); | ||
| for ( $i = 0; $i < $match_cnt; $i++ ) { | ||
| if ( empty( $old_matches[0][ $i ] ) || empty( $new_matches[0][ $i ] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this change isn't needed.
src/Search_Replace_Command.php
Outdated
| $new_after = substr( $new_after, 0, $new_matches[0][ $i + 1 ][1] - $new_end_offset ); | ||
| $after_shortened = true; | ||
| // On the next iteration, will append with no before context. | ||
| if ( ! empty( $old_matches[0][ $i + 1 ] ) && ! empty( $new_matches[0][ $i + 1 ] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nor this change.
src/Search_Replace_Command.php
Outdated
| $this->format = \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ); | ||
| $this->regex_limit = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-limit', -1 ); | ||
| if ( 0 === intval( $this->regex_limit ) ) { | ||
| WP_CLI::error( '`--regex-limit` expects integer.' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a test for this anyway.
src/Search_Replace_Command.php
Outdated
| ); | ||
|
|
||
| $replacer = new \WP_CLI\SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter ); | ||
| $replacer = new \WP_CLI\SearchReplacer( $old, $new, $this->recurse_objects, $this->regex, $this->regex_flags, $this->regex_delimiter, $this->regex_limit ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See below.
|
Not sure why the review comments are listed in a weird order...?! |
|
Closing this in favor of #70 |
|
Re #70 (comment) okay I'll leave both open and step away for a while! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miya0001 the default should of course be -1 (and not 1).
src/Search_Replace_Command.php
Outdated
| private $regex; | ||
| private $regex_flags; | ||
| private $regex_delimiter; | ||
| private $regex_limit = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@miya0001 the default should of course be -1 (instead of 1).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks 👍
|
@gitlost |
gitlost
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @miya0001 , just some extra tests needed and some fiddling with the arg checking and a few doc and indentation nitpicks.
features/search-replace.feature
Outdated
|
|
||
| When I run `wp search-replace --regex "ap{2}le" "orange" --regex-limit=1 --log` | ||
| Then STDOUT should contain: | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you indent the PyString by 2 spaces here please (to match standard indentation).
| I have a pen, I have an orange. Pen, pine-apple, apple-pen. | ||
| """ | ||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And remove the extra newline here,
features/search-replace.feature
Outdated
|
|
||
| When I run `wp search-replace --regex "ap{2}le" "orange" --regex-limit=2 --log` | ||
| Then STDOUT should contain: | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again indent by standard 2 spaces please.
features/search-replace.feature
Outdated
| Then STDOUT should contain: | ||
| """ | ||
| I have a pen, I have an orange. Pen, pine-orange, apple-pen. | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a scenario to test for incorrect args here, eg
Scenario: Regex search/replace with incorrect or default `--regex-limit`
Given a WP install
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=asdf`
Then STDERR should be:
"""
Error: `--regex-limit` expects a non-zero positive integer or -1.
"""
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=0`
Then STDERR should be:
"""
Error: `--regex-limit` expects a non-zero positive integer or -1.
"""
When I try `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=-2`
Then STDERR should be:
"""
Error: `--regex-limit` expects a non-zero positive integer or -1.
"""
When I run `wp search-replace '(Hello)\s(world)' '$2, $1' --regex --regex-limit=-1`
Then STDOUT should contain:
"""
Success:
"""
(See below for disallowing zero.)
src/Search_Replace_Command.php
Outdated
| * : 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)`. | ||
| * | ||
| * [--regex-limit=<regex-limit>] | ||
| * : The maximum possible replacements for the regex in each unserialized data bit per row. Defaults to `-1` (no limit). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The serialized case is one case only and secondary, so I think The maximum possible replacements for the regex per row (or per unserialized data bit per row). Defaults to -1 (no limit). is better.
src/Search_Replace_Command.php
Outdated
| } | ||
| WP_CLI::error( $msg ); | ||
| } | ||
| if ( ( $regex_limit = (int) \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-limit', - 1 ) ) > - 1 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check should be moved to the previous block where the other options are checked (after line 183) and should not allow zero as it doesn't make sense to me. So I'd prefer:
if ( null !== ( $regex_limit = \WP_CLI\Utils\get_flag_value( $assoc_args, 'regex-limit' ) ) ) {
if ( ! preg_match( '/^(?:[0-9]+|-1)$/', $regex_limit ) || 0 === (int) $regex_limit ) {
WP_CLI::error( '`--regex-limit` expects a non-zero positive integer or -1.' );
}
$this->regex_limit = (int) $regex_limit;
}| if ( $i + 1 < $match_cnt && $old_end_offset + strlen( $old_after ) > $old_matches[0][ $i + 1 ][1] ) { | ||
| $old_after = substr( $old_after, 0, $old_matches[0][ $i + 1 ][1] - $old_end_offset ); | ||
| $new_after = substr( $new_after, 0, $new_matches[0][ $i + 1 ][1] - $new_end_offset ); | ||
| $old_after = substr( $old_after, 0, $old_matches[0][ $i + 1 ][1] - $old_end_offset ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These 2 inner indentation changes are unnecessary
src/WP_CLI/SearchReplacer.php
Outdated
| * @param bool $regex Whether `$from` is a regular expression. | ||
| * @param string $regex_flags Flags for regular expression. | ||
| * @param string $regex_delimiter Delimiter for regular expression. | ||
| * @param integer $regex_limit The maximum possible replacements for each pattern in each subject string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should appear last after the $logging arg now.
|
I forgot to work this ticket. You can review it now. :) |
|
Thanks for the PR, @miya0001 ! |
|
Also thanks to @alpipego for the collaboration, as his original PR is included in this as well. |
Add --regex-limit option.
Related: #61