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, $status; |
53 |
} |
54 |
} |
55 |
if(!$authid) { |
56 |
# look for matching authorities |
57 |
my $authorities = $heading->authorities(1); # $skipmetadata = true |
58 |
|
59 |
if ( $behavior eq 'default' && $#{$authorities} == 0 ) { |
60 |
$authid = $authorities->[0]->{'authid'}; |
61 |
}elsif ( $behavior eq 'first' && $#{$authorities} >= 0 ) { |
62 |
$authid = $authorities->[0]->{'authid'}; |
63 |
$fuzzy = $#{$authorities} > 0; |
64 |
} |
65 |
elsif ( $behavior eq 'last' && $#{$authorities} >= 0 ) { |
66 |
$authid = $authorities->[ $#{$authorities} ]->{'authid'}; |
67 |
$fuzzy = $#{$authorities} > 0; |
68 |
} |
69 |
|
70 |
if ( !defined $authid && $self->{'broader_headings'} ) { |
71 |
my $field = $heading->field(); |
72 |
my @subfields = $field->subfields(); |
73 |
if ( scalar @subfields > 1 ) { |
74 |
pop @subfields; |
75 |
$field->replace_with( |
76 |
MARC::Field->new( |
77 |
$field->tag, |
78 |
$field->indicator(1), |
79 |
$field->indicator(2), |
80 |
map { $_[0] => $_[1] } @subfields |
81 |
) |
82 |
); |
83 |
( $authid, $fuzzy ) = |
84 |
$self->get_link( C4::Heading->new_from_bib_field($field), |
85 |
$behavior ); |
86 |
} |
87 |
} |
88 |
} |
89 |
if($self->{'local_first'} && !$authid) { |
90 |
($authid, undef, $status) = $self->getZ3950Authority($heading); |
91 |
} |
92 |
|
93 |
$self->{'cache'}->{$search_form}->{'cached'} = 1; |
94 |
$self->{'cache'}->{$search_form}->{'authid'} = $authid; |
95 |
$self->{'cache'}->{$search_form}->{'fuzzy'} = $fuzzy; |
96 |
} |
97 |
return $self->SUPER::_handle_auth_limit($authid), $fuzzy, $status; |
98 |
} |
99 |
|
100 |
sub update_cache { |
101 |
my $self = shift; |
102 |
my $heading = shift; |
103 |
my $authid = shift; |
104 |
my $search_form = $heading->search_form(); |
105 |
my $fuzzy = 0; |
106 |
|
107 |
$self->{'cache'}->{$search_form}->{'cached'} = 1; |
108 |
$self->{'cache'}->{$search_form}->{'authid'} = $authid; |
109 |
$self->{'cache'}->{$search_form}->{'fuzzy'} = $fuzzy; |
110 |
} |
111 |
sub flip_heading { |
112 |
my $self = shift; |
113 |
my $heading = shift; |
114 |
|
115 |
# TODO: implement |
116 |
} |
117 |
|
118 |
|
119 |
=head1 getZ3950Authority |
120 |
|
121 |
($authid, $record, $status) = $self->getZ3950Authority($heading); |
122 |
|
123 |
Do a Z39.50 search for the heading using the $conn ZOOM::Connection object and the $heading Heading. |
124 |
The column origincode is used to check for duplicates. |
125 |
FIXME: Use thesaurus in search? As of Koha 3.8, the community stopped using them. |
126 |
|
127 |
RETURNS : |
128 |
$authid = the ID of the local copy of the authority record that was found. undef if nothing was found. |
129 |
$record = the MARC record of the found authority. |
130 |
$status = A string with additional informations on the search status (Z3950_CREATED, Z3950_UPDATED, Z3950_QUERY_ERROR, Z3950_SEARCH_ERROR) |
131 |
|
132 |
=cut |
133 |
|
134 |
sub getZ3950Authority { |
135 |
my $self = shift; |
136 |
my $heading = shift; |
137 |
|
138 |
# Try to find a match on the Z39.50 server if LinkerZ3950Server is set |
139 |
if(C4::Context->preference('LinkerZ3950Server')) { |
140 |
unless($self->{'conn'}) { |
141 |
my $sth = C4::Context->dbh->prepare("select * from z3950servers where servername=?"); |
142 |
$sth->execute(C4::Context->preference('LinkerZ3950Server')); |
143 |
my $server = $sth->fetchrow_hashref or undef; |
144 |
$sth->finish; |
145 |
|
146 |
if($server) { |
147 |
my $options = new ZOOM::Options(); |
148 |
$options->option('cclfile' => C4::Context->ModZebrations('authorityserver')->{"ccl2rpn"}); |
149 |
$options->option('elementSetName', 'F'); |
150 |
$options->option('async', 0); |
151 |
$options->option('databaseName', $server->{db}); |
152 |
$options->option('user', $server->{userid} ) if $server->{userid}; |
153 |
$options->option('password', $server->{password}) if $server->{password}; |
154 |
$options->option('preferredRecordSyntax', $server->{syntax}); |
155 |
$self->{'conn'} = create ZOOM::Connection($options); |
156 |
eval{ $self->{'conn'}->connect( $server->{host}, $server->{port} ) }; |
157 |
if($@) { |
158 |
return (undef, undef, 'NO_CONNECTION'); |
159 |
}} |
160 |
else { |
161 |
return (undef, undef, 'SERVER_NOT_FOUND'); |
162 |
} |
163 |
} |
164 |
} |
165 |
else { |
166 |
return; |
167 |
} |
168 |
my $query; |
169 |
if ($heading->{'auth_type'} eq 'PERSO_NAME' ){ |
170 |
$query =qq(Personal-name,do-not-truncate,ext="$heading->{'search_form'}"); |
171 |
}elsif ($heading->{'auth_type'} eq 'UNIF_TITLE' ) { |
172 |
$query =qq(Title-uniform,do-not-truncate,ext="$heading->{'search_form'}"); |
173 |
}elsif ($heading->{'auth_type'} eq 'MEETI_NAME' ){ |
174 |
$query =qq(Meeting-name-heading,do-not-truncate,ext="$heading->{'search_form'}"); |
175 |
}elsif ($heading->{'auth_type'} eq 'GEOGR_NAME' ){ |
176 |
$query =qq(Name-geographic,do-not-truncate,ext="$heading->{'search_form'}"); |
177 |
}elsif ($heading->{'auth_type'} eq 'GENRE/FORM' ){ |
178 |
$query =qq(Term-genre-form,do-not-truncate,ext="$heading->{'search_form'}"); |
179 |
}elsif ($heading->{'auth_type'} eq 'CORPO_NAME' ) { |
180 |
$query =qq(Corporate-name,do-not-truncate,ext="$heading->{'search_form'}"); |
181 |
}elsif ( $heading->{'auth_type'} eq 'CHRON_TERM' ) { |
182 |
$query =qq(Chronological-term,do-not-truncate,ext="$heading->{'search_form'}"); |
183 |
}elsif ($heading->{'auth_type'} eq 'TOPIC_TERM' ){ |
184 |
$query =qq(Subject-topical,do-not-truncate,ext="$heading->{'search_form'}"); |
185 |
} |
186 |
|
187 |
my $zquery = eval{ new ZOOM::Query::CCL2RPN($query, $self->{'conn'}) }; |
188 |
if($@) { |
189 |
warn $query . "\n" . $@; |
190 |
return (undef, undef, 'Z3950_QUERY_ERROR'); |
191 |
} |
192 |
|
193 |
# Try to send the search query to the server. |
194 |
my $rs = eval{ $self->{'conn'}->search($zquery) }; |
195 |
if($@){ |
196 |
warn $query . "\n" . $@; |
197 |
return (undef, undef, 'Z3950_SEARCH_EMPTY'); |
198 |
} |
199 |
|
200 |
# If authorities are found, select the first valid one for addition in the local authority table. |
201 |
my $record; |
202 |
if($rs->size() != 0) { |
203 |
$record = MARC::Record::new_from_usmarc($rs->record(0)->raw()); |
204 |
}else{ |
205 |
return (undef, undef, 'Z3950_SEARCH_ERROR'); |
206 |
} |
207 |
$rs->destroy(); |
208 |
$zquery->destroy(); |
209 |
|
210 |
# If a valid authority was found, add it in the local authority database. |
211 |
if($record) { |
212 |
my $dbh=C4::Context->dbh; |
213 |
|
214 |
my $authtypecode = C4::AuthoritiesMarc::GuessAuthTypeCode($record); |
215 |
my $authId; |
216 |
|
217 |
# Use the control number to prevent the creation of duplicate authorities. |
218 |
my $controlNumber = $record->field('970')->subfield('0'); |
219 |
my $sthExist=$dbh->prepare("SELECT authid FROM auth_header WHERE origincode =?"); |
220 |
$sthExist->execute($controlNumber) or die $sthExist->errstr; |
221 |
($authId) = $sthExist->fetchrow; |
222 |
$sthExist->finish; |
223 |
|
224 |
#------------------------------------------------------------------------------------------ |
225 |
# Corrections and verifications before insertion |
226 |
my $format; |
227 |
my $leader=' nz a22 o 4500';# Leader for incomplete MARC21 record |
228 |
|
229 |
if (uc(C4::Context->preference('marcflavour')) eq 'UNIMARC') { |
230 |
$format= 'UNIMARCAUTH'; |
231 |
} |
232 |
else { |
233 |
$format= 'MARC21'; |
234 |
} |
235 |
|
236 |
if ($format eq "MARC21") { |
237 |
if (!$record->leader) { |
238 |
$record->leader($leader); |
239 |
} |
240 |
if (!$record->field('003')) { |
241 |
$record->insert_fields_ordered( |
242 |
MARC::Field->new('003',C4::Context->preference('MARCOrgCode')) |
243 |
); |
244 |
} |
245 |
my $time=POSIX::strftime("%Y%m%d%H%M%S",localtime); |
246 |
if (!$record->field('005')) { |
247 |
$record->insert_fields_ordered( |
248 |
MARC::Field->new('005',$time.".0") |
249 |
); |
250 |
} |
251 |
my $date=POSIX::strftime("%y%m%d",localtime); |
252 |
if (!$record->field('008')) { |
253 |
# Get a valid default value for field 008 |
254 |
my $default_008 = C4::Context->preference('MARCAuthorityControlField008'); |
255 |
if(!$default_008 or length($default_008)<34) { |
256 |
$default_008 = '|| aca||aabn | a|a d'; |
257 |
} |
258 |
else { |
259 |
$default_008 = substr($default_008,0,34); |
260 |
} |
261 |
$record->insert_fields_ordered( MARC::Field->new('008',$date.$default_008) ); |
262 |
} |
263 |
if (!$record->field('040')) { |
264 |
$record->insert_fields_ordered( |
265 |
MARC::Field->new('040','','', |
266 |
'a' => C4::Context->preference('MARCOrgCode'), |
267 |
'c' => C4::Context->preference('MARCOrgCode') |
268 |
) |
269 |
); |
270 |
} |
271 |
} |
272 |
if ($format eq "UNIMARCAUTH") { |
273 |
$record->leader(" nx j22 ") unless ($record->leader()); |
274 |
my $date=POSIX::strftime("%Y%m%d",localtime); |
275 |
if (my $string=$record->subfield('100',"a")){ |
276 |
$string=~s/fre50/frey50/; |
277 |
$record->field('100')->update('a'=>$string); |
278 |
} |
279 |
elsif ($record->field('100')){ |
280 |
$record->field('100')->update('a'=>$date."afrey50 ba0"); |
281 |
} else { |
282 |
$record->append_fields( |
283 |
MARC::Field->new('100',' ',' ' |
284 |
,'a'=>$date."afrey50 ba0") |
285 |
); |
286 |
} |
287 |
} |
288 |
my ($auth_type_tag, $auth_type_subfield) = C4::AuthoritiesMarc::get_auth_type_location($authtypecode); |
289 |
if (!$authId and $format eq "MARC21") { |
290 |
# only need to do this fix when modifying an existing authority |
291 |
C4::AuthoritiesMarc::MARC21::fix_marc21_auth_type_location($record, $auth_type_tag, $auth_type_subfield); |
292 |
} |
293 |
if (my $field=$record->field($auth_type_tag)){ |
294 |
$field->update($auth_type_subfield=>$authtypecode); |
295 |
} |
296 |
else { |
297 |
$record->add_fields($auth_type_tag,'','', $auth_type_subfield=>$authtypecode); |
298 |
} |
299 |
#------------------------------------------------------------------------------------------ |
300 |
if ($authId) { |
301 |
my $oldRecord=C4::AuthoritiesMarc::GetAuthority($authId); |
302 |
|
303 |
my $sth=$dbh->prepare("UPDATE auth_header SET authtypecode=?,marc=?,marcxml=? WHERE origincode =?"); |
304 |
eval { $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$controlNumber) or die $sth->errstr; }; |
305 |
$sth->finish; |
306 |
warn "Problem with authority $controlNumber : Cannot update" if $@; |
307 |
$dbh->commit unless $dbh->{AutoCommit}; |
308 |
return if $@; |
309 |
|
310 |
C4::Biblio::ModZebra($authId,'linkerUpdate',"authorityserver",$oldRecord,$record); |
311 |
return ($authId, $record, 'Z3950_UPDATED'); |
312 |
} |
313 |
else { |
314 |
my $sth=$dbh->prepare("INSERT INTO auth_header (datecreated,authtypecode,marc,marcxml,origincode) VALUES (NOW(),?,?,?,?)"); |
315 |
eval { $sth->execute($authtypecode,$record->as_usmarc,$record->as_xml_record($format),$controlNumber) or die $sth->errstr; }; |
316 |
$sth->finish; |
317 |
warn "Problem with authority $controlNumber : Cannot insert" if $@; |
318 |
my $id = $dbh->{'mysql_insertid'}; |
319 |
$dbh->commit unless $dbh->{AutoCommit}; |
320 |
return if $@; |
321 |
|
322 |
logaction( "AUTHORITIES", "ADD", $id, "authority" ) if C4::Context->preference("AuthoritiesLog"); |
323 |
C4::Biblio::ModZebra($id,'linkerUpdate',"authorityserver",undef,$record); |
324 |
return ($id, $record, 'Z3950_CREATED'); |
325 |
} |
326 |
} |
327 |
return ; |
328 |
} |
329 |
|
330 |
1; |