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
25 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
27 # set the version for version checking
32 C4::Search - Functions for searching the Koha MARC catalog
38 my ($count, @results) = catalogsearch();
42 This module provides the searching facilities for the Koha MARC catalog
44 C<&catalogsearch> is a front end to all the other searches. Depending
45 on what is passed to it, it calls the appropriate search function.
54 @EXPORT = qw(&catalogsearch);
55 # make all your functions, whether exported or not;
57 # marcsearch : search in the MARC biblio table.
58 # everything is choosen by the user : what to search, the conditions...
61 my ($dbh, $tags, $subfields, $and_or, $excluding, $operator, $value, $offset,$length) = @_;
62 # build the sql request. She will look like :
64 # from marc_subfield_table as m1, marc_subfield_table as m2
65 # where m1.bibid=m2.bibid and
66 # (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
70 my @normal_subfields = ();
71 my @normal_and_or = ();
72 my @normal_operator = ();
73 my @normal_value = ();
75 # Extracts the NOT statements from the list of statements
77 my @not_subfields = ();
79 my @not_operator = ();
83 for(my $i = 0 ; $i <= $#{$value} ; $i++)
85 if(@$excluding[$i]) # NOT statements
88 if(@$operator[$i] eq "contains")
90 foreach my $word (split(/ /, @$value[$i])) # if operator is contains, splits the words in separate requests
92 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
93 push @not_tags, @$tags[$i];
94 push @not_subfields, @$subfields[$i];
95 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
96 push @not_operator, @$operator[$i];
97 push @not_value, $word;
103 push @not_tags, @$tags[$i];
104 push @not_subfields, @$subfields[$i];
105 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
106 push @not_operator, @$operator[$i];
107 push @not_value, @$value[$i];
110 else # NORMAL statements
112 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
114 foreach my $word (split(/ /, @$value[$i]))
116 unless (C4::Context->stopwords->{uc($word)}) { #it's NOT a stopword => use it. Otherwise, ignore
117 push @normal_tags, @$tags[$i];
118 push @normal_subfields, @$subfields[$i];
119 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
120 push @normal_operator, @$operator[$i];
121 push @normal_value, $word;
127 push @normal_tags, @$tags[$i];
128 push @normal_subfields, @$subfields[$i];
129 push @normal_and_or, @$and_or[$i];
130 push @normal_operator, @$operator[$i];
131 push @normal_value, @$value[$i];
136 # Finds the basic results without the NOT requests
137 my ($sql_tables, $sql_where1, $sql_where2) = create_request(\@normal_tags, \@normal_subfields, \@normal_and_or, \@normal_operator, \@normal_value);
140 # warn "HERE (NORMAL)";
142 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
143 # warn("-->select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
145 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where1");
146 # warn("==>select m1.bibid from $sql_tables where $sql_where1");
152 # Processes the NOT if any and there are results
153 my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
155 if( ($sth->rows) && $any_not ) # some results to tune up and some NOT statements
157 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request(\@not_tags, \@not_subfields, \@not_and_or, \@not_operator, \@not_value);
161 while (my ($bibid) = $sth->fetchrow) {
162 push @tmpresult,$bibid;
166 if ($not_sql_where2) {
167 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
168 # warn("-->select m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
170 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
171 # warn("==>select m1.bibid from $not_sql_tables where $not_sql_where1");
179 while(my $bibid = $sth_not->fetchrow()) {
180 $not_bibids{$bibid} = 1; # populates the hashtable with the bibids matching the NOT statement
183 foreach my $bibid (@tmpresult)
185 if(!$not_bibids{$bibid})
187 push @result, $bibid;
193 else # no NOT statements
195 while (my ($bibid) = $sth->fetchrow) {
200 # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
201 my $counter = $offset;
202 $sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
203 my @finalresult = ();
204 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
205 $sth->execute($result[$counter]);
206 my ($author,$title) = $sth->fetchrow;
208 $line{bibid}=$result[$counter];
209 $line{author}=$author;
211 push @finalresult, \%line;
215 my $nbresults = $#result + 1;
216 return (\@finalresult, $nbresults);
219 # Creates the SQL Request
222 my ($tags, $subfields, $and_or, $operator, $value) = @_;
224 my $sql_tables; # will contain marc_subfield_table as m1,...
225 my $sql_where1; # will contain the "true" where
226 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
227 my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
228 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
230 for(my $i=0; $i<=@$value;$i++) {
234 if (@$operator[$i] eq "start") {
235 $sql_tables .= "marc_subfield_table as m$nb_table,";
236 $sql_where1 .= "(m1.subfieldvalue like '@$value[$i]%'";
238 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
241 } elsif (@$operator[$i] eq "contains") {
242 $sql_tables .= "marc_word as m$nb_table,";
243 $sql_where1 .= "(m1.word like '@$value[$i]%'";
245 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldid='@$subfields[$i]'";
249 $sql_tables .= "marc_subfield_table as m$nb_table,";
250 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] '@$value[$i]' ";
252 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
257 if (@$operator[$i] eq "start") {
259 $sql_tables .= "marc_subfield_table as m$nb_table,";
260 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like '@$value[$i]%'";
262 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
265 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
266 } elsif (@$operator[$i] eq "contains") {
267 if (@$and_or[$i] eq 'and') {
269 $sql_tables .= "marc_word as m$nb_table,";
270 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
272 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
275 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
277 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
279 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
282 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
286 $sql_tables .= "marc_subfield_table as m$nb_table,";
287 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] '@$value[$i]'";
289 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
291 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
298 if($sql_where2 ne "(") # some datas added to sql_where2, processing
300 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
303 else # no sql_where2 statement, deleting '('
307 chop $sql_tables; # deletes the trailing ','
308 return ($sql_tables, $sql_where1, $sql_where2);
312 END { } # module clean-up code here (global destructor)
321 Koha Developement team <info@koha.org>