1 package C4::SearchMarc;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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
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.
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
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
30 # set the version for version checking
35 C4::Search - Functions for searching the Koha MARC catalog
39 This module provides the searching facilities for the Koha MARC catalog
44 @EXPORT = qw(&catalogsearch &findseealso &findsuggestion &getMARCnotes &getMARCsubjects);
46 =head1 findsuggestion($dbh,$values);
48 =head2 $dbh is a link to the DB handler.
51 my $dbh =C4::Context->dbh;
53 =head2 $values is a word
55 Searches words with the same soundex, ordered by frequency of use.
56 Useful to suggest other searches to the users.
61 my ($dbh,$values) = @_;
62 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 for(my $i = 0 ; $i <= $#{$values} ; $i++) {
65 if (length(@$values[$i]) >=5) {
66 $sth->execute(@$values[$i],@$values[$i]);
69 while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
70 push @results, "@$values[$i]|$word|$count";
71 # $results{@$values[$i]} = \@resline;
79 =head1 findseealso($dbh,$fields);
81 =head2 $dbh is a link to the DB handler.
84 my $dbh =C4::Context->dbh;
86 =head2 $fields is a reference to the fields array
88 This function modify the @$fields array and add related fields to search on.
93 my ($dbh, $fields) = @_;
94 my $tagslib = MARCgettagslib ($dbh,1);
95 for (my $i=0;$i<=$#{$fields};$i++) {
96 my ($tag) =substr(@$fields[$i],1,3);
97 my ($subfield) =substr(@$fields[$i],4,1);
98 @$fields[$i].=','.$tagslib->{$tag}->{$subfield}->{seealso} if ($tagslib->{$tag}->{$subfield}->{seealso});
102 =head1 my ($count, @results) = catalogsearch($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby);
104 =head2 $dbh is a link to the DB handler.
107 my $dbh =C4::Context->dbh;
109 $tags,$and_or, $excluding, $operator, $value are references to array
113 contains the list of tags+subfields (for example : $@tags[0] = '200a')
114 A field can be a list of fields : '200f','700a','700b','701a','701b'
120 contains a list of strings containing and or or. The 1st value is useless.
124 contains 0 or 1. If 1, then the request is negated.
128 contains contains,=,start,>,>=,<,<= the = and start work on the complete subfield. The contains operator works on every word in the subfield.
131 contains home, search home anywhere.
132 = home, search a string being home.
136 contains the value to search
137 If it contains a * or a %, then the search is partial.
139 =head2 $offset and $length
141 returns $length results, beginning at $offset
145 define the field used to order the request. Any field in the biblio/biblioitem tables can be used. DESC is possible too
147 (for example title, title DESC,...)
151 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.
155 =head2 my $marcnotesarray = &getMARCnotes($dbh,$bibid,$marcflavour);
157 Returns a reference to an array containing all the notes stored in the MARC database for the given bibid.
158 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
160 =head2 my $marcsubjctsarray = &getMARCsubjects($dbh,$bibid,$marcflavour);
162 Returns a reference to an array containing all the subjects stored in the MARC database for the given bibid.
163 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
168 my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby) = @_;
169 warn "@$tags[0], @$and_or[0], @$excluding[0], @$operator[0], @$value[0], $offset,$length,$orderby";
170 # build the sql request. She will look like :
172 # from marc_subfield_table as m1, marc_subfield_table as m2
173 # where m1.bibid=m2.bibid and
174 # (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
176 # last minute stripping out of stuff
177 # doesn't work @$value =~ s/\'/ /;
178 # @$value = map { $_ =~ s/\'/ /g } @$value;
180 # "Normal" statements
181 my @normal_tags = ();
182 my @normal_and_or = ();
183 my @normal_operator = ();
184 my @normal_value = ();
185 # Extracts the NOT statements from the list of statements
188 my @not_operator = ();
191 $orderby = "biblio.title" unless $orderby;
193 #last minute stripping out of ' and ,
194 # paul : quoting, it's done a few lines lated.
195 # foreach $_ (@$value) {
200 for(my $i = 0 ; $i <= $#{$value} ; $i++)
203 @$value[$i] =~ s/\*/%/g;
204 # remove % at the beginning
205 @$value[$i] =~ s/^%//g;
206 @$value[$i] =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g if @$operator[$i] eq "contains";
207 if(@$excluding[$i]) # NOT statements
210 if(@$operator[$i] eq "contains")
212 foreach my $word (split(/ /, @$value[$i])) # if operator is contains, splits the words in separate requests
214 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
215 # warn "word : $word";
216 $word =~ s/%//g unless length($word)>4;
217 unless (C4::Context->stopwords->{uc($word)} or length($word)==1) { #it's NOT a stopword => use it. Otherwise, ignore
218 push @not_tags, @$tags[$i];
219 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
220 push @not_operator, @$operator[$i];
221 push @not_value, $word;
227 push @not_tags, @$tags[$i];
228 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
229 push @not_operator, @$operator[$i];
230 push @not_value, @$value[$i];
233 else # NORMAL statements
235 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
237 foreach my $word (split(/ /, @$value[$i]))
239 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
240 # warn "word : $word";
241 $word =~ s/%//g unless length($word)>4;
242 unless (C4::Context->stopwords->{uc($word)} or length($word)==1) { #it's NOT a stopword => use it. Otherwise, ignore
243 my $tag = substr(@$tags[$i],0,3);
244 my $subf = substr(@$tags[$i],3,1);
245 push @normal_tags, @$tags[$i];
246 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
247 push @normal_operator, @$operator[$i];
248 push @normal_value, $word;
254 push @normal_tags, @$tags[$i];
255 push @normal_and_or, @$and_or[$i];
256 push @normal_operator, @$operator[$i];
257 push @normal_value, @$value[$i];
262 # Finds the basic results without the NOT requests
263 my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
265 $sql_where1 .= "and TO_DAYS( NOW( ) ) - TO_DAYS( biblio.timestamp ) <30" if $orderby =~ "biblio.timestamp";
268 $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");
269 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 term is @$value";
271 $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");
272 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";
277 # Processes the NOT if any and there are results
278 my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
280 if( ($sth->rows) && $any_not ) # some results to tune up and some NOT statements
282 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request($dbh,\@not_tags, \@not_and_or, \@not_operator, \@not_value);
286 while (my ($bibid) = $sth->fetchrow) {
287 push @tmpresult,$bibid;
290 warn "NOT : select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)";
291 if ($not_sql_where2) {
292 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
294 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
301 while(my $bibid = $sth_not->fetchrow()) {
302 $not_bibids{$bibid} = 1; # populates the hashtable with the bibids matching the NOT statement
305 foreach my $bibid (@tmpresult)
307 if(!$not_bibids{$bibid})
309 push @result, $bibid;
315 else # no NOT statements
317 while (my ($bibid) = $sth->fetchrow) {
322 # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
323 my $counter = $offset;
324 # HINT : biblionumber as bn is important. The hash is fills biblionumber with items.biblionumber.
325 # so if you dont' has an item, you get a not nice empty value.
326 $sth = $dbh->prepare("SELECT biblio.biblionumber as bn,biblio.*, biblioitems.*,marc_biblio.bibid,itemtypes.notforloan
327 FROM biblio, marc_biblio
328 LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
329 LEFT JOIN itemtypes on itemtypes.itemtype=biblioitems.itemtype
330 WHERE biblio.biblionumber = marc_biblio.biblionumber AND bibid = ?");
331 my @finalresult = ();
335 my ($oldbibid, $oldauthor, $oldtitle);
336 my $sth_itemCN = $dbh->prepare("select items.* from items where biblionumber=?");
337 my $sth_issue = $dbh->prepare("select date_due,returndate from issues where itemnumber=?");
338 # parse all biblios between start & end.
339 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
340 # search & parse all items & note itemcallnumber
341 $sth->execute($result[$counter]);
343 my $line = $sth->fetchrow_hashref;
344 my $biblionumber=$line->{bn};
345 # $continue=0 unless $line->{bn};
346 # my $lastitemnumber;
347 $sth_itemCN->execute($biblionumber);
349 my $notforloan=1; # to see if there is at least 1 item that can be issued
350 while (my $item = $sth_itemCN->fetchrow_hashref) {
351 # parse the result, putting holdingbranch & itemcallnumber in separate array
352 # then all other fields in the main array
354 # search if item is on loan
356 $sth_issue->execute($item->{itemnumber});
357 while (my $loan = $sth_issue->fetchrow_hashref) {
358 if ($loan->{date_due} and !$loan->{returndate}) {
359 $date_due = $loan->{date_due};
364 $lineCN{holdingbranch} = $item->{holdingbranch};
365 $lineCN{itemcallnumber} = $item->{itemcallnumber};
366 $lineCN{location} = $item->{location};
367 $lineCN{date_due} = format_date($date_due);
368 $notforloan=0 unless ($item->{notforloan} or $item->{wthdrawn} or $item->{itemlost});
369 push @CNresults,\%lineCN;
372 # save the biblio in the final array, with item and item issue status
375 $newline{totitem} = $totalitems;
376 $newline{biblionumber} = $biblionumber;
377 $newline{norequests} = 0;
378 $newline{norequests} = 1 if ($line->{notforloan}); # itemtype not issuable
379 $newline{norequests} = 1 if (!$line->{notforloan} && $notforloan); # itemtype issuable but all items not issuable for instance
380 my @CNresults2= @CNresults;
381 $newline{CN} = \@CNresults2;
382 $newline{'even'} = 1 if $#finalresult % 2 == 0;
383 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
384 $newline{'timestamp'} = format_date($newline{timestamp});
386 push @finalresult, \%newline;
390 my $nbresults = $#result+1;
391 return (\@finalresult, $nbresults);
394 # Creates the SQL Request
397 my ($dbh,$tags, $and_or, $operator, $value) = @_;
399 my $sql_tables; # will contain marc_subfield_table as m1,...
400 my $sql_where1; # will contain the "true" where
401 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
402 my $nb_active=0; # will contain the number of "active" entries. an entry is active if a value is provided.
403 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
405 my $maxloop=8; # the maximum number of words to avoid a too complex search.
406 $maxloop = @$value if @$value<$maxloop;
408 for(my $i=0; $i<=$maxloop;$i++) {
412 if (@$operator[$i] eq "start") {
413 $sql_tables .= "marc_subfield_table as m$nb_table,";
414 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
416 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
419 } elsif (@$operator[$i] eq "contains") {
420 $sql_tables .= "marc_word as m$nb_table,";
421 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]");
423 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
427 $sql_tables .= "marc_subfield_table as m$nb_table,";
428 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
430 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
435 if (@$operator[$i] eq "start") {
437 $sql_tables .= "marc_subfield_table as m$nb_table,";
438 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
440 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
443 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
444 } elsif (@$operator[$i] eq "contains") {
445 if (@$and_or[$i] eq 'and') {
447 $sql_tables .= "marc_word as m$nb_table,";
448 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
450 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
453 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
455 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
457 $sql_where1 .=" and m$nb_table.tagsubfield in (@$tags[$i])";
460 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
464 $sql_tables .= "marc_subfield_table as m$nb_table,";
465 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
467 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
469 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
476 if($sql_where2 ne "(") # some datas added to sql_where2, processing
478 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
481 else # no sql_where2 statement, deleting '('
485 chop $sql_tables; # deletes the trailing ','
486 return ($sql_tables, $sql_where1, $sql_where2);
490 my ($dbh, $bibid, $marcflavour) = @_;
491 my ($mintag, $maxtag);
492 if ($marcflavour eq "MARC21") {
495 } else { # assume unimarc if not marc21
500 my $sth=$dbh->prepare("SELECT subfieldvalue,tag FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
502 $sth->execute($bibid,$mintag,$maxtag);
509 while (my $data=$sth->fetchrow_arrayref) {
510 my $value=$data->[0];
511 my $thistag=$data->[1];
515 if ($thistag ne $tag && $note ne "") {
516 $marcnote = {MARCNOTES => $note,};
517 push @marcnotes, $marcnote;
521 if ($note ne $value) {
522 $note = $note." ".$value;
527 $marcnote = {MARCNOTES => $note};
528 push @marcnotes, $marcnote; #load last tag into array
534 my $marcnotesarray=\@marcnotes;
535 return $marcnotesarray;
539 sub getMARCsubjects {
540 my ($dbh, $bibid, $marcflavour) = @_;
541 my ($mintag, $maxtag);
542 if ($marcflavour eq "MARC21") {
545 } else { # assume unimarc if not marc21
549 my $sth=$dbh->prepare("SELECT subfieldvalue,subfieldcode FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
551 $sth->execute($bibid,$mintag,$maxtag);
558 while (my $data=$sth->fetchrow_arrayref) {
559 my $value = $data->[0];
560 my $subfield = $data->[1];
561 if ($subfield eq "a" && $value ne $subjct) {
562 $marcsubjct = {MARCSUBJCT => $value,};
563 push @marcsubjcts, $marcsubjct;
571 my $marcsubjctsarray=\@marcsubjcts;
572 return $marcsubjctsarray;
573 } #end getMARCsubjects
575 END { } # module clean-up code here (global destructor)
584 Koha Developement team <info@koha.org>