From 1088b08537d8ff491b1df82f24236860e8bb833c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9rick?= Date: Wed, 6 Nov 2013 12:34:21 -0500 Subject: [PATCH 1/2] Add support for custom Z39.50 attributes for Z39.50 servers. This feature can be useful if a Z39.50 server requires a certain attribute to always be set to return results. --- C4/Breeding.pm | 57 +++++++++++++++----- admin/z3950servers.pl | 17 ++++--- installer/data/mysql/updatedatabase.pl | 7 +++ .../prog/en/modules/admin/z3950servers.tt | 3 + 4 files changed, 63 insertions(+), 21 deletions(-) diff --git a/C4/Breeding.pm b/C4/Breeding.pm index 8e01d41..1d20f4a 100644 --- a/C4/Breeding.pm +++ b/C4/Breeding.pm @@ -254,59 +254,59 @@ sub Z3950Search { my @oResult; my @errconn; my $s = 0; - my $query; + my @query = ''; my $nterms=0; my $imported=0; my @serverinfo; #replaces former serverhost, servername, encoding if ($isbn) { $term=$isbn; - $query .= " \@attr 1=7 \@attr 5=1 \"$term\" "; + push @query, "\@attr 1=7 \@attr 5=1 \"$term\""; $nterms++; } if ($issn) { $term=$issn; - $query .= " \@attr 1=8 \@attr 5=1 \"$term\" "; + push @query, "\@attr 1=8 \@attr 5=1 \"$term\""; $nterms++; } if ($title) { - $query .= " \@attr 1=4 \"$title\" "; + push @query, "\@attr 1=4 \"$title\""; $nterms++; } if ($author) { - $query .= " \@attr 1=1003 \"$author\" "; + push @query, "\@attr 1=1003 \"$author\""; $nterms++; } if ($dewey) { - $query .= " \@attr 1=16 \"$dewey\" "; + push @query, "\@attr 1=16 \"$dewey\""; $nterms++; } if ($subject) { - $query .= " \@attr 1=21 \"$subject\" "; + push @query, "\@attr 1=21 \"$subject\""; $nterms++; } if ($lccn) { - $query .= " \@attr 1=9 $lccn "; + push @query, "\@attr 1=9 $lccn"; $nterms++; } if ($lccall) { - $query .= " \@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\" "; + push @query, "\@attr 1=16 \@attr 2=3 \@attr 3=1 \@attr 4=1 \@attr 5=1 \@attr 6=1 \"$lccall\""; $nterms++; } if ($controlnumber) { - $query .= " \@attr 1=12 \"$controlnumber\" "; + push @query, "\@attr 1=12 \"$controlnumber\""; $nterms++; } if($srchany) { - $query .= " \@attr 1=1016 \"$srchany\" "; + push @query, "\@attr 1=1016 \"$srchany\""; $nterms++; } if($stdid) { - $query .= " \@attr 1=1007 \"$stdid\" "; + push @query, "\@attr 1=1007 \"$stdid\""; $nterms++; } for my $i (1..$nterms-1) { - $query = "\@and " . $query; + $query[0] = "\@and " . $query[0]; } my $dbh = C4::Context->dbh; @@ -326,6 +326,35 @@ sub Z3950Search { $oConnection[$s]->connect( $server->{host}, $server->{port} ); $serverinfo[$s]->{host}= $server->{host}; $serverinfo[$s]->{name}= $server->{name}; + # Add additional attributes if some specified + if($server->{attributes}) { + # Extract all additional server attributes + my %additional_attributes; + my @split_attributes = split(/\s+/, $server->{attributes}); + for(my $i=0; $i<=$#split_attributes; $i++) { + if($split_attributes[$i] eq "\@attr" and $split_attributes[$i+1] =~ /^(\d+)=\d+$/) { + $additional_attributes{$1} = $split_attributes[$i] . ' ' . $split_attributes[$i+1]; + $i++; + } + } + + # Clone the query and modify it + $serverinfo[$s]->{query} = [@query]; + foreach my $querypart (@{$serverinfo[$s]->{query}}) { + # Ignore parts without attributes + next if $querypart !~ /\@attr/; + # Only add additional attributes if they don't already exist in the query part + foreach my $attr_type (keys %additional_attributes) { + if($querypart !~ /\@attr $attr_type=/) { + $querypart = $additional_attributes{$attr_type} . ' ' . $querypart; + } + } + } + } + else { + # Use a reference to the original query + $serverinfo[$s]->{query} = \@query; + } $serverinfo[$s]->{encd}= $server->{encoding} // "iso-5426"; $s++; } ## while fetch @@ -333,7 +362,7 @@ sub Z3950Search { my $nremaining = $s; for ( my $z = 0 ; $z < $s ; $z++ ) { - $oResult[$z] = $oConnection[$z]->search_pqf($query); + $oResult[$z] = $oConnection[$z]->search_pqf( join(' ', @{$serverinfo[$z]->{query}}) ) } while ( $nremaining-- ) { diff --git a/admin/z3950servers.pl b/admin/z3950servers.pl index 1dbd118..8907f69 100755 --- a/admin/z3950servers.pl +++ b/admin/z3950servers.pl @@ -93,14 +93,14 @@ if ( $op eq 'add_form' ) { if ($searchfield) { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare( -"select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype from z3950servers where (name = ?) order by rank,name" +"select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype,attributes from z3950servers where (name = ?) order by rank,name" ); $sth->execute($searchfield); $data = $sth->fetchrow_hashref; $sth->finish; } $template->param( $_ => $data->{$_} ) - for (qw( host port db userid password checked rank timeout encoding )); + for (qw( host port db userid password checked rank timeout encoding attributes )); $template->param( $_ . $data->{$_} => 1 ) for (qw( syntax recordtype )); # END $OP eq ADD_FORM @@ -113,7 +113,7 @@ if ( $op eq 'add_form' ) { my $checked = $input->param('checked') ? 1 : 0; if ($sth->rows) { $template->param(confirm_update => 1); - $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=?,encoding=?,timeout=?,recordtype=? where name=?"); + $sth=$dbh->prepare("update z3950servers set host=?, port=?, db=?, userid=?, password=?, name=?, checked=?, rank=?,syntax=?,encoding=?,timeout=?,recordtype=?,attributes=? where name=?"); $sth->execute($input->param('host'), $input->param('port'), $input->param('db'), @@ -126,6 +126,7 @@ if ( $op eq 'add_form' ) { $input->param('encoding'), $input->param('timeout'), $input->param('recordtype'), + $input->param('attributes'), $input->param('searchfield'), ); } @@ -133,15 +134,16 @@ if ( $op eq 'add_form' ) { $template->param(confirm_add => 1); $sth=$dbh->prepare( "INSERT INTO z3950servers " . - "(host,port,db,userid,password,name,checked,rank,syntax,encoding,timeout,recordtype) " . - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ); + "(host,port,db,userid,password,name,checked,rank,syntax,encoding,timeout,recordtype,attributes) " . + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ); $sth->execute( $input->param('host'), $input->param('port'), $input->param('db'), $input->param('userid'), $input->param('password'), $input->param('searchfield'), $checked, $input->param('rank'), $input->param('syntax'), $input->param('encoding'), - $input->param('timeout'), $input->param('recordtype') + $input->param('timeout'), $input->param('recordtype'), + $input->param('attributes') ); } $sth->finish; @@ -154,7 +156,7 @@ if ( $op eq 'add_form' ) { my $dbh = C4::Context->dbh; my $sth2 = $dbh->prepare( -"select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype from z3950servers where (name = ?) order by rank,name" +"select host,port,db,userid,password,name,id,checked,rank,syntax,encoding,timeout,recordtype,attributes from z3950servers where (name = ?) order by rank,name" ); $sth2->execute($searchfield); my $data = $sth2->fetchrow_hashref; @@ -171,6 +173,7 @@ if ( $op eq 'add_form' ) { syntax => $data->{'syntax'}, timeout => $data->{'timeout'}, recordtype => $data->{'recordtype'}, + attributes => $data->{'attributes'}, encoding => $data->{'encoding'} ); diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index b031ed8..9ace5cf 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -7330,6 +7330,13 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } +$DBversion = "3.13.00.XXX"; +if ( CheckVersion($DBversion) ) { + $dbh->do("ALTER TABLE z3950servers ADD COLUMN attributes VARCHAR(255);"); + print "Upgrade to $DBversion done (Add new column z3950servers.attributes)\n"; + SetVersion($DBversion); +} + =head1 FUNCTIONS =head2 TableExists($table) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/z3950servers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/z3950servers.tt index 915e531..b3249c1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/z3950servers.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/z3950servers.tt @@ -139,6 +139,8 @@
  • +
  • +
  • -- 1.7.2.5