Bug 26673: Remove shebangs from Perl modules
[koha.git] / Koha / Z3950Responder / RPN.pm
1 package Net::Z3950::RPN::Term;
2
3 # Copyright The National Library of Finland 2018
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 =head1 NAME
23
24 Koha::Z3950Responder::RPN
25
26 =head1 SYNOPSIS
27
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.
30
31 =head1 DESCRIPTION
32
33 The method used here is described in C<samples/render-search.pl> of
34 C<Net::Z3950::SimpleServer>.
35
36 =cut
37
38 sub to_koha {
39     my ($self, $mappings) = @_;
40
41     my $attrs = $self->{'attributes'};
42     my $fields = $mappings->{use}{default};
43     my $split = 0;
44     my $prefix = '';
45     my $suffix = '';
46     my $term = $self->{'term'};
47     utf8::decode($term);
48
49     if ($attrs) {
50         foreach my $attr (@$attrs) {
51             if ($attr->{'attributeType'} == 1) { # use
52                 my $use = $attr->{'attributeValue'};
53                 $fields = $mappings->{use}{$use} if defined $mappings->{use}{$use};
54             } elsif ($attr->{'attributeType'} == 4) { # structure
55                 $split = 1 if ($attr->{'attributeValue'} == 2);
56             } elsif ($attr->{'attributeType'} == 5) { # truncation
57                 my $truncation = $attr->{'attributeValue'};
58                 $prefix = '*' if ($truncation == 2 || $truncation == 3);
59                 $suffix = '*' if ($truncation == 1 || $truncation == 3);
60             }
61         }
62     }
63
64     $fields = [$fields] unless !defined $fields || ref($fields) eq 'ARRAY';
65
66     if ($split) {
67         my @terms;
68         foreach my $word (split(/\s/, $term)) {
69             $word =~ s/^[\,\.;:\\\/\"\'\-\=]+//g;
70             $word =~ s/[\,\.;:\\\/\"\'\-\=]+$//g;
71             next if (!$word);
72             $word = $self->escape($word);
73             my @words;
74             if( $fields ) {
75                 foreach my $field (@{$fields}) {
76                     push(@words, "$field:($prefix$word$suffix)");
77                 }
78             } else {
79                 push(@words, "($prefix$word$suffix)");
80             }
81             push (@terms, join(' OR ', @words));
82         }
83         return '(' . join(' AND ', @terms) . ')';
84     }
85
86     my @terms;
87     $term = $self->escape($term);
88     return "($prefix$term$suffix)" unless $fields;
89     foreach my $field (@{$fields}) {
90         push(@terms, "$field:($prefix$term$suffix)");
91     }
92     return '(' . join(' OR ', @terms) . ')';
93 }
94
95 sub escape {
96     my ($self, $term) = @_;
97
98     $term =~ s/([()])/\\$1/g;
99     return $term;
100 }
101
102 package Net::Z3950::RPN::And;
103 sub to_koha {
104     my ($self, $mappings) = @_;
105
106     return '(' . $self->[0]->to_koha($mappings) . ' AND ' .
107                  $self->[1]->to_koha($mappings) . ')';
108 }
109
110 package Net::Z3950::RPN::Or;
111 sub to_koha {
112     my ($self, $mappings) = @_;
113
114     return '(' . $self->[0]->to_koha($mappings) . ' OR ' .
115                  $self->[1]->to_koha($mappings) . ')';
116 }
117
118 package Net::Z3950::RPN::AndNot;
119 sub to_koha {
120     my ($self, $mappings) = @_;
121
122     return '(' . $self->[0]->to_koha($mappings) . ' NOT ' .
123                  $self->[1]->to_koha($mappings) . ')';
124 }
125
126 1;