5 use Getopt::Long qw( GetOptions );
8 use Pod::Usage qw( pod2usage );
9 use Time::HiRes qw( gettimeofday );
13 use C4::Biblio qw( GetMarcBiblio GetMarcFromKohaField );
14 use C4::Items qw( ModItemFromMarc );
18 batchRebuildBiblioTables.pl - rebuilds the non-MARC DB items table from the MARC values
20 You can/must use it when you change items mapping.
24 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
27 -h --help (or without arguments) shows this help message
28 -c Confirm: rebuild non marc DB (may be long)
29 -t test only, change nothing in DB
30 --where add where condition on default query (eg. --where 'biblio.biblionumber<100')
36 my $starttime = gettimeofday;
38 my ( $confirm, $help, $test_parameter, $where );
42 't' => \$test_parameter,
46 pod2usage(1) if $help || ( !$confirm && !$test_parameter );
47 print "### Database will not be modified ###\n" if $test_parameter;
49 my $dbh = C4::Context->dbh;
50 my $schema = Koha::Database->schema;
54 C4::Context->disable_syspref_cache() if ( defined( C4::Context->disable_syspref_cache() ) );
55 my $CataloguingLog = C4::Context->preference('CataloguingLog');
56 my $mergelimit = C4::Context->preference('AuthorityMergeLimit');
57 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
58 $dbh->do("UPDATE systempreferences SET value=0 where variable='AuthorityMergeLimit'");
59 unless ($test_parameter){
63 my ( $itemfield, $itemnumbersubfield ) = &GetMarcFromKohaField( "items.itemnumber" );
67 qq{SELECT biblio.biblionumber AS biblionumber, biblioitems.biblioitemnumber AS biblioitemnumber, biblio.frameworkcode AS frameworkcode FROM biblio JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber};
68 $query .= qq{ WHERE $where } if ($where);
70 my $sth = $dbh->prepare($query);
72 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) {
74 warn $count unless $count % 1000;
75 my $record = GetMarcBiblio({
76 biblionumber => $biblionumber,
78 unless ($record) { push @errors, "bad record biblionumber $biblionumber"; next; }
80 unless ($test_parameter) {
81 my $rqitemnumber = $dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
82 foreach my $itemfield ( $record->field($itemfield) ) {
83 my $marcitem = MARC::Record->new();
84 $marcitem->encoding('UTF-8');
85 $marcitem->append_fields($itemfield);
87 my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
88 foreach my $itemnumber (@itemnumbers) {
89 $rqitemnumber->execute( $itemnumber, $biblionumber );
90 if ( my $row = $rqitemnumber->fetchrow_hashref ) { $itemnum = $row->{itemnumber}; }
93 if ($itemnum) { ModItemFromMarc( $marcitem, $biblionumber, $itemnum ) }
94 else { die("$biblionumber"); }
96 if ($@) { warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
100 unless ($test_parameter || $count % 1000) {
106 my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
107 $sthCataloguingLog->execute($CataloguingLog);
108 my $sthmergelimit = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='AuthorityMergeLimit'");
109 $sthmergelimit->execute($mergelimit);
110 $schema->txn_commit unless $test_parameter;
111 my $timeneeded = time() - $starttime;
112 print "$count MARC record done in $timeneeded seconds\n";
113 if ( scalar(@errors) > 0 ) {
114 print "Some biblionumber could not be processed though: ", join( " ", @errors );