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