|
Line 0
Link Here
|
|
|
1 |
package C4::Linker::Z3950Server; |
| 2 |
|
| 3 |
# Copyright 2012 Frédérick Capovilla - Libéo |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
use Carp; |
| 21 |
use MARC::Field; |
| 22 |
use MARC::Record; |
| 23 |
use C4::Context; |
| 24 |
use C4::Heading; |
| 25 |
use ZOOM; # For Z39.50 Searches |
| 26 |
use C4::AuthoritiesMarc; # For Authority addition |
| 27 |
use C4::Log; # logaction |
| 28 |
|
| 29 |
use base qw(C4::Linker); |
| 30 |
|
| 31 |
|
| 32 |
sub get_link { |
| 33 |
my $self = shift; |
| 34 |
my $heading = shift; |
| 35 |
my $behavior = shift || 'default'; |
| 36 |
my $search_form = $heading->search_form(); |
| 37 |
my $authid; |
| 38 |
my $fuzzy = 0; |
| 39 |
my $status = ''; |
| 40 |
|
| 41 |
if ( $self->{'cache'}->{$search_form}->{'cached'} ) { |
| 42 |
$authid = $self->{'cache'}->{$search_form}->{'authid'}; |
| 43 |
$fuzzy = $self->{'cache'}->{$search_form}->{'fuzzy'}; |
| 44 |
}else { |
| 45 |
# Look for matching authorities on the Z39.50 server |
| 46 |
unless($self->{'local_first'}) { |
| 47 |
($authid, undef, $status) = $self->getZ3950Authority($heading); |
| 48 |
if($status eq 'Z3950_SEARCH_ERROR' or |
| 49 |
$status eq 'Z3950_QUERY_ERROR' or |
| 50 |
$status eq 'NO_CONNECTION' or |
| 51 |
$status eq 'SERVER_NOT_FOUND') { |
| 52 |
return $authid, $fuzzy, 0, $status; |
| 53 |
} |
| 54 |
} |
| 55 |
if(!$authid) { |
| 56 |
# look for matching authorities |
| 57 |
my $authorities = $heading->authorities(1); # $skipmetadata = true |
| 58 |
$match_count = scalar @$authorities; |
| 59 |
|
| 60 |
if ( $behavior eq 'default' && $#{$authorities} >= 0 ) { |
| 61 |
$authid = $authorities->[0]->{'authid'}; |
| 62 |
}elsif ( $behavior eq 'first' && $#{$authorities} >= 0 ) { |
| 63 |
$authid = $authorities->[0]->{'authid'}; |
| 64 |
$fuzzy = $#{$authorities} > 0; |
| 65 |
} |
| 66 |
elsif ( $behavior eq 'last' && $#{$authorities} >= 0 ) { |
| 67 |
$authid = $authorities->[ $#{$authorities} ]->{'authid'}; |
| 68 |
$fuzzy = $#{$authorities} > 0; |
| 69 |
} |
| 70 |
|
| 71 |
if ( !defined $authid && $self->{'broader_headings'} ) { |
| 72 |
my $field = $heading->field(); |
| 73 |
my @subfields = $field->subfields(); |
| 74 |
if ( scalar @subfields > 1 ) { |
| 75 |
pop @subfields; |
| 76 |
$field->replace_with( |
| 77 |
MARC::Field->new( |
| 78 |
$field->tag, |
| 79 |
$field->indicator(1), |
| 80 |
$field->indicator(2), |
| 81 |
map { $_[0] => $_[1] } @subfields |
| 82 |
) |
| 83 |
); |
| 84 |
( $authid, $fuzzy ) = |
| 85 |
$self->get_link( C4::Heading->new_from_bib_field($field), |
| 86 |
$behavior ); |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
if($self->{'local_first'} && !$authid) { |
| 91 |
($authid, undef, $status) = $self->getZ3950Authority($heading); |
| 92 |
} |
| 93 |
|
| 94 |
$self->{'cache'}->{$search_form}->{'cached'} = 1; |
| 95 |
$self->{'cache'}->{$search_form}->{'authid'} = $authid; |
| 96 |
$self->{'cache'}->{$search_form}->{'fuzzy'} = $fuzzy; |
| 97 |
} |
| 98 |
return $self->SUPER::_handle_auth_limit($authid), $fuzzy, $match_count, $status; |
| 99 |
} |
| 100 |
|
| 101 |
sub update_cache { |
| 102 |
my $self = shift; |
| 103 |
my $heading = shift; |
| 104 |
my $authid = shift; |
| 105 |
my $search_form = $heading->search_form(); |
| 106 |
my $fuzzy = 0; |
| 107 |
|
| 108 |
$self->{'cache'}->{$search_form}->{'cached'} = 1; |
| 109 |
$self->{'cache'}->{$search_form}->{'authid'} = $authid; |
| 110 |
$self->{'cache'}->{$search_form}->{'fuzzy'} = $fuzzy; |
| 111 |
} |
| 112 |
sub flip_heading { |
| 113 |
my $self = shift; |
| 114 |
my $heading = shift; |
| 115 |
|
| 116 |
# TODO: implement |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
=head1 getZ3950Authority |
| 121 |
|
| 122 |
($authid, $record, $status) = $self->getZ3950Authority($heading); |
| 123 |
|
| 124 |
Do a Z39.50 search for the heading using the $conn ZOOM::Connection object and the $heading Heading. |
| 125 |
The column origincode is used to check for duplicates. |
| 126 |
FIXME: Use thesaurus in search? As of Koha 3.8, the community stopped using them. |
| 127 |
|
| 128 |
RETURNS : |
| 129 |
$authid = the ID of the local copy of the authority record that was found. undef if nothing was found. |
| 130 |
$record = the MARC record of the found authority. |
| 131 |
$status = A string with additional informations on the search status (Z3950_CREATED, Z3950_UPDATED, Z3950_QUERY_ERROR, Z3950_SEARCH_ERROR) |
| 132 |
|
| 133 |
=cut |
| 134 |
|
| 135 |
sub getZ3950Authority { |
| 136 |
my $self = shift; |
| 137 |
my $heading = shift; |
| 138 |
|
| 139 |
# Try to find a match on the Z39.50 server if LinkerZ3950Server is set |
| 140 |
if(C4::Context->preference('LinkerZ3950Server')) { |
| 141 |
unless($self->{'conn'}) { |
| 142 |
my $sth = C4::Context->dbh->prepare("select * from z3950servers where servername=?"); |
| 143 |
$sth->execute(C4::Context->preference('LinkerZ3950Server')); |
| 144 |
my $server = $sth->fetchrow_hashref or undef; |
| 145 |
$sth->finish; |
| 146 |
|
| 147 |
if($server) { |
| 148 |
my $options = new ZOOM::Options(); |
| 149 |
$options->option('cclfile' => C4::Context->ModZebrations('authorityserver')->{"ccl2rpn"}); |
| 150 |
$options->option('elementSetName', 'F'); |
| 151 |
$options->option('async', 0); |
| 152 |
$options->option('databaseName', $server->{db}); |
| 153 |
$options->option('user', $server->{userid} ) if $server->{userid}; |
| 154 |
$options->option('password', $server->{password}) if $server->{password}; |
| 155 |
$options->option('preferredRecordSyntax', $server->{syntax}); |
| 156 |
$self->{'conn'} = create ZOOM::Connection($options); |
| 157 |
eval{ $self->{'conn'}->connect( $server->{host}, $server->{port} ) }; |
| 158 |
if($@) { |
| 159 |
return (undef, undef, 'NO_CONNECTION'); |
| 160 |
}} |
| 161 |
else { |
| 162 |
return (undef, undef, 'SERVER_NOT_FOUND'); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
else { |
| 167 |
return; |
| 168 |
} |
| 169 |
my $query =qq(Match-Heading,do-not-truncate,ext="$heading->{'search_form'}"); |
| 170 |
my $zquery = eval{ new ZOOM::Query::CCL2RPN($query, $self->{'conn'}) }; |
| 171 |
if($@) { |
| 172 |
warn $query . "\n" . $@; |
| 173 |
return (undef, undef, 'Z3950_QUERY_ERROR'); |
| 174 |
} |
| 175 |
|
| 176 |
# Try to send the search query to the server. |
| 177 |
my $rs = eval{ $self->{'conn'}->search($zquery) }; |
| 178 |
if($@){ |
| 179 |
warn $query . "\n" . $@; |
| 180 |
return (undef, undef, 'Z3950_SEARCH_EMPTY'); |
| 181 |
} |
| 182 |
|
| 183 |
# If authorities are found, select the first valid one for addition in the local authority table. |
| 184 |
my $record; |
| 185 |
if($rs->size() != 0) { |
| 186 |
$record = MARC::Record::new_from_usmarc($rs->record(0)->raw()); |
| 187 |
}else{ |
| 188 |
return (undef, undef, 'Z3950_SEARCH_ERROR'); |
| 189 |
} |
| 190 |
$rs->destroy(); |
| 191 |
$zquery->destroy(); |
| 192 |
|
| 193 |
# If a valid authority was found, add it in the local authority database. |
| 194 |
if($record) { |
| 195 |
my $dbh=C4::Context->dbh; |
| 196 |
|
| 197 |
my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record); |
| 198 |
my $authId; |
| 199 |
|
| 200 |
# Use the control number to prevent the creation of duplicate authorities. |
| 201 |
my $controlNumber = $record->field('970')->subfield('0'); |
| 202 |
my $sthExist=$dbh->prepare("SELECT authid FROM auth_header WHERE origincode =?"); |
| 203 |
$sthExist->execute($controlNumber) or die $sthExist->errstr; |
| 204 |
($authId) = $sthExist->fetchrow; |
| 205 |
$sthExist->finish; |
| 206 |
|
| 207 |
#------------------------------------------------------------------------------------------ |
| 208 |
# Corrections and verifications before insertion |
| 209 |
my $format; |
| 210 |
my $leader=' nz a22 o 4500';# Leader for incomplete MARC21 record |
| 211 |
|
| 212 |
if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') { |
| 213 |
$format= 'UNIMARCAUTH'; |
| 214 |
} |
| 215 |
else { |
| 216 |
$format= 'MARC21'; |
| 217 |
} |
| 218 |
|
| 219 |
if ($format eq "MARC21") { |
| 220 |
if (!$record->leader) { |
| 221 |
$record->leader($leader); |
| 222 |
} |
| 223 |
if (!$record->field('003')) { |
| 224 |
$record->insert_fields_ordered( |
| 225 |
MARC::Field->new('003',C4::Context->preference('MARCOrgCode')) |
| 226 |
); |
| 227 |
} |
| 228 |
my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime); |
| 229 |
if (!$record->field('005')) { |
| 230 |
$record->insert_fields_ordered( |
| 231 |
MARC::Field->new('005',$time.".0") |
| 232 |
); |
| 233 |
} |
| 234 |
my $date=POSIX::strftime("%y%m%d",localtime); |
| 235 |
if (!$record->field('008')) { |
| 236 |
# Get a valid default value for field 008 |
| 237 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
| 238 |
if(!$default_008 or length($default_008)<34) { |
| 239 |
$default_008 = '|| aca||aabn | a|a d'; |
| 240 |
} |
| 241 |
else { |
| 242 |
$default_008 = substr($default_008,0,34); |
| 243 |
} |
| 244 |
$record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) ); |
| 245 |
} |
| 246 |
if (!$record->field('040')) { |
| 247 |
$record->insert_fields_ordered( |
| 248 |
MARC::Field->new('040','','', |
| 249 |
'a' => C4::Context->preference('MARCOrgCode'), |
| 250 |
'c' => C4::Context->preference('MARCOrgCode') |
| 251 |
) |
| 252 |
); |
| 253 |
} |
| 254 |
} |
| 255 |
if ($format eq "UNIMARCAUTH") { |
| 256 |
$record->leader(" nx j22 ") unless ($record->leader()); |
| 257 |
my $date=POSIX::strftime("%Y%m%d",localtime); |
| 258 |
if (my $string=$record->subfield('100',"a")){ |
| 259 |
$string=~s/fre50/frey50/; |
| 260 |
$record->field('100')->update('a'=>$string); |
| 261 |
} |
| 262 |
elsif ($record->field('100')){ |
| 263 |
$record->field('100')->update('a'=>$date."afrey50 ba0"); |
| 264 |
} else { |
| 265 |
$record->append_fields( |
| 266 |
MARC::Field->new('100',' ',' ' |
| 267 |
,'a'=>$date."afrey50 ba0") |
| 268 |
); |
| 269 |
} |
| 270 |
} |
| 271 |
my ($auth_type_tag, $auth_type_subfield) = C4::AuthoritiesMarc::get_auth_type_location($authtypecode); |
| 272 |
if (!$authId and $format eq "MARC21") { |
| 273 |
# only need to do this fix when modifying an existing authority |
| 274 |
C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield); |
| 275 |
} |
| 276 |
if (my $field=$record->field($auth_type_tag)){ |
| 277 |
$field->update($auth_type_subfield=>$authtypecode); |
| 278 |
} |
| 279 |
else { |
| 280 |
$record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode); |
| 281 |
} |
| 282 |
#------------------------------------------------------------------------------------------ |
| 283 |
if ($authId) { |
| 284 |
my $oldRecord=C4::AuthoritiesMarc::GetAuthority($authId); |
| 285 |
|
| 286 |
my $sth=$dbh->prepare("UPDATE auth_header SET authtypecode=?,marc=?,marcxml=? WHERE origincode =?"); |
| 287 |
eval { $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$controlNumber) or die $sth->errstr; }; |
| 288 |
$sth->finish; |
| 289 |
warn "Problem with authority $controlNumber : Cannot update" if $@; |
| 290 |
$dbh->commit unless $dbh->{AutoCommit}; |
| 291 |
return if $@; |
| 292 |
|
| 293 |
C4::Biblio::ModZebra($authId,'linkerUpdate',"authorityserver",$oldRecord,$record); |
| 294 |
return ($authId, $record, 'Z3950_UPDATED'); |
| 295 |
} |
| 296 |
else { |
| 297 |
my $sth=$dbh->prepare("INSERT INTO auth_header (datecreated,authtypecode,marc,marcxml,origincode) VALUES (NOW(),?,?,?,?)"); |
| 298 |
eval { $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$controlNumber) or die $sth->errstr; }; |
| 299 |
$sth->finish; |
| 300 |
warn "Problem with authority $controlNumber : Cannot insert" if $@; |
| 301 |
my $id = $dbh->{'mysql_insertid'}; |
| 302 |
$dbh->commit unless $dbh->{AutoCommit}; |
| 303 |
return if $@; |
| 304 |
|
| 305 |
logaction( "AUTHORITIES", "ADD", $id, "authority" ) if C4::Context->preference("AuthoritiesLog"); |
| 306 |
C4::Biblio::ModZebra($id,'linkerUpdate',"authorityserver",undef,$record); |
| 307 |
return ($id, $record, 'Z3950_CREATED'); |
| 308 |
} |
| 309 |
} |
| 310 |
return ; |
| 311 |
} |
| 312 |
|
| 313 |
1; |