View | Details | Raw Unified | Return to bug 20334
Collapse All | Expand All

(-)a/Koha/SearchEngine/Elasticsearch/QueryBuilder.pm (+34 lines)
Lines 892-900 sub _clean_search_term { Link Here
892
    # Remove unquoted colons that have whitespace on either side of them
892
    # Remove unquoted colons that have whitespace on either side of them
893
    $term =~ s/(\:[:\s]+|[:\s]+:)$lookahead//g;
893
    $term =~ s/(\:[:\s]+|[:\s]+:)$lookahead//g;
894
894
895
    $term = $self->_query_regex_escape_process($term);
896
895
    return $term;
897
    return $term;
896
}
898
}
897
899
900
=head2 _query_regex_escape_process
901
902
    my $query = $self->_query_regex_escape_process($query);
903
904
Processes query in accordance with current "QueryRegexEscapeOptions" system preference setting.
905
906
=cut
907
908
sub _query_regex_escape_process {
909
    my ($self, $query) = @_;
910
    my $regex_escape_options = C4::Context->preference("QueryRegexEscapeOptions");
911
    if ($regex_escape_options ne 'dont_escape') {
912
        if ($regex_escape_options eq 'escape') {
913
            # Will escape unescaped slashes (/) while preserving
914
            # unescaped slashes within quotes
915
            # @TODO: assumes quotes are always balanced and will
916
            # not handle escaped qoutes properly, should perhaps be
917
            # replaced with a more general parser solution
918
            # so that this function is ever only provided with unqouted
919
            # query parts
920
            $query =~ s@(?:(?<!\\)((?:[\\]{2})*)(?=/))(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)@\\$1@g;
921
        }
922
        elsif($regex_escape_options eq 'unescape_escaped') {
923
            # Will unescape escaped slashes (\/) and escape
924
            # unescaped slashes (/) while preserving slashes within quotes
925
            # The same limitatations as above apply for handling of quotes
926
            $query =~ s@(?:(?<!\\)(?:((?:[\\]{2})*[\\])|((?:[\\]{2})*))(?=/))(?![^"]*"(?:[^"]*"[^"]*")*[^"]*$)@($1 ? substr($1, 0, -1) : ($2 . "\\"))@ge;
927
        }
928
    }
929
    return $query;
930
}
931
898
=head2 _fix_limit_special_cases
932
=head2 _fix_limit_special_cases
899
933
900
    my $limits = $self->_fix_limit_special_cases($limits);
934
    my $limits = $self->_fix_limit_special_cases($limits);
(-)a/installer/data/mysql/atomicupdate/bug_20334-add-syspref-QueryRegexEscapeOptions.sql (+1 lines)
Line 0 Link Here
1
INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type`) VALUES  ('QueryRegexEscapeOptions', 'dont_escape', 'dont_escape|escape|unescape_escaped', 'Escape option for regexps delimiters in Elasicsearch queries.', 'Choice');
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref (+7 lines)
Lines 30-35 Searching: Link Here
30
                  no: Disable
30
                  no: Disable
31
            - ranking of search results by relevance (REQUIRES ZEBRA).
31
            - ranking of search results by relevance (REQUIRES ZEBRA).
32
        -
32
        -
33
            - pref: QueryRegexEscapeOptions
34
              choices:
35
                  escape: Escape
36
                  unescape_escaped: Unescape escaped
37
                  dont_escape: Don't escape
38
            - "regular expressions within query strings. If \"Unescape escaped\" is selected this will allow writing regular expressions \"\/like this\/\" while \"/this/\", \"or/this\" will be escaped and interpreted by Elasticsearch as regular strings."
39
        -
33
            - pref: OpacGroupResults
40
            - pref: OpacGroupResults
34
              default: 0
41
              default: 0
35
              choices:
42
              choices:
(-)a/t/Koha/SearchEngine/ElasticSearch/QueryBuilder.t (-1 / +125 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
use t::lib::Mocks;
22
23
use_ok('Koha::SearchEngine::Elasticsearch::QueryBuilder');
24
25
subtest 'query_regex_escape_options' => sub {
26
    plan tests => 12;
27
28
    t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'dont_escape');
29
30
    my $query_with_regexp = "query /with regexp/";
31
32
    my $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
33
    is(
34
        $processed_query,
35
        $query_with_regexp,
36
        "Unescaped query regexp has not been escaped when escaping is disabled"
37
    );
38
39
    t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'escape');
40
41
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
42
    is(
43
        $processed_query,
44
        "query \\/with regexp\\/",
45
        "Unescaped query regexp has been escaped when escaping is enabled"
46
    );
47
48
    my $query_with_escaped_regex = "query \\/with regexp\\/";
49
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_escaped_regex);
50
    is(
51
        $processed_query,
52
        $query_with_escaped_regex,
53
        "Escaped query regexp has been left unmodified when escaping is enabled"
54
    );
55
56
    my $query_with_even_preceding_escapes_regex = "query \\\\/with regexp\\\\/";
57
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_even_preceding_escapes_regex);
58
    is(
59
        $processed_query,
60
        "query \\\\\\/with regexp\\\\\\/",
61
        "Query regexp with even preceding escapes, thus unescaped, has been escaped when escaping is enabled"
62
    );
63
64
    my $query_with_odd_preceding_escapes_regex = 'query \\\\\\/with regexp\\\\\\/';
65
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_odd_preceding_escapes_regex);
66
    is(
67
        $processed_query,
68
        $query_with_odd_preceding_escapes_regex,
69
        "Query regexp with odd preceding escapes, thus escaped, has been left unmodified when escaping is enabled"
70
    );
71
72
    my $query_with_quoted_slash = "query with / and \"/ within quotes\"";
73
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_quoted_slash);
74
    is(
75
        $processed_query,
76
        "query with \\/ and \"/ within quotes\"",
77
        "Unescaped slash outside of quotes has been escaped while unescaped slash within quotes is left as is when escaping is enabled."
78
    );
79
80
    t::lib::Mocks::mock_preference('QueryRegexEscapeOptions', 'unescape_escaped');
81
82
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_regexp);
83
    is(
84
        $processed_query,
85
        "query \\/with regexp\\/",
86
        "Unescaped query regexp has been escaped when unescape escaping is enabled"
87
    );
88
89
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_escaped_regex);
90
    is(
91
        $processed_query,
92
        "query /with regexp/",
93
        "Escaped query regexp has been unescaped when unescape escaping is enabled"
94
    );
95
96
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_even_preceding_escapes_regex);
97
    is(
98
        $processed_query,
99
        "query \\\\\\/with regexp\\\\\\/",
100
        "Query regexp with even preceding escapes, thus unescaped, has been escaped when unescape escaping is enabled"
101
    );
102
103
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_odd_preceding_escapes_regex);
104
    is(
105
        $processed_query,
106
        "query \\\\/with regexp\\\\/",
107
        "Query regexp with odd preceding escapes, thus escaped, has been unescaped when unescape escaping is enabled"
108
    );
109
110
    my $regexp_at_start_of_string_with_odd_preceding_escapes_regex = '\\\\\\/regexp\\\\\\/';
111
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($regexp_at_start_of_string_with_odd_preceding_escapes_regex);
112
    is(
113
        $processed_query,
114
        "\\\\/regexp\\\\/",
115
        "Regexp at start of string with odd preceding escapes, thus escaped, has been unescaped when unescape escaping is enabled"
116
    );
117
118
    my $query_with_quoted_escaped_slash = "query with \\/ and \"\\/ within quotes\"";
119
    $processed_query = Koha::SearchEngine::Elasticsearch::QueryBuilder->_query_regex_escape_process($query_with_quoted_escaped_slash);
120
    is(
121
        $processed_query,
122
        "query with / and \"\\/ within quotes\"",
123
        "Escaped slash outside of quotes has been unescaped while escaped slash within quotes is left as is when unescape escaping is enabled."
124
    );
125
};

Return to bug 20334