Searches with NOT are now fully functionnal
[koha.git] / C4 / SearchMarc.pm
1 package C4::SearchMarc;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 require Exporter;
22 use DBI;
23 use C4::Context;
24
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
26
27 # set the version for version checking
28 $VERSION = 0.02;
29
30 =head1 NAME
31
32 C4::Search - Functions for searching the Koha MARC catalog
33
34 =head1 SYNOPSIS
35
36   use C4::Search;
37
38   my ($count, @results) = catalogsearch();
39
40 =head1 DESCRIPTION
41
42 This module provides the searching facilities for the Koha MARC catalog
43
44 C<&catalogsearch> is a front end to all the other searches. Depending
45 on what is passed to it, it calls the appropriate search function.
46
47 =head1 FUNCTIONS
48
49 =over 2
50
51 =cut
52
53 @ISA = qw(Exporter);
54 @EXPORT = qw(&catalogsearch);
55 # make all your functions, whether exported or not;
56
57 # marcsearch : search in the MARC biblio table.
58 # everything is choosen by the user : what to search, the conditions...
59
60 sub catalogsearch {
61         my ($dbh, $tags, $subfields, $and_or, $excluding, $operator, $value, $offset,$length) = @_;
62         # build the sql request. She will look like :
63         # select m1.bibid
64         #               from marc_subfield_table as m1, marc_subfield_table as m2
65         #               where m1.bibid=m2.bibid and
66         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
67
68         # "Normal" statements
69         my @normal_tags = ();
70         my @normal_subfields = ();
71         my @normal_and_or = ();
72         my @normal_operator = ();
73         my @normal_value = ();
74
75         # Extracts the NOT statements from the list of statements
76         my @not_tags = ();
77         my @not_subfields = ();
78         my @not_and_or = ();
79         my @not_operator = ();
80         my @not_value = ();
81         my $any_not = 0;
82
83         for(my $i = 0 ; $i <= $#{$value} ; $i++)
84         {
85                 if(@$excluding[$i])     # NOT statements
86                 {
87                         $any_not = 1;
88                         if(@$operator[$i] eq "contains")
89                         {
90                                 foreach my $word (split(/ /, @$value[$i]))      # if operator is contains, splits the words in separate requests
91                                 {
92                                         push @not_tags, @$tags[$i];
93                                         push @not_subfields, @$subfields[$i];
94                                         push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
95                                         push @not_operator, @$operator[$i];
96                                         push @not_value, $word;
97                                 }
98                         }
99                         else
100                         {
101                                 push @not_tags, @$tags[$i];
102                                 push @not_subfields, @$subfields[$i];
103                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
104                                 push @not_operator, @$operator[$i];
105                                 push @not_value, @$value[$i];
106                         }
107                 }
108                 else    # NORMAL statements
109                 {
110                         if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
111                         {
112                                 foreach my $word (split(/ /, @$value[$i]))
113                                 {
114                                         push @normal_tags, @$tags[$i];
115                                         push @normal_subfields, @$subfields[$i];
116                                         push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
117                                         push @normal_operator, @$operator[$i];
118                                         push @normal_value, $word;
119                                 }
120                         }
121                         else
122                         {
123                                 push @normal_tags, @$tags[$i];
124                                 push @normal_subfields, @$subfields[$i];
125                                 push @normal_and_or, @$and_or[$i];
126                                 push @normal_operator, @$operator[$i];
127                                 push @normal_value, @$value[$i];
128                         }
129                 }
130         }
131
132         # Finds the basic results without the NOT requests
133         my ($sql_tables, $sql_where1, $sql_where2) = create_request(\@normal_tags, \@normal_subfields, \@normal_and_or, \@normal_operator, \@normal_value);
134
135         my $sth;
136 #       warn "HERE (NORMAL)";
137         if ($sql_where2) {
138                 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
139 #               warn("-->select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
140         } else {
141                 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where1");
142 #               warn("==>select m1.bibid from $sql_tables where $sql_where1");
143         }
144
145         $sth->execute();
146         my @result = ();
147
148         # Processes the NOT if any and there are results
149         my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
150
151         if( ($sth->rows) && $any_not )  # some results to tune up and some NOT statements
152         {
153                 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request(\@not_tags, \@not_subfields, \@not_and_or, \@not_operator, \@not_value);
154
155                 my @tmpresult;
156
157                 while (my ($bibid) = $sth->fetchrow) {
158                         push @tmpresult,$bibid;
159                 }
160                 my $sth_not;
161 #               warn "HERE (NOT)";
162                 if ($not_sql_where2) {
163                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
164 #                       warn("-->select m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
165                 } else {
166                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
167 #                       warn("==>select m1.bibid from $not_sql_tables where $not_sql_where1");
168                 }
169
170                 $sth_not->execute();
171
172                 if($sth_not->rows)
173                 {
174                         my %not_bibids = ();
175                         while(my $bibid = $sth_not->fetchrow()) {
176                                 $not_bibids{$bibid} = 1;        # populates the hashtable with the bibids matching the NOT statement
177                         }
178
179                         foreach my $bibid (@tmpresult)
180                         {
181                                 if(!$not_bibids{$bibid})
182                                 {
183                                         push @result, $bibid;
184                                 }
185                         }
186                 }
187                 $sth_not->finish();
188         }
189         else    # no NOT statements
190         {
191                 while (my ($bibid) = $sth->fetchrow) {
192                         push @result,$bibid;
193                 }
194         }
195
196         # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
197         my $counter = $offset;
198         $sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
199         my @finalresult = ();
200         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
201                 $sth->execute($result[$counter]);
202                 my ($author,$title) = $sth->fetchrow;
203                 my %line;
204                 $line{bibid}=$result[$counter];
205                 $line{author}=$author;
206                 $line{title}=$title;
207                 push @finalresult, \%line;
208                 $counter++;
209         }
210
211         my $nbresults = $#result + 1;
212         return (\@finalresult, $nbresults);
213 }
214
215 # Creates the SQL Request
216
217 sub create_request {
218         my ($tags, $subfields, $and_or, $operator, $value) = @_;
219
220         my $sql_tables; # will contain marc_subfield_table as m1,...
221         my $sql_where1; # will contain the "true" where
222         my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
223         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
224         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
225
226         for(my $i=0; $i<=@$value;$i++) {
227                 if (@$value[$i]) {
228                         $nb_active++;
229                         if ($nb_active==1) {
230                                 if (@$operator[$i] eq "start") {
231                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
232                                         $sql_where1 .= "(m1.subfieldvalue like '@$value[$i]%'";
233                                         if (@$tags[$i]) {
234                                                 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
235                                         }
236                                         $sql_where1.=")";
237                                 } elsif (@$operator[$i] eq "contains") {
238                                         $sql_tables .= "marc_word as m$nb_table,";
239                                         $sql_where1 .= "(m1.word  like '@$value[$i]%'";
240                                         if (@$tags[$i]) {
241                                                  $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldid='@$subfields[$i]'";
242                                         }
243                                         $sql_where1.=")";
244                                 } else {
245                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
246                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] '@$value[$i]' ";
247                                         if (@$tags[$i]) {
248                                                  $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
249                                         }
250                                         $sql_where1.=")";
251                                 }
252                         } else {
253                                 if (@$operator[$i] eq "start") {
254                                         $nb_table++;
255                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
256                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like '@$value[$i]%'";
257                                         if (@$tags[$i]) {
258                                                 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
259                                         }
260                                         $sql_where1.=")";
261                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
262                                 } elsif (@$operator[$i] eq "contains") {
263                                         if (@$and_or[$i] eq 'and') {
264                                                 $nb_table++;
265                                                 $sql_tables .= "marc_word as m$nb_table,";
266                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
267                                                 if (@$tags[$i]) {
268                                                         $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
269                                                 }
270                                                 $sql_where1.=")";
271                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
272                                         } else {
273                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
274                                                 if (@$tags[$i]) {
275                                                         $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
276                                                 }
277                                                 $sql_where1.=")";
278                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
279                                         }
280                                 } else {
281                                         $nb_table++;
282                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
283                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] '@$value[$i]'";
284                                         if (@$tags[$i]) {
285                                                 $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
286                                         }
287                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
288                                         $sql_where1.=")";
289                                 }
290                         }
291                 }
292         }
293
294         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
295         {
296                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
297                 $sql_where2 .= ")";
298         }
299         else    # no sql_where2 statement, deleting '('
300         {
301                 $sql_where2 = "";
302         }
303         chop $sql_tables;       # deletes the trailing ','
304         return ($sql_tables, $sql_where1, $sql_where2);
305 }
306
307
308 END { }       # module clean-up code here (global destructor)
309
310 1;
311 __END__
312
313 =back
314
315 =head1 AUTHOR
316
317 Koha Developement team <info@koha.org>
318
319 =cut