| 1 | package Search::Tools::QueryParser; | ||||||||||||||||||||||||||||
| 2 | 26 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 26 | extends 'Search::Tools::Object'; | |||||||||||||||||||||||||||
| 4 | 26 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 26 | ▶ | use Data::Dump qw( dump ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 26 | ▶ | use Search::Query::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 26 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 26 | ▶ | use Data::Dump; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 26 | ▶ | use Search::Tools::Query; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 26 | ▶ | use Search::Tools::UTF8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 26 | ▶ | use Search::Tools::XML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 26 | ▶ | use Search::Tools::RegEx; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 26 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 26 | our $VERSION = '1.007'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 26 | my $XML = Search::Tools::XML->new(); | |||||||||||||||||||||||||||
| 19 | 26 | my $C2E = $XML->char2ent_map; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # we turn locale pragma on in a small block | ||||||||||||||||||||||||||||
| 22 | # because we don't want it to mess up our regex building | ||||||||||||||||||||||||||||
| 23 | # or taint vars in other areas. We just want to use setlocale() | ||||||||||||||||||||||||||||
| 24 | # and make sure we get correct ->utf8 encoding | ||||||||||||||||||||||||||||
| 25 | 26 | my ( $locale, $lang, $charset ); | |||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | 26 | ▶ | use POSIX qw(locale_h); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 26 | $locale = setlocale(LC_CTYPE); | |||||||||||||||||||||||||||
| 29 | 26 | ( $lang, $charset ) = split( m/\./, $locale ); | |||||||||||||||||||||||||||
| 30 | 26 | ▶ | $charset ||= q/UTF-8/; # <v0.24 this was iso-8895-1 | ||||||||||||||||||||||||||
|
Condition50%1/2
$charset ||= "UTF-8"
MC/DC0%0/1
$charset ||= "UTF-8"
$charset
Truth table50%1/2
A
A= $charset
| |||||||||||||||||||||||||||||
| 31 | 26 | ▶ | $lang = q/en_US/ if $lang =~ m/^(posix|c)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 26 | my %Defaults = ( | |||||||||||||||||||||||||||
| 35 | and_word => q/and|near\d*/, | ||||||||||||||||||||||||||||
| 36 | charset => $charset, | ||||||||||||||||||||||||||||
| 37 | default_field => "", | ||||||||||||||||||||||||||||
| 38 | ignore_case => 1, | ||||||||||||||||||||||||||||
| 39 | ignore_fields => {}, | ||||||||||||||||||||||||||||
| 40 | ignore_first_char => quotemeta(q/'-/), | ||||||||||||||||||||||||||||
| 41 | ignore_last_char => quotemeta(q/'-/), | ||||||||||||||||||||||||||||
| 42 | lang => $lang, | ||||||||||||||||||||||||||||
| 43 | locale => $locale, | ||||||||||||||||||||||||||||
| 44 | not_word => q/not/, | ||||||||||||||||||||||||||||
| 45 | or_word => q/or/, | ||||||||||||||||||||||||||||
| 46 | phrase_delim => q/"/, | ||||||||||||||||||||||||||||
| 47 | query_class => 'Search::Tools::Query', | ||||||||||||||||||||||||||||
| 48 | query_dialect => "Search::Query::Dialect::Native", | ||||||||||||||||||||||||||||
| 49 | stemmer => undef, | ||||||||||||||||||||||||||||
| 50 | stopwords => [], | ||||||||||||||||||||||||||||
| 51 | tag_re => $XML->tag_re, | ||||||||||||||||||||||||||||
| 52 | term_re => qr/\w+(?:[\'\-]\w+)*/, | ||||||||||||||||||||||||||||
| 53 | term_min_length => 1, | ||||||||||||||||||||||||||||
| 54 | treat_uris_like_phrases => 1, | ||||||||||||||||||||||||||||
| 55 | whitespace => $XML->html_whitespace, | ||||||||||||||||||||||||||||
| 56 | wildcard => q/*/, | ||||||||||||||||||||||||||||
| 57 | word_characters => q/\w/ . quotemeta(q/'-/), | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 26 | for my $attr ( keys %Defaults ) { | |||||||||||||||||||||||||||
| 61 | 1147 | ▶ | has( $attr => ( is => 'rw', default => sub { $Defaults{$attr} } ) ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 26 | has 'start_bound' => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 64 | 26 | has 'end_bound' => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 65 | 26 | has 'plain_phrase_bound' => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 66 | 26 | has 'html_phrase_bound' => ( is => 'ro' ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub get_defaults { | ||||||||||||||||||||||||||
|
Subroutine
get_defaults: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | return {%Defaults}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 51 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 51 | my $self = shift; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # TODO handle case where both term_re and word_characters are defined | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # charset/locale/lang are a bit interdependent | ||||||||||||||||||||||||||||
| 78 | # so make sure charset/lang are set if locale is explicitly passed. | ||||||||||||||||||||||||||||
| 79 | 51 | ▶ | if ( $self->{locale} ne $Defaults{locale} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | ( $self->{lang}, $self->{charset} ) = split( m/\./, $self->{locale} ); | |||||||||||||||||||||||||||
| 81 | 1 | ▶ | $self->{lang} = 'en_US' if $self->{lang} =~ m/^(posix|c)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | $self->{charset} ||= $Defaults{charset}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"charset"} ||= $Defaults{"charset"}
MC/DC0%0/2
$self->{"charset"} ||= $Defaults{"charset"}
$self->{"charset"}
$Defaults{"charset"}
Truth table33%1/3
A ||= B
A= $self->{"charset"}B= $Defaults{"charset"}
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # make sure ignore_fields is a hash ref | ||||||||||||||||||||||||||||
| 86 | 51 | ▶ | if ( ref( $self->{ignore_fields} ) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | $self->{ignore_fields} | ||||||||||||||||||||||||||||
| 88 | 0 | = { map { $_ => $_ } @{ $self->{ignore_fields} } }; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 51 | $self->_setup_regex_builder; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 51 | return $self; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 63 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 63 | my $self = shift; | |||||||||||||||||||||||||||
| 98 | 63 | my $query_str = shift; | |||||||||||||||||||||||||||
| 99 | 63 | ▶ | confess "query required" unless defined $query_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 63 | ▶ | if ( ref $query_str ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | croak "query must be a scalar string"; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | #$query_str = to_utf8( $query_str, $self->charset ); | ||||||||||||||||||||||||||||
| 105 | 63 | my $extracted = $self->_extract_terms($query_str); | |||||||||||||||||||||||||||
| 106 | 63 | my %regex; | |||||||||||||||||||||||||||
| 107 | 63 | TERM: for my $term ( @{ $extracted->{terms} } ) { | |||||||||||||||||||||||||||
| 108 | 145 | my ( $plain, $html, $escaped ) = $self->_build_regex($term); | |||||||||||||||||||||||||||
| 109 | 145 | my $is_phrase = $term =~ m/\ /; | |||||||||||||||||||||||||||
| 110 | 145 | my @phrase_terms; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # if the term is a phrase, | ||||||||||||||||||||||||||||
| 113 | # build regex for each term in the phrase | ||||||||||||||||||||||||||||
| 114 | 145 | ▶ | if ($is_phrase) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 32 | my @pts = split( /\ /, $term ); | |||||||||||||||||||||||||||
| 116 | 32 | for my $pt (@pts) { | |||||||||||||||||||||||||||
| 117 | 87 | my ( $pt_plain, $pt_html, $pt_esc ) | |||||||||||||||||||||||||||
| 118 | = $self->_build_regex($pt); | ||||||||||||||||||||||||||||
| 119 | 87 | push @phrase_terms, | |||||||||||||||||||||||||||
| 120 | Search::Tools::RegEx->new( | ||||||||||||||||||||||||||||
| 121 | plain => $pt_plain, | ||||||||||||||||||||||||||||
| 122 | html => $pt_html, | ||||||||||||||||||||||||||||
| 123 | term => $pt, | ||||||||||||||||||||||||||||
| 124 | term_re => qr/$pt_esc/i, | ||||||||||||||||||||||||||||
| 125 | is_phrase => 0, | ||||||||||||||||||||||||||||
| 126 | ); | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 145 | $regex{$term} = Search::Tools::RegEx->new( | |||||||||||||||||||||||||||
| 130 | plain => $plain, | ||||||||||||||||||||||||||||
| 131 | html => $html, | ||||||||||||||||||||||||||||
| 132 | term => $term, | ||||||||||||||||||||||||||||
| 133 | term_re => qr/$escaped/i, | ||||||||||||||||||||||||||||
| 134 | is_phrase => $is_phrase, | ||||||||||||||||||||||||||||
| 135 | phrase_terms => \@phrase_terms, | ||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | return $self->{query_class}->new( | ||||||||||||||||||||||||||||
| 140 | dialect => $extracted->{dialect}, | ||||||||||||||||||||||||||||
| 141 | terms => $extracted->{terms}, | ||||||||||||||||||||||||||||
| 142 | fields => $extracted->{fields}, | ||||||||||||||||||||||||||||
| 143 | 63 | str => to_utf8( $query_str, $self->charset ), | |||||||||||||||||||||||||||
| 144 | regex => \%regex, | ||||||||||||||||||||||||||||
| 145 | qp => $self, | ||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 63 | ▶ | sub _extract_terms { | ||||||||||||||||||||||||||
|
Subroutine
_extract_terms: called CC=44 SCAR=40.3
| |||||||||||||||||||||||||||||
| 150 | 63 | my $self = shift; | |||||||||||||||||||||||||||
| 151 | 63 | my $query = shift; | |||||||||||||||||||||||||||
| 152 | 63 | ▶ | confess "need query to extract terms" unless defined $query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 63 | my $stopwords = $self->stopwords; | |||||||||||||||||||||||||||
| 154 | 63 | my $and_word = $self->and_word; | |||||||||||||||||||||||||||
| 155 | 63 | my $or_word = $self->or_word; | |||||||||||||||||||||||||||
| 156 | 63 | my $not_word = $self->not_word; | |||||||||||||||||||||||||||
| 157 | 63 | my $wildcard = $self->wildcard; | |||||||||||||||||||||||||||
| 158 | 63 | my $phrase = $self->phrase_delim; | |||||||||||||||||||||||||||
| 159 | 63 | my $igf = $self->ignore_first_char; | |||||||||||||||||||||||||||
| 160 | 63 | my $igl = $self->ignore_last_char; | |||||||||||||||||||||||||||
| 161 | 63 | my $wordchar = $self->word_characters; | |||||||||||||||||||||||||||
| 162 | 63 | my $default_field = $self->default_field; | |||||||||||||||||||||||||||
| 163 | 63 | my $esc_wildcard = quotemeta($wildcard); | |||||||||||||||||||||||||||
| 164 | 63 | my $word_re = qr/(($esc_wildcard)?[$wordchar]+($esc_wildcard)?)/; | |||||||||||||||||||||||||||
| 165 | 63 | my $min_length = $self->term_min_length; | |||||||||||||||||||||||||||
| 166 | 63 | my $raw_query = $query; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 63 | ▶ | $stopwords = [ split( /\s+/, $stopwords ) ] unless ref $stopwords; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 63 | my %stophash = map { to_utf8( lc($_), $self->charset ) => 1 } @$stopwords; | |||||||||||||||||||||||||||
| 170 | 63 | my ( %words, %uniq, $c ); | |||||||||||||||||||||||||||
| 171 | 63 | my $parser = Search::Query::Parser->new( | |||||||||||||||||||||||||||
| 172 | and_regex => qr{$and_word}i, | ||||||||||||||||||||||||||||
| 173 | or_regex => qr{$or_word}i, | ||||||||||||||||||||||||||||
| 174 | not_regex => qr{$not_word}i, | ||||||||||||||||||||||||||||
| 175 | default_field => $default_field, | ||||||||||||||||||||||||||||
| 176 | query_class => $self->query_dialect, | ||||||||||||||||||||||||||||
| 177 | ); | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 63 | my $baked_query = $raw_query; | |||||||||||||||||||||||||||
| 180 | 63 | ▶ | $baked_query = lc($baked_query) if $self->ignore_case; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 63 | $baked_query = to_utf8( $baked_query, $self->charset ); | |||||||||||||||||||||||||||
| 182 | 63 | ▶ | my $dialect = $parser->parse($baked_query) or croak $parser->error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 63 | ▶ | $self->debug && carp "parsetree: " . Data::Dump::dump( $dialect->tree ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 63 | my $fields_searched | |||||||||||||||||||||||||||
| 185 | = $self->_get_value_from_tree( \%uniq, $dialect->tree, $c ); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 63 | ▶ | $self->debug && carp "parsed: " . Data::Dump::dump( \%uniq ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 63 | my $count = scalar( keys %uniq ); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # parse uniq into word tokens | ||||||||||||||||||||||||||||
| 192 | # including removing stop words | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 63 | ▶ | $self->debug && carp "word_re: $word_re"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 63 | U: for my $u ( sort { $uniq{$a} <=> $uniq{$b} } keys %uniq ) { | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 152 | my $n = $uniq{$u}; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # only phrases have space | ||||||||||||||||||||||||||||
| 201 | # but due to our word_re, a single non-spaced string | ||||||||||||||||||||||||||||
| 202 | # might actually be multiple word tokens | ||||||||||||||||||||||||||||
| 203 | 152 | ▶ | my $isphrase = $u =~ m/\s/ || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$u =~ /\s/ || 0
MC/DC100%1/1
$u =~ /\s/ || 0
$u =~ /\s/
Truth table100%2/2
A
A= $u =~ /\s/
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 152 | ▶ | if ( $self->treat_uris_like_phrases ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | # special case: treat email addresses, uris, as phrase | ||||||||||||||||||||||||||||
| 208 | 152 | ▶ | $isphrase ||= $u =~ m/[$wordchar][\@\.\\\/][$wordchar]/ || 0; | ||||||||||||||||||||||||||
|
Condition100%5/5
$isphrase ||= $u =~ m[[$wordchar][\@\.\\/][$wordchar]] || 0
$u =~ m[[$wordchar][\@\.\\/][$wordchar]] || 0
MC/DC100%2/2
$isphrase ||= $u =~ m[[$wordchar][\@\.\\/][$wordchar]] || 0
$isphrase
$u =~ m[[$wordchar][\@\.\\/][$wordchar]]
Truth table100%3/3
A ||= B
A= $isphraseB= $u =~ m[[$wordchar][\@\.\\/][$wordchar]]
| |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 152 | ▶ | $self->debug && carp "$u -> isphrase = $isphrase"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 152 | my @w = (); | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 152 | TOK: for my $w ( split( m/\s+/, to_utf8( $u, $self->charset ) ) ) { | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 199 | ▶ | next TOK unless $w =~ m/\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 199 | $w =~ s/\Q$phrase\E//g; | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 199 | ▶ | while ( $w =~ m/$word_re/g ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 208 | my $tok = _untaint($1); | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | # strip ignorable chars | ||||||||||||||||||||||||||||
| 225 | 208 | ▶ | $tok =~ s/^[$igf]+// if length($igf); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 208 | ▶ | $tok =~ s/[$igl]+$// if length($igl); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 208 | ▶ | unless ($tok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | ▶ | $self->debug && carp "no token for '$w' $word_re"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 0 | next TOK; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 208 | ▶ | $self->debug && carp "found token: $tok"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 208 | ▶ | if ( exists $stophash{ lc($tok) } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 13 | ▶ | $self->debug && carp "$tok = stopword"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 13 | ▶ | next TOK unless $isphrase; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 202 | ▶ | unless ($isphrase) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 241 | 115 | ▶ | next TOK if $tok =~ m/^($and_word|$or_word|$not_word)$/i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # if tainting was on, odd things can happen. | ||||||||||||||||||||||||||||
| 245 | # so check one more time | ||||||||||||||||||||||||||||
| 246 | 202 | $tok = to_utf8( $tok, $self->charset ); | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # final sanity check | ||||||||||||||||||||||||||||
| 249 | 202 | ▶ | if ( !Encode::is_utf8($tok) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | carp "$tok is NOT utf8"; | |||||||||||||||||||||||||||
| 251 | 0 | next TOK; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | #$self->debug && carp "pushing $tok into wordlist"; | ||||||||||||||||||||||||||||
| 255 | 202 | push( @w, $tok ); | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 152 | ▶ | next U unless @w; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | #$self->debug && carp "joining \@w: " . Data::Dump::dump(\@w); | ||||||||||||||||||||||||||||
| 264 | 146 | ▶ | if ($isphrase) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 32 | $words{ join( ' ', @w ) } = $n + $count++; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | else { | ||||||||||||||||||||||||||||
| 268 | 114 | for (@w) { | |||||||||||||||||||||||||||
| 269 | 115 | $words{$_} = $n + $count++; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 63 | ▶ | $self->debug && carp "tokenized: " . Data::Dump::dump( \%words ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # make sure we don't have 'foo' and 'foo*' | ||||||||||||||||||||||||||||
| 278 | 63 | for ( keys %words ) { | |||||||||||||||||||||||||||
| 279 | 147 | ▶ | if ( $_ =~ m/$esc_wildcard/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 12 | ( my $copy = $_ ) =~ s,$esc_wildcard,,g; | |||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | # delete the more exact of the two | ||||||||||||||||||||||||||||
| 283 | # since the * will match both | ||||||||||||||||||||||||||||
| 284 | 12 | delete( $words{$copy} ); | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 147 | ▶ | if ( length $_ < $min_length ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 1 | ▶ | $self->debug and carp "token too short: '$_'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 1 | delete $words{$_}; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 63 | ▶ | $self->debug && carp "wildcards removed: " . Data::Dump::dump( \%words ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # if any words need to be stemmed | ||||||||||||||||||||||||||||
| 297 | 63 | ▶ | if ( $self->stemmer ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # split each $word into words | ||||||||||||||||||||||||||||
| 300 | # stem each word | ||||||||||||||||||||||||||||
| 301 | # if stem ne word, break into chars and find first N common | ||||||||||||||||||||||||||||
| 302 | # rejoin $uniq | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | #carp "stemming ON\n"; | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 8 | K: for ( keys %words ) { | |||||||||||||||||||||||||||
| 307 | 15 | my (@w) = split /\s+/; | |||||||||||||||||||||||||||
| 308 | 15 | W: for my $w (@w) { | |||||||||||||||||||||||||||
| 309 | 27 | my $func = $self->stemmer; | |||||||||||||||||||||||||||
| 310 | 27 | my $f = &$func( $self, $w ); | |||||||||||||||||||||||||||
| 311 | 27 | ▶ | if ( !defined $f or !length $f ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $f and length $f
MC/DC0%0/2
defined $f and length $f
defined $f
length $f
Truth table33%1/3
A and B
A= defined $fB= length $f
| |||||||||||||||||||||||||||||
| 312 | 0 | next W; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | 27 | $f = to_utf8($f); | |||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | #warn "w: $w\nf: $f\n"; | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | # add wildcard to indicate chars were lost | ||||||||||||||||||||||||||||
| 319 | 27 | $w = $f . $wildcard; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | 15 | my $new = join ' ', @w; | |||||||||||||||||||||||||||
| 323 | 15 | ▶ | if ( $new ne $_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 15 | $words{$new} = $words{$_}; | |||||||||||||||||||||||||||
| 325 | 15 | delete $words{$_}; | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 63 | ▶ | $self->debug && carp "stemming done: " . Data::Dump::dump( \%words ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | # sort keeps query in same order as we entered | ||||||||||||||||||||||||||||
| 334 | return { | ||||||||||||||||||||||||||||
| 335 | 63 | terms => [ sort { $words{$a} <=> $words{$b} } keys %words ], | |||||||||||||||||||||||||||
| 336 | fields => [ keys %$fields_searched ], | ||||||||||||||||||||||||||||
| 337 | dialect => $dialect, | ||||||||||||||||||||||||||||
| 338 | query => $raw_query, | ||||||||||||||||||||||||||||
| 339 | }; | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | # stolen nearly verbatim from Taint::Runtime | ||||||||||||||||||||||||||||
| 344 | # apparently regex can be tainted when running under 'use locale'. | ||||||||||||||||||||||||||||
| 345 | # as of version 0.24 this should not be needed but until I can find a way | ||||||||||||||||||||||||||||
| 346 | # to easily test the Taint feature, we just do this. It's low overhead. | ||||||||||||||||||||||||||||
| 347 | 208 | ▶ | sub _untaint { | ||||||||||||||||||||||||||
|
Subroutine
_untaint: called CC=5 SCAR=18.9
| |||||||||||||||||||||||||||||
| 348 | 208 | my $str = shift; | |||||||||||||||||||||||||||
| 349 | 208 | ▶ | my $ref = ref($str) ? $str : \$str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 208 | ▶ | if ( !defined $$ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | 0 | $$ref = undef; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | else { | ||||||||||||||||||||||||||||
| 354 | $$ref | ||||||||||||||||||||||||||||
| 355 | = ( $$ref =~ /(.*)/ ) | ||||||||||||||||||||||||||||
| 356 | ? $1 | ||||||||||||||||||||||||||||
| 357 | 0 | ▶ | : do { confess("Couldn't find data to untaint") }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | 208 | ▶ | return ref($str) ? 1 : $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 63 | ▶ | sub _get_value_from_tree { | ||||||||||||||||||||||||||
|
Subroutine
_get_value_from_tree: called CC=15 SCAR=27.9
| |||||||||||||||||||||||||||||
| 363 | 63 | my $self = shift; | |||||||||||||||||||||||||||
| 364 | 63 | my $uniq = shift; | |||||||||||||||||||||||||||
| 365 | 63 | my $parseTree = shift; | |||||||||||||||||||||||||||
| 366 | 63 | my $c = shift; | |||||||||||||||||||||||||||
| 367 | 63 | my %fields = (); | |||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | # we only want the values from non minus queries | ||||||||||||||||||||||||||||
| 370 | 63 | for my $node ( '+', '' ) { | |||||||||||||||||||||||||||
| 371 | 126 | ▶ | next unless exists $parseTree->{$node}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 63 | my @branches = @{ $parseTree->{$node} }; | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | #warn dump \@branches; | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 63 | for my $leaf (@branches) { | |||||||||||||||||||||||||||
| 378 | 150 | my $v = $leaf->{value}; | |||||||||||||||||||||||||||
| 379 | 150 | ▶ | if ( !defined $v ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 0 | croak "undefined value in query tree: " . dump($leaf); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | 150 | ▶ | if ( defined $leaf->{field} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $leaf->{"field"} and exists $self->ignore_fields->{$leaf->{"field"}}
MC/DC0%0/2
defined $leaf->{"field"} and exists $self->ignore_fields->{$leaf->{"field"}}
defined $leaf->{"field"}
exists $self->ignore_fields->{$leaf->{"field"}}
Truth table66%2/3
A and B
A= defined $leaf->{"field"}B= exists $self->ignore_fields->{$leaf->{"field"}}
| |||||||||||||||||||||||||||||
| 383 | and exists $self->ignore_fields->{ $leaf->{field} } ) | ||||||||||||||||||||||||||||
| 384 | { | ||||||||||||||||||||||||||||
| 385 | 0 | next; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | 150 | my $field = $leaf->{field}; | |||||||||||||||||||||||||||
| 388 | 150 | ▶ | if ( defined $field ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 389 | 3 | $fields{$field}++; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | 150 | ▶ | if ( ref $v eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 392 | 0 | my $f = $self->_get_value_from_tree( $uniq, $v, $c ); | |||||||||||||||||||||||||||
| 393 | 0 | $fields{$_} = $f->{$_} for ( keys %$f ); | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | elsif ( ref $v eq 'ARRAY' ) { | ||||||||||||||||||||||||||||
| 396 | 1 | for my $value (@$v) { | |||||||||||||||||||||||||||
| 397 | 2 | $value =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 398 | 2 | $uniq->{$value} = ++$c; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | else { | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | # if the $leaf is a proximity query, | ||||||||||||||||||||||||||||
| 404 | # ignore the "phrase-ness" of it and split | ||||||||||||||||||||||||||||
| 405 | # on whitespace. This is a compromise, | ||||||||||||||||||||||||||||
| 406 | # mitigated by the tendency of HeatMap | ||||||||||||||||||||||||||||
| 407 | # to reward proximity anyway. | ||||||||||||||||||||||||||||
| 408 | 149 | ▶ | if ( $leaf->{proximity} and $leaf->{proximity} > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$leaf->{"proximity"} and $leaf->{"proximity"} > 1
MC/DC50%1/2
$leaf->{"proximity"} and $leaf->{"proximity"} > 1
$leaf->{"proximity"}
$leaf->{"proximity"} > 1
Truth table66%2/3
A and B
A= $leaf->{"proximity"}B= $leaf->{"proximity"} > 1
| |||||||||||||||||||||||||||||
| 409 | 1 | my @tokens = split( m/\ +/, $v ); | |||||||||||||||||||||||||||
| 410 | 1 | $uniq->{$_} = ++$c for @tokens; | |||||||||||||||||||||||||||
| 411 | 1 | next; | |||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | # collapse any whitespace | ||||||||||||||||||||||||||||
| 415 | 148 | $v =~ s,\s+,\ ,g; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 148 | $uniq->{$v} = ++$c; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | 63 | return \%fields; | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | 51 | ▶ | sub _setup_regex_builder { | ||||||||||||||||||||||||||
|
Subroutine
_setup_regex_builder: called CC=9 SCAR=23.9
| |||||||||||||||||||||||||||||
| 425 | 51 | my $self = shift; | |||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | # TODO optional for term_re | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | # a search for a '<' or '>' should still highlight, | ||||||||||||||||||||||||||||
| 430 | # since < or > can be indexed as literal < and > | ||||||||||||||||||||||||||||
| 431 | # but this causes a great deal of hassle | ||||||||||||||||||||||||||||
| 432 | # so we just ignore them. | ||||||||||||||||||||||||||||
| 433 | 51 | my $wordchars = $self->word_characters; | |||||||||||||||||||||||||||
| 434 | 51 | $wordchars =~ s,[<>&],,g; | |||||||||||||||||||||||||||
| 435 | 51 | $self->{html_safe_wordchars} = $wordchars; # remember for build | |||||||||||||||||||||||||||
| 436 | 51 | my $ignore_first = $self->ignore_first_char; | |||||||||||||||||||||||||||
| 437 | 51 | my $ignore_last = $self->ignore_last_char; | |||||||||||||||||||||||||||
| 438 | 51 | my $html_whitespace = $self->whitespace; | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | # what's the boundary between a word and a not-word? | ||||||||||||||||||||||||||||
| 441 | # by default: | ||||||||||||||||||||||||||||
| 442 | # the beginning of a string | ||||||||||||||||||||||||||||
| 443 | # the end of a string | ||||||||||||||||||||||||||||
| 444 | # whatever we've defined as WhiteSpace | ||||||||||||||||||||||||||||
| 445 | # any character that is not a WordChar | ||||||||||||||||||||||||||||
| 446 | # any character we explicitly ignore at start or end of word | ||||||||||||||||||||||||||||
| 447 | # | ||||||||||||||||||||||||||||
| 448 | # the \A and \Z (beginning and end) should help if the word butts up | ||||||||||||||||||||||||||||
| 449 | # against the beginning or end of a tagset | ||||||||||||||||||||||||||||
| 450 | # like <p>Word or Word</p> | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | 51 | my @start_bound = ( | |||||||||||||||||||||||||||
| 453 | '\A', | ||||||||||||||||||||||||||||
| 454 | '[>]', | ||||||||||||||||||||||||||||
| 455 | '(?:&[\w\#]+;)', # because a ; might be a legitimate wordchar | ||||||||||||||||||||||||||||
| 456 | # and we treat a char entity like a single char. | ||||||||||||||||||||||||||||
| 457 | # if &char; resolves to a legit wordchar | ||||||||||||||||||||||||||||
| 458 | # this might give unexpected results. | ||||||||||||||||||||||||||||
| 459 | # NOTE that etc is in $WhiteSpace | ||||||||||||||||||||||||||||
| 460 | $html_whitespace, | ||||||||||||||||||||||||||||
| 461 | '[^' . $wordchars . ']' | ||||||||||||||||||||||||||||
| 462 | ); | ||||||||||||||||||||||||||||
| 463 | 51 | ▶ | push( @start_bound, qr/[$ignore_first]+/i ) if length $ignore_first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | my @end_bound | ||||||||||||||||||||||||||||
| 466 | 51 | = ( '\Z', '[<&]', $html_whitespace, '[^' . $wordchars . ']' ); | |||||||||||||||||||||||||||
| 467 | 51 | ▶ | push( @end_bound, qr/[$ignore_last]+/i ) if length $ignore_last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | 51 | ▶ | $self->{start_bound} ||= join( '|', @start_bound ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"start_bound"} ||= join("|", @start_bound)
MC/DC0%0/2
$self->{"start_bound"} ||= join("|", @start_bound)
$self->{"start_bound"}
join("|", @start_bound)
Truth table33%1/3
A ||= B
A= $self->{"start_bound"}B= join("|", @start_bound)
| |||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 51 | ▶ | $self->{end_bound} ||= join( '|', @end_bound ); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"end_bound"} ||= join("|", @end_bound)
MC/DC0%0/2
$self->{"end_bound"} ||= join("|", @end_bound)
$self->{"end_bound"}
join("|", @end_bound)
Truth table33%1/3
A ||= B
A= $self->{"end_bound"}B= join("|", @end_bound)
| |||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | # the whitespace in a query phrase might be: | ||||||||||||||||||||||||||||
| 474 | # any ignore_last_char, followed by | ||||||||||||||||||||||||||||
| 475 | # one or more nonwordchar or whitespace, followed by | ||||||||||||||||||||||||||||
| 476 | # any ignore_first_char | ||||||||||||||||||||||||||||
| 477 | # define for both text and html | ||||||||||||||||||||||||||||
| 478 | # NOTE the first/last swap for plain vs html | ||||||||||||||||||||||||||||
| 479 | # is intentional because of how regex are built. | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 51 | ▶ | my @plain_phrase_bound = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 482 | ( length($ignore_last) ? qr/[$ignore_last]*/i : '' ), | ||||||||||||||||||||||||||||
| 483 | qr/(?:[\s\x20]|[^$wordchars])+/is, | ||||||||||||||||||||||||||||
| 484 | ( length($ignore_first) ? qr/[$ignore_first]?/i : '' ), | ||||||||||||||||||||||||||||
| 485 | ); | ||||||||||||||||||||||||||||
| 486 | 51 | $self->{plain_phrase_bound} = join( '', @plain_phrase_bound ); | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 51 | ▶ | my @html_phrase_bound = ( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 489 | ( length($ignore_first) ? qr/[$ignore_first]*/i : '' ), | ||||||||||||||||||||||||||||
| 490 | qr/(?:$html_whitespace|[^$wordchars])+/is, | ||||||||||||||||||||||||||||
| 491 | ( length($ignore_last) ? qr/[$ignore_last]?/i : '' ), | ||||||||||||||||||||||||||||
| 492 | ); | ||||||||||||||||||||||||||||
| 493 | 51 | $self->{html_phrase_bound} = join( '', @html_phrase_bound ); | |||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 232 | ▶ | sub _build_regex { | ||||||||||||||||||||||||||
|
Subroutine
_build_regex: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 498 | 232 | my $self = shift; | |||||||||||||||||||||||||||
| 499 | 232 | ▶ | my $q = shift or croak "need query to build()"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | 232 | my $wild = $self->{html_safe_wordchars}; | |||||||||||||||||||||||||||
| 501 | 232 | my $st_bound = $self->{start_bound}; | |||||||||||||||||||||||||||
| 502 | 232 | my $end_bound = $self->{end_bound}; | |||||||||||||||||||||||||||
| 503 | 232 | my $wc = $self->{html_safe_wordchars}; | |||||||||||||||||||||||||||
| 504 | 232 | my $ppb = $self->{plain_phrase_bound}; | |||||||||||||||||||||||||||
| 505 | 232 | my $hpb = $self->{html_phrase_bound}; | |||||||||||||||||||||||||||
| 506 | 232 | my $wildcard = $self->wildcard; | |||||||||||||||||||||||||||
| 507 | 232 | my $wild_esc = quotemeta($wildcard); | |||||||||||||||||||||||||||
| 508 | 232 | my $tag_re = $self->tag_re; | |||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | # define simple pattern for plain text | ||||||||||||||||||||||||||||
| 511 | # and complex pattern for HTML markup | ||||||||||||||||||||||||||||
| 512 | 232 | my ( $plain, $html ); | |||||||||||||||||||||||||||
| 513 | 232 | my $escaped = quotemeta($q); | |||||||||||||||||||||||||||
| 514 | 232 | $escaped =~ s/\\[$wild_esc]/[$wc]*/g; # wildcard | |||||||||||||||||||||||||||
| 515 | 232 | $escaped =~ s/\\[\s]/$ppb/g; # whitespace | |||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | 232 | $plain = qr/ | |||||||||||||||||||||||||||
| 518 | ( | ||||||||||||||||||||||||||||
| 519 | \A|$ppb | ||||||||||||||||||||||||||||
| 520 | ) | ||||||||||||||||||||||||||||
| 521 | ( | ||||||||||||||||||||||||||||
| 522 | ${escaped} | ||||||||||||||||||||||||||||
| 523 | ) | ||||||||||||||||||||||||||||
| 524 | ( | ||||||||||||||||||||||||||||
| 525 | \Z|$ppb | ||||||||||||||||||||||||||||
| 526 | ) | ||||||||||||||||||||||||||||
| 527 | /xis; | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 232 | my (@char) = split( m//, $q ); | |||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | 232 | my $counter = -1; | |||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | 232 | CHAR: foreach my $c (@char) { | |||||||||||||||||||||||||||
| 534 | 1461 | $counter++; | |||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | 1461 | ▶ | my $ent = $C2E->{$c} || undef; | ||||||||||||||||||||||||||
|
Condition100%2/2
$C2E->{$c} || undef
MC/DC100%1/1
$C2E->{$c} || undef
$C2E->{$c}
Truth table100%2/2
A
A= $C2E->{$c}
| |||||||||||||||||||||||||||||
| 537 | 1461 | my $num = ord($c); | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | # if this is a special regexp char, protect it | ||||||||||||||||||||||||||||
| 540 | 1461 | $c = quotemeta($c); | |||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | # if it's a *, replace it with the Wild class | ||||||||||||||||||||||||||||
| 543 | 1461 | ▶ | $c = "[$wild]*" if $c eq $wild_esc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | 1461 | ▶ | if ( $c eq '\ ' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 546 | 55 | $c = $hpb . $tag_re . '*'; | |||||||||||||||||||||||||||
| 547 | 55 | next CHAR; | |||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | 1406 | my $aka; | |||||||||||||||||||||||||||
| 551 | 1406 | ▶ | if ($ent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 552 | 1404 | ▶ | $aka = $ent eq "&#$num;" ? $ent : "$ent|&#$num;"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | else { | ||||||||||||||||||||||||||||
| 555 | 2 | $aka = "&#$num;"; | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | # make $c into a regexp | ||||||||||||||||||||||||||||
| 559 | 1406 | ▶ | $c = qr/$c|$aka/i unless $c eq "[$wild]*"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | # any char might be followed by zero or more tags, unless it's the last char | ||||||||||||||||||||||||||||
| 562 | 1406 | ▶ | $c .= $tag_re . '*' unless $counter == $#char; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||
| 566 | # re-join the chars into a single string | ||||||||||||||||||||||||||||
| 567 | 232 | my $safe = join( "\n", @char ); # use \n to make it legible in debugging | |||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | # for debugging legibility we include newlines, so make sure we s//x in matches | ||||||||||||||||||||||||||||
| 570 | 232 | $html = qr/ | |||||||||||||||||||||||||||
| 571 | ( | ||||||||||||||||||||||||||||
| 572 | ${st_bound} | ||||||||||||||||||||||||||||
| 573 | ) | ||||||||||||||||||||||||||||
| 574 | ( | ||||||||||||||||||||||||||||
| 575 | ${safe} | ||||||||||||||||||||||||||||
| 576 | ) | ||||||||||||||||||||||||||||
| 577 | ( | ||||||||||||||||||||||||||||
| 578 | ${end_bound} | ||||||||||||||||||||||||||||
| 579 | ) | ||||||||||||||||||||||||||||
| 580 | /xis; | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 232 | return ( $plain, $html, $escaped ); | |||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | 0 | ▶ | sub _build_term_re { | ||||||||||||||||||||||||||
|
Subroutine
_build_term_re: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | # this based on SWISH::PhraseHighlight::set_match_regexp() | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | #dump $self; | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | 0 | my $wc = $self->word_characters; | |||||||||||||||||||||||||||
| 594 | $self->{_wc_regexp} | ||||||||||||||||||||||||||||
| 595 | 0 | = qr/[^$wc]+/io; # regexp for splitting into swish-words | |||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | 0 | my $igf = $self->ignore_first_char; | |||||||||||||||||||||||||||
| 598 | 0 | my $igl = $self->ignore_last_char; | |||||||||||||||||||||||||||
| 599 | 0 | for ( $igf, $igl ) { | |||||||||||||||||||||||||||
| 600 | 0 | ▶ | if ($_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 601 | 0 | $_ = "[$_]*"; | |||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||
| 603 | else { | ||||||||||||||||||||||||||||
| 604 | 0 | $_ = ''; | |||||||||||||||||||||||||||
| 605 | } | ||||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | 0 | $self->{_ignoreFirst} = $igf; | |||||||||||||||||||||||||||
| 609 | 0 | $self->{_ignoreLast} = $igl; | |||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | 26 | 1; | |||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | __END__ | ||||||||||||||||||||||||||||