Lines 10-16
use File::Path;
Link Here
|
10 |
use C4::Biblio; |
10 |
use C4::Biblio; |
11 |
use C4::AuthoritiesMarc; |
11 |
use C4::AuthoritiesMarc; |
12 |
use C4::Items; |
12 |
use C4::Items; |
13 |
use Koha::RecordProcessor; |
13 |
use C4::ZebraIndex; |
14 |
|
14 |
|
15 |
# |
15 |
# |
16 |
# script that checks zebradir structure & create directories & mandatory files if needed |
16 |
# script that checks zebradir structure & create directories & mandatory files if needed |
Lines 156-167
if ( $verbose_logging ) {
Link Here
|
156 |
} |
156 |
} |
157 |
if ($keep_export) { |
157 |
if ($keep_export) { |
158 |
print "NOTHING cleaned : the export $directory has been kept.\n"; |
158 |
print "NOTHING cleaned : the export $directory has been kept.\n"; |
159 |
print "You can re-run this script with the -s "; |
159 |
print "You can re-run this script with the -s and -d $directory parameters"; |
160 |
if ($use_tempdir) { |
|
|
161 |
print " and -d $directory parameters"; |
162 |
} else { |
163 |
print "parameter"; |
164 |
} |
165 |
print "\n"; |
160 |
print "\n"; |
166 |
print "if you just want to rebuild zebra after changing the record.abs\n"; |
161 |
print "if you just want to rebuild zebra after changing the record.abs\n"; |
167 |
print "or another zebra config file\n"; |
162 |
print "or another zebra config file\n"; |
Lines 196-263
sub check_zebra_dirs {
Link Here
|
196 |
sub index_records { |
191 |
sub index_records { |
197 |
my ($record_type, $directory, $skip_export, $skip_index, $process_zebraqueue, $as_xml, $noxml, $nosanitize, $do_not_clear_zebraqueue, $verbose_logging, $zebraidx_log_opt, $server_dir) = @_; |
192 |
my ($record_type, $directory, $skip_export, $skip_index, $process_zebraqueue, $as_xml, $noxml, $nosanitize, $do_not_clear_zebraqueue, $verbose_logging, $zebraidx_log_opt, $server_dir) = @_; |
198 |
|
193 |
|
199 |
my $num_records_exported = 0; |
|
|
200 |
my $records_deleted; |
201 |
my $need_reset = check_zebra_dirs($server_dir); |
194 |
my $need_reset = check_zebra_dirs($server_dir); |
202 |
if ($need_reset) { |
195 |
if ($need_reset) { |
203 |
print "$0: found broken zebra server directories: forcing a rebuild\n"; |
196 |
print "$0: found broken zebra server directories: forcing a rebuild\n"; |
204 |
$reset = 1; |
197 |
$reset = 1; |
205 |
} |
198 |
} |
206 |
if ($skip_export && $verbose_logging) { |
199 |
|
|
|
200 |
if ( $verbose_logging ) { |
207 |
print "====================\n"; |
201 |
print "====================\n"; |
208 |
print "SKIPPING $record_type export\n"; |
202 |
print "REINDEXING zebra\n"; |
209 |
print "====================\n"; |
203 |
print "====================\n"; |
210 |
} else { |
|
|
211 |
if ( $verbose_logging ) { |
212 |
print "====================\n"; |
213 |
print "exporting $record_type\n"; |
214 |
print "====================\n"; |
215 |
} |
216 |
mkdir "$directory" unless (-d $directory); |
217 |
mkdir "$directory/$record_type" unless (-d "$directory/$record_type"); |
218 |
if ($process_zebraqueue) { |
219 |
my $entries = select_zebraqueue_records($record_type, 'deleted'); |
220 |
mkdir "$directory/del_$record_type" unless (-d "$directory/del_$record_type"); |
221 |
$records_deleted = generate_deleted_marc_records($record_type, $entries, "$directory/del_$record_type", $as_xml); |
222 |
mark_zebraqueue_batch_done($entries); |
223 |
$entries = select_zebraqueue_records($record_type, 'updated'); |
224 |
mkdir "$directory/upd_$record_type" unless (-d "$directory/upd_$record_type"); |
225 |
$num_records_exported = export_marc_records_from_list($record_type, |
226 |
$entries, "$directory/upd_$record_type", $as_xml, $noxml, $records_deleted); |
227 |
mark_zebraqueue_batch_done($entries); |
228 |
} else { |
229 |
my $sth = select_all_records($record_type); |
230 |
$num_records_exported = export_marc_records_from_sth($record_type, $sth, "$directory/$record_type", $as_xml, $noxml, $nosanitize); |
231 |
unless ($do_not_clear_zebraqueue) { |
232 |
mark_all_zebraqueue_done($record_type); |
233 |
} |
234 |
} |
235 |
} |
204 |
} |
236 |
|
205 |
|
237 |
# |
206 |
my $record_fmt = ($as_xml) ? 'marcxml' : 'iso2709' ; |
238 |
# and reindexing everything |
207 |
if ($process_zebraqueue) { |
239 |
# |
208 |
# Process 'deleted' records |
240 |
if ($skip_index) { |
209 |
my $entries = select_zebraqueue_records( $record_type, 'deleted' ); |
241 |
if ($verbose_logging) { |
210 |
my @entries_to_delete = map { $_->{biblio_auth_id} } @$entries; |
242 |
print "====================\n"; |
211 |
|
243 |
print "SKIPPING $record_type indexing\n"; |
212 |
C4::ZebraIndex::DeleteRecordIndex($record_type, \@entries_to_delete, { |
244 |
print "====================\n"; |
213 |
as_xml => $as_xml, noshadow => $noshadow, record_format => $record_fmt, |
245 |
} |
214 |
zebraidx_log_opt => $zebraidx_log_opt, verbose => $verbose_logging, |
|
|
215 |
skip_export => $skip_export, skip_index => $skip_index, |
216 |
keep_export => $keep_export, directory => $directory |
217 |
}); |
218 |
|
219 |
mark_zebraqueue_batch_done($entries); |
220 |
|
221 |
# Process 'updated' records' |
222 |
$entries = select_zebraqueue_records( $record_type, 'updated' ); |
223 |
my @entries_to_update = map { |
224 |
my $id = $_->{biblio_auth_id}; |
225 |
# do not try to update deleted records |
226 |
(0 == grep { $id == $_ } @entries_to_delete) ? $id : () |
227 |
} @$entries; |
228 |
|
229 |
C4::ZebraIndex::IndexRecord($record_type, \@entries_to_update, { |
230 |
as_xml => $as_xml, noxml => $noxml, reset_index => $reset, |
231 |
noshadow => $noshadow, record_format => $record_fmt, |
232 |
zebraidx_log_opt => $zebraidx_log_opt, verbose => $verbose_logging, |
233 |
skip_export => $skip_export, skip_index => $skip_index, |
234 |
keep_export => $keep_export, directory => $directory |
235 |
}); |
236 |
|
237 |
mark_zebraqueue_batch_done($entries); |
246 |
} else { |
238 |
} else { |
247 |
if ( $verbose_logging ) { |
239 |
my $sth = select_all_records($record_type); |
248 |
print "====================\n"; |
240 |
my $entries = $sth->fetchall_arrayref([]); |
249 |
print "REINDEXING zebra\n"; |
241 |
my @entries_to_update = map { $_->[0] } @$entries; |
250 |
print "====================\n"; |
242 |
|
251 |
} |
243 |
C4::ZebraIndex::IndexRecord($record_type, \@entries_to_update, { |
252 |
my $record_fmt = ($as_xml) ? 'marcxml' : 'iso2709' ; |
244 |
as_xml => $as_xml, noxml => $noxml, reset_index => $reset, |
253 |
if ($process_zebraqueue) { |
245 |
noshadow => $noshadow, record_format => $record_fmt, |
254 |
do_indexing($record_type, 'adelete', "$directory/del_$record_type", $reset, $noshadow, $record_fmt, $zebraidx_log_opt) |
246 |
zebraidx_log_opt => $zebraidx_log_opt, nosanitize => $nosanitize, |
255 |
if %$records_deleted; |
247 |
verbose => $verbose_logging, skip_export => $skip_export, |
256 |
do_indexing($record_type, 'update', "$directory/upd_$record_type", $reset, $noshadow, $record_fmt, $zebraidx_log_opt) |
248 |
skip_index => $skip_index, keep_export => $keep_export, |
257 |
if $num_records_exported; |
249 |
directory => $directory |
258 |
} else { |
250 |
}); |
259 |
do_indexing($record_type, 'update', "$directory/$record_type", $reset, $noshadow, $record_fmt, $zebraidx_log_opt) |
251 |
|
260 |
if ($num_records_exported or $skip_export); |
252 |
unless ($do_not_clear_zebraqueue) { |
|
|
253 |
mark_all_zebraqueue_done($record_type); |
261 |
} |
254 |
} |
262 |
} |
255 |
} |
263 |
} |
256 |
} |
Lines 327-651
sub select_all_biblios {
Link Here
|
327 |
return $sth; |
320 |
return $sth; |
328 |
} |
321 |
} |
329 |
|
322 |
|
330 |
sub include_xml_wrapper { |
|
|
331 |
my $as_xml = shift; |
332 |
my $record_type = shift; |
333 |
|
334 |
return 0 unless $as_xml; |
335 |
return 1 if $record_type eq 'biblio' and $bib_index_mode eq 'dom'; |
336 |
return 1 if $record_type eq 'authority' and $auth_index_mode eq 'dom'; |
337 |
return 0; |
338 |
|
339 |
} |
340 |
|
341 |
sub export_marc_records_from_sth { |
342 |
my ($record_type, $sth, $directory, $as_xml, $noxml, $nosanitize) = @_; |
343 |
|
344 |
my $num_exported = 0; |
345 |
open my $fh, '>:encoding(UTF-8) ', "$directory/exported_records" or die $!; |
346 |
if (include_xml_wrapper($as_xml, $record_type)) { |
347 |
# include XML declaration and root element |
348 |
print {$fh} '<?xml version="1.0" encoding="UTF-8"?><collection>'; |
349 |
} |
350 |
my $i = 0; |
351 |
my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",''); |
352 |
while (my ($record_number) = $sth->fetchrow_array) { |
353 |
print "." if ( $verbose_logging ); |
354 |
print "\r$i" unless ($i++ %100 or !$verbose_logging); |
355 |
if ( $nosanitize ) { |
356 |
my $marcxml = $record_type eq 'biblio' |
357 |
? GetXmlBiblio( $record_number ) |
358 |
: GetAuthorityXML( $record_number ); |
359 |
if ($record_type eq 'biblio'){ |
360 |
my @items = GetItemsInfo($record_number); |
361 |
if (@items){ |
362 |
my $record = MARC::Record->new; |
363 |
$record->encoding('UTF-8'); |
364 |
my @itemsrecord; |
365 |
foreach my $item (@items){ |
366 |
my $record = Item2Marc($item, $record_number); |
367 |
push @itemsrecord, $record->field($itemtag); |
368 |
} |
369 |
$record->insert_fields_ordered(@itemsrecord); |
370 |
my $itemsxml = $record->as_xml_record(); |
371 |
$marcxml = |
372 |
substr($marcxml, 0, length($marcxml)-10) . |
373 |
substr($itemsxml, index($itemsxml, "</leader>\n", 0) + 10); |
374 |
} |
375 |
} |
376 |
if ( $marcxml ) { |
377 |
print {$fh} $marcxml if $marcxml; |
378 |
$num_exported++; |
379 |
} |
380 |
next; |
381 |
} |
382 |
my ($marc) = get_corrected_marc_record($record_type, $record_number, $noxml); |
383 |
if (defined $marc) { |
384 |
eval { |
385 |
my $rec; |
386 |
if ($as_xml) { |
387 |
$rec = $marc->as_xml_record(C4::Context->preference('marcflavour')); |
388 |
$rec =~ s!<\?xml version="1.0" encoding="UTF-8"\?>\n!!; |
389 |
} else { |
390 |
$rec = $marc->as_usmarc(); |
391 |
} |
392 |
print {$fh} $rec; |
393 |
$num_exported++; |
394 |
}; |
395 |
if ($@) { |
396 |
warn "Error exporting record $record_number ($record_type) ".($noxml ? "not XML" : "XML"); |
397 |
} |
398 |
} |
399 |
} |
400 |
print "\nRecords exported: $num_exported\n" if ( $verbose_logging ); |
401 |
print {$fh} '</collection>' if (include_xml_wrapper($as_xml, $record_type)); |
402 |
close $fh; |
403 |
return $num_exported; |
404 |
} |
405 |
|
406 |
sub export_marc_records_from_list { |
407 |
my ($record_type, $entries, $directory, $as_xml, $noxml, $records_deleted) = @_; |
408 |
|
409 |
my $num_exported = 0; |
410 |
open my $fh, '>:encoding(UTF-8)', "$directory/exported_records" or die $!; |
411 |
if (include_xml_wrapper($as_xml, $record_type)) { |
412 |
# include XML declaration and root element |
413 |
print {$fh} '<?xml version="1.0" encoding="UTF-8"?><collection>'; |
414 |
} |
415 |
my $i = 0; |
416 |
|
417 |
# Skip any deleted records. We check for this anyway, but this reduces error spam |
418 |
my %found = %$records_deleted; |
419 |
foreach my $record_number ( map { $_->{biblio_auth_number} } |
420 |
grep { !$found{ $_->{biblio_auth_number} }++ } |
421 |
@$entries ) { |
422 |
print "." if ( $verbose_logging ); |
423 |
print "\r$i" unless ($i++ %100 or !$verbose_logging); |
424 |
my ($marc) = get_corrected_marc_record($record_type, $record_number, $noxml); |
425 |
if (defined $marc) { |
426 |
eval { |
427 |
my $rec; |
428 |
if ($as_xml) { |
429 |
$rec = $marc->as_xml_record(C4::Context->preference('marcflavour')); |
430 |
$rec =~ s!<\?xml version="1.0" encoding="UTF-8"\?>\n!!; |
431 |
} else { |
432 |
$rec = $marc->as_usmarc(); |
433 |
} |
434 |
print {$fh} $rec; |
435 |
$num_exported++; |
436 |
}; |
437 |
if ($@) { |
438 |
warn "Error exporting record $record_number ($record_type) ".($noxml ? "not XML" : "XML"); |
439 |
} |
440 |
$num_exported++; |
441 |
} |
442 |
} |
443 |
print "\nRecords exported: $num_exported\n" if ( $verbose_logging ); |
444 |
print {$fh} '</collection>' if (include_xml_wrapper($as_xml, $record_type)); |
445 |
close $fh; |
446 |
return $num_exported; |
447 |
} |
448 |
|
449 |
sub generate_deleted_marc_records { |
450 |
my ($record_type, $entries, $directory, $as_xml) = @_; |
451 |
|
452 |
my $records_deleted = {}; |
453 |
open my $fh, '>:encoding(UTF-8)', "$directory/exported_records" or die $!; |
454 |
if (include_xml_wrapper($as_xml, $record_type)) { |
455 |
# include XML declaration and root element |
456 |
print {$fh} '<?xml version="1.0" encoding="UTF-8"?><collection>'; |
457 |
} |
458 |
my $i = 0; |
459 |
foreach my $record_number (map { $_->{biblio_auth_number} } @$entries ) { |
460 |
print "\r$i" unless ($i++ %100 or !$verbose_logging); |
461 |
print "." if ( $verbose_logging ); |
462 |
|
463 |
my $marc = MARC::Record->new(); |
464 |
if ($record_type eq 'biblio') { |
465 |
fix_biblio_ids($marc, $record_number, $record_number); |
466 |
} else { |
467 |
fix_authority_id($marc, $record_number); |
468 |
} |
469 |
if (C4::Context->preference("marcflavour") eq "UNIMARC") { |
470 |
fix_unimarc_100($marc); |
471 |
} |
472 |
|
473 |
my $rec; |
474 |
if ($as_xml) { |
475 |
$rec = $marc->as_xml_record(C4::Context->preference('marcflavour')); |
476 |
$rec =~ s!<\?xml version="1.0" encoding="UTF-8"\?>\n!!; |
477 |
} else { |
478 |
$rec = $marc->as_usmarc(); |
479 |
} |
480 |
print {$fh} $rec; |
481 |
|
482 |
$records_deleted->{$record_number} = 1; |
483 |
} |
484 |
print "\nRecords exported: $i\n" if ( $verbose_logging ); |
485 |
print {$fh} '</collection>' if (include_xml_wrapper($as_xml, $record_type)); |
486 |
close $fh; |
487 |
return $records_deleted; |
488 |
|
489 |
|
490 |
} |
491 |
|
492 |
sub get_corrected_marc_record { |
493 |
my ($record_type, $record_number, $noxml) = @_; |
494 |
|
495 |
my $marc = get_raw_marc_record($record_type, $record_number, $noxml); |
496 |
|
497 |
if (defined $marc) { |
498 |
fix_leader($marc); |
499 |
if ($record_type eq 'authority') { |
500 |
fix_authority_id($marc, $record_number); |
501 |
} elsif ($record_type eq 'biblio' && C4::Context->preference('IncludeSeeFromInSearches')) { |
502 |
my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } ); |
503 |
$marc = $normalizer->process($marc); |
504 |
} |
505 |
if (C4::Context->preference("marcflavour") eq "UNIMARC") { |
506 |
fix_unimarc_100($marc); |
507 |
} |
508 |
} |
509 |
|
510 |
return $marc; |
511 |
} |
512 |
|
513 |
sub get_raw_marc_record { |
514 |
my ($record_type, $record_number, $noxml) = @_; |
515 |
|
516 |
my $marc; |
517 |
if ($record_type eq 'biblio') { |
518 |
if ($noxml) { |
519 |
my $fetch_sth = $dbh->prepare_cached("SELECT marc FROM biblioitems WHERE biblionumber = ?"); |
520 |
$fetch_sth->execute($record_number); |
521 |
if (my ($blob) = $fetch_sth->fetchrow_array) { |
522 |
$marc = MARC::Record->new_from_usmarc($blob); |
523 |
unless ($marc) { |
524 |
warn "error creating MARC::Record from $blob"; |
525 |
} |
526 |
} |
527 |
# failure to find a bib is not a problem - |
528 |
# a delete could have been done before |
529 |
# trying to process a record update |
530 |
|
531 |
$fetch_sth->finish(); |
532 |
return unless $marc; |
533 |
} else { |
534 |
eval { $marc = GetMarcBiblio($record_number, 1); }; |
535 |
if ($@ || !$marc) { |
536 |
# here we do warn since catching an exception |
537 |
# means that the bib was found but failed |
538 |
# to be parsed |
539 |
warn "error retrieving biblio $record_number"; |
540 |
return; |
541 |
} |
542 |
} |
543 |
} else { |
544 |
eval { $marc = GetAuthority($record_number); }; |
545 |
if ($@) { |
546 |
warn "error retrieving authority $record_number"; |
547 |
return; |
548 |
} |
549 |
} |
550 |
return $marc; |
551 |
} |
552 |
|
553 |
sub fix_leader { |
554 |
# FIXME - this routine is suspect |
555 |
# It blanks the Leader/00-05 and Leader/12-16 to |
556 |
# force them to be recalculated correct when |
557 |
# the $marc->as_usmarc() or $marc->as_xml() is called. |
558 |
# But why is this necessary? It would be a serious bug |
559 |
# in MARC::Record (definitely) and MARC::File::XML (arguably) |
560 |
# if they are emitting incorrect leader values. |
561 |
my $marc = shift; |
562 |
|
563 |
my $leader = $marc->leader; |
564 |
substr($leader, 0, 5) = ' '; |
565 |
substr($leader, 10, 7) = '22 '; |
566 |
$marc->leader(substr($leader, 0, 24)); |
567 |
} |
568 |
|
569 |
sub fix_biblio_ids { |
570 |
# FIXME - it is essential to ensure that the biblionumber is present, |
571 |
# otherwise, Zebra will choke on the record. However, this |
572 |
# logic belongs in the relevant C4::Biblio APIs. |
573 |
my $marc = shift; |
574 |
my $biblionumber = shift; |
575 |
my $biblioitemnumber; |
576 |
if (@_) { |
577 |
$biblioitemnumber = shift; |
578 |
} else { |
579 |
my $sth = $dbh->prepare( |
580 |
"SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?"); |
581 |
$sth->execute($biblionumber); |
582 |
($biblioitemnumber) = $sth->fetchrow_array; |
583 |
$sth->finish; |
584 |
unless ($biblioitemnumber) { |
585 |
warn "failed to get biblioitemnumber for biblio $biblionumber"; |
586 |
return 0; |
587 |
} |
588 |
} |
589 |
|
590 |
# FIXME - this is cheating on two levels |
591 |
# 1. C4::Biblio::_koha_marc_update_bib_ids is meant to be an internal function |
592 |
# 2. Making sure that the biblionumber and biblioitemnumber are correct and |
593 |
# present in the MARC::Record object ought to be part of GetMarcBiblio. |
594 |
# |
595 |
# On the other hand, this better for now than what rebuild_zebra.pl used to |
596 |
# do, which was duplicate the code for inserting the biblionumber |
597 |
# and biblioitemnumber |
598 |
C4::Biblio::_koha_marc_update_bib_ids($marc, '', $biblionumber, $biblioitemnumber); |
599 |
|
600 |
return 1; |
601 |
} |
602 |
|
603 |
sub fix_authority_id { |
604 |
# FIXME - as with fix_biblio_ids, the authid must be present |
605 |
# for Zebra's sake. However, this really belongs |
606 |
# in C4::AuthoritiesMarc. |
607 |
my ($marc, $authid) = @_; |
608 |
unless ($marc->field('001') and $marc->field('001')->data() eq $authid){ |
609 |
$marc->delete_field($marc->field('001')); |
610 |
$marc->insert_fields_ordered(MARC::Field->new('001',$authid)); |
611 |
} |
612 |
} |
613 |
|
614 |
sub fix_unimarc_100 { |
615 |
# FIXME - again, if this is necessary, it belongs in C4::AuthoritiesMarc. |
616 |
my $marc = shift; |
617 |
|
618 |
my $string; |
619 |
if ( length($marc->subfield( 100, "a" )) == 36 ) { |
620 |
$string = $marc->subfield( 100, "a" ); |
621 |
my $f100 = $marc->field(100); |
622 |
$marc->delete_field($f100); |
623 |
} |
624 |
else { |
625 |
$string = POSIX::strftime( "%Y%m%d", localtime ); |
626 |
$string =~ s/\-//g; |
627 |
$string = sprintf( "%-*s", 35, $string ); |
628 |
} |
629 |
substr( $string, 22, 6, "frey50" ); |
630 |
unless ( length($marc->subfield( 100, "a" )) == 36 ) { |
631 |
$marc->delete_field($marc->field(100)); |
632 |
$marc->insert_grouped_field(MARC::Field->new( 100, "", "", "a" => $string )); |
633 |
} |
634 |
} |
635 |
|
636 |
sub do_indexing { |
637 |
my ($record_type, $op, $record_dir, $reset_index, $noshadow, $record_format, $zebraidx_log_opt) = @_; |
638 |
|
639 |
my $zebra_server = ($record_type eq 'biblio') ? 'biblioserver' : 'authorityserver'; |
640 |
my $zebra_db_name = ($record_type eq 'biblio') ? 'biblios' : 'authorities'; |
641 |
my $zebra_config = C4::Context->zebraconfig($zebra_server)->{'config'}; |
642 |
my $zebra_db_dir = C4::Context->zebraconfig($zebra_server)->{'directory'}; |
643 |
|
644 |
system("zebraidx -c $zebra_config $zebraidx_log_opt -g $record_format -d $zebra_db_name init") if $reset_index; |
645 |
system("zebraidx -c $zebra_config $zebraidx_log_opt $noshadow -g $record_format -d $zebra_db_name $op $record_dir"); |
646 |
system("zebraidx -c $zebra_config $zebraidx_log_opt -g $record_format -d $zebra_db_name commit") unless $noshadow; |
647 |
|
648 |
} |
649 |
|
323 |
|
650 |
sub print_usage { |
324 |
sub print_usage { |
651 |
print <<_USAGE_; |
325 |
print <<_USAGE_; |
652 |
- |
|
|