search on MARC datas.

first draft
This commit is contained in:
tipaul 2003-01-28 14:56:39 +00:00
parent d8c2e556ce
commit c23d07724e
4 changed files with 432 additions and 0 deletions

140
C4/SearchMarc.pm Normal file
View file

@ -0,0 +1,140 @@
package C4::SearchMarc;
# Copyright 2000-2002 Katipo Communications
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
require Exporter;
use DBI;
use C4::Context;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
# set the version for version checking
$VERSION = 0.02;
=head1 NAME
C4::Search - Functions for searching the Koha MARC catalog
=head1 SYNOPSIS
use C4::Search;
my ($count, @results) = catalogsearch();
=head1 DESCRIPTION
This module provides the searching facilities for the Koha MARC catalog
C<&catalogsearch> is a front end to all the other searches. Depending
on what is passed to it, it calls the appropriate search function.
=head1 FUNCTIONS
=over 2
=cut
@ISA = qw(Exporter);
@EXPORT = qw(&catalogsearch);
# make all your functions, whether exported or not;
# marcsearch : search in the MARC biblio table.
# everything is choosen by the user : what to search, the conditions...
# FIXME this is a 1st version of the Marc Search. It does not use fulltext searching or marc_words table
sub catalogsearch {
my ($dbh, $tags, $subfields, $and_or, $excluding, $operator, $value, $offset,$length) = @_;
# build the sql request. She will look like :
# select m1.bibid
# from marc_subfield_table as m1, marc_subfield_table as m2
# where m1.bibid=m2.bibid and
# (m1.subfieldvalue like "Des%" and m2.subfieldvalue like "27%")
my $sql_tables; # will contain marc_subfield_table as m1,...
my $sql_where1; # will contain the "true" where
my $sql_where2; # will contain m1.bibid=m2.bibid
my $nb=1;
for(my $i=0; $i<=@$tags;$i++) {
if (@$tags[$i] && @$value[$i]) {
$sql_tables .= "marc_subfield_table as m$nb,";
if ($nb==1) {
if (@$operator[$i] eq "starts") {
$sql_where1 .= "@$excluding[$i](m1.subfieldvalue like '@$value[$i] %' and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]')";
} elsif (@$operator[$i] eq "contains") {
$sql_where1 .= "@$excluding[$i](m1.subfieldvalue like '%@$value[$i]%' and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]')";
} else {
$sql_where1 .= "@$excluding[$i](m1.subfieldvalue @$operator[$i] '@$value[$i]' and m1.tag=@$tags[$i] and m1.subfieldcode='@$subfields[$i]')";
}
} else {
if (@$operator[$i] eq "starts") {
$sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb.subfieldvalue like '@$value[$i]%' and m$nb.tag=@$tags[$i] and m$nb.subfieldcode='@$subfields[$i]')";
$sql_where2 .= "m1.bibid=m$nb.bibid";
} elsif (@$operator[$i] eq "contains") {
$sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb.subfieldvalue like '%@$value[$i]%' and m$nb.tag=@$tags[$i] and m$nb.subfieldcode='@$subfields[$i]')";
$sql_where2 .= "m1.bibid=m$nb.bibid";
} else {
$sql_where1 .= "@$and_or[$i] @$excluding[$i](m$nb.subfieldvalue @$operator[$i] '@$value[$i]' and m$nb.tag=@$tags[$i] and m$nb.subfieldcode='@$subfields[$i]')";
$sql_where2 .= "m1.bibid=m$nb.bibid";
}
}
$nb++;
}
}
chop $sql_tables;
warn "select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)";
my $sth;
if ($sql_where2) {
$sth = $dbh->prepare("select m1.bibid from $sql_tables where $sql_where2 and ($sql_where1)");
} else {
$sth = $dbh->prepare("select m1.bibid from $sql_tables where $sql_where1");
}
$sth->execute;
my @result;
while (my ($bibid) = $sth->fetchrow) {
push @result,$bibid;
}
# we have bibid list. Now, loads title and author from [offset] to [offset]+[length]
my $counter = $offset;
$sth = $dbh->prepare("select author,title from biblio,marc_biblio where biblio.biblionumber=marc_biblio.biblionumber and bibid=?");
my @finalresult = ();
while ($counter <= ($offset + $length)) {
$sth->execute($result[$counter]);
my ($author,$title) = $sth->fetchrow;
my %line;
$line{bibid}=$result[$counter];
$line{author}=$author;
$line{title}=$title;
push @finalresult, \%line;
$counter++;
}
return @finalresult;
}
END { } # module clean-up code here (global destructor)
1;
__END__
=back
=head1 AUTHOR
Koha Developement team <info@koha.org>
=cut

