9 use Time
::HiRes
qw(gettimeofday);
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;
50 my $dbh = C4
::Context
->dbh;
51 $dbh->{AutoCommit
} = 0;
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 $dbh->commit() unless $test_parameter;
60 my ( $itemfield, $itemnumbersubfield ) = &GetMarcFromKohaField
( "items.itemnumber", '' );
64 qq{SELECT biblio
.biblionumber AS biblionumber
, biblioitems
.biblioitemnumber AS biblioitemnumber
, biblio
.frameworkcode AS frameworkcode FROM biblio JOIN biblioitems ON biblio
.biblionumber
=biblioitems
.biblionumber
};
65 $query .= qq{ WHERE
$where } if ($where);
67 my $sth = $dbh->prepare($query);
69 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) {
71 warn $count unless $count % 1000;
72 my $record = GetMarcBiblio
({
73 biblionumber
=> $biblionumber,
75 unless ($record) { push @errors, "bad record biblionumber $biblionumber"; next; }
77 unless ($test_parameter) {
78 my $rqitemnumber = $dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
79 foreach my $itemfield ( $record->field($itemfield) ) {
80 my $marcitem = MARC
::Record
->new();
81 $marcitem->encoding('UTF-8');
82 $marcitem->append_fields($itemfield);
84 my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
85 foreach my $itemnumber (@itemnumbers) {
86 $rqitemnumber->execute( $itemnumber, $biblionumber );
87 if ( my $row = $rqitemnumber->fetchrow_hashref ) { $itemnum = $row->{itemnumber
}; }
90 if ($itemnum) { ModItemFromMarc
( $marcitem, $biblionumber, $itemnum ) }
91 else { die("$biblionumber"); }
93 if ($@
) { warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
96 unless ($test_parameter) {
97 $dbh->commit() unless $count % 1000;
101 my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
102 $sthCataloguingLog->execute($CataloguingLog);
103 my $sthmergelimit = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='AuthorityMergeLimit'");
104 $sthmergelimit->execute($mergelimit);
105 $dbh->commit() unless $test_parameter;
106 my $timeneeded = time() - $starttime;
107 print "$count MARC record done in $timeneeded seconds\n";
108 if ( scalar(@errors) > 0 ) {
109 print "Some biblionumber could not be processed though: ", join( " ", @errors );