Removed bogus #! line (this isn't a script!)
[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 sub z3950servername {
89     # inputs
90     my (
91         $dbh,           # FIXME - Unused argument
92         $srvid,         # server id number 
93         $default,
94     )=@_;
95     # return
96     my $longname;
97     #----
98
99     $dbh = C4::Context->dbh;
100
101         # FIXME - Fix indentation
102         my $sti=$dbh->prepare("select name 
103                 from z3950servers 
104                 where id=?");
105         $sti->execute($srvid);
106         if ( ! $sti->err ) {
107             ($longname)=$sti->fetchrow;
108         }
109         if (! $longname) {
110             $longname="$default";
111         }
112         return $longname;
113 } # sub z3950servername
114
115 #---------------------------------------
116
117 =item addz3950queue
118
119   $errmsg = &addz3950queue($dbh, $query, $type, $request_id, @servers);
120
121 Adds a Z39.50 search query for the Z39.50 server to look up.
122
123 C<$dbh> is obsolete and is ignored.
124
125 C<$query> is the term to search for.
126
127 C<$type> is the query type, e.g. C<isbn>, C<lccn>, etc.
128
129 C<$request_id> is a unique string that will identify this query.
130
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.
136
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.
139
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
142 this new request.
143
144 C<&addz3950queue> returns an error message. If it was successful, the
145 error message is the empty string.
146
147 =cut
148 #'
149 sub addz3950queue {
150     use strict;
151     # input
152     my (
153         $dbh,           # DBI handle
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
160     )=@_;
161     # Returns:
162     my $error;
163
164     my (
165         $sth,
166         @serverlist,
167         $server,
168         $failed,
169         $servername,
170     );
171
172     # FIXME - Should be configurable, probably in /etc/koha.conf.
173     my $pidfile='/var/log/koha/processz3950queue.pid';
174     
175     $error="";
176
177     $dbh = C4::Context->dbh;
178
179         # FIXME - Fix indentation
180
181         # list of servers: entry can be a fully qualified URL-type entry
182         #   or simply just a server ID number.
183
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
189                   from z3950servers 
190                   where checked <> 0 ");
191                 $sth->execute;
192                 while ( my ($host, $port, $db, $userid, $password,$servername) 
193                         = $sth->fetchrow ) {
194                     push @serverlist, "$servername/$host\:$port/$db/$userid/$password";
195                 } # while
196             } else {
197                 $sth=$dbh->prepare("select host,port,db,userid,password
198                   from z3950servers 
199                   where id=? ");
200                 $sth->execute($server);
201                 my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
202                 push @serverlist, "$server/$host\:$port/$db/$userid/$password";
203             }
204         }
205
206         my $serverlist='';
207         # FIXME - $serverlist = join(" ", @serverlist);
208         foreach (@serverlist) {
209             $serverlist.="$_ ";
210         } # foreach
211         chop $serverlist;
212
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);
224             if ( ! $sth->rows) {
225                 $sth=$dbh->prepare("insert into z3950queue 
226                     (term,type,servers, identifier) 
227                     values (?, ?, ?, ?)");
228                 $sth->execute($query, $type, $serverlist, $requestid);
229                 if ( -r $pidfile ) { 
230                     # FIXME - Perl is good at opening files. No need to
231                     # spawn a separate 'cat' process.
232                     my $pid=`cat $pidfile`;
233                     chomp $pid;
234                     # Kill -HUP the Z39.50 daemon to tell it to process
235                     # this query.
236                     my $processcount=kill 1, $pid;
237                     if ($processcount==0) {
238                         $error.="Z39.50 search daemon error: no process signalled. ";
239                     }
240                 } else {
241                     # FIXME - Error-checking like this should go close
242                     # to the test.
243                     $error.="No Z39.50 search daemon running: no file $pidfile. ";
244                 } # if $pidfile
245             } else {
246                 # FIXME - Error-checking like this should go close
247                 # to the test.
248                 $error.="Duplicate request ID $requestid. ";
249             } # if rows
250         } else {
251             # FIXME - Error-checking like this should go close to the
252             # test. I.e.,
253             #   return "No Z39.50 search servers specified. "
254             #           if @serverlist eq ();
255
256             # server list is empty
257             $error.="No Z39.50 search servers specified. ";
258         } # if serverlist empty
259         
260         return $error;
261
262 } # sub addz3950queue
263
264 1;
265 __END__
266
267 =back
268
269 =head1 AUTHOR
270
271 Koha Developement team <info@koha.org>
272
273 =cut
274
275 #--------------------------------------
276 # $Log$
277 # Revision 1.5  2002/10/13 06:13:23  arensb
278 # Removed bogus #! line (this isn't a script!)
279 # Removed unused global variables.
280 # Added POD.
281 # Added some explanatory comments.
282 # Added some FIXME comments.
283 #
284 # Revision 1.4  2002/10/11 12:35:35  arensb
285 # Replaced &requireDBI with C4::Context->dbh
286 #
287 # Revision 1.3  2002/08/14 18:12:52  tonnesen
288 # Added copyright statement to all .pl and .pm files
289 #
290 # Revision 1.2  2002/07/02 20:31:33  tonnesen
291 # module added from rel-1-2 branch
292 #
293 # Revision 1.1.2.5  2002/06/29 17:33:47  amillar
294 # Allow DEFAULT as input to addz3950search.
295 # Check for existence of pid file (cat crashed otherwise).
296 # Return error messages in addz3950search.
297 #
298 # Revision 1.1.2.4  2002/06/28 18:07:27  tonnesen
299 # marcimport.pl will print an error message if it can not signal the
300 # processz3950queue program.  The message contains instructions for starting the
301 # daemon.
302 #
303 # Revision 1.1.2.3  2002/06/28 17:45:39  tonnesen
304 # z3950queue now listens for a -HUP signal before processing the queue.  Z3950.pm
305 # sends the -HUP signal when queries are added to the queue.
306 #
307 # Revision 1.1.2.2  2002/06/26 20:54:31  tonnesen
308 # use warnings breaks on perl 5.005...
309 #
310 # Revision 1.1.2.1  2002/06/26 07:26:41  amillar
311 # New module for Z39.50 searching
312 #