Bug 22417: Restore the concept of namespace
[koha.git] / misc / batchRebuildItemsTables.pl
blob5bb40e5d35331cc9f6fa84e652ad6e849e787674
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Getopt::Long;
6 use MARC::Field;
7 use MARC::Record;
8 use Pod::Usage;
9 use Time::HiRes qw(gettimeofday);
11 use Koha::Script;
12 use C4::Context;
13 use C4::Biblio;
14 use C4::Items;
16 =head1 NAME
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.
22 =head1 SYNOPSIS
24 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
26 Options:
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')
32 =cut
34 my $count = 0;
35 my $errorcount = 0;
36 my $starttime = gettimeofday;
37 my @errors;
38 my ( $confirm, $help, $test_parameter, $where );
39 GetOptions(
40 'c' => \$confirm,
41 'help|h' => \$help,
42 't' => \$test_parameter,
43 'where:s' => \$where,
44 ) or pod2usage(2);
46 pod2usage(1) if $help || ( !$confirm && !$test_parameter );
47 print "### Database will not be modified ###\n" if $test_parameter;
49 #dbh
50 my $dbh = C4::Context->dbh;
51 $dbh->{AutoCommit} = 0;
53 #sysprefs
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" );
62 #dbh query init
63 my $query =
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);
68 $sth->execute();
69 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) {
70 $count++;
71 warn $count unless $count % 1000;
72 my $record = GetMarcBiblio({
73 biblionumber => $biblionumber,
74 embed_items => 1 });
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);
83 my $itemnum;
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}; }
89 eval {
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 );