|
Line 0
Link Here
|
| 0 |
- |
1 |
package C4::Catalog::Zebra; |
|
|
2 |
# |
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 6 |
# terms of the GNU General Public License as published by the Free Software |
| 7 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 8 |
# version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along with |
| 15 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
| 16 |
# Suite 330, Boston, MA 02111-1307 USA |
| 17 |
|
| 18 |
# Derived from rebuild_zebra.pl (2005-08-11) Paul Poulain and others |
| 19 |
# Rewriten 02/03/2011 by Tomas Cohen Arazi (tomascohen@gmail.com) |
| 20 |
# Universidad Nacional de Cordoba / Argentina |
| 21 |
|
| 22 |
# Library for managing updates in zebra, usually from zebraqueue |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
use C4::Context; |
| 27 |
use Getopt::Long; |
| 28 |
use File::Temp qw/ tempdir /; |
| 29 |
use File::Path; |
| 30 |
use Time::HiRes qw(time); |
| 31 |
use C4::Biblio; |
| 32 |
use C4::AuthoritiesMarc; |
| 33 |
|
| 34 |
use vars qw($VERSION @ISA @EXPORT); |
| 35 |
|
| 36 |
BEGIN { |
| 37 |
# set the version for version checking |
| 38 |
$VERSION = 0.01; |
| 39 |
|
| 40 |
require Exporter; |
| 41 |
@ISA = qw(Exporter); |
| 42 |
@EXPORT = qw( |
| 43 |
&UpdateAuths |
| 44 |
&UpdateBiblios |
| 45 |
&UpdateAuthsAndBiblios |
| 46 |
&IndexZebraqueueRecords |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
=head1 NAME |
| 52 |
|
| 53 |
C4::Catalog::Zebra |
| 54 |
|
| 55 |
Comment: |
| 56 |
This should be used when merging the rest of the rebuild_zebra.pl indexing logic |
| 57 |
my $nosanitize = (C4::Context->preference('ZebraNoSanitize')) ? 1 : 0; |
| 58 |
|
| 59 |
|
| 60 |
=head2 UpdateAuths |
| 61 |
|
| 62 |
( $num_records_updated ) = &UpdateAuths (); |
| 63 |
|
| 64 |
returns the number of updated+deleted authority records |
| 65 |
|
| 66 |
=cut |
| 67 |
|
| 68 |
sub UpdateAuths |
| 69 |
{ |
| 70 |
# Update authorities |
| 71 |
return IndexZebraqueueRecords('authority'); |
| 72 |
} |
| 73 |
|
| 74 |
=head2 UpdateBiblios |
| 75 |
|
| 76 |
( $num_records_updated ) = &UpdateBiblios (); |
| 77 |
|
| 78 |
returns the number of updated+deleted biblio records |
| 79 |
|
| 80 |
=cut |
| 81 |
|
| 82 |
sub UpdateBiblios |
| 83 |
{ |
| 84 |
# Update authorities |
| 85 |
return IndexZebraqueueRecords('biblio'); |
| 86 |
} |
| 87 |
|
| 88 |
=head2 UpdateAuthsAndBiblios |
| 89 |
|
| 90 |
( $num_records_updated ) = &UpdateAuthsAndBiblios (); |
| 91 |
|
| 92 |
returns the number of updated+deleted authority and biblio records |
| 93 |
|
| 94 |
=cut |
| 95 |
|
| 96 |
sub UpdateAuthsAndBiblios |
| 97 |
{ |
| 98 |
my $ret; |
| 99 |
# Update authorities |
| 100 |
$ret = UpdateAuths(); |
| 101 |
|
| 102 |
# Update biblios |
| 103 |
$ret += UpdateBiblios(); |
| 104 |
|
| 105 |
return $ret; |
| 106 |
} |
| 107 |
|
| 108 |
=head2 IndexZebraqueueRecords |
| 109 |
|
| 110 |
( $num_records_updated ) = &IndexZebraqueueRecords ($record_type); |
| 111 |
|
| 112 |
returns the number of updated+deleted $record_type records |
| 113 |
|
| 114 |
Comment : |
| 115 |
$record_type can be either 'biblio' or 'authority' |
| 116 |
|
| 117 |
=cut |
| 118 |
|
| 119 |
sub IndexZebraqueueRecords |
| 120 |
{ |
| 121 |
my ($record_type) = @_; |
| 122 |
my $as_xml = (C4::Context->preference('ZebraUseXml')) ? 1 : 0; |
| 123 |
my $noxml = ($as_xml) ? 0 : 1; |
| 124 |
my $record_format = ($as_xml) ? 'marcxml' : 'iso2709' ; |
| 125 |
|
| 126 |
my ($num_records_updated,$num_records_deleted); |
| 127 |
|
| 128 |
$num_records_deleted = (IndexZebraqueueByAction('deleted',$record_type,$record_format,$as_xml,$noxml)||0); |
| 129 |
$num_records_updated = (IndexZebraqueueByAction('updated',$record_type,$record_format,$as_xml,$noxml)||0); |
| 130 |
|
| 131 |
return $num_records_deleted + $num_records_updated; |
| 132 |
} |
| 133 |
|
| 134 |
=head2 IndexZebraqueueByAction |
| 135 |
|
| 136 |
( $num_records_updated ) = &IndexZebraqueueByAction ($action,$record_type, |
| 137 |
$record_format,$as_xml,$noxml); |
| 138 |
|
| 139 |
returns the number of updated+deleted $record_type records |
| 140 |
|
| 141 |
Comment : |
| 142 |
$record_type can be 'biblio' or 'authority' |
| 143 |
$record_format can be 'marcxml' or 'iso2709' |
| 144 |
$action can be 'updated' or 'deleted' |
| 145 |
$as_xml and $noxml are maintained for legacy reasons, one is enough. They |
| 146 |
indicate whether to use marcxml for indexing in zebra or iso2709. They should |
| 147 |
all be deduced from C4::Context->preference('ZebraUseXml'). |
| 148 |
|
| 149 |
=cut |
| 150 |
|
| 151 |
sub IndexZebraqueueByAction |
| 152 |
{ |
| 153 |
my ($action,$record_type,$record_format,$as_xml,$noxml) = @_; |
| 154 |
my ($num_records_exported,$ret,$zaction); |
| 155 |
|
| 156 |
if ($action eq 'updated' or $action eq 'deleted') { |
| 157 |
# get records by action |
| 158 |
my $entries = select_zebraqueue_records($record_type, $action); |
| 159 |
# Create tmp dir |
| 160 |
my $directory = File::Temp->newdir(); |
| 161 |
|
| 162 |
# get records from zebraqueue, export to file for zebraidx |
| 163 |
if ($action eq 'updated') { |
| 164 |
$zaction = 'update'; |
| 165 |
$num_records_exported = export_marc_records_from_list($record_type, |
| 166 |
$entries, "$directory", $as_xml, $noxml); |
| 167 |
} else { |
| 168 |
# $action eq 'deleted' |
| 169 |
$zaction = 'delete'; |
| 170 |
$num_records_exported = generate_deleted_marc_records($record_type, |
| 171 |
$entries, "$directory", $as_xml); |
| 172 |
} |
| 173 |
|
| 174 |
if ($num_records_exported) { |
| 175 |
# log export |
| 176 |
my $time = localtime(time); |
| 177 |
print "$time $num_records_exported $record_type record(s) exported for $zaction\n"; |
| 178 |
# TODO error handling / and better logging |
| 179 |
$ret = DoIndexing($record_type,$zaction,"$directory",$record_format); |
| 180 |
if ($ret) { |
| 181 |
print "$time $num_records_exported $record_type record(s) $action\n"; |
| 182 |
mark_zebraqueue_batch_done($entries); |
| 183 |
print "$time $num_records_exported $record_type record(s) marked done in zebraqueue\n"; |
| 184 |
} |
| 185 |
# /TODO |
| 186 |
} |
| 187 |
} else { |
| 188 |
# Wrong action |
| 189 |
$ret = -1; |
| 190 |
} |
| 191 |
|
| 192 |
return $ret; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
sub select_zebraqueue_records { |
| 197 |
my ($record_type, $update_type) = @_; |
| 198 |
|
| 199 |
my $dbh = C4::Context->dbh; |
| 200 |
my $server = ($record_type eq 'biblio') ? 'biblioserver' : 'authorityserver'; |
| 201 |
my $op = ($update_type eq 'deleted') ? 'recordDelete' : 'specialUpdate'; |
| 202 |
|
| 203 |
my $sth = $dbh->prepare(<<'SQL'); |
| 204 |
SELECT id, biblio_auth_number |
| 205 |
FROM zebraqueue |
| 206 |
WHERE server = ? |
| 207 |
AND operation = ? |
| 208 |
AND done = 0 |
| 209 |
ORDER BY id DESC; |
| 210 |
SQL |
| 211 |
|
| 212 |
$sth->execute($server, $op); |
| 213 |
my $entries = $sth->fetchall_arrayref({}); |
| 214 |
} |
| 215 |
|
| 216 |
sub mark_zebraqueue_batch_done { |
| 217 |
my ($entries) = @_; |
| 218 |
|
| 219 |
my $dbh = C4::Context->dbh; |
| 220 |
|
| 221 |
$dbh->{AutoCommit} = 0; |
| 222 |
my $sth = $dbh->prepare("UPDATE zebraqueue SET done = 1 WHERE id = ?"); |
| 223 |
$dbh->commit(); |
| 224 |
foreach my $id (map { $_->{id} } @$entries) { |
| 225 |
$sth->execute($id); |
| 226 |
} |
| 227 |
$dbh->{AutoCommit} = 1; |
| 228 |
} |
| 229 |
|
| 230 |
sub export_marc_records_from_list { |
| 231 |
my ($record_type, $entries, $directory, $as_xml, $noxml) = @_; |
| 232 |
my $verbose_logging = (C4::Context->preference('ZebraqueueVerboseLogging')) ? 1 : 0; |
| 233 |
|
| 234 |
my $num_exported = 0; |
| 235 |
open (OUT, ">:utf8 ", "$directory/exported_records") or die $!; |
| 236 |
my $i = 0; |
| 237 |
my %found = (); |
| 238 |
foreach my $record_number ( map { $_->{biblio_auth_number} } |
| 239 |
grep { !$found{ $_->{biblio_auth_number} }++ } |
| 240 |
@$entries ) { |
| 241 |
print "." if ( $verbose_logging ); |
| 242 |
print "\r$i" unless ($i++ %100 or !$verbose_logging); |
| 243 |
my ($marc) = get_corrected_marc_record($record_type, $record_number, $noxml); |
| 244 |
if (defined $marc) { |
| 245 |
# FIXME - when more than one record is exported and $as_xml is true, |
| 246 |
# the output file is not valid XML - it's just multiple <record> elements |
| 247 |
# strung together with no single root element. zebraidx doesn't seem |
| 248 |
# to care, though, at least if you're using the GRS-1 filter. It does |
| 249 |
# care if you're using the DOM filter, which requires valid XML file(s). |
| 250 |
print OUT ($as_xml) ? $marc->as_xml_record() : $marc->as_usmarc(); |
| 251 |
$num_exported++; |
| 252 |
} |
| 253 |
} |
| 254 |
print "\nRecords exported: $num_exported\n" if ( $verbose_logging ); |
| 255 |
close OUT; |
| 256 |
return $num_exported; |
| 257 |
} |
| 258 |
|
| 259 |
sub generate_deleted_marc_records { |
| 260 |
my ($record_type, $entries, $directory, $as_xml) = @_; |
| 261 |
my $verbose_logging = (C4::Context->preference('ZebraqueueVerboseLogging')) ? 1 : 0; |
| 262 |
|
| 263 |
my $num_exported = 0; |
| 264 |
open (OUT, ">:utf8 ", "$directory/exported_records") or die $!; |
| 265 |
my $i = 0; |
| 266 |
foreach my $record_number (map { $_->{biblio_auth_number} } @$entries ) { |
| 267 |
print "\r$i" unless ($i++ %100 or !$verbose_logging); |
| 268 |
print "." if ( $verbose_logging ); |
| 269 |
|
| 270 |
my $marc = MARC::Record->new(); |
| 271 |
if ($record_type eq 'biblio') { |
| 272 |
fix_biblio_ids($marc, $record_number, $record_number); |
| 273 |
} else { |
| 274 |
fix_authority_id($marc, $record_number); |
| 275 |
} |
| 276 |
if (C4::Context->preference("marcflavour") eq "UNIMARC") { |
| 277 |
fix_unimarc_100($marc); |
| 278 |
} |
| 279 |
|
| 280 |
print OUT ($as_xml) ? $marc->as_xml_record() : $marc->as_usmarc(); |
| 281 |
$num_exported++; |
| 282 |
} |
| 283 |
print "\nRecords exported: $num_exported\n" if ( $verbose_logging ); |
| 284 |
close OUT; |
| 285 |
return $num_exported; |
| 286 |
} |
| 287 |
|
| 288 |
sub get_corrected_marc_record { |
| 289 |
my ($record_type, $record_number, $noxml) = @_; |
| 290 |
|
| 291 |
my $marc = get_raw_marc_record($record_type, $record_number, $noxml); |
| 292 |
|
| 293 |
if (defined $marc) { |
| 294 |
fix_leader($marc); |
| 295 |
if ($record_type eq 'biblio') { |
| 296 |
my $succeeded = fix_biblio_ids($marc, $record_number); |
| 297 |
return unless $succeeded; |
| 298 |
} else { |
| 299 |
fix_authority_id($marc, $record_number); |
| 300 |
} |
| 301 |
if (C4::Context->preference("marcflavour") eq "UNIMARC") { |
| 302 |
fix_unimarc_100($marc); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
return $marc; |
| 307 |
} |
| 308 |
|
| 309 |
sub get_raw_marc_record { |
| 310 |
my ($record_type, $record_number, $noxml) = @_; |
| 311 |
my $dbh = C4::Context->dbh; |
| 312 |
|
| 313 |
my $marc; |
| 314 |
if ($record_type eq 'biblio') { |
| 315 |
if ($noxml) { |
| 316 |
my $fetch_sth = $dbh->prepare_cached("SELECT marc FROM biblioitems WHERE biblionumber = ?"); |
| 317 |
$fetch_sth->execute($record_number); |
| 318 |
if (my ($blob) = $fetch_sth->fetchrow_array) { |
| 319 |
$marc = MARC::Record->new_from_usmarc($blob); |
| 320 |
$fetch_sth->finish(); |
| 321 |
} else { |
| 322 |
return; # failure to find a bib is not a problem - |
| 323 |
# a delete could have been done before |
| 324 |
# trying to process a record update |
| 325 |
} |
| 326 |
} else { |
| 327 |
eval { $marc = GetMarcBiblio($record_number); }; |
| 328 |
if ($@) { |
| 329 |
# here we do warn since catching an exception |
| 330 |
# means that the bib was found but failed |
| 331 |
# to be parsed |
| 332 |
warn "error retrieving biblio $record_number"; |
| 333 |
return; |
| 334 |
} |
| 335 |
} |
| 336 |
} else { |
| 337 |
eval { $marc = GetAuthority($record_number); }; |
| 338 |
if ($@) { |
| 339 |
warn "error retrieving authority $record_number"; |
| 340 |
return; |
| 341 |
} |
| 342 |
} |
| 343 |
return $marc; |
| 344 |
} |
| 345 |
|
| 346 |
sub fix_leader { |
| 347 |
# FIXME - this routine is suspect |
| 348 |
# It blanks the Leader/00-05 and Leader/12-16 to |
| 349 |
# force them to be recalculated correct when |
| 350 |
# the $marc->as_usmarc() or $marc->as_xml() is called. |
| 351 |
# But why is this necessary? It would be a serious bug |
| 352 |
# in MARC::Record (definitely) and MARC::File::XML (arguably) |
| 353 |
# if they are emitting incorrect leader values. |
| 354 |
my $marc = shift; |
| 355 |
|
| 356 |
my $leader = $marc->leader; |
| 357 |
substr($leader, 0, 5) = ' '; |
| 358 |
substr($leader, 10, 7) = '22 '; |
| 359 |
$marc->leader(substr($leader, 0, 24)); |
| 360 |
} |
| 361 |
|
| 362 |
sub fix_biblio_ids { |
| 363 |
# FIXME - it is essential to ensure that the biblionumber is present, |
| 364 |
# otherwise, Zebra will choke on the record. However, this |
| 365 |
# logic belongs in the relevant C4::Biblio APIs. |
| 366 |
my $marc = shift; |
| 367 |
my $biblionumber = shift; |
| 368 |
my $biblioitemnumber; |
| 369 |
my $dbh = C4::Context->dbh; |
| 370 |
|
| 371 |
if (@_) { |
| 372 |
$biblioitemnumber = shift; |
| 373 |
} else { |
| 374 |
my $sth = $dbh->prepare( |
| 375 |
"SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?"); |
| 376 |
$sth->execute($biblionumber); |
| 377 |
($biblioitemnumber) = $sth->fetchrow_array; |
| 378 |
$sth->finish; |
| 379 |
unless ($biblioitemnumber) { |
| 380 |
warn "failed to get biblioitemnumber for biblio $biblionumber"; |
| 381 |
return 0; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
# FIXME - this is cheating on two levels |
| 386 |
# 1. C4::Biblio::_koha_marc_update_bib_ids is meant to be an internal function |
| 387 |
# 2. Making sure that the biblionumber and biblioitemnumber are correct and |
| 388 |
# present in the MARC::Record object ought to be part of GetMarcBiblio. |
| 389 |
# |
| 390 |
# On the other hand, this better for now than what rebuild_zebra.pl used to |
| 391 |
# do, which was duplicate the code for inserting the biblionumber |
| 392 |
# and biblioitemnumber |
| 393 |
C4::Biblio::_koha_marc_update_bib_ids($marc, '', $biblionumber, $biblioitemnumber); |
| 394 |
|
| 395 |
return 1; |
| 396 |
} |
| 397 |
|
| 398 |
sub fix_authority_id { |
| 399 |
# FIXME - as with fix_biblio_ids, the authid must be present |
| 400 |
# for Zebra's sake. However, this really belongs |
| 401 |
# in C4::AuthoritiesMarc. |
| 402 |
my ($marc, $authid) = @_; |
| 403 |
unless ($marc->field('001') and $marc->field('001')->data() eq $authid){ |
| 404 |
$marc->delete_field($marc->field('001')); |
| 405 |
$marc->insert_fields_ordered(MARC::Field->new('001',$authid)); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
sub fix_unimarc_100 { |
| 410 |
# FIXME - again, if this is necessary, it belongs in C4::AuthoritiesMarc. |
| 411 |
my $marc = shift; |
| 412 |
|
| 413 |
my $string; |
| 414 |
if ( length($marc->subfield( 100, "a" )) == 35 ) { |
| 415 |
$string = $marc->subfield( 100, "a" ); |
| 416 |
my $f100 = $marc->field(100); |
| 417 |
$marc->delete_field($f100); |
| 418 |
} |
| 419 |
else { |
| 420 |
$string = POSIX::strftime( "%Y%m%d", localtime ); |
| 421 |
$string =~ s/\-//g; |
| 422 |
$string = sprintf( "%-*s", 35, $string ); |
| 423 |
} |
| 424 |
substr( $string, 22, 6, "frey50" ); |
| 425 |
unless ( length($marc->subfield( 100, "a" )) == 35 ) { |
| 426 |
$marc->delete_field($marc->field(100)); |
| 427 |
$marc->insert_grouped_field(MARC::Field->new( 100, "", "", "a" => $string )); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
=head2 DoIndexing |
| 432 |
|
| 433 |
( $error_code ) = &DoIndexing($record_type,$op,$record_dir,$record_format); |
| 434 |
|
| 435 |
returns the corresponding zebraidx error code |
| 436 |
|
| 437 |
Comment : |
| 438 |
$record_type can be 'biblio' or 'authority' |
| 439 |
$zaction can be 'delete' or 'update' |
| 440 |
$record_dir is the directory where the exported records are |
| 441 |
$record_format can be 'marcxml' or 'iso2709' |
| 442 |
|
| 443 |
=cut |
| 444 |
|
| 445 |
sub DoIndexing { |
| 446 |
my ($record_type, $zaction, $record_dir, $record_format) = @_; |
| 447 |
my $zebra_server = ($record_type eq 'biblio') ? 'biblioserver' : 'authorityserver'; |
| 448 |
my $zebra_db_name = ($record_type eq 'biblio') ? 'biblios' : 'authorities'; |
| 449 |
my $zebra_config = C4::Context->zebraconfig($zebra_server)->{'config'}; |
| 450 |
my $zebra_db_dir = C4::Context->zebraconfig($zebra_server)->{'directory'}; |
| 451 |
my $noshadow = (C4::Context->preference('ZebraNoshadow')) ? '-n' : ''; |
| 452 |
my $zebraidx_log_opt = " -v none,fatal "; |
| 453 |
|
| 454 |
# TODO better error handling!! |
| 455 |
system("zebraidx -c $zebra_config $zebraidx_log_opt $noshadow -g $record_format -d $zebra_db_name $zaction $record_dir"); |
| 456 |
system("zebraidx -c $zebra_config $zebraidx_log_opt -g $record_format -d $zebra_db_name commit") unless $noshadow; |
| 457 |
# /TODO |
| 458 |
|
| 459 |
return 1; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
END { } |
| 464 |
|
| 465 |
1; |
| 466 |
__END__ |
| 467 |
|
| 468 |
=head1 AUTHOR |
| 469 |
|
| 470 |
Koha Development Team <http://koha-community.org/> |
| 471 |
|
| 472 |
Tomas Cohen Arazi tomascohen@gmail.com |
| 473 |
|
| 474 |
=cut |