View file

@ -0,0 +1,37 @@
<TMPL_IF NAME="opac">
<TMPL_INCLUDE NAME="opac-top.inc">
<TMPL_ELSE>
<TMPL_INCLUDE NAME="cat-top.inc">
</TMPL_IF>
<TMPL_IF NAME="loggedinuser">
<p align=left>Logged in as: <TMPL_VAR NAME="loggedinuser"> [<a href=/cgi-bin/koha/logout.pl>Log Out</a>]</p>
<TMPL_ELSE>
<p align=left><a href=/cgi-bin/koha/userpage.pl>Log In</a> to Koha</p>
</TMPL_IF>
<center>
<table border=0 cellspacing=0 cellpadding=2>
<tr
<TMPL_IF NAME="opac">
bgcolor=#99cccc background=/images/background-opac.gif
<TMPL_ELSE>
background="/images/background-mem.gif"
</TMPL_IF>
>
<th colspan=5>Result</th>
</tr>
<TMPL_LOOP name="result">
<tr>
<td><TMPL_VAR name="bibid"></td>
<td><TMPL_VAR name="author"></td>
<td><TMPL_VAR name="title"></td>
</tr>
</TMPL_LOOP>
</table>
<TMPL_IF NAME="opac">
<TMPL_INCLUDE NAME="opac-bottom.inc">
<TMPL_ELSE>
<TMPL_INCLUDE NAME="cat-bottom.inc">
</TMPL_IF>

View file

@ -0,0 +1,145 @@
<TMPL_IF NAME="opac">
<TMPL_INCLUDE NAME="opac-top.inc">
<TMPL_ELSE>
<TMPL_INCLUDE NAME="cat-top.inc">
</TMPL_IF>
<TMPL_IF NAME="loggedinuser">
<p align=left>Logged in as: <TMPL_VAR NAME="loggedinuser"> [<a href=/cgi-bin/koha/logout.pl>Log Out</a>]</p>
<TMPL_ELSE>
<p align=left><a href=/cgi-bin/koha/userpage.pl>Log In</a> to Koha</p>
</TMPL_IF>
<center>
<form name="f" method="post">
<input type="hidden" name="type" value="intranet">
<table border=0 cellspacing=0 cellpadding=2>
<tr
<TMPL_IF NAME="opac">
bgcolor=#99cccc background=/images/background-opac.gif
<TMPL_ELSE>
background="/images/background-mem.gif"
</TMPL_IF>
>
<th colspan=5>Search</th>
</tr>
<tr align="center">
<td colspan=5>
<div name="sql">
<input type="hidden" name="op" value="do_search">
<textarea name="sql" rows=5 cols=85 disabled readonly></textarea>
</td>
</tr>
<!-- 000000000000000000000000000000000000000000000000000000 -->
<tr>
<td rowspan=3
<TMPL_IF NAME="opac">
bgcolor=#99cccc background=/images/background-opac.gif
<TMPL_ELSE>
background="/images/background-mem.gif"
</TMPL_IF>
align=center valign=center>
<b>0</b>
</td>
<td>
<input type=hidden name="and_or" value="">&nbsp;
</td>
<td><TMPL_VAR name="marclist"></td>
<td>
<select name="excluding" size="1" onchange="sql_update()">
<option value=""> </option>
<option value="not">not</option>
</select>
</td>
<td>
<select name="operator" size="1" onchange="sql_update()">
<option value="=">equals</option>
<option value=">">More than</option>
<option value=">=">More or equal than</option>
<option value="<">Less than</option>
<option value="<=">Less or equal than</option>
<option value="start">start</option>
<option value="contains">contains</option>
</select>
</td>
<td><input type="text" name="value" onChange="sql_update()"></td>
</tr>
<tr>
<td>
<select name="and_or" size="1" onchange="sql_update()">
<option value="and">and</option>
<option value="or">or</option>
</select>
</td>
<td><TMPL_VAR name="marclist"></td>
<td>
<select name="excluding" size="1" onchange="sql_update()">
<option value=""> </option>
<option value="not">not</option>
</select>
</td>
<td>
<select name="operator" size="1" onchange="sql_update()">
<option value="=">equals</option>
<option value=">">More than</option>
<option value=">=">More or equal than</option>
<option value="<">Less than</option>
<option value="<=">Less or equal than</option>
<option value="start">start</option>
<option value="contains">contains</option>
</select>
</td>
<td><input type="text" name="value" onChange="sql_update()"></td>
</tr>
<tr>
<td>
<select name="and_or" size="1" onchange="sql_update()">
<option value="and">and</option>
<option value="or">or</option>
</select>
</td>
<td><TMPL_VAR name="marclist"></td>
<td>
<select name="excluding" size="1" onchange="sql_update()">
<option value=""> </option>
<option value="not">not</option>
</select>
</td>
<td>
<select name="operator" size="1" onchange="sql_update()">
<option value="=">equals</option>
<option value=">">More than</option>
<option value=">=">More or equal than</option>
<option value="<">Less than</option>
<option value="<=">Less or equal than</option>
<option value="start">start</option>
<option value="contains">contains</option>
</select>
</td>
<td><input type="text" name="value" onChange="sql_update()"></td>
</tr>
<tr><td colspan=5 align=center><input type="submit" value="OK"></td></tr>
</table>
</form>
<script>
function sql_update() {
document.f.sql.value="";
for (i=0 ; i<document.f.marclist.length ; i++) {
if (document.f.marclist[i].value != '') {
document.f.sql.value = document.f.sql.value+
document.f.and_or[i].value + ' (' +
document.f.excluding[i].value + ' ' +
document.f.marclist[i].value + '' +
document.f.operator[i].value + '' +
'\''+document.f.value[i].value + '\') ';
}
}
}
</script>
<TMPL_IF NAME="opac">
<TMPL_INCLUDE NAME="opac-bottom.inc">
<TMPL_ELSE>
<TMPL_INCLUDE NAME="cat-bottom.inc">
</TMPL_IF>

