Tweaking searching
[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 use C4::Date;
26 use Date::Manip;
27 use ZOOM;
28
29 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30
31 # set the version for version checking
32 $VERSION = 0.02;
33
34 =head1 NAME
35
36 C4::Search - Functions for searching the Koha MARC catalog
37
38 =head1 FUNCTIONS
39
40 This module provides the searching facilities for the Koha MARC catalog
41
42 =cut
43
44 @ISA = qw(Exporter);
45 @EXPORT = qw(&catalogsearch &findseealso &findsuggestion &getMARCnotes &getMARCsubjects);
46
47 =head1 findsuggestion($dbh,$values);
48
49 =head2 $dbh is a link to the DB handler.
50
51 use C4::Context;
52 my $dbh =C4::Context->dbh;
53
54 =head2 $values is a word
55
56 Searches words with the same soundex, ordered by frequency of use.
57 Useful to suggest other searches to the users.
58
59 =cut
60
61 sub findsuggestion {
62         my ($dbh,$values) = @_;
63         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");
64         my @results;
65         for(my $i = 0 ; $i <= $#{$values} ; $i++) {
66                 if (length(@$values[$i]) >=5) {
67                         $sth->execute(@$values[$i],@$values[$i]);
68                         my $resfound = 1;
69                         my @resline;
70                         while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
71                                 push @results, "@$values[$i]|$word|$count";
72 #                               $results{@$values[$i]} = \@resline;
73                                 $resfound++;
74                         }
75                 }
76         }
77         return \@results;
78 }
79
80 =head1 findseealso($dbh,$fields);
81
82 =head2 $dbh is a link to the DB handler.
83
84 use C4::Context;
85 my $dbh =C4::Context->dbh;
86
87 =head2 $fields is a reference to the fields array
88
89 This function modify the @$fields array and add related fields to search on.
90
91 =cut
92
93 sub findseealso {
94         my ($dbh, $fields) = @_;
95         my $tagslib = MARCgettagslib ($dbh,1);
96         for (my $i=0;$i<=$#{$fields};$i++) {
97                 next unless @$fields[$i];
98                 my ($tag) =substr(@$fields[$i],1,3);
99                 my ($subfield) =substr(@$fields[$i],4,1);
100                 @$fields[$i].=','.$tagslib->{$tag}->{$subfield}->{seealso} if ($tagslib->{$tag}->{$subfield}->{seealso});
101         }
102 }
103
104 =head1  my ($count, @results) = catalogsearch($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby,$sqlstring);
105
106 =head2 $dbh is a link to the DB handler.
107
108 use C4::Context;
109 my $dbh =C4::Context->dbh;
110
111 $tags,$and_or, $excluding, $operator, $value are references to array
112
113 =head2 $tags
114
115 contains the list of tags+subfields (for example : $@tags[0] = '200a')
116 A field can be a list of fields : '200f','700a','700b','701a','701b'
117
118 Example
119
120 =head2 $and_or
121
122 contains  a list of strings containing and or or. The 1st value is useless.
123
124 =head2 $excluding
125
126 contains 0 or 1. If 1, then the request is negated.
127
128 =head2 $operator
129
130 contains contains,=,start,>,>=,<,<= the = and start work on the complete subfield. The contains operator works on every word in the subfield.
131
132 examples :
133 contains home, search home anywhere.
134 = home, search a string being home.
135
136 =head2 $value
137
138 contains the value to search
139 If it contains a * or a %, then the search is partial.
140
141 =head2 $offset and $length
142
143 returns $length results, beginning at $offset
144
145 =head2 $orderby
146
147 define the field used to order the request. Any field in the biblio/biblioitem tables can be used. DESC is possible too
148
149 (for example title, title DESC,...)
150
151 =head2 $sqlstring
152
153 optional argument containing an sql string to be used in the 'where' statement. see usage in opac-search.pl.
154
155 =head2 $extratables
156
157 optional argument containing extra tables to search. Used in conjunction with $sqlstring. See usage in opac-search.pl.
158 String... so ',items,issues,reserves' allows the items, issues and reserves tables to be used.in a where.
159
160 =head2 RETURNS
161
162 returns an array containing hashes. The hash contains all biblio & biblioitems fields and a reference to an item hash. The "item hash contains one line for each callnumber & the number of items related to the callnumber.
163
164 =cut
165 =head2 my $marcurlsarray = &getMARCurls($dbh,$bibid,$marcflavour);
166
167 Returns a reference to an array containing all the URLS stored in the MARC database for the given bibid.
168 $marcflavour ("MARC21" or "UNIMARC") isn't used in this version because both flavours of MARC use the same subfield for URLS (but eventually when we get the lables working we'll need to change this.
169
170 =cut
171 sub catalogsearch {
172         my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby,$desc_or_asc,$sqlstring, $extratables) = @_;
173
174 # the item.notforloan contains an integer. Every value <>0 means "book unavailable for loan".
175 # but each library can have it's own table of meaning for each value. Get them
176 # 1st search if there is a list of authorised values connected to items.notforloan
177         my $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield="items.notforloan"');
178         $sth->execute;
179         my %notforloanstatus;
180         my ($authorised_valuecode) = $sth->fetchrow;
181         if ($authorised_valuecode) {
182                 $sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=?");
183                 $sth->execute($authorised_valuecode);
184                 while (my ($authorised_value,$lib) = $sth->fetchrow) {
185                         $notforloanstatus{$authorised_value} = $lib?$lib:$authorised_value;
186                 }
187         }
188         my $subtitle; # Added by JF for Subtitles
189
190         # prepare the query to find item status
191         my $sth_itemCN;
192         if (C4::Context->preference('hidelostitem')) {
193                 $sth_itemCN = $dbh->prepare("select items.* from items where biblionumber=? and (itemlost = 0 or itemlost is NULL)");
194         } else {
195                 $sth_itemCN = $dbh->prepare("select items.* from items where biblionumber=?");
196         }
197         # prepare the query to find date_due where applicable
198         my $sth_issue = $dbh->prepare("select date_due,returndate from issues where itemnumber=?");
199         my $sth_itemtype = $dbh->prepare("select itemtypes.description,itemtypes.notforloan,itemtypes.imageurl from itemtypes where itemtype=?");
200         
201         # prepare the query to find subtitles
202         my $sth_subtitle = $dbh->prepare("SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?"); # Added BY JF for Subtitles
203
204         #
205         # now, do stupid things, that have to be modified for 3.0 :
206         # retrieve the 1st MARC tag.
207         # find the matching non-MARC field
208         # find bib1 attribute. This way, we will be MARC-independant (as title is in 200$a in UNIMARC and 245ùa in MARC21, we use "title" !)
209         # the best method to do this would probably to add a "bib1 attribute" column to marc_subfield_structure
210         # (or a CQL attribute name if we don't want to build bib1 requests)
211         # for instance, we manage only author / title / isbn. Any other field is considered as a keyword/anywhere search
212         #
213         my $tagslib = MARCgettagslib($dbh,$1,'');
214         my $query='';
215         for(my $i = 0 ; $i <= $#{$value} ; $i++){
216                 # 1st split on , then remove ' in the 1st, the find koha field
217                 my @x = split /,/, @$tags[$i];
218                 $x[0] =~ s/'//g if $x[0];
219                 $x[0] =~ /(...)(.)/ if $x[0];
220                 my ($tag,$subfield) = ($1,$2);
221                 if (@$value[$i]) { # if there is something to search, build the request
222                         # if $query already contains something, add @and
223                         $query .= " and " if ($query);
224                         my $field = $tagslib->{$tag}->{$subfield}->{kohafield};
225                         if ($field eq 'biblio.author') {
226                                 $query .= "Author all \"".@$value[$i]."\"";
227                         } elsif ($field eq 'biblio.title') {
228                                 $query .= "Title all \"".@$value[$i]."\"";
229                         } elsif ($field eq 'biblioitems.isbn') {
230                                 $query .= "Isbn= ".@$value[$i];
231                         } else {
232                                 my @spacedout=split(/ /,@$value[$i]);
233                                 my $text = join(" and ",@spacedout);
234                                 $query .= "$text";
235                         }
236                 }
237 #               warn "$i : ".@$tags[$i]. "=> $tag / $subfield = ".$tagslib->{$tag}->{$subfield}->{kohafield};
238         }
239         warn "QUERY : $query";
240         my $Zconn = C4::Context->Zconn or die "unable to set Zconn";
241         my $q = new ZOOM::Query::CQL2RPN( $query, $Zconn);
242         my $rs = $Zconn->search($q);
243         my $numresults=$rs->size();
244         if ($numresults eq 0) {
245                 warn "no records found\n";
246         } else {
247                 warn "$numresults records found, retrieving them (max 80)\n";
248         }
249         my $result='';
250         my $scantimerstart=time();
251         my @finalresult = ();
252         my @CNresults=();
253         my $totalitems=0;
254         $offset=1 unless $offset;
255         # calculate max offset
256         my $maxrecordnum = $offset+$length<$numresults?$offset+$length:($numresults);
257         for (my $i=$offset-1; $i <= $maxrecordnum-1; $i++) {
258                 # get the MARC record (in XML)...
259                 # warn "REC $i = ".$rs->record($i)->raw();
260 # FIXME : it's a silly way to do things : XML => MARC::Record => hash. We had better developping a XML=> hash (in biblio.pm)
261                 my $record = MARC::Record->new_from_xml($rs->record($i)->raw());
262                 # transform it into a meaningul hash
263                 my $line = MARCmarc2koha($dbh,$record);
264                 my $biblionumber=$line->{biblionumber};
265         # Return subtitles first ADDED BY JF
266 #                 $sth_subtitle->execute($biblionumber);
267 #                 my $subtitle_here.= $sth_subtitle->fetchrow." ";
268 #                 chop $subtitle_here;
269 #                 $subtitle = $subtitle_here;
270 #               warn "Here's the Biblionumber ".$biblionumber;
271 #                warn "and here's the subtitle: ".$subtitle_here;
272
273         # /ADDED BY JF
274                 # search itemtype information
275                 $sth_itemtype->execute($line->{itemtype});
276                 my ($itemtype_description,$itemtype_notforloan,$itemtype_imageurl) = $sth_itemtype->fetchrow;
277                 $line->{description} = $itemtype_description;
278                 $line->{imageurl} = $itemtype_imageurl;
279                 $line->{notforloan} = $itemtype_notforloan;
280                 $sth_itemCN->execute($biblionumber);
281                 my @CNresults = ();
282                 my $notforloan=1; # to see if there is at least 1 item that can be issued
283                 while (my $item = $sth_itemCN->fetchrow_hashref) {
284                         # parse the result, putting holdingbranch & itemcallnumber in separate array
285                         # then all other fields in the main array
286                         
287                         # search if item is on loan
288                         my $date_due;
289                         $sth_issue->execute($item->{itemnumber});
290                         while (my $loan = $sth_issue->fetchrow_hashref) {
291                                 if ($loan->{date_due} and !$loan->{returndate}) {
292                                         $date_due = $loan->{date_due};
293                                 }
294                         }
295                         # store this item
296                         my %lineCN;
297                         $lineCN{holdingbranch} = $item->{holdingbranch};
298                         $lineCN{itemcallnumber} = $item->{itemcallnumber};
299                         $lineCN{location} = $item->{location};
300                         $lineCN{date_due} = format_date($date_due);
301                         $lineCN{notforloan} = $notforloanstatus{$line->{notforloan}} if ($line->{notforloan}); # setting not forloan if itemtype is not for loan
302                         $lineCN{notforloan} = $notforloanstatus{$item->{notforloan}} if ($item->{notforloan}); # setting not forloan it this item is not for loan
303                         $notforloan=0 unless ($item->{notforloan} or $item->{wthdrawn} or $item->{itemlost});
304                         push @CNresults,\%lineCN;
305                         $totalitems++;
306                 }
307                 # save the biblio in the final array, with item and item issue status
308                 my %newline;
309                 %newline = %$line;
310                 $newline{totitem} = $totalitems;
311                 # if $totalitems == 0, check if it's being ordered.
312                 if ($totalitems == 0) {
313                         my $sth = $dbh->prepare("select count(*) from aqorders where biblionumber=? and datecancellationprinted is NULL");
314                         $sth->execute($biblionumber);
315                         my ($ordered) = $sth->fetchrow;
316                         $newline{onorder} = 1 if $ordered;
317                 }
318                 $newline{biblionumber} = $biblionumber;
319                 $newline{norequests} = 0;
320                 $newline{norequests} = 1 if ($line->{notforloan}); # itemtype not issuable
321                 $newline{norequests} = 1 if (!$line->{notforloan} && $notforloan); # itemtype issuable but all items not issuable for instance
322                 $newline{subtitle} = $subtitle;  # put the subtitle in ADDED BY JF
323
324                 my @CNresults2= @CNresults;
325                 $newline{CN} = \@CNresults2;
326                 $newline{'even'} = 1 if $#finalresult % 2 == 0;
327                 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
328                 $newline{'timestamp'} = format_date($newline{timestamp});
329                 @CNresults = ();
330                 push @finalresult, \%newline;
331                 $totalitems=0;
332         }
333         my $nbresults = $#finalresult+1;
334         return (\@finalresult, $nbresults);
335 }
336
337 =head2 my $marcnotesarray = &getMARCnotes($dbh,$bibid,$marcflavour);
338
339 Returns a reference to an array containing all the notes stored in the MARC database for the given bibid.
340 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
341
342 =cut
343
344 sub getMARCnotes {
345         my ($dbh, $bibid, $marcflavour) = @_;
346         my ($mintag, $maxtag);
347         if ($marcflavour eq "MARC21") {
348                 $mintag = "500";
349                 $maxtag = "599";
350         } else {           # assume unimarc if not marc21
351                 $mintag = "300";
352                 $maxtag = "399";
353         }
354
355         my $sth=$dbh->prepare("SELECT subfieldvalue,tag FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
356
357         $sth->execute($bibid,$mintag,$maxtag);
358
359         my @marcnotes;
360         my $note = "";
361         my $tag = "";
362         my $marcnote;
363
364         while (my $data=$sth->fetchrow_arrayref) {
365                 my $value=$data->[0];
366                 my $thistag=$data->[1];
367                 if ($value=~/\.$/) {
368                         $value=$value . "  ";
369                 }
370                 if ($thistag ne $tag && $note ne "") {
371                         $marcnote = {marcnote => $note,};
372                         push @marcnotes, $marcnote;
373                         $note=$value;
374                         $tag=$thistag;
375                 }
376                 if ($note ne $value) {
377                         $note = $note." ".$value;
378                 }
379         }
380
381         if ($note) {
382                 $marcnote = {marcnote => $note};
383                 push @marcnotes, $marcnote;   #load last tag into array
384         }
385
386         $sth->finish;
387
388         my $marcnotesarray=\@marcnotes;
389         return $marcnotesarray;
390 }  # end getMARCnotes
391
392
393 =head2 my $marcsubjctsarray = &getMARCsubjects($dbh,$bibid,$marcflavour);
394
395 Returns a reference to an array containing all the subjects stored in the MARC database for the given bibid.
396 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
397
398 =cut
399
400 sub getMARCsubjects {
401     my ($dbh, $bibid, $marcflavour) = @_;
402         my ($mintag, $maxtag);
403         if ($marcflavour eq "MARC21") {
404                 $mintag = "600";
405                 $maxtag = "699";
406         } else {           # assume unimarc if not marc21
407                 $mintag = "600";
408                 $maxtag = "699";
409         }
410         my $sth=$dbh->prepare("SELECT `subfieldvalue`,`subfieldcode`,`tagorder`,`tag` FROM `marc_subfield_table` WHERE `bibid`= ? AND `subfieldcode` NOT IN ('2','4','6','8') AND `tag` BETWEEN ? AND ? ORDER BY `tagorder`,`subfieldorder`");
411         # Subfield exclusion for $2, $4, $6, $8 protects against searching for
412         # variant data in otherwise invariant authorised subject headings when all
413         # returned subfields are used to form a query for matching subjects.  One
414         # example is the use of $2 in MARC 21 where the value of $2 changes for
415         # different editions of the thesaurus used, even where the subject heading
416         # is otherwise the same.  There is certainly a better fix for many cases
417         # where the value of the subfield may be parsed for the invariant data.  
418         # More complete display values may also be separated from query values
419         # containing only the actual invariant authorised subject headings.  More
420         # coding is required for careful value parsing, or display and query
421         # separation; instead of blanket subfield exclusion.
422         # 
423         # As implemented, $3 is passed and might still pose a problem.  Passing $3
424         # could have benefits for some proper use of $3 for UNIMARC, however, might
425         # restrict query usage to a given material type.  -- thd
426
427         $sth->execute($bibid,$mintag,$maxtag);
428
429         my @marcsubjcts;
430         my $subject = "";
431         my $marcsubjct;
432         my $field9;
433         my $activetagorder=0;
434         my $lasttag;
435         my ($subfieldvalue,$subfieldcode,$tagorder,$tag);
436         while (($subfieldvalue,$subfieldcode,$tagorder,$tag)=$sth->fetchrow) {
437                 $lasttag=$tag if $tag;
438                 if ($activetagorder && $tagorder != $activetagorder) {
439                         $subject=~ s/ -- $//;
440                         $marcsubjct = {MARCSUBJCT => $subject,
441                                                         link => $tag."9",
442                                                         linkvalue => $field9,
443                                                         };
444                         push @marcsubjcts, $marcsubjct;
445                         $subject='';
446                         $tag='';
447                         $field9='';
448                 }
449                 if ($subfieldcode eq 9) {
450                         $field9=$subfieldvalue;
451                 } elsif ($subfieldcode eq (3 || 5)) {
452                         $subject .= $subfieldvalue . " ";
453                 } else {
454                         $subject .= $subfieldvalue . " -- ";
455                 }
456                 $activetagorder=$tagorder;
457         }
458         $subject=~ s/ -- $//;
459         $marcsubjct = {MARCSUBJCT => $subject,
460                                         link => $lasttag."9",
461                                         linkvalue => $field9,
462                                         };
463         push @marcsubjcts, $marcsubjct;
464
465         $sth->finish;
466
467         my $marcsubjctsarray=\@marcsubjcts;
468         return $marcsubjctsarray;
469 }  #end getMARCsubjects
470
471 END { }       # module clean-up code here (global destructor)
472
473 1;
474 __END__
475
476 =back
477
478 =head1 AUTHOR
479
480 Koha Developement team <info@koha.org>
481
482 =cut