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