Koha/misc/batchRebuildBiblioTables.pl
Jonathan Druart 238fabc4ab Bug 28617: Remove kohalib.pl and rely on PERL5LIB
The purpose of this script was to load the relevant Koha lib for the
different scripts (installation, cronjob, CLI, etc.)
However it is not used consistently and we prefer to rely on PERL5LIB.

From bug 28617 comment 6 from Galen:
"""
Time marches on, and one of the motivations for having kohalib.pl - making
it possible to install Koha without setting a single environment variable -
has been obviated by the vast improvements in the ease of installing Koha.

Consequently, I think kohalib.pl can go away.
"""

Test plan:
confirm that the changes make sense and that kohalib.pl can be removed
safely.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2021-12-07 12:16:28 -10:00

80 lines
2.1 KiB
Perl
Executable file

#!/usr/bin/perl
# Small script that rebuilds the non-MARC DB
# Formerly named rebuildnonmarc.pl
use strict;
#use warnings; FIXME - Bug 2505
# Koha modules used
use Koha::Script;
use MARC::Record;
use C4::Charset;
use C4::Context;
use C4::Biblio qw(
GetXmlBiblio
TransformMarcToKoha
);
use Time::HiRes qw( gettimeofday );
use Getopt::Long qw( GetOptions );
my ($version, $confirm);
GetOptions(
'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!
Syntax:
\t./batchRebuildBiblioTables.pl -h (or without arguments => show this screen)
\t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non-MARC fields (may take long)
EOF
;
exit;
}
$|=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');
$sth->execute();
my @errors;
while (my ($biblionumber, $frameworkcode) = $sth->fetchrow) {
my $marcxml = GetXmlBiblio($biblionumber);
if (not defined $marcxml) {
push @errors, $biblionumber;
next;
}
$marcxml = C4::Charset::StripNonXmlChars( $marcxml );
my $record = eval {
MARC::Record::new_from_xml($marcxml, 'UTF-8', $marcflavour);
};
if ($@) {
push @errors, $biblionumber;
next;
}
my $biblio = TransformMarcToKoha($record);
C4::Biblio::_koha_modify_biblio($dbh, $biblio, $frameworkcode);
C4::Biblio::_koha_modify_biblioitem_nonmarc($dbh, $biblio);
$i++;
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);
}