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