Removed the unecessary $dbh argument from being passed.
[koha.git] / C4 / Z3950.pm
1 package C4::Z3950;
2
3 # $Id$
4
5 # Routines for handling Z39.50 lookups
6
7 # Koha library project  www.koha.org
8
9 # Licensed under the GPL
10
11
12 # Copyright 2000-2002 Katipo Communications
13 #
14 # This file is part of Koha.
15 #
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
19 # version.
20 #
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.
24 #
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
28
29 use strict;
30
31 # standard or CPAN modules used
32 use DBI;
33
34 # Koha modules used
35 use C4::Database;
36 use C4::Input;
37 use C4::Biblio;
38
39 #------------------
40
41 require Exporter;
42
43 use vars qw($VERSION @ISA @EXPORT);
44
45 # set the version for version checking
46 $VERSION = 0.01;
47
48 =head1 NAME
49
50 C4::Z3950 - Functions dealing with Z39.50 queries
51
52 =head1 SYNOPSIS
53
54   use C4::Z3950;
55
56 =head1 DESCRIPTION
57
58 This module contains functions for looking up Z39.50 servers, and for
59 entering Z39.50 lookup requests.
60
61 =head1 FUNCTIONS
62
63 =over 2
64
65 =cut
66
67 @ISA = qw(Exporter);
68 @EXPORT = qw(
69          &z3950servername
70          &addz3950queue
71 );
72
73 #------------------------------------------------
74
75 =item z3950servername
76
77   $name = &z3950servername($dbh, $server_id, $default_name);
78
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>.
81
82 C<$server_id> is the Z39.50 server ID to look up.
83
84 C<$dbh> is ignored.
85
86 =cut
87 #'
88
89 sub z3950servername {
90     # inputs
91     my (
92         $srvid,         # server id number
93         $default,
94     )=@_;
95     # return
96     my $longname;
97     #----
98
99     $dbh = C4::Context->dbh;
100
101     my $sti=$dbh->prepare("
102         select name 
103         from z3950servers 
104         where id=?");
105         
106     $sti->execute($srvid);
107     if ( ! $sti->err ) {
108         ($longname)=$sti->fetchrow;
109     }
110     if (! $longname) {
111         $longname="$default";
112     }
113         return $longname;
114 } # sub z3950servername
115
116 #---------------------------------------
117
118 =item addz3950queue
119
120   $errmsg = &addz3950queue($dbh, $query, $type, $request_id, @servers);
121
122 Adds a Z39.50 search query for the Z39.50 server to look up.
123
124 C<$dbh> is obsolete and is ignored.
125
126 C<$query> is the term to search for.
127
128 C<$type> is the query type, e.g. C<isbn>, C<lccn>, etc.
129
130 C<$request_id> is a unique string that will identify this query.
131
132 C<@servers> is a list of servers to query (obviously, this can be
133 given either as an array, or as a list of scalars). Each element may
134 be either a Z39.50 server ID from the z3950server table of the Koha
135 database, the string C<DEFAULT> or C<CHECKED>, or a complete server
136 specification containing a colon.
137
138 C<DEFAULT> and C<CHECKED> are synonymous, and refer to those servers
139 in the z3950servers table whose 'checked' field is set and non-NULL.
140
141 Once the query has been submitted to the Z39.50 daemon,
142 C<&addz3950queue> sends a SIGHUP to the daemon to tell it to process
143 this new request.
144
145 C<&addz3950queue> returns an error message. If it was successful, the
146 error message is the empty string.
147
148 =cut
149 #'
150 sub addz3950queue {
151     use strict;
152     # input
153     my (
154         $query,         # value to look up
155         $type,          # type of value ("isbn", "lccn", etc).
156                         # FIXME - What other values are legal?
157         $requestid,     # Unique value to prevent duplicate searches from multiple HTML form submits
158         @z3950list,     # list of z3950 servers to query
159     )=@_;
160     # Returns:
161     my $error;
162
163     my (
164         $sth,
165         @serverlist,
166         $server,
167         $failed,
168         $servername,
169     );
170
171     # FIXME - Should be configurable, probably in /etc/koha.conf.
172     my $pidfile='/var/log/koha/processz3950queue.pid';
173
174     $error="";
175
176     $dbh = C4::Context->dbh;
177
178         # FIXME - Fix indentation
179
180         # list of servers: entry can be a fully qualified URL-type entry
181         #   or simply just a server ID number.
182
183         foreach $server (@z3950list) {
184             if ($server =~ /:/ ) {
185                 push @serverlist, $server;
186             } elsif ($server eq 'DEFAULT' || $server eq 'CHECKED' ) {
187                 $sth=$dbh->prepare("select host,port,db,userid,password ,name
188                   from z3950servers
189                   where checked <> 0 ");
190                 $sth->execute;
191                 while ( my ($host, $port, $db, $userid, $password,$servername)
192                         = $sth->fetchrow ) {
193                     push @serverlist, "$servername/$host\:$port/$db/$userid/$password";
194                 } # while
195             } else {
196                 $sth=$dbh->prepare("select host,port,db,userid,password
197                   from z3950servers
198                   where id=? ");
199                 $sth->execute($server);
200                 my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
201                 push @serverlist, "$server/$host\:$port/$db/$userid/$password";
202             }
203         }
204
205         my $serverlist='';
206         
207         $severlist = join(" ", @serverlist);
208         chop $serverlist;
209
210         # FIXME - Is this test supposed to test whether @serverlist is
211         # empty? If so, then a) there are better ways to do that in
212         # Perl (e.g., "if (@serverlist eq ())"), and b) it doesn't
213         # work anyway, since it checks whether $serverlist is composed
214         # of one or more spaces, which is never the case, not even
215         # when there are 0 or 1 elements in @serverlist.
216         if ( $serverlist !~ /^ +$/ ) {
217             # Don't allow reinsertion of the same request identifier.
218             $sth=$dbh->prepare("select identifier from z3950queue
219                 where identifier=?");
220             $sth->execute($requestid);
221             if ( ! $sth->rows) {
222                 $sth=$dbh->prepare("insert into z3950queue
223                     (term,type,servers, identifier)
224                     values (?, ?, ?, ?)");
225                 $sth->execute($query, $type, $serverlist, $requestid);
226                 if ( -r $pidfile ) {
227                     # FIXME - Perl is good at opening files. No need to
228                     # spawn a separate 'cat' process.
229                     my $pid=`cat $pidfile`;
230                     chomp $pid;
231                     # Kill -HUP the Z39.50 daemon to tell it to process
232                     # this query.
233                     my $processcount=kill 1, $pid;
234                     if ($processcount==0) {
235                         $error.="Z39.50 search daemon error: no process signalled. ";
236                     }
237                 } else {
238                     # FIXME - Error-checking like this should go close
239                     # to the test.
240                     $error.="No Z39.50 search daemon running: no file $pidfile. ";
241                 } # if $pidfile
242             } else {
243                 # FIXME - Error-checking like this should go close
244                 # to the test.
245                 $error.="Duplicate request ID $requestid. ";
246             } # if rows
247         } else {
248             # FIXME - Error-checking like this should go close to the
249             # test. I.e.,
250             #   return "No Z39.50 search servers specified. "
251             #           if @serverlist eq ();
252
253             # server list is empty
254             $error.="No Z39.50 search servers specified. ";
255         } # if serverlist empty
256
257         return $error;
258
259 } # sub addz3950queue
260
261 1;
262 __END__
263
264 =back
265
266 =head1 AUTHOR
267
268 Koha Developement team <info@koha.org>
269
270 =cut
271
272 #--------------------------------------
273 # $Log$
274 # Revision 1.7  2003/02/19 01:01:06  wolfpac444
275 # Removed the unecessary $dbh argument from being passed.
276 # Resolved a few minor FIXMEs.
277 #
278 # Revision 1.6  2002/10/13 08:30:53  arensb
279 # Deleted unused variables.
280 # Removed trailing whitespace.
281 #
282 # Revision 1.5  2002/10/13 06:13:23  arensb
283 # Removed bogus #! line (this isn't a script!)
284 # Removed unused global variables.
285 # Added POD.
286 # Added some explanatory comments.
287 # Added some FIXME comments.
288 #
289 # Revision 1.4  2002/10/11 12:35:35  arensb
290 # Replaced &requireDBI with C4::Context->dbh
291 #
292 # Revision 1.3  2002/08/14 18:12:52  tonnesen
293 # Added copyright statement to all .pl and .pm files
294 #
295 # Revision 1.2  2002/07/02 20:31:33  tonnesen
296 # module added from rel-1-2 branch
297 #
298 # Revision 1.1.2.5  2002/06/29 17:33:47  amillar
299 # Allow DEFAULT as input to addz3950search.
300 # Check for existence of pid file (cat crashed otherwise).
301 # Return error messages in addz3950search.
302 #
303 # Revision 1.1.2.4  2002/06/28 18:07:27  tonnesen
304 # marcimport.pl will print an error message if it can not signal the
305 # processz3950queue program.  The message contains instructions for starting the
306 # daemon.
307 #
308 # Revision 1.1.2.3  2002/06/28 17:45:39  tonnesen
309 # z3950queue now listens for a -HUP signal before processing the queue.  Z3950.pm
310 # sends the -HUP signal when queries are added to the queue.
311 #
312 # Revision 1.1.2.2  2002/06/26 20:54:31  tonnesen
313 # use warnings breaks on perl 5.005...
314 #
315 # Revision 1.1.2.1  2002/06/26 07:26:41  amillar
316 # New module for Z39.50 searching
317 #