5 # Routines for handling Z39.50 lookups
7 # Koha library project www.koha.org
9 # Licensed under the GPL
12 # Copyright 2000-2002 Katipo Communications
14 # This file is part of Koha.
16 # Koha is free software; you can redistribute it and/or modify it under the
17 # terms of the GNU General Public License as published by the Free Software
18 # Foundation; either version 2 of the License, or (at your option) any later
21 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
22 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
23 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License along with
26 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
27 # Suite 330, Boston, MA 02111-1307 USA
31 # standard or CPAN modules used
43 use vars qw($VERSION @ISA @EXPORT);
45 # set the version for version checking
50 C4::Z3950 - Functions dealing with Z39.50 queries
58 This module contains functions for looking up Z39.50 servers, and for
59 entering Z39.50 lookup requests.
73 #------------------------------------------------
77 $name = &z3950servername($dbh, $server_id, $default_name);
79 Looks up a Z39.50 server by ID number, and returns its full name. If
80 the server is not found, returns C<$default_name>.
82 C<$server_id> is the Z39.50 server ID to look up.
91 $dbh, # FIXME - Unused argument
92 $srvid, # server id number
99 $dbh = C4::Context->dbh;
101 # FIXME - Fix indentation
102 my $sti=$dbh->prepare("select name
105 $sti->execute($srvid);
107 ($longname)=$sti->fetchrow;
110 $longname="$default";
113 } # sub z3950servername
115 #---------------------------------------
119 $errmsg = &addz3950queue($dbh, $query, $type, $request_id, @servers);
121 Adds a Z39.50 search query for the Z39.50 server to look up.
123 C<$dbh> is obsolete and is ignored.
125 C<$query> is the term to search for.
127 C<$type> is the query type, e.g. C<isbn>, C<lccn>, etc.
129 C<$request_id> is a unique string that will identify this query.
131 C<@servers> is a list of servers to query (obviously, this can be
132 given either as an array, or as a list of scalars). Each element may
133 be either a Z39.50 server ID from the z3950server table of the Koha
134 database, the string C<DEFAULT> or C<CHECKED>, or a complete server
135 specification containing a colon.
137 C<DEFAULT> and C<CHECKED> are synonymous, and refer to those servers
138 in the z3950servers table whose 'checked' field is set and non-NULL.
140 Once the query has been submitted to the Z39.50 daemon,
141 C<&addz3950queue> sends a SIGHUP to the daemon to tell it to process
144 C<&addz3950queue> returns an error message. If it was successful, the
145 error message is the empty string.
154 # FIXME - Unused argument
155 $query, # value to look up
156 $type, # type of value ("isbn", "lccn", etc).
157 # FIXME - What other values are legal?
158 $requestid, # Unique value to prevent duplicate searches from multiple HTML form submits
159 @z3950list, # list of z3950 servers to query
172 # FIXME - Should be configurable, probably in /etc/koha.conf.
173 my $pidfile='/var/log/koha/processz3950queue.pid';
177 $dbh = C4::Context->dbh;
179 # FIXME - Fix indentation
181 # list of servers: entry can be a fully qualified URL-type entry
182 # or simply just a server ID number.
184 foreach $server (@z3950list) {
185 if ($server =~ /:/ ) {
186 push @serverlist, $server;
187 } elsif ($server eq 'DEFAULT' || $server eq 'CHECKED' ) {
188 $sth=$dbh->prepare("select host,port,db,userid,password ,name
190 where checked <> 0 ");
192 while ( my ($host, $port, $db, $userid, $password,$servername)
194 push @serverlist, "$servername/$host\:$port/$db/$userid/$password";
197 $sth=$dbh->prepare("select host,port,db,userid,password
200 $sth->execute($server);
201 my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
202 push @serverlist, "$server/$host\:$port/$db/$userid/$password";
207 # FIXME - $serverlist = join(" ", @serverlist);
208 foreach (@serverlist) {
213 # FIXME - Is this test supposed to test whether @serverlist is
214 # empty? If so, then a) there are better ways to do that in
215 # Perl (e.g., "if (@serverlist eq ())"), and b) it doesn't
216 # work anyway, since it checks whether $serverlist is composed
217 # of one or more spaces, which is never the case, not even
218 # when there are 0 or 1 elements in @serverlist.
219 if ( $serverlist !~ /^ +$/ ) {
220 # Don't allow reinsertion of the same request identifier.
221 $sth=$dbh->prepare("select identifier from z3950queue
222 where identifier=?");
223 $sth->execute($requestid);
225 $sth=$dbh->prepare("insert into z3950queue
226 (term,type,servers, identifier)
227 values (?, ?, ?, ?)");
228 $sth->execute($query, $type, $serverlist, $requestid);
230 # FIXME - Perl is good at opening files. No need to
231 # spawn a separate 'cat' process.
232 my $pid=`cat $pidfile`;
234 # Kill -HUP the Z39.50 daemon to tell it to process
236 my $processcount=kill 1, $pid;
237 if ($processcount==0) {
238 $error.="Z39.50 search daemon error: no process signalled. ";
241 # FIXME - Error-checking like this should go close
243 $error.="No Z39.50 search daemon running: no file $pidfile. ";
246 # FIXME - Error-checking like this should go close
248 $error.="Duplicate request ID $requestid. ";
251 # FIXME - Error-checking like this should go close to the
253 # return "No Z39.50 search servers specified. "
254 # if @serverlist eq ();
256 # server list is empty
257 $error.="No Z39.50 search servers specified. ";
258 } # if serverlist empty
262 } # sub addz3950queue
271 Koha Developement team <info@koha.org>
275 #--------------------------------------
277 # Revision 1.6 2002/10/13 08:30:53 arensb
278 # Deleted unused variables.
279 # Removed trailing whitespace.
281 # Revision 1.5 2002/10/13 06:13:23 arensb
282 # Removed bogus #! line (this isn't a script!)
283 # Removed unused global variables.
285 # Added some explanatory comments.
286 # Added some FIXME comments.
288 # Revision 1.4 2002/10/11 12:35:35 arensb
289 # Replaced &requireDBI with C4::Context->dbh
291 # Revision 1.3 2002/08/14 18:12:52 tonnesen
292 # Added copyright statement to all .pl and .pm files
294 # Revision 1.2 2002/07/02 20:31:33 tonnesen
295 # module added from rel-1-2 branch
297 # Revision 1.1.2.5 2002/06/29 17:33:47 amillar
298 # Allow DEFAULT as input to addz3950search.
299 # Check for existence of pid file (cat crashed otherwise).
300 # Return error messages in addz3950search.
302 # Revision 1.1.2.4 2002/06/28 18:07:27 tonnesen
303 # marcimport.pl will print an error message if it can not signal the
304 # processz3950queue program. The message contains instructions for starting the
307 # Revision 1.1.2.3 2002/06/28 17:45:39 tonnesen
308 # z3950queue now listens for a -HUP signal before processing the queue. Z3950.pm
309 # sends the -HUP signal when queries are added to the queue.
311 # Revision 1.1.2.2 2002/06/26 20:54:31 tonnesen
312 # use warnings breaks on perl 5.005...
314 # Revision 1.1.2.1 2002/06/26 07:26:41 amillar
315 # New module for Z39.50 searching