1 package C4::SearchMarcTest;
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
41 A COPY of official SearchMarc, with some tests for inverted index table
42 works only with 1 MARC tag/subfield
47 @EXPORT = qw(&catalogsearch &findseealso &findsuggestion &getMARCnotes &getMARCsubjects);
49 =head1 findsuggestion($dbh,$values);
51 =head2 $dbh is a link to the DB handler.
54 my $dbh =C4::Context->dbh;
56 =head2 $values is a word
58 Searches words with the same soundex, ordered by frequency of use.
59 Useful to suggest other searches to the users.
64 my ($dbh,$values) = @_;
65 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");
67 for(my $i = 0 ; $i <= $#{$values} ; $i++) {
68 if (length(@$values[$i]) >=5) {
69 $sth->execute(@$values[$i],@$values[$i]);
72 while ((my ($count,$word) = $sth->fetchrow) and $resfound <=10) {
73 push @results, "@$values[$i]|$word|$count";
74 # $results{@$values[$i]} = \@resline;
82 =head1 findseealso($dbh,$fields);
84 =head2 $dbh is a link to the DB handler.
87 my $dbh =C4::Context->dbh;
89 =head2 $fields is a reference to the fields array
91 This function modify the @$fields array and add related fields to search on.
96 my ($dbh, $fields) = @_;
97 my $tagslib = MARCgettagslib ($dbh,1);
98 for (my $i=0;$i<=$#{$fields};$i++) {
99 my ($tag) =substr(@$fields[$i],1,3);
100 my ($subfield) =substr(@$fields[$i],4,1);
101 @$fields[$i].=','.$tagslib->{$tag}->{$subfield}->{seealso} if ($tagslib->{$tag}->{$subfield}->{seealso});
105 =head1 my ($count, @results) = catalogsearch($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby);
107 =head2 $dbh is a link to the DB handler.
110 my $dbh =C4::Context->dbh;
112 $tags,$and_or, $excluding, $operator, $value are references to array
116 contains the list of tags+subfields (for example : $@tags[0] = '200a')
117 A field can be a list of fields : '200f','700a','700b','701a','701b'
123 contains a list of strings containing and or or. The 1st value is useless.
127 contains 0 or 1. If 1, then the request is negated.
131 contains contains,=,start,>,>=,<,<= the = and start work on the complete subfield. The contains operator works on every word in the subfield.
134 contains home, search home anywhere.
135 = home, search a string being home.
139 contains the value to search
140 If it contains a * or a %, then the search is partial.
142 =head2 $offset and $length
144 returns $length results, beginning at $offset
148 define the field used to order the request. Any field in the biblio/biblioitem tables can be used. DESC is possible too
150 (for example title, title DESC,...)
154 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.
158 =head2 my $marcnotesarray = &getMARCnotes($dbh,$bibid,$marcflavour);
160 Returns a reference to an array containing all the notes stored in the MARC database for the given bibid.
161 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
163 =head2 my $marcsubjctsarray = &getMARCsubjects($dbh,$bibid,$marcflavour);
165 Returns a reference to an array containing all the subjects stored in the MARC database for the given bibid.
166 $marcflavour ("MARC21" or "UNIMARC") determines which tags are used for retrieving subjects.
171 my ($dbh, $tags, $and_or, $excluding, $operator, $value, $offset,$length,$orderby,$desc_or_asc) = @_;
172 # "Normal" statements
173 my @normal_tags = ();
174 my @normal_and_or = ();
175 my @normal_operator = ();
176 my @normal_value = ();
177 # Extracts the NOT statements from the list of statements
180 my @not_operator = ();
183 $orderby = "biblio.title" unless $orderby;
184 $desc_or_asc = "ASC" unless $desc_or_asc;
186 # the item.notforloan contains an integer. Every value <>0 means "book unavailable for loan".
187 # but each library can have it's own table of meaning for each value. Get them
188 # 1st search if there is a list of authorised values connected to items.notforloan
189 my $sth = $dbh->prepare('select authorised_value from marc_subfield_structure where kohafield="items.notforloan"');
191 my %notforloanstatus;
192 my ($authorised_valuecode) = $sth->fetchrow;
193 if ($authorised_valuecode) {
194 $sth = $dbh->prepare("select authorised_value,lib from authorised_values where category=?");
195 $sth->execute($authorised_valuecode);
196 while (my ($authorised_value,$lib) = $sth->fetchrow) {
197 $notforloanstatus{$authorised_value} = $lib?$lib:$authorised_value;
202 # marc_T_word PROOF OF CONCEPT BEGINNING
204 # fixme : only do a search on "contains every word"
206 # - begins or is equal to
209 # the global array result.
211 for(my $i = 0 ; $i <= $#{$value} ; $i++)
214 @$value[$i] =~ s/\*/%/g;
215 # remove % at the beginning
216 @$value[$i] =~ s/^%//g;
217 @$value[$i] =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\}|\/)/ /g if @$operator[$i] eq "contains";
218 foreach my $word (split(/ /, @$value[$i])) # if operator is contains, splits the words in separate requests
220 # the array from this word
222 my $Tquery = $dbh->prepare("select tagsubfield,usedin from marc_Tword where tagsubfield in (@$tags[$i]) and word like ?");
223 $Tquery->execute($word);
224 warn "EXECUTING select tagsubfield,usedin from marc_Tword where tagsubfield in (@$tags[$i]) and word like $word";
225 # get the list of biblionumber - title
226 while (my ($tagsubfield,$usedin) = $Tquery->fetchrow) {
227 # warn "$word with ".@$tags[$i]." used in $usedin";
228 # split it in an array
229 my @lines = split /,/,$usedin;
230 # and copy it to an hash.
231 foreach my $line (@lines) {
232 # warn "PUSHING $line" if $line; # the if $line avoid pushing the 1st entry, that is empty (usedin begins by a ,)
233 push @thiswordresults, $line if $line;
236 # now, as it's a AND, merge %results & %thiswordresults in 1 hash
237 @result = @thiswordresults if $#result<0; #for the 1st loop, fill the global array
240 my $x; # temp variable
241 foreach $x (@result, @thiswordresults) {
242 $union{$x}++ && $intersect{$x}++;
244 @result = keys %intersect;
248 # we have biblionumber array.
250 my @result = sort @result;
252 #Now, loads title and author from [offset] to [offset]+[length]
253 my $counter = $offset;
254 # HINT : biblionumber as bn is important. The hash is fills biblionumber with items.biblionumber.
255 # so if you dont' has an item, you get a not nice empty value.
256 $sth = $dbh->prepare("SELECT biblio.biblionumber as bn,biblioitems.*,biblio.*, marc_biblio.bibid,itemtypes.notforloan,itemtypes.description
257 FROM biblio, marc_biblio
258 LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber
259 LEFT JOIN itemtypes on itemtypes.itemtype=biblioitems.itemtype
260 WHERE biblio.biblionumber = marc_biblio.biblionumber AND biblio.biblionumber = ?");
263 # marc_Tword Proof of concept
267 my $sth_subtitle = $dbh->prepare("SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?"); # Added BY JF for Subtitles
268 my @finalresult = ();
272 my ($oldbibid, $oldauthor, $oldtitle);
273 my $sth_itemCN = $dbh->prepare("select items.* from items where biblionumber=? and (itemlost = 0 or itemlost is NULL)");
274 my $sth_issue = $dbh->prepare("select date_due,returndate from issues where itemnumber=?");
275 # parse all biblios between start & end.
276 warn "RESULT SIZE : ".$#result;
277 while (($counter <= $#result) && ($counter <= ($offset + $length))) {
278 # search & parse all items & note itemcallnumber
279 # 1st, get the biblionumber
280 $result[$counter] =~ /(.*)-(.*)/;
282 warn "EXECUTING SELECT biblio.biblionumber as bn,biblioitems.*,biblio.*, marc_biblio.bibid,itemtypes.notforloan,itemtypes.description FROM biblio, marc_biblio LEFT JOIN biblioitems on biblio.biblionumber = biblioitems.biblionumber LEFT JOIN itemtypes on itemtypes.itemtype=biblioitems.itemtype WHERE biblio.biblionumber = marc_biblio.biblionumber AND biblio.biblionumber = $2";
284 my $line = $sth->fetchrow_hashref;
285 my $biblionumber=$line->{bn};
286 # Return subtitles first ADDED BY JF
287 $sth_subtitle->execute($biblionumber);
288 warn "EXECUTING SELECT subtitle FROM bibliosubtitle WHERE biblionumber=$biblionumber";
289 my $subtitle_here.= $sth_subtitle->fetchrow." ";
291 $subtitle = $subtitle_here;
294 # $continue=0 unless $line->{bn};
295 # my $lastitemnumber;
296 $sth_itemCN->execute($biblionumber);
297 warn "EXECUTING itemCN select items.* from items where biblionumber=$biblionumber and (itemlost = 0 or itemlost is NULL)";
299 my $notforloan=1; # to see if there is at least 1 item that can be issued
300 while (my $item = $sth_itemCN->fetchrow_hashref) {
301 # parse the result, putting holdingbranch & itemcallnumber in separate array
302 # then all other fields in the main array
304 # search if item is on loan
306 $sth_issue->execute($item->{itemnumber});
307 warn "EXECUTING ISSUES select date_due,returndate from issues where itemnumber=".$item->{itemnumber};
308 while (my $loan = $sth_issue->fetchrow_hashref) {
309 if ($loan->{date_due} and !$loan->{returndate}) {
310 $date_due = $loan->{date_due};
315 $lineCN{holdingbranch} = $item->{holdingbranch};
316 $lineCN{itemcallnumber} = $item->{itemcallnumber};
317 $lineCN{location} = $item->{location};
318 $lineCN{date_due} = format_date($date_due);
319 $lineCN{notforloan} = $notforloanstatus{$line->{notforloan}} if ($line->{notforloan}); # setting not forloan if itemtype is not for loan
320 $lineCN{notforloan} = $notforloanstatus{$item->{notforloan}} if ($item->{notforloan}); # setting not forloan it this item is not for loan
321 $notforloan=0 unless ($item->{notforloan} or $item->{wthdrawn} or $item->{itemlost});
322 push @CNresults,\%lineCN;
325 # save the biblio in the final array, with item and item issue status
328 $newline{totitem} = $totalitems;
329 # if $totalitems == 0, check if it's being ordered.
330 if ($totalitems == 0) {
331 my $sth = $dbh->prepare("select count(*) from aqorders where biblionumber=? and datecancellationprinted is NULL");
332 $sth->execute($biblionumber);
333 warn "EXECUTING select count(*) from aqorders where biblionumber=$biblionumber and datecancellationprinted is NULL";
334 my ($ordered) = $sth->fetchrow;
335 $newline{onorder} = 1 if $ordered;
337 $newline{biblionumber} = $biblionumber;
338 $newline{norequests} = 0;
339 $newline{norequests} = 1 if ($line->{notforloan}); # itemtype not issuable
340 $newline{norequests} = 1 if (!$line->{notforloan} && $notforloan); # itemtype issuable but all items not issuable for instance
341 $newline{subtitle} = $subtitle; # put the subtitle in ADDED BY JF
343 my @CNresults2= @CNresults;
344 $newline{CN} = \@CNresults2;
345 $newline{'even'} = 1 if $#finalresult % 2 == 0;
346 $newline{'odd'} = 1 if $#finalresult % 2 == 1;
347 $newline{'timestamp'} = format_date($newline{timestamp});
349 push @finalresult, \%newline;
353 my $nbresults = $#result+1;
354 return (\@finalresult, $nbresults);
357 # Creates the SQL Request
360 my ($dbh,$tags, $and_or, $operator, $value) = @_;
362 my $sql_tables; # will contain marc_subfield_table as m1,...
363 my $sql_where1; # will contain the "true" where
364 my $sql_where2 = "("; # will contain m1.bibid=m2.bibid
365 my $nb_active=0; # will contain the number of "active" entries. an entry is active if a value is provided.
366 my $nb_table=1; # will contain the number of table. ++ on each entry EXCEPT when an OR is provided.
368 my $maxloop=8; # the maximum number of words to avoid a too complex search.
369 $maxloop = @$value if @$value<$maxloop;
371 for(my $i=0; $i<=$maxloop;$i++) {
375 if (@$operator[$i] eq "start") {
376 $sql_tables .= "marc_subfield_table as m$nb_table,";
377 $sql_where1 .= "(m1.subfieldvalue like ".$dbh->quote("@$value[$i]%");
379 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
382 } elsif (@$operator[$i] eq "contains") {
383 $sql_tables .= "marc_word as m$nb_table,";
384 $sql_where1 .= "(m1.word like ".$dbh->quote("@$value[$i]");
386 $sql_where1 .=" and m1.tagsubfield in (@$tags[$i])";
390 $sql_tables .= "marc_subfield_table as m$nb_table,";
391 $sql_where1 .= "(m1.subfieldvalue @$operator[$i] ".$dbh->quote("@$value[$i]");
393 $sql_where1 .=" and concat(m1.tag,m1.subfieldcode) in (@$tags[$i])";
398 if (@$operator[$i] eq "start") {
400 $sql_tables .= "marc_subfield_table as m$nb_table,";
401 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue like ".$dbh->quote("@$value[$i]%");
403 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
406 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
407 } elsif (@$operator[$i] eq "contains") {
408 if (@$and_or[$i] eq 'and') {
410 $sql_tables .= "marc_word as m$nb_table,";
411 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
413 $sql_where1 .=" and m$nb_table.tagsubfield in(@$tags[$i])";
416 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
418 $sql_where1 .= "@$and_or[$i] (m$nb_table.word like ".$dbh->quote("@$value[$i]");
420 $sql_where1 .=" and m$nb_table.tagsubfield in (@$tags[$i])";
423 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
427 $sql_tables .= "marc_subfield_table as m$nb_table,";
428 $sql_where1 .= "@$and_or[$i] (m$nb_table.subfieldvalue @$operator[$i] ".$dbh->quote(@$value[$i]);
430 $sql_where1 .=" and concat(m$nb_table.tag,m$nb_table.subfieldcode) in (@$tags[$i])";
432 $sql_where2 .= "m1.bibid=m$nb_table.bibid and ";
439 if($sql_where2 ne "(") # some datas added to sql_where2, processing
441 $sql_where2 = substr($sql_where2, 0, (length($sql_where2)-5)); # deletes the trailing ' and '
444 else # no sql_where2 statement, deleting '('
448 chop $sql_tables; # deletes the trailing ','
449 return ($sql_tables, $sql_where1, $sql_where2);
453 my ($dbh, $bibid, $marcflavour) = @_;
454 my ($mintag, $maxtag);
455 if ($marcflavour eq "MARC21") {
458 } else { # assume unimarc if not marc21
463 my $sth=$dbh->prepare("SELECT subfieldvalue,tag FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
465 $sth->execute($bibid,$mintag,$maxtag);
472 while (my $data=$sth->fetchrow_arrayref) {
473 my $value=$data->[0];
474 my $thistag=$data->[1];
478 if ($thistag ne $tag && $note ne "") {
479 $marcnote = {marcnote => $note,};
480 push @marcnotes, $marcnote;
484 if ($note ne $value) {
485 $note = $note." ".$value;
490 $marcnote = {marcnote => $note};
491 push @marcnotes, $marcnote; #load last tag into array
497 my $marcnotesarray=\@marcnotes;
498 return $marcnotesarray;
502 sub getMARCsubjects {
503 my ($dbh, $bibid, $marcflavour) = @_;
504 my ($mintag, $maxtag);
505 if ($marcflavour eq "MARC21") {
508 } else { # assume unimarc if not marc21
512 my $sth=$dbh->prepare("SELECT subfieldvalue,subfieldcode FROM marc_subfield_table WHERE bibid=? AND tag BETWEEN ? AND ? ORDER BY tagorder");
514 $sth->execute($bibid,$mintag,$maxtag);
521 while (my $data=$sth->fetchrow_arrayref) {
522 my $value = $data->[0];
523 my $subfield = $data->[1];
524 if ($subfield eq "a" && $value ne $subjct) {
525 $marcsubjct = {MARCSUBJCT => $value,};
526 push @marcsubjcts, $marcsubjct;
534 my $marcsubjctsarray=\@marcsubjcts;
535 return $marcsubjctsarray;
536 } #end getMARCsubjects
538 END { } # module clean-up code here (global destructor)
547 Koha Developement team <info@koha.org>