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 push @not_tags, @$tags[$i];
93 push @not_subfields, @$subfields[$i];
94 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
95 push @not_operator, @$operator[$i];
96 push @not_value, $word;
101 push @not_tags, @$tags[$i];
102 push @not_subfields, @$subfields[$i];
103 push @not_and_or, "or"; # as request is negated, finds "foo" or "bar" if final request is NOT "foo" and "bar"
104 push @not_operator, @$operator[$i];
105 push @not_value, @$value[$i];
108 else # NORMAL statements
110 if(@$operator[$i] eq "contains") # if operator is contains, splits the words in separate requests
112 foreach my $word (split(/ /, @$value[$i]))
114 push @normal_tags, @$tags[$i];
115 push @normal_subfields, @$subfields[$i];
116 push @normal_and_or, "and"; # assumes "foo" and "bar" if "foo bar" is entered
117 push @normal_operator, @$operator[$i];
118 push @normal_value, $word;
123 push @normal_tags, @$tags[$i];
124 push @normal_subfields, @$subfields[$i];
125 push @normal_and_or, @$and_or[$i];
126 push @normal_operator, @$operator[$i];
127 push @normal_value, @$value[$i];
132 # Finds the basic results without the NOT requests
133 my ($sql_tables, $sql_where1, $sql_where2) = create_request(\@normal_tags, \@normal_subfields, \@normal_and_or, \@normal_operator, \@normal_value);
136 # warn "HERE (NORMAL)";
138 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
139 # warn("-->select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
141 $sth = $dbh->prepare("select distinct m1.bibid from $sql_tables where $sql_where1");
142 # warn("==>select m1.bibid from $sql_tables where $sql_where1");
148 # Processes the NOT if any and there are results
149 my ($not_sql_tables, $not_sql_where1, $not_sql_where2);
151 if( ($sth->rows) && $any_not ) # some results to tune up and some NOT statements
153 ($not_sql_tables, $not_sql_where1, $not_sql_where2) = create_request(\@not_tags, \@not_subfields, \@not_and_or, \@not_operator, \@not_value);
157 while (my ($bibid) = $sth->fetchrow) {
158 push @tmpresult,$bibid;
162 if ($not_sql_where2) {
163 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
164 # warn("-->select m1.bibid from $not_sql_tables where $not_sql_where2 and ($not_sql_where1)");
166 $sth_not = $dbh->prepare("select distinct m1.bibid from $not_sql_tables where $not_sql_where1");
167 # warn("==>select m1.bibid from $not_sql_tables where $not_sql_where1");
175 while(my $bibid = $sth_not->fetchrow()) {
176 $not_bibids{$bibid} = 1; # populates the hashtable with the bibids matching the NOT statement
179 foreach my $bibid (@tmpresult)
181 if(!$not_bibids{$bibid})
183 push @result, $bibid;
189 else # no NOT statements
191 while (my ($bibid) = $sth->fetchrow) {
196 # we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
197 my $counter = $offset;
198 $sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
199 my @finalresult = ();
200 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
201 $sth->execute($result[$counter]);
202 my ($author,$title) = $sth->fetchrow;
204 $line{bibid}=$result[$counter];
205 $line{author}=$author;
207 push @finalresult, \%line;
211 my $nbresults = $#result + 1;
212 return (\@finalresult, $nbresults);
215 # Creates the SQL Request
218 my ($tags, $subfields, $and_or, $operator, $value) = @_;
220 my $sql_tables; # will contain marc_subfield_table as m1,...
221 my $sql_where1; # will contain the "true" where
222 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
223 my $nb_active=0; # will contain the number of "active" entries. and entry is active is a value is provided.
224 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
226 for(my $i=0; $i<=@$value;$i++) {
230 if (@$operator[$i] eq "start") {
231 $sql_tables .= "marc_subfield_table as m$nb_table,";
232 $sql_where1 .= "(m1.subfieldvalue like '@$value[$i]%'";
234 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
237 } elsif (@$operator[$i] eq "contains") {
238 $sql_tables .= "marc_word as m$nb_table,";
239 $sql_where1 .= "(m1.word like '@$value[$i]%'";
241 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldid='@$subfields[$i]'";
245 $sql_tables .= "marc_subfield_table as m$nb_table,";
246 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] '@$value[$i]' ";
248 $sql_where1 .=" and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]'";
253 if (@$operator[$i] eq "start") {
255 $sql_tables .= "marc_subfield_table as m$nb_table,";
256 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like '@$value[$i]%'";
258 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
261 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
262 } elsif (@$operator[$i] eq "contains") {
263 if (@$and_or[$i] eq 'and') {
265 $sql_tables .= "marc_word as m$nb_table,";
266 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
268 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
271 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
273 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like '@$value[$i]%'";
275 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldid='@$subfields[$i]'";
278 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
282 $sql_tables .= "marc_subfield_table as m$nb_table,";
283 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] '@$value[$i]'";
285 $sql_where1 .=" and m$nb_table.tag=@$tags[$i] and m$nb_table.subfieldcode='@$subfields[$i]'";
287 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
294 if($sql_where2 ne "(") # some datas added to sql_where2, processing
296 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
299 else # no sql_where2 statement, deleting '('
303 chop $sql_tables; # deletes the trailing ','
304 return ($sql_tables, $sql_where1, $sql_where2);
308 END { } # module clean-up code here (global destructor)
317 Koha Developement team <info@koha.org>