From 0e4f36b653af8425650dc76130cfa25af50429ae Mon Sep 17 00:00:00 2001 From: Ere Maijala Date: Thu, 8 Nov 2018 15:30:12 +0200 Subject: [PATCH] Bug 11529: Simplify and optimize batchRebuildBiblioTables.pl Signed-off-by: Michal Denar Signed-off-by: Michal Denar Signed-off-by: Josef Moravec Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize --- misc/batchRebuildBiblioTables.pl | 94 +++++++++++++------------------- 1 file changed, 37 insertions(+), 57 deletions(-) diff --git a/misc/batchRebuildBiblioTables.pl b/misc/batchRebuildBiblioTables.pl index d66c4ce6db..15f650377a 100755 --- a/misc/batchRebuildBiblioTables.pl +++ b/misc/batchRebuildBiblioTables.pl @@ -15,90 +15,70 @@ BEGIN { # 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 ( $input_marc_file, $number) = ('', 0); -my ($version, $confirm, $test_parameter); + +my ($version, $confirm); GetOptions( 'c' => \$confirm, - 'h' => \$version, - 't' => \$test_parameter, + 'h' => \$version ); if ($version || (!$confirm)) { print < shows this screen) -\t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long) -\t-t => test only, change nothing in DB +\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 = time(); - -$|=1; # flushes output -$starttime = gettimeofday; +my $starttime = gettimeofday; +my $marcflavour = C4::Context->preference('marcflavour'); +my $sth = $dbh->prepare('SELECT biblionumber, frameworkcode FROM biblio'); +$sth->execute(); -#1st of all, find item MARC tag. -my ($tagfield,$tagsubfield) = &GetMarcFromKohaField( "items.itemnumber" ); -# $dbh->do("lock tables biblio write, biblioitems write, items write, marc_biblio write, marc_subfield_table write, marc_blob_subfield write, marc_word write, marc_subfield_structure write"); -my $sth = $dbh->prepare("SELECT biblionumber FROM biblio"); -$sth->execute; -# my ($biblionumbermax) = $sth->fetchrow; -# warn "$biblionumbermax <<=="; my @errors; -while (my ($biblionumber)= $sth->fetchrow) { - #now, parse the record, extract the item fields, and store them in somewhere else. - my $record = GetMarcBiblio({ biblionumber => $biblionumber }); - if (not defined $record) { - push @errors, $biblionumber; - next; +while (my ($biblionumber, $frameworkcode) = $sth->fetchrow) { + my $marcxml = GetXmlBiblio($biblionumber); + if (not defined $marcxml) { + push @errors, $biblionumber; + next; } - my @fields = $record->field($tagfield); - my @items; - my $nbitems=0; - print "."; - my $timeneeded = gettimeofday - $starttime; - print "$i in $timeneeded s\n" unless ($i % 50); - $i++; - foreach my $field (@fields) { - my $item = MARC::Record->new(); - $item->append_fields($field); - push @items,$item; - $record->delete_field($field); - $nbitems++; + + $marcxml = C4::Charset::StripNonXmlChars( $marcxml ); + my $record = eval { + MARC::Record::new_from_xml($marcxml, 'utf8', $marcflavour); + }; + if ($@) { + push @errors, $biblionumber; + next; } -# print "$biblionumber\n"; - my $frameworkcode = GetFrameworkCode($biblionumber); - localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter; -} -# $dbh->do("unlock tables"); -my $timeneeded = time() - $starttime; -print "$i MARC record done in $timeneeded seconds\n"; -if (scalar(@errors) > 0) { - print "Some biblionumber could not be processed though: ", join(" ", @errors); + + 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); } -# modified NEWmodbiblio to jump the MARC part of the biblio modif -# highly faster -sub localNEWmodbiblio { - my ($dbh,$record,$biblionumber,$frameworkcode) =@_; - $frameworkcode="" unless $frameworkcode; - my $oldbiblio = TransformMarcToKoha($record,$frameworkcode); - C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); - C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio ); - return 1; +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); } -- 2.39.5