| File: | C4/Z3950.pm |
| Coverage: | 23.8% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package C4::Z3950; | |||||
| 2 | ||||||
| 3 | ||||||
| 4 | # Routines for handling Z39.50 lookups | |||||
| 5 | ||||||
| 6 | # Koha library project www.koha-community.org | |||||
| 7 | ||||||
| 8 | # Licensed under the GPL | |||||
| 9 | ||||||
| 10 | # Copyright 2000-2002 Katipo Communications | |||||
| 11 | # | |||||
| 12 | # This file is part of Koha. | |||||
| 13 | # | |||||
| 14 | # Koha is free software; you can redistribute it and/or modify it under the | |||||
| 15 | # terms of the GNU General Public License as published by the Free Software | |||||
| 16 | # Foundation; either version 2 of the License, or (at your option) any later | |||||
| 17 | # version. | |||||
| 18 | # | |||||
| 19 | # Koha is distributed in the hope that it will be useful, but WITHOUT ANY | |||||
| 20 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | |||||
| 21 | # A PARTICULAR PURPOSE. See the GNU General Public License for more details. | |||||
| 22 | # | |||||
| 23 | # You should have received a copy of the GNU General Public License along | |||||
| 24 | # with Koha; if not, write to the Free Software Foundation, Inc., | |||||
| 25 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
| 26 | ||||||
| 27 | 3 3 3 | 21827 18 86 | use strict; | |||
| 28 | #use warnings; FIXME - Bug 2505 | |||||
| 29 | ||||||
| 30 | # standard or CPAN modules used | |||||
| 31 | 3 3 3 | 269 25420 150 | use DBI; | |||
| 32 | ||||||
| 33 | # Koha modules used | |||||
| 34 | 3 3 3 | 693 6 310 | use C4::Input; | |||
| 35 | 3 3 3 | 189 6 1485 | use C4::Biblio; | |||
| 36 | ||||||
| 37 | 3 3 3 | 16 4 271 | use vars qw($VERSION @ISA @EXPORT); | |||
| 38 | ||||||
| 39 | BEGIN { | |||||
| 40 | # set the version for version checking | |||||
| 41 | 3 | 4 | $VERSION = 3.01; | |||
| 42 | 3 | 11 | require Exporter; | |||
| 43 | 3 | 20 | @ISA = qw(Exporter); | |||
| 44 | 3 | 788 | @EXPORT = qw( | |||
| 45 | &getz3950servers | |||||
| 46 | &z3950servername | |||||
| 47 | &addz3950queue | |||||
| 48 | &checkz3950searchdone | |||||
| 49 | ); | |||||
| 50 | } | |||||
| 51 | ||||||
| 52 - 78 | =head1 NAME C4::Z3950 - Functions dealing with Z39.50 queries =head1 SYNOPSIS use C4::Z3950; =head1 DESCRIPTION This module contains functions for looking up Z39.50 servers, and for entering Z39.50 lookup requests. =head1 FUNCTIONS =over 2 =item getz3950servers @servers= &getz3950servers(checked); Returns the list of declared z3950 servers C<$checked> should always be true (1) => returns only active servers. If 0 => returns all servers =cut | |||||
| 79 | ||||||
| 80 | sub getz3950servers { | |||||
| 81 | 0 | my ($checked) = @_; | ||||
| 82 | 0 | my $dbh = C4::Context->dbh; | ||||
| 83 | 0 | my $sth; | ||||
| 84 | 0 | if ($checked) { | ||||
| 85 | 0 | $sth = $dbh->prepare("select * from z3950servers where checked=1"); | ||||
| 86 | } else { | |||||
| 87 | 0 | $sth = $dbh->prepare("select * from z3950servers"); | ||||
| 88 | } | |||||
| 89 | 0 | my @result; | ||||
| 90 | 0 | while ( my ($host, $port, $db, $userid, $password,$servername) = $sth->fetchrow ) { | ||||
| 91 | 0 | push @result, "$servername/$host\:$port/$db/$userid/$password"; | ||||
| 92 | } # while | |||||
| 93 | 0 | return @result; | ||||
| 94 | } | |||||
| 95 | ||||||
| 96 - 107 | =item z3950servername $name = &z3950servername($dbh, $server_id, $default_name); Looks up a Z39.50 server by ID number, and returns its full name. If the server is not found, returns C<$default_name>. C<$server_id> is the Z39.50 server ID to look up. C<$dbh> is ignored. =cut | |||||
| 108 | ||||||
| 109 | #' | |||||
| 110 | ||||||
| 111 | sub z3950servername { | |||||
| 112 | # inputs | |||||
| 113 | 0 | my ($srvid, # server id number | ||||
| 114 | $default,)=@_; | |||||
| 115 | # return | |||||
| 116 | 0 | my $longname; | ||||
| 117 | #---- | |||||
| 118 | ||||||
| 119 | 0 | my $dbh = C4::Context->dbh; | ||||
| 120 | ||||||
| 121 | 0 | my $sti=$dbh->prepare("select name from z3950servers where id=?"); | ||||
| 122 | ||||||
| 123 | 0 | $sti->execute($srvid); | ||||
| 124 | 0 | if ( ! $sti->err ) { | ||||
| 125 | 0 | ($longname)=$sti->fetchrow; | ||||
| 126 | } | |||||
| 127 | 0 | if (! $longname) { | ||||
| 128 | 0 | $longname="$default"; | ||||
| 129 | } | |||||
| 130 | 0 | return $longname; | ||||
| 131 | } # sub z3950servername | |||||
| 132 | ||||||
| 133 | #--------------------------------------- | |||||
| 134 | ||||||
| 135 - 163 | =item addz3950queue $errmsg = &addz3950queue($query, $type, $request_id, @servers); Adds a Z39.50 search query for the Z39.50 server to look up. C<$query> is the term to search for. C<$type> is the query type, e.g. C<isbn>, C<lccn>, etc. C<$request_id> is a unique string that will identify this query. C<@servers> is a list of servers to query (obviously, this can be given either as an array, or as a list of scalars). Each element may be either a Z39.50 server ID from the z3950server table of the Koha database, the string C<DEFAULT> or C<CHECKED>, or a complete server specification containing a colon. C<DEFAULT> and C<CHECKED> are synonymous, and refer to those servers in the z3950servers table whose 'checked' field is set and non-NULL. Once the query has been submitted to the Z39.50 daemon, C<&addz3950queue> sends a SIGHUP to the daemon to tell it to process this new request. C<&addz3950queue> returns an error message. If it was successful, the error message is the empty string. =cut | |||||
| 164 | ||||||
| 165 | #' | |||||
| 166 | sub addz3950queue { | |||||
| 167 | 3 3 3 | 12 4 2068 | use strict; | |||
| 168 | # input | |||||
| 169 | my ( | |||||
| 170 | 0 | $query, # value to look up | ||||
| 171 | $type, # type of value ("isbn", "lccn", "title", "author", "keyword") | |||||
| 172 | $requestid, # Unique value to prevent duplicate searches from multiple HTML form submits | |||||
| 173 | @z3950list, # list of z3950 servers to query | |||||
| 174 | )=@_; | |||||
| 175 | # Returns: | |||||
| 176 | 0 | my $error; | ||||
| 177 | ||||||
| 178 | my ( | |||||
| 179 | 0 | $sth, | ||||
| 180 | @serverlist, | |||||
| 181 | $server, | |||||
| 182 | $failed, | |||||
| 183 | $servername, | |||||
| 184 | ); | |||||
| 185 | ||||||
| 186 | # FIXME - Should be configurable, probably in /etc/koha.conf. | |||||
| 187 | 0 | my $pidfile='/var/log/koha/processz3950queue.pid'; | ||||
| 188 | ||||||
| 189 | 0 | $error=""; | ||||
| 190 | ||||||
| 191 | 0 | my $dbh = C4::Context->dbh; | ||||
| 192 | # list of servers: entry can be a fully qualified URL-type entry | |||||
| 193 | # or simply just a server ID number. | |||||
| 194 | 0 | foreach $server (@z3950list) { | ||||
| 195 | 0 | if ($server =~ /:/ ) { | ||||
| 196 | 0 | push @serverlist, $server; | ||||
| 197 | } elsif ($server eq 'DEFAULT' || $server eq 'CHECKED' ) { | |||||
| 198 | 0 | $sth=$dbh->prepare("select host,port,db,userid,password ,name,syntax from z3950servers where checked <> 0 "); | ||||
| 199 | 0 | $sth->execute; | ||||
| 200 | 0 | while ( my ($host, $port, $db, $userid, $password,$servername,$syntax) = $sth->fetchrow ) { | ||||
| 201 | 0 | push @serverlist, "$servername/$host\:$port/$db/$userid/$password/$syntax"; | ||||
| 202 | } # while | |||||
| 203 | } else { | |||||
| 204 | 0 | $sth=$dbh->prepare("select host,port,db,userid,password,syntax from z3950servers where id=? "); | ||||
| 205 | 0 | $sth->execute($server); | ||||
| 206 | 0 | my ($host, $port, $db, $userid, $password,$syntax) = $sth->fetchrow; | ||||
| 207 | 0 | push @serverlist, "$server/$host\:$port/$db/$userid/$password/$syntax"; | ||||
| 208 | } | |||||
| 209 | } | |||||
| 210 | ||||||
| 211 | 0 | my $serverlist=''; | ||||
| 212 | ||||||
| 213 | 0 | $serverlist = join("|", @serverlist); | ||||
| 214 | # chop $serverlist; | |||||
| 215 | ||||||
| 216 | # FIXME - Is this test supposed to test whether @serverlist is | |||||
| 217 | # empty? If so, then a) there are better ways to do that in | |||||
| 218 | # Perl (e.g., "if (@serverlist eq ())"), and b) it doesn't | |||||
| 219 | # work anyway, since it checks whether $serverlist is composed | |||||
| 220 | # of one or more spaces, which is never the case, not even | |||||
| 221 | # when there are 0 or 1 elements in @serverlist. | |||||
| 222 | 0 | if ( $serverlist !~ /^ +$/ ) { | ||||
| 223 | # Don't allow reinsertion of the same request identifier. | |||||
| 224 | 0 | $sth=$dbh->prepare("select identifier from z3950queue | ||||
| 225 | where identifier=?"); | |||||
| 226 | 0 | $sth->execute($requestid); | ||||
| 227 | 0 | if ( ! $sth->rows) { | ||||
| 228 | 0 | $sth=$dbh->prepare("insert into z3950queue (term,type,servers, identifier) values (?, ?, ?, ?)"); | ||||
| 229 | 0 | $sth->execute($query, $type, $serverlist, $requestid); | ||||
| 230 | 0 | if ( -r $pidfile ) { | ||||
| 231 | # FIXME - Perl is good at opening files. No need to | |||||
| 232 | # spawn a separate 'cat' process. | |||||
| 233 | 0 | my $pid=`cat $pidfile`; | ||||
| 234 | 0 | chomp $pid; | ||||
| 235 | # Kill -HUP the Z39.50 daemon to tell it to process | |||||
| 236 | # this query. | |||||
| 237 | 0 | my $processcount=kill 1, $pid; | ||||
| 238 | 0 | if ($processcount==0) { | ||||
| 239 | 0 | $error.="Z39.50 search daemon error: no process signalled. "; | ||||
| 240 | } | |||||
| 241 | } else { | |||||
| 242 | # FIXME - Error-checking like this should go close | |||||
| 243 | # to the test. | |||||
| 244 | 0 | $error.="No Z39.50 search daemon running: no file $pidfile. "; | ||||
| 245 | } # if $pidfile | |||||
| 246 | } else { | |||||
| 247 | # FIXME - Error-checking like this should go close | |||||
| 248 | # to the test. | |||||
| 249 | 0 | $error.="Duplicate request ID $requestid. "; | ||||
| 250 | } # if rows | |||||
| 251 | } else { | |||||
| 252 | # FIXME - Error-checking like this should go close to the | |||||
| 253 | # test. I.e., | |||||
| 254 | # return "No Z39.50 search servers specified. " | |||||
| 255 | # if @serverlist eq (); | |||||
| 256 | ||||||
| 257 | # server list is empty | |||||
| 258 | 0 | $error.="No Z39.50 search servers specified. "; | ||||
| 259 | } # if serverlist empty | |||||
| 260 | ||||||
| 261 | 0 | return $error; | ||||
| 262 | ||||||
| 263 | } # sub addz3950queue | |||||
| 264 | ||||||
| 265 - 273 | =item &checkz3950searchdone $numberpending= & &checkz3950searchdone($random); Returns the number of pending z3950 requests C<$random> is the random z3950 query number. =cut | |||||
| 274 | ||||||
| 275 | sub checkz3950searchdone { | |||||
| 276 | 0 | my ($z3950random) = @_; | ||||
| 277 | 0 | my $dbh = C4::Context->dbh; | ||||
| 278 | # first, check that the deamon already created the requests... | |||||
| 279 | 0 | my $sth = $dbh->prepare("select count(*) from z3950queue,z3950results where z3950queue.id = z3950results.queryid and z3950queue.identifier=?"); | ||||
| 280 | 0 | $sth->execute($z3950random); | ||||
| 281 | 0 | my ($result) = $sth->fetchrow; | ||||
| 282 | 0 | if ($result eq 0) { # search not yet begun => should be searches to do ! | ||||
| 283 | 0 | return "??"; | ||||
| 284 | } | |||||
| 285 | # second, count pending requests | |||||
| 286 | 0 | $sth = $dbh->prepare("select count(*) from z3950queue,z3950results where z3950queue.id = z3950results.queryid and z3950results.enddate is null and z3950queue.identifier=?"); | ||||
| 287 | 0 | $sth->execute($z3950random); | ||||
| 288 | 0 | ($result) = $sth->fetchrow; | ||||
| 289 | 0 | return $result; | ||||
| 290 | } | |||||
| 291 | ||||||
| 292 | 1; | |||||