Skip to content

Commit a22a55c

Browse files
Merge branch 'master' into use-trusty
2 parents a2dcd65 + 92157c1 commit a22a55c

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

features/db-search.feature

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,8 @@ Feature: Search through the database
613613
Scenario: Search with multibyte strings
614614
Given a WP install
615615
And I run `wp option update multibytetest 'あいうえおかきくけこさしすせとたちつてと'`
616+
# Note ö is o with combining umlaut.
617+
And I run `wp option update plaintst_combining 'lllllムnöppppp'`
616618

617619
When I run `wp db search "かきくけこ" --before_context=0 --after_context=0`
618620
Then STDOUT should contain:
@@ -647,6 +649,38 @@ Feature: Search through the database
647649
:あいうえおかきくけこさしすせとたちつてと
648650
"""
649651

652+
When I run `wp db search 'ppppp' --before_context=3 --after_context=4`
653+
Then STDOUT should contain:
654+
"""
655+
:ムnöppppp
656+
"""
657+
658+
When I run `wp db search 'ppppp' --before_context=1 --after_context=1`
659+
Then STDOUT should contain:
660+
"""
661+
:öppppp
662+
"""
663+
664+
When I run `wp db search 'ムn' --before_context=2 --after_context=1`
665+
Then STDOUT should contain:
666+
"""
667+
:llムnö
668+
"""
669+
And STDOUT should not contain:
670+
"""
671+
:llムnöp
672+
"""
673+
674+
When I run `wp db search 'ムn' --before_context=2 --after_context=2`
675+
Then STDOUT should contain:
676+
"""
677+
:llムnöp
678+
"""
679+
And STDOUT should not contain:
680+
"""
681+
:llムnöpp
682+
"""
683+
650684
Scenario: Search with regular expressions
651685
Given a WP install
652686
And I run `wp option update regextst '12345é789あhttps://regextst.com1234567890123456789éhttps://regextst.com12345678901234567890regextst.com34567890t.com67890'`
@@ -714,15 +748,19 @@ Feature: Search through the database
714748
https://r
715749
"""
716750

717-
# Bug: context counts combining characters as 2 characters.
718751
When I run `wp db search 'ppppp' --regex --before_context=3 --after_context=4`
719752
Then STDOUT should contain:
720753
"""
721-
:nöppppp
754+
:ムnöppppp
722755
"""
723756

724-
# Bug: context counts combining characters as 2 characters.
725-
When I run `wp db search 'ムn' --regex --before_context=2 --after_context=2`
757+
When I run `wp db search 'ppppp' --regex --before_context=1 --after_context=1`
758+
Then STDOUT should contain:
759+
"""
760+
:öppppp
761+
"""
762+
763+
When I run `wp db search 'ムn' --before_context=2 --after_context=1`
726764
Then STDOUT should contain:
727765
"""
728766
:llムnö
@@ -732,6 +770,16 @@ Feature: Search through the database
732770
:llムnöp
733771
"""
734772

773+
When I run `wp db search 'ムn' --regex --before_context=2 --after_context=2`
774+
Then STDOUT should contain:
775+
"""
776+
:llムnöp
777+
"""
778+
And STDOUT should not contain:
779+
"""
780+
:llムnöpp
781+
"""
782+
735783
When I run `wp db search 't\.c' --regex --before_context=1 --after_context=1`
736784
Then STDOUT should contain:
737785
"""

src/DB_Command.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,11 @@ public function search( $args, $assoc_args ) {
844844
}
845845
} else {
846846
$safe_search = preg_quote( $search, '#' );
847-
$search_regex = '#(.{0,' . $before_context . '})(' . $safe_search .')(.{0,' . $after_context . '})#i';
848847
if ( 0 === strpos( $wpdb->charset, 'utf8' ) ) {
849-
$search_regex .= 'u';
848+
$context_re = \cli\can_use_pcre_x() ? '\X' : '.';
849+
$search_regex = '#(' . $context_re . '{0,' . $before_context . '})(' . $safe_search .')(' . $context_re . '{0,' . $after_context . '})#iu';
850+
} else {
851+
$search_regex = '#(.{0,' . $before_context . '})(' . $safe_search .')(.{0,' . $after_context . '})#i';
850852
}
851853
$esc_like_search = '%' . self::esc_like( $search ) . '%';
852854
}
@@ -910,14 +912,13 @@ public function search( $args, $assoc_args ) {
910912
foreach ( $matches[0] as $match_arr ) {
911913
$match = $match_arr[0];
912914
$offset = $match_arr[1];
913-
// Offsets are in bytes, so need to use `strlen()` and `substr()` before using `safe_substr()`. Note: not catering for combining chars.
915+
// Offsets are in bytes, so need to use `strlen()` and `substr()` before using `safe_substr()`.
914916
$before = $before_context && $offset ? \cli\safe_substr( substr( $col_val, 0, $offset ), -$before_context, null /*length*/, false /*is_width*/, $encoding ) : '';
915917
$after = $after_context ? \cli\safe_substr( substr( $col_val, $offset + strlen( $match ) ), 0, $after_context, false /*is_width*/, $encoding ) : '';
916918
$bits[] = $before . $colors['match'][0] . $match . $colors['match'][1] . $after;
917919
}
918920
} else {
919921
foreach ( $matches[0] as $key => $value ) {
920-
// Note: not catering for combining chars.
921922
$bits[] = $matches[1][ $key ] . $colors['match'][0] . $matches[2][ $key ] . $colors['match'][1] . $matches[3][ $key ];
922923
}
923924
}

0 commit comments

Comments
 (0)