110
search.marc/search.pl Executable file
View file

@ -0,0 +1,110 @@
#!/usr/bin/perl
# Copyright 2000-2002 Katipo Communications
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307 USA
use strict;
require Exporter;
use CGI;
use C4::Auth;
use HTML::Template;
use C4::Context;
use C4::Search;
use C4::Auth;
use C4::Output;
use C4::Biblio;
use C4::SearchMarc;
my $query=new CGI;
my $type=$query->param('type');
my $op = $query->param('op');
#$type="opac" unless $type;
my $dbh = C4::Context->dbh;
my ($loggedinuser, $cookie, $sessionID) = checkauth($query, ($type eq 'opac') ? (1) : (0));
my $startfrom=$query->param('startfrom');
($startfrom) || ($startfrom=0);
my ($template, $loggedinuser, $cookie);
if ($op eq "do_search") {
my @marclist = $query->param('marclist');
my @and_or = $query->param('and_or');
my @excluding = $query->param('excluding');
my @operator = $query->param('operator');
my @value = $query->param('value');
# builds tag and subfield arrays
my @tags;
my @subfields;
foreach my $marc (@marclist) {
push @tags, substr($marc,0,3);
push @subfields, substr($marc,3,1);
}
my @results = catalogsearch($dbh, \@tags, \@subfields, \@and_or,
\@excluding, \@operator, \@value,
$startfrom, 20);
($template, $loggedinuser, $cookie)
= get_template_and_user({template_name => "search.marc/result.tmpl",
query => $query,
type => $type,
authnotrequired => 0,
flagsrequired => {catalogue => 1},
debug => 1,
});
$template->param(loggedinuser => $loggedinuser,
result => \@results);
} else {
($template, $loggedinuser, $cookie)
= get_template_and_user({template_name => "search.marc/search.tmpl",
query => $query,
type => $type,
authnotrequired => 0,
flagsrequired => {catalogue => 1},
debug => 1,
});
$template->param(loggedinuser => $loggedinuser);
my $tagslib;
if ($type eq "opac") {
$tagslib = &MARCgettagslib($dbh,1);
} else {
$tagslib = &MARCgettagslib($dbh,1);
}
my @marcarray;
push @marcarray,"";
for (my $tabloop = 0; $tabloop<=9;$tabloop++) {
push @marcarray,"--------------------------------------- $tabloop ---------------------------------------";
foreach my $tag (sort(keys (%{$tagslib}))) {
foreach my $subfield (sort(keys %{$tagslib->{$tag}})) {
next if ($subfield eq 'lib'); # skip lib and tabs, which are koha internal
next if ($subfield eq 'tab');
next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
push @marcarray, "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
}
}
}
my $marclist = CGI::scrolling_list(-name=>"marclist",
-values=> \@marcarray,
-size=>1,
-multiple=>0,
-onChange => "sql_update()",
);
$template->param("marclist" => $marclist);
}
# Print the page
print $query->header(-cookie => $cookie), $template->output;