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.
55 @EXPORT = qw(&catalogsearch &findseealso &findsuggestion);
57 # make all your functions, whether exported or not;
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");
63 for(my $i = 0 ; $i <= $#{$values} ; $i++) {
64 if (length(@$values[$i]) >=5) {
65 $sth->execute(@$values[$i],@$values[$i]);
68 while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
69 push @results, "@$values[$i]|$word|$count";
70 # $results{@$values[$i]} = \@resline;
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});
87 # marcsearch : search in the MARC biblio table.
88 # everything is choosen by the user : what to search, the conditions...
91 my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby) = @_;
92 # build the sql request. She will look like :
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%")
98 # last minute stripping out of stuff
99 # doesn't work @$value =~ s/\'/ /;
100 # @$value = map { $_ =~ s/\'/ /g } @$value;
102 # "Normal" statements
103 my @normal_tags = ();
104 my @normal_and_or = ();
105 my @normal_operator = ();
106 my @normal_value = ();
107 # Extracts the NOT statements from the list of statements
110 my @not_operator = ();
113 $orderby = "biblio.title" unless $orderby;
115 #last minute stripping out of ' and ,
116 foreach $_ (@$value) {
121 for(my $i = 0 ; $i <= $#{$value} ; $i++)
124 @$value[$i] =~ s/\*/%/g;
125 if(@$excluding[$i]) # NOT statements
128 if(@$operator[$i] eq "contains")
130 foreach my $word (split(/ /, @$value[$i])) # if operator is contains, splits the words in separate requests
132 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
134 $word =~ s/%//g unless length($word)>4;
135 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
136 push @not_tags, @$tags[$i];
137 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
138 push @not_operator, @$operator[$i];
139 push @not_value, $word;
145 push @not_tags, @$tags[$i];
146 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
147 push @not_operator, @$operator[$i];
148 push @not_value, @$value[$i];
151 else # NORMAL statements
153 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
155 foreach my $word (split(/ /, @$value[$i]))
157 # remove the "%" for small word (3 letters. (note : the >4 is due to the % at the end)
159 $word =~ s/%//g unless length($word)>4;
160 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
161 my $tag = substr(@$tags[$i],0,3);
162 my $subf = substr(@$tags[$i],3,1);
163 push @normal_tags, @$tags[$i];
164 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
165 push @normal_operator, @$operator[$i];
166 push @normal_value, $word;
172 push @normal_tags, @$tags[$i];
173 push @normal_and_or, @$and_or[$i];
174 push @normal_operator, @$operator[$i];
175 push @normal_value, @$value[$i];
180 # Finds the basic results without the NOT requests
181 my ($sql_tables, $sql_where1, $sql_where2) = create_request($dbh,\@normal_tags, \@normal_and_or, \@normal_operator, \@normal_value);
185 $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");
186 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";
188 $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");
189 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";
194 # Processes the NOT if any and there are results
195 my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
197 if( ($sth->rows) && $any_not ) # some results to tune up and some NOT statements
199 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request($dbh,\@not_tags, \@not_and_or, \@not_operator, \@not_value);
203 while (my ($bibid) = $sth->fetchrow) {
204 push @tmpresult,$bibid;
207 warn "NOT : select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)";
208 if ($not_sql_where2) {
209 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
211 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
218 while(my $bibid = $sth_not->fetchrow()) {
219 $not_bibids{$bibid} = 1; # populates the hashtable with the bibids matching the NOT statement
222 foreach my $bibid (@tmpresult)
224 if(!$not_bibids{$bibid})
226 push @result, $bibid;
232 else # no NOT statements
234 while (my ($bibid) = $sth->fetchrow) {
239 # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
240 my $counter = $offset;
241 $sth = $dbh->prepare("SELECT biblio.*, biblioitems.*, items.*,marc_biblio.bibid
242 FROM biblio, marc_biblio
243 LEFT JOIN items on items.biblionumber = biblio.biblionumber
244 LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
245 WHERE biblio.biblionumber = marc_biblio.biblionumber AND bibid = ?
246 GROUP BY items.biblionumber, items.holdingbranch, items.itemcallnumber");
247 my @finalresult = ();
249 my $oldbiblionumber=0;
252 # my ($biblionumber,$author,$title,$holdingbranch, $itemcallnumber, $bibid);
253 my ($oldbibid, $oldauthor, $oldtitle,$oldbiblionumber);
254 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
255 $sth->execute($result[$counter]);
256 while (my $line = $sth->fetchrow_hashref) {
257 # parse the result, putting holdingbranch & itemcallnumber in separate array
258 # then all other fields in the main array
259 if ($oldbiblionumber && ($oldbiblionumber ne $line->{biblionumber})) {
261 %newline = %$oldline;
262 $newline{totitem} = $totalitems;
263 $newline{biblionumber} = $oldbiblionumber;
264 my @CNresults2= @CNresults;
265 $newline{CN} = \@CNresults2;
266 $newline{'even'} = 1 if $#finalresult % 2 == 0;
267 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
269 push @finalresult, \%newline;
272 $oldbiblionumber = $line->{biblionumber};
273 $totalitems++ if ($line->{holdingbranch});
275 # item callnumber & branch
277 $lineCN{holdingbranch} = $line->{holdingbranch};
278 $lineCN{itemcallnumber} = $line->{itemcallnumber};
279 push @CNresults,\%lineCN;
283 # add the last line, that is not reached byt the loop / if ($oldbiblionumber...)
286 %newline = %$oldline;
287 $newline{totitem} = $totalitems;
288 $newline{biblionumber} = $oldbiblionumber;
289 my @CNresults2= @CNresults;
290 $newline{CN} = \@CNresults2;
291 $newline{'even'} = 1 if $counter % 2 == 0;
292 $newline{'odd'} = 1 if $counter % 2 == 1;
294 my @CNresults2= @CNresults;
295 $newline{CN} = \@CNresults2;
297 push @finalresult, \%newline;
299 my $nbresults = $#result + 1;
300 return (\@finalresult, $nbresults);
303 # Creates the SQL Request
306 my ($dbh,$tags, $and_or, $operator, $value) = @_;
308 my $sql_tables; # will contain marc_subfield_table as m1,...
309 my $sql_where1; # will contain the "true" where
310 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
311 my $nb_active=0; # will contain the number of "active" entries. an entry is active if a value is provided.
312 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
314 for(my $i=0; $i<=@$value;$i++) {
318 if (@$operator[$i] eq "start") {
319 $sql_tables .= "marc_subfield_table as m$nb_table,";
320 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]");
322 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
325 } elsif (@$operator[$i] eq "contains") {
326 $sql_tables .= "marc_word as m$nb_table,";
327 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]");
329 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
333 $sql_tables .= "marc_subfield_table as m$nb_table,";
334 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
336 $sql_where1 .=" and m1.tag+m1.subfieldcode in (@$tags[$i])";
341 if (@$operator[$i] eq "start") {
343 $sql_tables .= "marc_subfield_table as m$nb_table,";
344 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]");
346 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
349 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
350 } elsif (@$operator[$i] eq "contains") {
351 if (@$and_or[$i] eq 'and') {
353 $sql_tables .= "marc_word as m$nb_table,";
354 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
356 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
359 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
361 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
363 $sql_where1 .=" and m$nb_table.tagsubfield in (@$tags[$i])";
366 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
370 $sql_tables .= "marc_subfield_table as m$nb_table,";
371 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
373 $sql_where1 .=" and m$nb_table.tag+m$nb_table.subfieldcode in (@$tags[$i])";
375 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
382 if($sql_where2 ne "(") # some datas added to sql_where2, processing
384 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
387 else # no sql_where2 statement, deleting '('
391 chop $sql_tables; # deletes the trailing ','
392 return ($sql_tables, $sql_where1, $sql_where2);
396 END { } # module clean-up code here (global destructor)
405 Koha Developement team <info@koha.org>