fixing a bug in searches with stopwords (that where not dropped).
[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                                         unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
93                                                 push @not_tags, @$tags[$i];
94                                                 push @not_subfields, @$subfields[$i];
95                                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
96                                                 push @not_operator, @$operator[$i];
97                                                 push @not_value, $word;
98                                         }
99                                 }
100                         }
101                         else
102                         {
103                                 push @not_tags, @$tags[$i];
104                                 push @not_subfields, @$subfields[$i];
105                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
106                                 push @not_operator, @$operator[$i];
107                                 push @not_value, @$value[$i];
108                         }
109                 }
110                 else    # NORMAL statements
111                 {
112                         if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
113                         {
114                                 foreach my $word (split(/ /, @$value[$i]))
115                                 {
116                                         unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
117                                                 push @normal_tags, @$tags[$i];
118                                                 push @normal_subfields, @$subfields[$i];
119                                                 push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
120                                                 push @normal_operator, @$operator[$i];
121                                                 push @normal_value, $word;
122                                         }
123                                 }
124                         }
125                         else
126                         {
127                                 push @normal_tags, @$tags[$i];
128                                 push @normal_subfields, @$subfields[$i];
129                                 push @normal_and_or, @$and_or[$i];
130                                 push @normal_operator, @$operator[$i];
131                                 push @normal_value, @$value[$i];
132                         }
133                 }
134         }
135
136         # Finds the basic results without the NOT requests
137         my ($sql_tables, $sql_where1, $sql_where2) = create_request(\@normal_tags, \@normal_subfields, \@normal_and_or, \@normal_operator, \@normal_value);
138
139         my $sth;
140 #       warn "HERE (NORMAL)";
141         if ($sql_where2) {
142                 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
143 #               warn("-->select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
144         } else {
145                 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where1");
146 #               warn("==>select m1.bibid from $sql_tables where $sql_where1");
147         }
148
149         $sth->execute();
150         my @result = ();
151
152         # Processes the NOT if any and there are results
153         my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
154
155         if( ($sth->rows) && $any_not )  # some results to tune up and some NOT statements
156         {
157                 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request(\@not_tags, \@not_subfields, \@not_and_or, \@not_operator, \@not_value);
158
159                 my @tmpresult;
160
161                 while (my ($bibid) = $sth->fetchrow) {
162                         push @tmpresult,$bibid;
163                 }
164                 my $sth_not;
165 #               warn "HERE (NOT)";
166                 if ($not_sql_where2) {
167                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
168 #                       warn("-->select m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
169                 } else {
170                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
171 #                       warn("==>select m1.bibid from $not_sql_tables where $not_sql_where1");
172                 }
173
174                 $sth_not->execute();
175
176                 if($sth_not->rows)
177                 {
178                         my %not_bibids = ();
179                         while(my $bibid = $sth_not->fetchrow()) {
180                                 $not_bibids{$bibid} = 1;        # populates the hashtable with the bibids matching the NOT statement
181                         }
182
183                         foreach my $bibid (@tmpresult)
184                         {
185                                 if(!$not_bibids{$bibid})
186                                 {
187                                         push @result, $bibid;
188                                 }
189                         }
190                 }
191                 $sth_not->finish();
192         }
193         else    # no NOT statements
194         {
195                 while (my ($bibid) = $sth->fetchrow) {
196                         push @result,$bibid;
197                 }
198         }
199
200         # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
201         my $counter = $offset;
202         $sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
203         my @finalresult = ();
204         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
205                 $sth->execute($result[$counter]);
206                 my ($author,$title) = $sth->fetchrow;
207                 my %line;
208                 $line{bibid}=$result[$counter];
209                 $line{author}=$author;
210                 $line{title}=$title;
211                 push @finalresult, \%line;
212                 $counter++;
213         }
214
215         my $nbresults = $#result + 1;
216         return (\@finalresult, $nbresults);
217 }
218
219 # Creates the SQL Request
220
221 sub create_request {
222         my ($tags, $subfields, $and_or, $operator, $value) = @_;
223
224         my $sql_tables; # will contain marc_subfield_table as m1,...
225         my $sql_where1; # will contain the "true" where
226         my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
227         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
228         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
229
230         for(my $i=0; $i<=@$value;$i++) {
231                 if (@$value[$i]) {
232                         $nb_active++;
233                         if ($nb_active==1) {
234                                 if (@$operator[$i] eq "start") {
235                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
236                                         $sql_where1 .= "(m1.subfieldvalue like '@$value[$i]%'";
237                                         if (@$tags[$i]) {
238                                                 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
239                                         }
240                                         $sql_where1.=")";
241                                 } elsif (@$operator[$i] eq "contains") {
242                                         $sql_tables .= "marc_word as m$nb_table,";
243                                         $sql_where1 .= "(m1.word  like '@$value[$i]%'";
244                                         if (@$tags[$i]) {
245                                                  $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldid='@$subfields[$i]'";
246                                         }
247                                         $sql_where1.=")";
248                                 } else {
249                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
250                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] '@$value[$i]' ";
251                                         if (@$tags[$i]) {
252                                                  $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
253                                         }
254                                         $sql_where1.=")";
255                                 }
256                         } else {
257                                 if (@$operator[$i] eq "start") {
258                                         $nb_table++;
259                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
260                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like '@$value[$i]%'";
261                                         if (@$tags[$i]) {
262                                                 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
263                                         }
264                                         $sql_where1.=")";
265                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
266                                 } elsif (@$operator[$i] eq "contains") {
267                                         if (@$and_or[$i] eq 'and') {
268                                                 $nb_table++;
269                                                 $sql_tables .= "marc_word as m$nb_table,";
270                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
271                                                 if (@$tags[$i]) {
272                                                         $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
273                                                 }
274                                                 $sql_where1.=")";
275                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
276                                         } else {
277                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
278                                                 if (@$tags[$i]) {
279                                                         $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
280                                                 }
281                                                 $sql_where1.=")";
282                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
283                                         }
284                                 } else {
285                                         $nb_table++;
286                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
287                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] '@$value[$i]'";
288                                         if (@$tags[$i]) {
289                                                 $sql_where1 .="  and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
290                                         }
291                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
292                                         $sql_where1.=")";
293                                 }
294                         }
295                 }
296         }
297
298         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
299         {
300                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
301                 $sql_where2 .= ")";
302         }
303         else    # no sql_where2 statement, deleting '('
304         {
305                 $sql_where2 = "";
306         }
307         chop $sql_tables;       # deletes the trailing ','
308         return ($sql_tables, $sql_where1, $sql_where2);
309 }
310
311
312 END { }       # module clean-up code here (global destructor)
313
314 1;
315 __END__
316
317 =back
318
319 =head1 AUTHOR
320
321 Koha Developement team <info@koha.org>
322
323 =cut