From d7d862071be90f29bd4f092e9368984b796cfd08 Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Wed, 26 Sep 2018 13:06:04 +0300 Subject: [PATCH] Bug 20447: Add a batch rebuild script for holdings table. This script can be used to update the columns in the holdings table from the MARC record using the rules in the framework. In practice this will only be required if the koha link field is changed in the framework, and that's disabled in the UI so it must be done in the database. --- C4/Holdings.pm | 1 + misc/batchRebuildHoldingsTables.pl | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100755 misc/batchRebuildHoldingsTables.pl diff --git a/C4/Holdings.pm b/C4/Holdings.pm index 4661b63..6347c1f 100644 --- a/C4/Holdings.pm +++ b/C4/Holdings.pm @@ -32,6 +32,7 @@ use MARC::File::USMARC; use MARC::File::XML; use POSIX qw(strftime); +use C4::Biblio; use C4::Koha; use C4::Log; # logaction use C4::ClassSource; diff --git a/misc/batchRebuildHoldingsTables.pl b/misc/batchRebuildHoldingsTables.pl new file mode 100755 index 0000000..c052357 --- /dev/null +++ b/misc/batchRebuildHoldingsTables.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +# Small script that rebuilds the non-MARC Holdings DB + +use strict; +use warnings; + +BEGIN { + # find Koha's Perl modules + # test carefully before changing this + use FindBin; + eval { require "$FindBin::Bin/kohalib.pl" }; +} + +# Koha modules used +use MARC::Record; +use C4::Context; +use C4::Holdings; + +use Getopt::Long; + +my ($input_marc_file, $number) = ('', 0); +my ($help, $confirm, $test_parameter); +GetOptions( + 'c' => \$confirm, + 'h' => \$help, + 't' => \$test_parameter, +); + +if ($help || !$confirm) { + print < shows this screen) +\t./batchRebuildHoldingsTables.pl -c (c like confirm => rebuild non-MARC DB (may takes long) +\t-t => test only, change nothing in DB +EOF +; + exit; +} + +my $dbh = C4::Context->dbh; +my $i = 0; +my $starttime = time(); + +$| = 1; # flushes output +$starttime = time(); + +my $sth = $dbh->prepare("SELECT holding_id FROM holdings"); +$sth->execute(); +my @errors; +while (my ($holding_id) = $sth->fetchrow()) { + my $record = C4::Holdings::GetMarcHolding({ holding_id => $holding_id }); + if (not defined $record) { + push @errors, $holding_id; + next; + } + my $rowData = C4::Holdings::TransformMarcHoldingToKoha($record); + my $frameworkcode = C4::Holdings::GetHoldingFrameworkCode($holding_id); + C4::Holdings::_koha_modify_holding($dbh, $holding_id, $rowData, $frameworkcode) unless $test_parameter; + ++$i; +} +$sth->finish(); +my $timeneeded = time() - $starttime; +print "$i MARC records done in $timeneeded seconds\n"; +if (scalar(@errors) > 0) { + print 'Records that could not be processed: ', join(' ', @errors) . "\n"; +} -- 2.7.4