Marc search ported to OPAC.
[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 &findsuggestion);
56
57 # make all your functions, whether exported or not;
58
59 sub findsuggestion {
60         my ($dbh,$values) = @_;
61         my $sth = $dbh->prepare("SELECT count( * ) AS total, word FROM marc_word WHERE sndx_word = soundex( ? ) AND word <> ? GROUP BY word ORDER BY total DESC");
62         my @results;
63         for(my $i = 0 ; $i <= $#{$values} ; $i++) {
64                 if (length(@$values[$i]) >=5) {
65                         $sth->execute(@$values[$i],@$values[$i]);
66                         my $resfound = 1;
67                         my @resline;
68                         while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
69                                 push @results, "@$values[$i]|$word|$count";
70 #                               $results{@$values[$i]} = \@resline;
71                                 $resfound++;
72                         }
73                 }
74         }
75         return \@results;
76 }
77 sub findseealso {
78         my ($dbh, $fields) = @_;
79         my $tagslib = MARCgettagslib ($dbh,1);
80         for (my $i=0;$i<=$#{$fields};$i++) {
81                 my ($tag) =substr(@$fields[$i],1,3);
82                 my ($subfield) =substr(@$fields[$i],4,1);
83                 @$fields[$i].=','.$tagslib->{$tag}->{$subfield}->{seealso} if ($tagslib->{$tag}->{$subfield}->{seealso});
84         }
85 }
86
87 # marcsearch : search in the MARC biblio table.
88 # everything is choosen by the user : what to search, the conditions...
89
90 sub catalogsearch {
91         my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby) = @_;
92         # build the sql request. She will look like :
93         # select m1.bibid
94         #               from marc_subfield_table as m1, marc_subfield_table as m2
95         #               where m1.bibid=m2.bibid and
96         #               (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
97
98         # "Normal" statements
99         my @normal_tags = ();
100         my @normal_and_or = ();
101         my @normal_operator = ();
102         my @normal_value = ();
103         # Extracts the NOT statements from the list of statements
104         my @not_tags = ();
105         my @not_and_or = ();
106         my @not_operator = ();
107         my @not_value = ();
108         my $any_not = 0;
109         $orderby = "biblio.title" unless $orderby;
110         for(my $i = 0 ; $i <= $#{$value} ; $i++)
111         {
112                 if(@$excluding[$i])     # NOT statements
113                 {
114                         $any_not = 1;
115                         if(@$operator[$i] eq "contains")
116                         {
117                                 foreach my $word (split(/ /, @$value[$i]))      # if operator is contains, splits the words in separate requests
118                                 {
119                                         unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
120                                                 push @not_tags, @$tags[$i];
121                                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
122                                                 push @not_operator, @$operator[$i];
123                                                 push @not_value, $word;
124                                         }
125                                 }
126                         }
127                         else
128                         {
129                                 push @not_tags, @$tags[$i];
130                                 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
131                                 push @not_operator, @$operator[$i];
132                                 push @not_value, @$value[$i];
133                         }
134                 }
135                 else    # NORMAL statements
136                 {
137                         if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
138                         {
139                                 foreach my $word (split(/ /, @$value[$i]))
140                                 {
141                                         unless (C4::Context->stopwords->{uc($word)}) {  #it's NOT a stopword => use it. Otherwise, ignore
142                                                 my $tag = substr(@$tags[$i],0,3);
143                                                 my $subf = substr(@$tags[$i],3,1);
144                                                 push @normal_tags, @$tags[$i];
145                                                 push @normal_and_or, "and";     # assumes "foo" and "bar" if "foo bar" is entered
146                                                 push @normal_operator, @$operator[$i];
147                                                 push @normal_value, $word;
148                                         }
149                                 }
150                         }
151                         else
152                         {
153                                 push @normal_tags, @$tags[$i];
154                                 push @normal_and_or, @$and_or[$i];
155                                 push @normal_operator, @$operator[$i];
156                                 push @normal_value, @$value[$i];
157                         }
158                 }
159         }
160
161         # Finds the basic results without the NOT requests
162         my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
163
164         my $sth;
165         if ($sql_where2) {
166                 $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");
167                 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";
168         } else {
169                 $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");
170                 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";
171         }
172         $sth->execute();
173         my @result = ();
174
175         # Processes the NOT if any and there are results
176         my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
177
178         if( ($sth->rows) && $any_not )  # some results to tune up and some NOT statements
179         {
180                 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request($dbh,\@not_tags, \@not_and_or, \@not_operator, \@not_value);
181
182                 my @tmpresult;
183
184                 while (my ($bibid) = $sth->fetchrow) {
185                         push @tmpresult,$bibid;
186                 }
187                 my $sth_not;
188                 warn "NOT : select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)";
189                 if ($not_sql_where2) {
190                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
191                 } else {
192                         $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
193                 }
194                 $sth_not->execute();
195
196                 if($sth_not->rows)
197                 {
198                         my %not_bibids = ();
199                         while(my $bibid = $sth_not->fetchrow()) {
200                                 $not_bibids{$bibid} = 1;        # populates the hashtable with the bibids matching the NOT statement
201                         }
202
203                         foreach my $bibid (@tmpresult)
204                         {
205                                 if(!$not_bibids{$bibid})
206                                 {
207                                         push @result, $bibid;
208                                 }
209                         }
210                 }
211                 $sth_not->finish();
212         }
213         else    # no NOT statements
214         {
215                 while (my ($bibid) = $sth->fetchrow) {
216                         push @result,$bibid;
217                 }
218         }
219
220         # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
221         my $counter = $offset;
222         $sth = $dbh->prepare("SELECT biblio.biblionumber,author, title, items.holdingbranch, items.itemcallnumber, bibid
223                                                         FROM biblio, marc_biblio left join items on items.biblionumber = biblio.biblionumber
224                                                         WHERE biblio.biblionumber = marc_biblio.biblionumber AND bibid = ?
225                                                         GROUP BY items.biblionumber, items.holdingbranch, items.itemcallnumber");
226         my @finalresult = ();
227         my @CNresults=();
228         my $oldbiblionumber=0;
229         my $totalitems=0;
230         my ($biblionumber,$author,$title,$holdingbranch, $itemcallnumber, $bibid);
231         my ($oldbibid, $oldauthor, $oldtitle,$oldbiblionumber);
232         while (($counter <= $#result) && ($counter <= ($offset + $length))) {
233                 $sth->execute($result[$counter]);
234                 while (($biblionumber,$author,$title,$holdingbranch, $itemcallnumber, $bibid) = $sth->fetchrow) {
235 #                       warn "bibid : $oldbiblionumber ($biblionumber,$author,$title,$holdingbranch, $itemcallnumber, $bibid)";
236                         # parse the result, putting holdingbranch & itemcallnumber in separate array
237                         # then author, title & 1st array in main array
238                         if ($oldbiblionumber && ($oldbiblionumber ne $biblionumber)) {
239                                 my %line;
240                                 $line{bibid}=$oldbibid;
241                                 $line{author}=$oldauthor;
242                                 $line{title}=$oldtitle;
243                                 $line{totitem} = $totalitems;
244                                 $line{biblionumber} = $oldbiblionumber;
245                                 my @CNresults2= @CNresults;
246                                 $line{CN} = \@CNresults2;
247                                 @CNresults = ();
248                                 push @finalresult, \%line;
249                                 $totalitems=0;
250                         }
251                         $oldbibid = $bibid;
252                         $oldauthor = $author;
253                         $oldtitle = $title;
254                         $oldbiblionumber = $biblionumber;
255                         $totalitems++ if ($holdingbranch);
256                         my %lineCN;
257                         $lineCN{holdingbranch} = $holdingbranch;
258                         $lineCN{itemcallnumber} = $itemcallnumber;
259                         push @CNresults,\%lineCN;
260                 }
261                 $counter++;
262         }
263 # add the last line, that is not reached byt the loop / if ($oldbiblionumber...)
264         my %line;
265         $line{bibid}=$oldbibid;
266         $line{author}=$oldauthor;
267         $line{title}=$oldtitle;
268         $line{totitem} = $totalitems;
269         $line{biblionumber} = $oldbiblionumber;
270         my @CNresults2= @CNresults;
271         $line{CN} = \@CNresults2;
272         @CNresults = ();
273         push @finalresult, \%line;
274         my $nbresults = $#result + 1;
275         return (\@finalresult, $nbresults);
276 }
277
278 # Creates the SQL Request
279
280 sub create_request {
281         my ($dbh,$tags, $and_or, $operator, $value) = @_;
282
283         my $sql_tables; # will contain marc_subfield_table as m1,...
284         my $sql_where1; # will contain the "true" where
285         my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
286         my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
287         my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR  is provided.
288
289         for(my $i=0; $i<=@$value;$i++) {
290                 if (@$value[$i]) {
291                         $nb_active++;
292                         if ($nb_active==1) {
293                                 if (@$operator[$i] eq "start") {
294                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
295                                         $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
296                                         if (@$tags[$i]) {
297                                                 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
298                                         }
299                                         $sql_where1.=")";
300                                 } elsif (@$operator[$i] eq "contains") {
301                                         $sql_tables .= "marc_word as m$nb_table,";
302                                         $sql_where1 .= "(m1.word  like ".$dbh->quote("@$value[$i]%");
303                                         if (@$tags[$i]) {
304                                                  $sql_where1 .=" and m1.tag+m1.subfieldid in (@$tags[$i])";
305                                         }
306                                         $sql_where1.=")";
307                                 } else {
308                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
309                                         $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
310                                         if (@$tags[$i]) {
311                                                  $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
312                                         }
313                                         $sql_where1.=")";
314                                 }
315                         } else {
316                                 if (@$operator[$i] eq "start") {
317                                         $nb_table++;
318                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
319                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
320                                         if (@$tags[$i]) {
321                                                 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
322                                         }
323                                         $sql_where1.=")";
324                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
325                                 } elsif (@$operator[$i] eq "contains") {
326                                         if (@$and_or[$i] eq 'and') {
327                                                 $nb_table++;
328                                                 $sql_tables .= "marc_word as m$nb_table,";
329                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
330                                                 if (@$tags[$i]) {
331                                                         $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldid in(@$tags[$i])";
332                                                 }
333                                                 $sql_where1.=")";
334                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
335                                         } else {
336                                                 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]%");
337                                                 if (@$tags[$i]) {
338                                                         $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldid in (@$tags[$i])";
339                                                 }
340                                                 $sql_where1.=")";
341                                                 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
342                                         }
343                                 } else {
344                                         $nb_table++;
345                                         $sql_tables .= "marc_subfield_table as m$nb_table,";
346                                         $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
347                                         if (@$tags[$i]) {
348                                                 $sql_where1 .="  and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
349                                         }
350                                         $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
351                                         $sql_where1.=")";
352                                 }
353                         }
354                 }
355         }
356
357         if($sql_where2 ne "(")  # some datas added to sql_where2, processing
358         {
359                 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
360                 $sql_where2 .= ")";
361         }
362         else    # no sql_where2 statement, deleting '('
363         {
364                 $sql_where2 = "";
365         }
366         chop $sql_tables;       # deletes the trailing ','
367         return ($sql_tables, $sql_where1, $sql_where2);
368 }
369
370
371 END { }       # module clean-up code here (global destructor)
372
373 1;
374 __END__
375
376 =back
377
378 =head1 AUTHOR
379
380 Koha Developement team <info@koha.org>
381
382 =cut