Main Koha release repository
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.2 KiB

# Small script that rebuilds the non-MARC DB
# Formerly named
use strict;
#use warnings; FIXME - Bug 2505
# find Koha's Perl modules
# test carefully before changing this
use FindBin;
eval { require "$FindBin::Bin/" };
# Koha modules used
use Koha::Script;
use MARC::Record;
use C4::Charset;
use C4::Context;
use C4::Biblio;
use Time::HiRes qw(gettimeofday);
use Getopt::Long;
my ($version, $confirm);
'c' => \$confirm,
'h' => \$version
if ($version || (!$confirm)) {
print <<EOF
This script rebuilds the non-MARC fields from the MARC values.
You can/must use it when you change your mapping.
Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a).
Run this script or you will have strange results in the UI!
\t./ -h (or without arguments => show this screen)
\t./ -c (c like confirm => rebuild non-MARC fields (may take long)
$|=1; # non-buffered output
my $dbh = C4::Context->dbh;
my $i=0;
my $starttime = gettimeofday;
my $marcflavour = C4::Context->preference('marcflavour');
my $sth = $dbh->prepare('SELECT biblionumber, frameworkcode FROM biblio');
my @errors;
while (my ($biblionumber, $frameworkcode) = $sth->fetchrow) {
my $marcxml = GetXmlBiblio($biblionumber);
if (not defined $marcxml) {
push @errors, $biblionumber;
$marcxml = C4::Charset::StripNonXmlChars( $marcxml );
my $record = eval {
MARC::Record::new_from_xml($marcxml, 'UTF-8', $marcflavour);
if ($@) {
push @errors, $biblionumber;
my $biblio = TransformMarcToKoha($record);
C4::Biblio::_koha_modify_biblio($dbh, $biblio, $frameworkcode);
C4::Biblio::_koha_modify_biblioitem_nonmarc($dbh, $biblio);
printf("%lu records processed in %.2f seconds\n", $i, gettimeofday() - $starttime) unless ($i % 100);
printf("\n%lu records processed in %.2f seconds\n", $i, gettimeofday() - $starttime);
if (scalar(@errors) > 0) {
print "Some records could not be processed though: ", join(' ', @errors);