[followup] (bug #4051) fix default values
[koha.git] / C4 / Z3950.pm
old mode 100755 (executable)
new mode 100644 (file)
index a6d2586..4c830ec
@@ -1,6 +1,5 @@
 package C4::Z3950;
 
-# $Id$
 
 # Routines for handling Z39.50 lookups
 
@@ -8,7 +7,6 @@ package C4::Z3950;
 
 # Licensed under the GPL
 
-
 # Copyright 2000-2002 Katipo Communications
 #
 # This file is part of Koha.
@@ -32,18 +30,23 @@ use strict;
 use DBI;
 
 # Koha modules used
-use C4::Database;
 use C4::Input;
 use C4::Biblio;
 
-#------------------
-
-require Exporter;
-
 use vars qw($VERSION @ISA @EXPORT);
 
-# set the version for version checking
-$VERSION = 0.01;
+BEGIN {
+       # set the version for version checking
+       $VERSION = 3.01;
+       require Exporter;
+       @ISA = qw(Exporter);
+       @EXPORT = qw(
+               &getz3950servers
+               &z3950servername
+               &addz3950queue
+               &checkz3950searchdone
+       );
+}
 
 =head1 NAME
 
@@ -62,16 +65,6 @@ entering Z39.50 lookup requests.
 
 =over 2
 
-=cut
-
-@ISA = qw(Exporter);
-@EXPORT = qw(
-       &getz3950servers
-       &z3950servername
-       &addz3950queue
-);
-
-#------------------------------------------------
 =item getz3950servers
 
   @servers= &getz3950servers(checked);
@@ -82,6 +75,7 @@ C<$checked> should always be true (1) => returns only active servers.
 If 0 => returns all servers
 
 =cut
+
 sub getz3950servers {
        my ($checked) = @_;
        my $dbh = C4::Context->dbh;
@@ -110,6 +104,7 @@ C<$server_id> is the Z39.50 server ID to look up.
 C<$dbh> is ignored.
 
 =cut
+
 #'
 
 sub z3950servername {
@@ -165,6 +160,7 @@ C<&addz3950queue> returns an error message. If it was successful, the
 error message is the empty string.
 
 =cut
+
 #'
 sub addz3950queue {
        use strict;
@@ -198,23 +194,23 @@ sub addz3950queue {
                if ($server =~ /:/ ) {
                        push @serverlist, $server;
                } elsif ($server eq 'DEFAULT' || $server eq 'CHECKED' ) {
-                       $sth=$dbh->prepare("select host,port,db,userid,password ,name from z3950servers where checked <> 0 ");
+                       $sth=$dbh->prepare("select host,port,db,userid,password ,name,syntax from z3950servers where checked <> 0 ");
                        $sth->execute;
-                       while ( my ($host, $port, $db, $userid, $password,$servername) = $sth->fetchrow ) {
-                               push @serverlist, "$servername/$host\:$port/$db/$userid/$password";
+                       while ( my ($host, $port, $db, $userid, $password,$servername,$syntax) = $sth->fetchrow ) {
+                               push @serverlist, "$servername/$host\:$port/$db/$userid/$password/$syntax";
                        } # while
                } else {
-                       $sth=$dbh->prepare("select host,port,db,userid,password from z3950servers where id=? ");
+                       $sth=$dbh->prepare("select host,port,db,userid,password,syntax from z3950servers where id=? ");
                        $sth->execute($server);
-                       my ($host, $port, $db, $userid, $password) = $sth->fetchrow;
-                       push @serverlist, "$server/$host\:$port/$db/$userid/$password";
+                       my ($host, $port, $db, $userid, $password,$syntax) = $sth->fetchrow;
+                       push @serverlist, "$server/$host\:$port/$db/$userid/$password/$syntax";
                }
        }
 
        my $serverlist='';
 
-       $serverlist = join(" ", @serverlist);
-       chop $serverlist;
+       $serverlist = join("|", @serverlist);
+#      chop $serverlist;
 
        # FIXME - Is this test supposed to test whether @serverlist is
        # empty? If so, then a) there are better ways to do that in
@@ -266,6 +262,33 @@ sub addz3950queue {
 
 } # sub addz3950queue
 
+=item &checkz3950searchdone
+
+  $numberpending= &    &checkz3950searchdone($random);
+
+Returns the number of pending z3950 requests
+
+C<$random> is the random z3950 query number.
+
+=cut
+
+sub checkz3950searchdone {
+       my ($z3950random) = @_;
+       my $dbh = C4::Context->dbh;
+       # first, check that the deamon already created the requests...
+       my $sth = $dbh->prepare("select count(*) from z3950queue,z3950results where z3950queue.id = z3950results.queryid and z3950queue.identifier=?");
+       $sth->execute($z3950random);
+       my ($result) = $sth->fetchrow;
+       if ($result eq 0) { # search not yet begun => should be searches to do !
+               return "??";
+       }
+       # second, count pending requests
+       $sth = $dbh->prepare("select count(*) from z3950queue,z3950results where z3950queue.id = z3950results.queryid and z3950results.enddate is null and z3950queue.identifier=?");
+       $sth->execute($z3950random);
+       ($result) = $sth->fetchrow;
+       return $result;
+}
+
 1;
 __END__
 
@@ -278,7 +301,30 @@ Koha Developement team <info@koha.org>
 =cut
 
 #--------------------------------------
-# $Log$
+# Revision 1.14  2007/03/09 14:31:47  tipaul
+# rel_3_0 moved to HEAD
+#
+# Revision 1.10.10.1  2006/12/22 15:09:54  toins
+# removing C4::Database;
+#
+# Revision 1.10  2003/10/01 15:08:14  tipaul
+# fix fog bug #622 : processz3950queue fails
+#
+# Revision 1.9  2003/04/29 16:50:51  tipaul
+# really proud of this commit :-)
+# z3950 search and import seems to works fine.
+# Let me explain how :
+# * a "search z3950" button is added in the addbiblio template.
+# * when clicked, a popup appears and z3950/search.pl is called
+# * z3950/search.pl calls addz3950search in the DB
+# * the z3950 daemon retrieve the records and stores them in import_batches/import_records/import_biblios tables.
+# * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
+# * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
+#
+# Note :
+# * 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.
+# * 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.
+#
 # Revision 1.8  2003/04/29 08:09:45  tipaul
 # z3950 support is coming...
 # * 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.