From 5e2a8ad101663491635f1e425f1f2537f57b02b3 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. Signed-off-by: Michal Denar Signed-off-by: Michal Denar --- C4/Holdings.pm | 4 ++- misc/batchRebuildHoldingsTables.pl | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 misc/batchRebuildHoldingsTables.pl diff --git a/C4/Holdings.pm b/C4/Holdings.pm index ca9879e2bf..9a8b71753d 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; @@ -435,7 +436,8 @@ Returns an array of MARC::Record objects of the holdings for the biblio. =cut sub GetMarcHoldingsFields { - my ( $biblionumber ) = @_; + my ( $biblionumber ) = @_; + # This is so much faster than using Koha::Holdings->search that it makes sense even if it's ugly. my $sth = C4::Context->dbh->prepare( 'SELECT * FROM holdings WHERE biblionumber = ?' ); $sth->execute( $biblionumber ); diff --git a/misc/batchRebuildHoldingsTables.pl b/misc/batchRebuildHoldingsTables.pl new file mode 100755 index 0000000000..c05235756a --- /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.11.0