Bug 20996: (QA follow-up) Fix pod
[koha.git] / misc / batchRebuildItemsTables.pl
blob0e611dd701533bb63465eea38f482e47f9d8cdfd
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 C4::Context;
12 use C4::Biblio;
13 use C4::Items;
15 =head1 NAME
17 batchRebuildBiblioTables.pl - rebuilds the non-MARC DB items table from the MARC values
19 You can/must use it when you change items mapping.
21 =head1 SYNOPSIS
23 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
25 Options:
26 -h --help (or without arguments) shows this help message
27 -c Confirm: rebuild non marc DB (may be long)
28 -t test only, change nothing in DB
29 --where add where condition on default query (eg. --where 'biblio.biblionumber<100')
31 =cut
33 my $count = 0;
34 my $errorcount = 0;
35 my $starttime = gettimeofday;
36 my @errors;
37 my ( $confirm, $help, $test_parameter, $where );
38 GetOptions(
39 'c' => \$confirm,
40 'help|h' => \$help,
41 't' => \$test_parameter,
42 'where:s' => \$where,
43 ) or pod2usage(2);
45 pod2usage(1) if $help || ( !$confirm && !$test_parameter );
46 print "### Database will not be modified ###\n" if $test_parameter;
48 #dbh
49 my $dbh = C4::Context->dbh;
50 $dbh->{AutoCommit} = 0;
52 #sysprefs
53 C4::Context->disable_syspref_cache() if ( defined( C4::Context->disable_syspref_cache() ) );
54 my $CataloguingLog = C4::Context->preference('CataloguingLog');
55 my $mergelimit = C4::Context->preference('AuthorityMergeLimit');
56 $dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
57 $dbh->do("UPDATE systempreferences SET value=0 where variable='AuthorityMergeLimit'");
58 $dbh->commit() unless $test_parameter;
59 my ( $itemfield, $itemnumbersubfield ) = &GetMarcFromKohaField( "items.itemnumber", '' );
61 #dbh query init
62 my $query =
63 qq{SELECT biblio.biblionumber AS biblionumber, biblioitems.biblioitemnumber AS biblioitemnumber, biblio.frameworkcode AS frameworkcode FROM biblio JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber};
64 $query .= qq{ WHERE $where } if ($where);
66 my $sth = $dbh->prepare($query);
67 $sth->execute();
68 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) {
69 $count++;
70 warn $count unless $count % 1000;
71 my $record = GetMarcBiblio({
72 biblionumber => $biblionumber,
73 embed_items => 1 });
74 unless ($record) { push @errors, "bad record biblionumber $biblionumber"; next; }
76 unless ($test_parameter) {
77 my $rqitemnumber = $dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
78 foreach my $itemfield ( $record->field($itemfield) ) {
79 my $marcitem = MARC::Record->new();
80 $marcitem->encoding('UTF-8');
81 $marcitem->append_fields($itemfield);
82 my $itemnum;
83 my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
84 foreach my $itemnumber (@itemnumbers) {
85 $rqitemnumber->execute( $itemnumber, $biblionumber );
86 if ( my $row = $rqitemnumber->fetchrow_hashref ) { $itemnum = $row->{itemnumber}; }
88 eval {
89 if ($itemnum) { ModItemFromMarc( $marcitem, $biblionumber, $itemnum ) }
90 else { die("$biblionumber"); }
92 if ($@) { warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
95 unless ($test_parameter) {
96 $dbh->commit() unless $count % 1000;
100 my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
101 $sthCataloguingLog->execute($CataloguingLog);
102 my $sthmergelimit = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='AuthorityMergeLimit'");
103 $sthmergelimit->execute($mergelimit);
104 $dbh->commit() unless $test_parameter;
105 my $timeneeded = time() - $starttime;
106 print "$count MARC record done in $timeneeded seconds\n";
107 if ( scalar(@errors) > 0 ) {
108 print "Some biblionumber could not be processed though: ", join( " ", @errors );