10 use Time
::HiRes
qw(gettimeofday);
15 batchRebuildBiblioTables.pl - rebuilds the non-MARC DB items table from the MARC values
17 You can/must use it when you change items mapping.
21 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
24 -h --help (or without arguments) shows this help message
25 -c Confirm: rebuild non marc DB (may be long)
26 -t test only, change nothing in DB
27 --where add where condition on default query (eg. --where 'biblio.biblionumber<100')
34 my $starttime = gettimeofday
;
37 my ($confirm, $help, $test_parameter, $where);
41 't' => \
$test_parameter,
45 pod2usage
(1) if ($help || (!$confirm));
46 print "### Database will not be modified ###\n" if $test_parameter;
48 my $dbh = C4
::Context
->dbh;
49 $dbh->{AutoCommit
} = 0;
51 C4
::Context
->disable_syspref_cache() if ( defined( C4
::Context
->disable_syspref_cache() ) );
52 my $CataloguingLog = C4
::Context
->preference('CataloguingLog');
53 my $dontmerge = C4
::Context
->preference('dontmerge');
54 $dontmerge="0" unless defnonull
($dontmerge);
55 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
56 $dbh->do("UPDATE systempreferences SET value=1 where variable='dontmerge'");
57 $dbh->commit() unless $test_parameter;
58 my ($itemfield,$itemnumbersubfield) = &GetMarcFromKohaField
("items.itemnumber",'');
60 my $query = qq{SELECT biblio
.biblionumber AS biblionumber
, biblioitems
.biblioitemnumber AS biblioitemnumber
, biblio
.frameworkcode AS frameworkcode FROM biblio JOIN biblioitems ON biblio
.biblionumber
=biblioitems
.biblionumber
};
61 $query.=qq{ WHERE
$where } if ($where);
63 my $sth = $dbh->prepare($query);
65 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow )
68 warn $count unless $count %1000;
70 my $record = GetMarcBiblio
($biblionumber,1);
71 unless ($record){ push @errors, "bad record biblionumber $biblionumber"; next; }
72 my ($tmptestfields,$tmptestdirectory,$reclen,$tmptestbaseaddress) = MARC
::File
::USMARC
::_build_tag_directory
($record);
73 if ($reclen > 99999) { push @errors, "bad record length for biblionumber $biblionumber (length : $reclen) "; next; }
74 #print "\n################################ record before ##################################\n".$record->as_formatted;#!test
75 unless ($test_parameter) {
76 my $rqitemnumber=$dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
77 foreach my $itemfield ($record->field($itemfield)){
78 my $marcitem=MARC
::Record
->new();
79 $marcitem->encoding('UTF-8');
80 $marcitem->append_fields($itemfield);
81 my $itemnum; my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
82 foreach my $itemnumber ( @itemnumbers ){
83 $rqitemnumber->execute($itemnumber, $biblionumber);
84 if( my $row = $rqitemnumber->fetchrow_hashref ){ $itemnum = $row->{itemnumber
};}
86 eval{ if($itemnum){ ModItemFromMarc
($marcitem,$biblionumber,$itemnum) }else{ die("$biblionumber"); } };
87 if ($@
){ warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
90 unless ($test_parameter) {
91 $dbh->commit() unless $count %1000;
95 my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
96 $sthCataloguingLog->execute($CataloguingLog);
97 my $sthdontmerge = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='dontmerge'");
98 $sthdontmerge->execute($dontmerge);
99 $dbh->commit() unless $test_parameter;
100 my $timeneeded = time() - $starttime;
101 print "$count MARC record done in $timeneeded seconds\n";
102 if (scalar(@errors) > 0) {
103 print "Some biblionumber could not be processed though: ", join(" ", @errors);
106 sub defnonull
{ my $var = shift; defined $var and $var ne "" }