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