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