@@ -, +, @@ --- C4/Holdings.pm | 4 +- misc/batchRebuildHoldingsTables.pl | 71 ++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 misc/batchRebuildHoldingsTables.pl --- a/C4/Holdings.pm +++ a/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 ); --- a/misc/batchRebuildHoldingsTables.pl +++ a/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"; +} --