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