4 # Routines for handling Z39.50 lookups
6 # Koha library project www.koha-community.org
8 # Licensed under the GPL
10 # Copyright 2000-2002 Katipo Communications
12 # This file is part of Koha.
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
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.
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.
28 #use warnings; FIXME - Bug 2505
30 # standard or CPAN modules used
37 use vars
qw($VERSION @ISA @EXPORT);
40 # set the version for version checking
54 C4::Z3950 - Functions dealing with Z39.50 queries
62 This module contains functions for looking up Z39.50 servers, and for
63 entering Z39.50 lookup requests.
71 @servers= &getz3950servers(checked);
73 Returns the list of declared z3950 servers
75 C<$checked> should always be true (1) => returns only active servers.
76 If 0 => returns all servers
82 my $dbh = C4
::Context
->dbh;
85 $sth = $dbh->prepare("select * from z3950servers where checked=1");
87 $sth = $dbh->prepare("select * from z3950servers");
90 while ( my ($host, $port, $db, $userid, $password,$servername) = $sth->fetchrow ) {
91 push @result, "$servername/$host\:$port/$db/$userid/$password";
98 $name = &z3950servername($dbh, $server_id, $default_name);
100 Looks up a Z39.50 server by ID number, and returns its full name. If
101 the server is not found, returns C<$default_name>.
103 C<$server_id> is the Z39.50 server ID to look up.
111 sub z3950servername
{
113 my ($srvid, # server id number
119 my $dbh = C4
::Context
->dbh;
121 my $sti=$dbh->prepare("select name from z3950servers where id=?");
123 $sti->execute($srvid);
125 ($longname)=$sti->fetchrow;
128 $longname="$default";
131 } # sub z3950servername
133 #---------------------------------------
137 $errmsg = &addz3950queue($query, $type, $request_id, @servers);
139 Adds a Z39.50 search query for the Z39.50 server to look up.
141 C<$query> is the term to search for.
143 C<$type> is the query type, e.g. C<isbn>, C<lccn>, etc.
145 C<$request_id> is a unique string that will identify this query.
147 C<@servers> is a list of servers to query (obviously, this can be
148 given either as an array, or as a list of scalars). Each element may
149 be either a Z39.50 server ID from the z3950server table of the Koha
150 database, the string C<DEFAULT> or C<CHECKED>, or a complete server
151 specification containing a colon.
153 C<DEFAULT> and C<CHECKED> are synonymous, and refer to those servers
154 in the z3950servers table whose 'checked' field is set and non-NULL.
156 Once the query has been submitted to the Z39.50 daemon,
157 C<&addz3950queue> sends a SIGHUP to the daemon to tell it to process
160 C<&addz3950queue> returns an error message. If it was successful, the
161 error message is the empty string.
170 $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
186 # FIXME - Should be configurable, probably in /etc/koha.conf.
187 my $pidfile='/var/log/koha/processz3950queue.pid';
191 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 foreach $server (@z3950list) {
195 if ($server =~ /:/ ) {
196 push @serverlist, $server;
197 } elsif ($server eq 'DEFAULT' || $server eq 'CHECKED' ) {
198 $sth=$dbh->prepare("select host,port,db,userid,password ,name,syntax from z3950servers where checked <> 0 ");
200 while ( my ($host, $port, $db, $userid, $password,$servername,$syntax) = $sth->fetchrow ) {
201 push @serverlist, "$servername/$host\:$port/$db/$userid/$password/$syntax";
204 $sth=$dbh->prepare("select host,port,db,userid,password,syntax from z3950servers where id=? ");
205 $sth->execute($server);
206 my ($host, $port, $db, $userid, $password,$syntax) = $sth->fetchrow;
207 push @serverlist, "$server/$host\:$port/$db/$userid/$password/$syntax";
213 $serverlist = join("|", @serverlist);
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 if ( $serverlist !~ /^ +$/ ) {
223 # Don't allow reinsertion of the same request identifier.
224 $sth=$dbh->prepare("select identifier from z3950queue
225 where identifier=?");
226 $sth->execute($requestid);
228 $sth=$dbh->prepare("insert into z3950queue (term,type,servers, identifier) values (?, ?, ?, ?)");
229 $sth->execute($query, $type, $serverlist, $requestid);
231 # FIXME - Perl is good at opening files. No need to
232 # spawn a separate 'cat' process.
233 my $pid=`cat $pidfile`;
235 # Kill -HUP the Z39.50 daemon to tell it to process
237 my $processcount=kill 1, $pid;
238 if ($processcount==0) {
239 $error.="Z39.50 search daemon error: no process signalled. ";
242 # FIXME - Error-checking like this should go close
244 $error.="No Z39.50 search daemon running: no file $pidfile. ";
247 # FIXME - Error-checking like this should go close
249 $error.="Duplicate request ID $requestid. ";
252 # FIXME - Error-checking like this should go close to the
254 # return "No Z39.50 search servers specified. "
255 # if @serverlist eq ();
257 # server list is empty
258 $error.="No Z39.50 search servers specified. ";
259 } # if serverlist empty
263 } # sub addz3950queue
265 =item &checkz3950searchdone
267 $numberpending= & &checkz3950searchdone($random);
269 Returns the number of pending z3950 requests
271 C<$random> is the random z3950 query number.
275 sub checkz3950searchdone
{
276 my ($z3950random) = @_;
277 my $dbh = C4
::Context
->dbh;
278 # first, check that the deamon already created the requests...
279 my $sth = $dbh->prepare("select count(*) from z3950queue,z3950results where z3950queue.id = z3950results.queryid and z3950queue.identifier=?");
280 $sth->execute($z3950random);
281 my ($result) = $sth->fetchrow;
282 if ($result eq 0) { # search not yet begun => should be searches to do !
285 # second, count pending requests
286 $sth = $dbh->prepare("select count(*) from z3950queue,z3950results where z3950queue.id = z3950results.queryid and z3950results.enddate is null and z3950queue.identifier=?");
287 $sth->execute($z3950random);
288 ($result) = $sth->fetchrow;
299 Koha Development Team <http://koha-community.org/>