3 # Copyright The National Library of Finland 2018
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under thes
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 Koha::Z3950Responder::RPN
28 Overrides for the C<Net::Z3950::RPN> classes adding a C<to_koha> method that
29 converts the query to a syntax that C<Koha::SearchEngine> understands.
33 The method used here is described in C<samples/render-search.pl> of
34 C<Net::Z3950::SimpleServer>.
38 package Net::Z3950::RPN::Term;
40 my ($self, $mappings) = @_;
42 my $attrs = $self->{'attributes'};
43 my $fields = $mappings->{use}{default} // '_all';
47 my $term = $self->{'term'};
51 foreach my $attr (@$attrs) {
52 if ($attr->{'attributeType'} == 1) { # use
53 my $use = $attr->{'attributeValue'};
54 $fields = $mappings->{use}{$use} if defined $mappings->{use}{$use};
55 } elsif ($attr->{'attributeType'} == 4) { # structure
56 $split = 1 if ($attr->{'attributeValue'} == 2);
57 } elsif ($attr->{'attributeType'} == 5) { # truncation
58 my $truncation = $attr->{'attributeValue'};
59 $prefix = '*' if ($truncation == 2 || $truncation == 3);
60 $suffix = '*' if ($truncation == 1 || $truncation == 3);
65 $fields = [$fields] unless ref($fields) eq 'ARRAY';
69 foreach my $word (split(/\s/, $term)) {
70 $word =~ s/^[\,\.;:\\\/\"\'\-\=]+//g;
71 $word =~ s/[\,\.;:\\\/\"\'\-\=]+$//g;
73 $word = $self->escape($word);
75 foreach my $field (@{$fields}) {
76 push(@words, "$field:($prefix$word$suffix)");
78 push (@terms, join(' OR ', @words));
80 return '(' . join(' AND ', @terms) . ')';
84 $term = $self->escape($term);
85 foreach my $field (@{$fields}) {
86 push(@terms, "$field:($prefix$term$suffix)");
88 return '(' . join(' OR ', @terms) . ')';
92 my ($self, $term) = @_;
94 $term =~ s/([()])/\\$1/g;
98 package Net::Z3950::RPN::And;
100 my ($self, $mappings) = @_;
102 return '(' . $self->[0]->to_koha($mappings) . ' AND ' .
103 $self->[1]->to_koha($mappings) . ')';
106 package Net::Z3950::RPN::Or;
108 my ($self, $mappings) = @_;
110 return '(' . $self->[0]->to_koha($mappings) . ' OR ' .
111 $self->[1]->to_koha($mappings) . ')';
114 package Net::Z3950::RPN::AndNot;
116 my ($self, $mappings) = @_;
118 return '(' . $self->[0]->to_koha($mappings) . ' NOT ' .
119 $self->[1]->to_koha($mappings) . ')';