Browse Source

Bug 28316: avoid messing up regexes in the search queries

This patch ensures that the behavior with
QueryRegexEscapeOptions set to values other than
"Escape" still will works as expected.

It does so by storing the contents of regexes
before escaping special characters and
then restores the contents of regexes back to how
it was before, ensuring that searching with regex is possible.

Signed-off-by: Alex Buckley <alexbuckley@catalyst.net.nz>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
21.11.x
Petro Vashchuk 3 years ago
committed by Jonathan Druart
parent
commit
3ce2810324
  1. 18
      Koha/SearchEngine/Elasticsearch/QueryBuilder.pm

18
Koha/SearchEngine/Elasticsearch/QueryBuilder.pm

@ -939,6 +939,19 @@ sub _clean_search_term {
}
$term = $self->_query_regex_escape_process($term);
# save all regex contents away before escaping brackets:
my @saved_regexes;
my $rgx_i = 0;
while(
$term =~ s@(
(?<!\\)(?:[\\]{2})*/
(?:[^/]+|(?<=\\)(?:[\\]{2})*/)+
(?<!\\)(?:[\\]{2})*/
)$lookahead@~~RE$rgx_i~~@x
) {
@saved_regexes[$rgx_i++] = $1;
}
# remove leading and trailing colons mixed with optional slashes and spaces
$term =~ s/^([\s\\]*:\s*)+//;
$term =~ s/([\s\\]*:\s*)+$//;
@ -956,6 +969,11 @@ sub _clean_search_term {
# screen all brackets with backslash
$term =~ s/(?<!\\)(?:[\\]{2})*([\{\}\[\]])$lookahead/\\$1/g;
# restore all regex contents after escaping brackets:
for (my $i = 0; $i < @saved_regexes; $i++) {
$term =~ s/~~RE$i~~/$saved_regexes[$i]/;
}
return $term;
}

Loading…
Cancel
Save