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 # build the sql request. She will look like :
171 # from marc_subfield_table as m1, marc_subfield_table as m2
172 # where m1.bibid=m2.bibid and
173 # (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
175 # last minute stripping out of stuff
176 # doesn't work @$value =~ s/\'/ /;
177 # @$value = map { $_ =~ s/\'/ /g } @$value;
179 # "Normal" statements
180 my @normal_tags = ();
181 my @normal_and_or = ();
182 my @normal_operator = ();
183 my @normal_value = ();
184 # Extracts the NOT statements from the list of statements
187 my @not_operator = ();
190 $orderby = "biblio.title" unless $orderby;
192 #last minute stripping out of ' and ,
193 foreach $_ (@$value) {
198 for(my $i = 0 ; $i <= $#{$value} ; $i++)
201 @$value[$i] =~ s/\*/%/g;
202 # remove % at the beginning
203 @$value[$i] =~ s/^%//g;
204 if(@$excluding[$i]) # NOT statements
207 if(@$operator[$i] eq "contains")
209 foreach my $word (split(/ /, @$value[$i])) # if operator is contains, splits the words in separate requests
211 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
212 # warn "word : $word";
213 $word =~ s/%//g unless length($word)>4;
214 unless (C4::Context->stopwords->{uc($word)} or length($word)==1) { #it's NOT a stopword => use it. Otherwise, ignore
215 push @not_tags, @$tags[$i];
216 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
217 push @not_operator, @$operator[$i];
218 push @not_value, $word;
224 push @not_tags, @$tags[$i];
225 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
226 push @not_operator, @$operator[$i];
227 push @not_value, @$value[$i];
230 else # NORMAL statements
232 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
234 foreach my $word (split(/ /, @$value[$i]))
236 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
237 # warn "word : $word";
238 $word =~ s/%//g unless length($word)>4;
239 unless (C4::Context->stopwords->{uc($word)} or length($word)==1) { #it's NOT a stopword => use it. Otherwise, ignore
240 my $tag = substr(@$tags[$i],0,3);
241 my $subf = substr(@$tags[$i],3,1);
242 push @normal_tags, @$tags[$i];
243 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
244 push @normal_operator, @$operator[$i];
245 push @normal_value, $word;
251 push @normal_tags, @$tags[$i];
252 push @normal_and_or, @$and_or[$i];
253 push @normal_operator, @$operator[$i];
254 push @normal_value, @$value[$i];
259 # Finds the basic results without the NOT requests
260 my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
262 $sql_where1 .= "and TO_DAYS( NOW( ) ) - TO_DAYS( biblio.timestamp ) <30" if $orderby =~ "biblio.timestamp";
265 $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");
266 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";
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_where1 order by $orderby");
269 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";
274 # Processes the NOT if any and there are results
275 my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
277 if( ($sth->rows) && $any_not ) # some results to tune up and some NOT statements
279 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request($dbh,\@not_tags, \@not_and_or, \@not_operator, \@not_value);
283 while (my ($bibid) = $sth->fetchrow) {
284 push @tmpresult,$bibid;
287 warn "NOT : select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)";
288 if ($not_sql_where2) {
289 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
291 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
298 while(my $bibid = $sth_not->fetchrow()) {
299 $not_bibids{$bibid} = 1; # populates the hashtable with the bibids matching the NOT statement
302 foreach my $bibid (@tmpresult)
304 if(!$not_bibids{$bibid})
306 push @result, $bibid;
312 else # no NOT statements
314 while (my ($bibid) = $sth->fetchrow) {
319 # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
320 my $counter = $offset;
321 # HINT : biblionumber as bn is important. The hash is fills biblionumber with items.biblionumber.
322 # so if you dont' has an item, you get a not nice empty value.
323 $sth = $dbh->prepare("SELECT biblio.biblionumber as bn,biblio.*, biblioitems.*,marc_biblio.bibid,itemtypes.notforloan
324 FROM biblio, marc_biblio
325 LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
326 LEFT JOIN itemtypes on itemtypes.itemtype=biblioitems.itemtype
327 WHERE biblio.biblionumber = marc_biblio.biblionumber AND bibid = ?");
328 my @finalresult = ();
332 my ($oldbibid, $oldauthor, $oldtitle);
333 my $sth_itemCN = $dbh->prepare("select items.* from items where biblionumber=?");
334 my $sth_issue = $dbh->prepare("select date_due,returndate from issues where itemnumber=?");
335 # parse all biblios between start & end.
336 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
337 # search & parse all items & note itemcallnumber
338 $sth->execute($result[$counter]);
340 my $line = $sth->fetchrow_hashref;
341 my $biblionumber=$line->{bn};
342 # $continue=0 unless $line->{bn};
343 # my $lastitemnumber;
344 $sth_itemCN->execute($biblionumber);
346 my $notforloan=1; # to see if there is at least 1 item that can be issued
347 while (my $item = $sth_itemCN->fetchrow_hashref) {
348 # parse the result, putting holdingbranch & itemcallnumber in separate array
349 # then all other fields in the main array
351 # search if item is on loan
353 $sth_issue->execute($item->{itemnumber});
354 while (my $loan = $sth_issue->fetchrow_hashref) {
355 if ($loan->{date_due} and !$loan->{returndate}) {
356 $date_due = $loan->{date_due};
361 $lineCN{holdingbranch} = $item->{holdingbranch};
362 $lineCN{itemcallnumber} = $item->{itemcallnumber};
363 $lineCN{location} = $item->{location};
364 $lineCN{date_due} = format_date($date_due);
365 $notforloan=0 unless ($item->{notforloan} or $item->{wthdrawn} or $item->{itemlost});
366 push @CNresults,\%lineCN;
369 # save the biblio in the final array, with item and item issue status
372 $newline{totitem} = $totalitems;
373 $newline{biblionumber} = $biblionumber;
374 $newline{norequests} = 0;
375 $newline{norequests} = 1 if ($line->{notforloan}); # itemtype not issuable
376 $newline{norequests} = 1 if (!$line->{notforloan} && $notforloan); # itemtype issuable but all items not issuable for instance
377 my @CNresults2= @CNresults;
378 $newline{CN} = \@CNresults2;
379 $newline{'even'} = 1 if $#finalresult % 2 == 0;
380 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
381 $newline{'timestamp'} = format_date($newline{timestamp});
383 push @finalresult, \%newline;
387 my $nbresults = $#result+1;
388 return (\@finalresult, $nbresults);
391 # Creates the SQL Request
394 my ($dbh,$tags, $and_or, $operator, $value) = @_;
396 my $sql_tables; # will contain marc_subfield_table as m1,...
397 my $sql_where1; # will contain the "true" where
398 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
399 my $nb_active=0; # will contain the number of "active" entries. an entry is active if a value is provided.
400 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
402 for(my $i=0; $i<=@$value;$i++) {
406 if (@$operator[$i] eq "start") {
407 $sql_tables .= "marc_subfield_table as m$nb_table,";
408 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]");
410 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
413 } elsif (@$operator[$i] eq "contains") {
414 $sql_tables .= "marc_word as m$nb_table,";
415 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]");
417 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
421 $sql_tables .= "marc_subfield_table as m$nb_table,";
422 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
424 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
429 if (@$operator[$i] eq "start") {
431 $sql_tables .= "marc_subfield_table as m$nb_table,";
432 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]");
434 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
437 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
438 } elsif (@$operator[$i] eq "contains") {
439 if (@$and_or[$i] eq 'and') {
441 $sql_tables .= "marc_word as m$nb_table,";
442 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
444 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
447 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
449 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
451 $sql_where1 .=" and m$nb_table.tagsubfield in (@$tags[$i])";
454 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
458 $sql_tables .= "marc_subfield_table as m$nb_table,";
459 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
461 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
463 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
470 if($sql_where2 ne "(") # some datas added to sql_where2, processing
472 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
475 else # no sql_where2 statement, deleting '('
479 chop $sql_tables; # deletes the trailing ','
480 return ($sql_tables, $sql_where1, $sql_where2);
484 my ($dbh, $bibid, $marcflavour) = @_;
485 my ($mintag, $maxtag);
486 if ($marcflavour eq "MARC21") {
489 } else { # assume unimarc if not marc21
494 my $sth=$dbh->prepare("SELECT subfieldvalue,tag FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
496 $sth->execute($bibid,$mintag,$maxtag);
503 while (my $data=$sth->fetchrow_arrayref) {
504 my $value=$data->[0];
505 my $thistag=$data->[1];
509 if ($thistag ne $tag && $note ne "") {
510 $marcnote = {MARCNOTES => $note,};
511 push @marcnotes, $marcnote;
515 if ($note ne $value) {
516 $note = $note." ".$value;
521 $marcnote = {MARCNOTES => $note};
522 push @marcnotes, $marcnote; #load last tag into array
528 my $marcnotesarray=\@marcnotes;
529 return $marcnotesarray;
533 sub getMARCsubjects {
534 my ($dbh, $bibid, $marcflavour) = @_;
535 my ($mintag, $maxtag);
536 if ($marcflavour eq "MARC21") {
539 } else { # assume unimarc if not marc21
543 my $sth=$dbh->prepare("SELECT subfieldvalue,subfieldcode FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
545 $sth->execute($bibid,$mintag,$maxtag);
552 while (my $data=$sth->fetchrow_arrayref) {
553 my $value = $data->[0];
554 my $subfield = $data->[1];
555 if ($subfield eq "a" && $value ne $subjct) {
556 $marcsubjct = {MARCSUBJCT => $value,};
557 push @marcsubjcts, $marcsubjct;
565 my $marcsubjctsarray=\@marcsubjcts;
566 return $marcsubjctsarray;
567 } #end getMARCsubjects
569 END { } # module clean-up code here (global destructor)
578 Koha Developement team <info@koha.org>