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