+=item addz3950queue
+
+ $errmsg = &addz3950queue($query, $type, $request_id, @servers);
+
+Adds a Z39.50 search query for the Z39.50 server to look up.
+
+C<$query> is the term to search for.
+
+C<$type> is the query type, e.g. C<isbn>, C<lccn>, etc.
+
+C<$request_id> is a unique string that will identify this query.
+
+C<@servers> is a list of servers to query (obviously, this can be
+given either as an array, or as a list of scalars). Each element may
+be either a Z39.50 server ID from the z3950server table of the Koha
+database, the string C<DEFAULT> or C<CHECKED>, or a complete server
+specification containing a colon.
+
+C<DEFAULT> and C<CHECKED> are synonymous, and refer to those servers
+in the z3950servers table whose 'checked' field is set and non-NULL.
+
+Once the query has been submitted to the Z39.50 daemon,
+C<&addz3950queue> sends a SIGHUP to the daemon to tell it to process
+this new request.
+
+C<&addz3950queue> returns an error message. If it was successful, the
+error message is the empty string.
+
+=cut
+
+#'
+sub addz3950queue {
+ use strict;
+ # input
+ my (
+ $query, # value to look up
+ $type, # type of value ("isbn", "lccn", "title", "author", "keyword")
+ $requestid, # Unique value to prevent duplicate searches from multiple HTML form submits
+ @z3950list, # list of z3950 servers to query
+ )=@_;
+ # Returns:
+ my $error;
+
+ my (
+ $sth,
+ @serverlist,
+ $server,
+ $failed,
+ $servername,
+ );
+
+ # FIXME - Should be configurable, probably in /etc/koha.conf.
+ my $pidfile='/var/log/koha/processz3950queue.pid';
+
+ $error="";
+
+ my $dbh = C4::Context->dbh;
+ # list of servers: entry can be a fully qualified URL-type entry
+ # or simply just a server ID number.
+ foreach $server (@z3950list) {
+ if ($server =~ /:/ ) {
+ push @serverlist, $server;
+ } elsif ($server eq 'DEFAULT' || $server eq 'CHECKED' ) {
+ $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,$syntax) = $sth->fetchrow ) {
+ push @serverlist, "$servername/$host\:$port/$db/$userid/$password/$syntax";
+ } # while
+ } else {
+ $sth=$dbh->prepare("select host,port,db,userid,password,syntax from z3950servers where id=? ");
+ $sth->execute($server);
+ 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;
+
+ # FIXME - Is this test supposed to test whether @serverlist is
+ # empty? If so, then a) there are better ways to do that in
+ # Perl (e.g., "if (@serverlist eq ())"), and b) it doesn't
+ # work anyway, since it checks whether $serverlist is composed
+ # of one or more spaces, which is never the case, not even
+ # when there are 0 or 1 elements in @serverlist.
+ if ( $serverlist !~ /^ +$/ ) {
+ # Don't allow reinsertion of the same request identifier.
+ $sth=$dbh->prepare("select identifier from z3950queue
+ where identifier=?");
+ $sth->execute($requestid);
+ if ( ! $sth->rows) {
+ $sth=$dbh->prepare("insert into z3950queue (term,type,servers, identifier) values (?, ?, ?, ?)");
+ $sth->execute($query, $type, $serverlist, $requestid);
+ if ( -r $pidfile ) {
+ # FIXME - Perl is good at opening files. No need to
+ # spawn a separate 'cat' process.
+ my $pid=`cat $pidfile`;
+ chomp $pid;
+ # Kill -HUP the Z39.50 daemon to tell it to process
+ # this query.
+ my $processcount=kill 1, $pid;
+ if ($processcount==0) {
+ $error.="Z39.50 search daemon error: no process signalled. ";
+ }
+ } else {
+ # FIXME - Error-checking like this should go close
+ # to the test.
+ $error.="No Z39.50 search daemon running: no file $pidfile. ";
+ } # if $pidfile
+ } else {
+ # FIXME - Error-checking like this should go close
+ # to the test.
+ $error.="Duplicate request ID $requestid. ";
+ } # if rows
+ } else {
+ # FIXME - Error-checking like this should go close to the
+ # test. I.e.,
+ # return "No Z39.50 search servers specified. "
+ # if @serverlist eq ();
+
+ # server list is empty
+ $error.="No Z39.50 search servers specified. ";
+ } # if serverlist empty