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
26 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 # set the version for version checking
33 C4::Search - Functions for searching the Koha MARC catalog
39 my ($count, @results) = catalogsearch();
43 This module provides the searching facilities for the Koha MARC catalog
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.
52 =item my $marcnotesarray = &getMARCnotes($dbh,$bibid,$marcflavour);
54 Returns a reference to an array containing all the notes stored in the MARC database for the given bibid.
55 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
57 =item my $marcsubjctsarray = &getMARCsubjects($dbh,$bibid,$marcflavour);
59 Returns a reference to an array containing all the subjects stored in the MARC database for the given bibid.
60 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
65 @EXPORT = qw(&catalogsearch &findseealso &findsuggestion &getMARCnotes &getMARCsubjects);
67 # make all your functions, whether exported or not;
70 my ($dbh,$values) = @_;
71 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");
73 for(my $i = 0 ; $i <= $#{$values} ; $i++) {
74 if (length(@$values[$i]) >=5) {
75 $sth->execute(@$values[$i],@$values[$i]);
78 while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
79 push @results, "@$values[$i]|$word|$count";
80 # $results{@$values[$i]} = \@resline;
88 my ($dbh, $fields) = @_;
89 my $tagslib = MARCgettagslib ($dbh,1);
90 for (my $i=0;$i<=$#{$fields};$i++) {
91 my ($tag) =substr(@$fields[$i],1,3);
92 my ($subfield) =substr(@$fields[$i],4,1);
93 @$fields[$i].=','.$tagslib->{$tag}->{$subfield}->{seealso} if ($tagslib->{$tag}->{$subfield}->{seealso});
97 # marcsearch : search in the MARC biblio table.
98 # everything is choosen by the user : what to search, the conditions...
101 my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby) = @_;
102 # build the sql request. She will look like :
104 # from marc_subfield_table as m1, marc_subfield_table as m2
105 # where m1.bibid=m2.bibid and
106 # (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
108 # last minute stripping out of stuff
109 # doesn't work @$value =~ s/\'/ /;
110 # @$value = map { $_ =~ s/\'/ /g } @$value;
112 # "Normal" statements
113 my @normal_tags = ();
114 my @normal_and_or = ();
115 my @normal_operator = ();
116 my @normal_value = ();
117 # Extracts the NOT statements from the list of statements
120 my @not_operator = ();
123 $orderby = "biblio.title" unless $orderby;
125 #last minute stripping out of ' and ,
126 foreach $_ (@$value) {
131 for(my $i = 0 ; $i <= $#{$value} ; $i++)
134 @$value[$i] =~ s/\*/%/g;
135 # remove % at the beginning
136 @$value[$i] =~ s/^%//g;
137 if(@$excluding[$i]) # NOT statements
140 if(@$operator[$i] eq "contains")
142 foreach my $word (split(/ /, @$value[$i])) # if operator is contains, splits the words in separate requests
144 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
145 # warn "word : $word";
146 $word =~ s/%//g unless length($word)>4;
147 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
148 push @not_tags, @$tags[$i];
149 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
150 push @not_operator, @$operator[$i];
151 push @not_value, $word;
157 push @not_tags, @$tags[$i];
158 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
159 push @not_operator, @$operator[$i];
160 push @not_value, @$value[$i];
163 else # NORMAL statements
165 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
167 foreach my $word (split(/ /, @$value[$i]))
169 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
170 # warn "word : $word";
171 $word =~ s/%//g unless length($word)>4;
172 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
173 my $tag = substr(@$tags[$i],0,3);
174 my $subf = substr(@$tags[$i],3,1);
175 push @normal_tags, @$tags[$i];
176 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
177 push @normal_operator, @$operator[$i];
178 push @normal_value, $word;
184 push @normal_tags, @$tags[$i];
185 push @normal_and_or, @$and_or[$i];
186 push @normal_operator, @$operator[$i];
187 push @normal_value, @$value[$i];
192 # Finds the basic results without the NOT requests
193 my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
197 $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");
198 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";
200 $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");
201 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";
206 # Processes the NOT if any and there are results
207 my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
209 if( ($sth->rows) && $any_not ) # some results to tune up and some NOT statements
211 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request($dbh,\@not_tags, \@not_and_or, \@not_operator, \@not_value);
215 while (my ($bibid) = $sth->fetchrow) {
216 push @tmpresult,$bibid;
219 warn "NOT : select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)";
220 if ($not_sql_where2) {
221 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
223 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
230 while(my $bibid = $sth_not->fetchrow()) {
231 $not_bibids{$bibid} = 1; # populates the hashtable with the bibids matching the NOT statement
234 foreach my $bibid (@tmpresult)
236 if(!$not_bibids{$bibid})
238 push @result, $bibid;
244 else # no NOT statements
246 while (my ($bibid) = $sth->fetchrow) {
251 # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
252 my $counter = $offset;
253 # HINT : biblionumber as bn is important. The hash is fills biblionumber with items.biblionumber.
254 # so if you dont' has an item, you get a not nice epty value.
255 $sth = $dbh->prepare("SELECT biblio.biblionumber as bn,biblio.*, biblioitems.*, items.*,marc_biblio.bibid
256 FROM biblio, marc_biblio
257 LEFT JOIN items on items.biblionumber = biblio.biblionumber
258 LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
259 WHERE biblio.biblionumber = marc_biblio.biblionumber AND bibid = ?
260 GROUP BY items.biblionumber, items.holdingbranch, items.itemcallnumber");
261 my @finalresult = ();
265 # my ($biblionumber,$author,$title,$holdingbranch, $itemcallnumber, $bibid);
266 my ($oldbibid, $oldauthor, $oldtitle);
267 # parse all biblios between start & end.
268 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
269 # warn " bibid :".$result[$counter];
270 # search & parse all items & note itemcallnumber
271 $sth->execute($result[$counter]);
273 my $line = $sth->fetchrow_hashref;
274 my $oldbiblionumber=$line->{bn};
275 $continue=0 unless $line->{bn};
277 # parse the result, putting holdingbranch & itemcallnumber in separate array
278 # then all other fields in the main array
279 if ($oldbiblionumber && ($oldbiblionumber ne $line->{bn}) && $oldline) {
280 # warn "HERE $oldbiblionumber && ($oldbiblionumber ne $line->{bn}) && $oldline";
282 %newline = %$oldline;
283 $newline{totitem} = $totalitems;
284 $newline{biblionumber} = $oldbiblionumber;
285 my @CNresults2= @CNresults;
286 $newline{CN} = \@CNresults2;
287 $newline{'even'} = 1 if $#finalresult % 2 == 0;
288 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
290 push @finalresult, \%newline;
293 $continue=0 unless $line->{bn};
295 # warn "IN ".$line->{bn}."<<" if($line->{bn});
296 $oldbiblionumber = $line->{bn};
297 $totalitems++ if ($line->{holdingbranch});
299 # item callnumber & branch
301 $lineCN{holdingbranch} = $line->{holdingbranch};
302 $lineCN{itemcallnumber} = $line->{itemcallnumber};
303 push @CNresults,\%lineCN;
304 $line = $sth->fetchrow_hashref;
309 #add the last line, that is not reached byt the loop / if ($oldbiblionumber...)
310 my $nbresults = $#result+1;
311 return (\@finalresult, $nbresults);
314 # Creates the SQL Request
317 my ($dbh,$tags, $and_or, $operator, $value) = @_;
319 my $sql_tables; # will contain marc_subfield_table as m1,...
320 my $sql_where1; # will contain the "true" where
321 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
322 my $nb_active=0; # will contain the number of "active" entries. an entry is active if a value is provided.
323 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
325 for(my $i=0; $i<=@$value;$i++) {
329 if (@$operator[$i] eq "start") {
330 $sql_tables .= "marc_subfield_table as m$nb_table,";
331 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]");
333 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
336 } elsif (@$operator[$i] eq "contains") {
337 $sql_tables .= "marc_word as m$nb_table,";
338 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]");
340 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
344 $sql_tables .= "marc_subfield_table as m$nb_table,";
345 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
347 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
352 if (@$operator[$i] eq "start") {
354 $sql_tables .= "marc_subfield_table as m$nb_table,";
355 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]");
357 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
360 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
361 } elsif (@$operator[$i] eq "contains") {
362 if (@$and_or[$i] eq 'and') {
364 $sql_tables .= "marc_word as m$nb_table,";
365 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
367 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
370 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
372 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
374 $sql_where1 .=" and m$nb_table.tagsubfield in (@$tags[$i])";
377 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
381 $sql_tables .= "marc_subfield_table as m$nb_table,";
382 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
384 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
386 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
393 if($sql_where2 ne "(") # some datas added to sql_where2, processing
395 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
398 else # no sql_where2 statement, deleting '('
402 chop $sql_tables; # deletes the trailing ','
403 return ($sql_tables, $sql_where1, $sql_where2);
407 my ($dbh, $bibid, $marcflavour) = @_;
408 my ($mintag, $maxtag);
409 if ($marcflavour eq "MARC21") {
412 } else { # assume unimarc if not marc21
417 my $sth=$dbh->prepare("SELECT subfieldvalue,tag FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
419 $sth->execute($bibid,$mintag,$maxtag);
426 while (my $data=$sth->fetchrow_arrayref) {
427 my $value=$data->[0];
428 my $thistag=$data->[1];
432 if ($thistag ne $tag && $note ne "") {
433 $marcnote = {MARCNOTES => $note,};
434 push @marcnotes, $marcnote;
438 if ($note ne $value) {
439 $note = $note." ".$value;
444 $marcnote = {MARCNOTES => $note};
445 push @marcnotes, $marcnote; #load last tag into array
451 my $marcnotesarray=\@marcnotes;
452 return $marcnotesarray;
456 sub getMARCsubjects {
457 my ($dbh, $bibid, $marcflavour) = @_;
458 my ($mintag, $maxtag);
459 if ($marcflavour eq "MARC21") {
462 } else { # assume unimarc if not marc21
466 my $sth=$dbh->prepare("SELECT subfieldvalue,subfieldcode FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
468 $sth->execute($bibid,$mintag,$maxtag);
475 while (my $data=$sth->fetchrow_arrayref) {
476 my $value = $data->[0];
477 my $subfield = $data->[1];
478 if ($subfield eq "a" && $value ne $subjct) {
479 $marcsubjct = {MARCSUBJCT => $value,};
480 push @marcsubjcts, $marcsubjct;
488 my $marcsubjctsarray=\@marcsubjcts;
489 return $marcsubjctsarray;
490 } #end getMARCsubjects
492 END { } # module clean-up code here (global destructor)
501 Koha Developement team <info@koha.org>