13f0c4117e455673aa4dc032fe062aaf14d4f4d4
[koha.git] / misc / batchRebuildItemsTables.pl
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Getopt::Long qw( GetOptions );
6 use MARC::Field;
7 use MARC::Record;
8 use Pod::Usage qw( pod2usage );
9 use Time::HiRes qw( gettimeofday );
10
11 use Koha::Script;
12 use C4::Context;
13 use C4::Biblio qw( GetMarcBiblio GetMarcFromKohaField );
14 use C4::Items qw( ModItemFromMarc );
15
16 =head1 NAME
17
18 batchRebuildBiblioTables.pl - rebuilds the non-MARC DB items table from the MARC values
19
20 You can/must use it when you change items mapping.
21
22 =head1 SYNOPSIS
23
24 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
25
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')
31
32 =cut
33
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);
45
46 pod2usage(1) if $help || ( !$confirm && !$test_parameter );
47 print "### Database will not be modified ###\n" if $test_parameter;
48
49 my $dbh = C4::Context->dbh;
50 my $schema = Koha::Database->schema;
51 $schema->txn_begin;
52
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 unless ($test_parameter){
60     $schema->txn_commit;
61     $schema->txn_begin;
62 }
63 my ( $itemfield, $itemnumbersubfield ) = &GetMarcFromKohaField( "items.itemnumber" );
64
65 #dbh query init
66 my $query =
67 qq{SELECT biblio.biblionumber AS biblionumber, biblioitems.biblioitemnumber AS biblioitemnumber, biblio.frameworkcode AS frameworkcode FROM biblio JOIN biblioitems ON biblio.biblionumber=biblioitems.biblionumber};
68 $query .= qq{ WHERE $where } if ($where);
69
70 my $sth = $dbh->prepare($query);
71 $sth->execute();
72 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) {
73     $count++;
74     warn $count unless $count % 1000;
75     my $record = GetMarcBiblio({
76         biblionumber => $biblionumber,
77         embed_items   => 1 });
78     unless ($record) { push @errors, "bad record biblionumber $biblionumber"; next; }
79
80     unless ($test_parameter) {
81         my $rqitemnumber = $dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
82         foreach my $itemfield ( $record->field($itemfield) ) {
83             my $marcitem = MARC::Record->new();
84             $marcitem->encoding('UTF-8');
85             $marcitem->append_fields($itemfield);
86             my $itemnum;
87             my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
88             foreach my $itemnumber (@itemnumbers) {
89                 $rqitemnumber->execute( $itemnumber, $biblionumber );
90                 if ( my $row = $rqitemnumber->fetchrow_hashref ) { $itemnum = $row->{itemnumber}; }
91             }
92             eval {
93                 if ($itemnum) { ModItemFromMarc( $marcitem, $biblionumber, $itemnum ) }
94                 else          { die("$biblionumber"); }
95             };
96             if ($@) { warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
97         }
98     }
99
100     unless ($test_parameter || $count % 1000) {
101         $schema->txn_commit;
102         $schema->txn_begin;
103     }
104 }
105
106 my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
107 $sthCataloguingLog->execute($CataloguingLog);
108 my $sthmergelimit = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='AuthorityMergeLimit'");
109 $sthmergelimit->execute($mergelimit);
110 $schema->txn_commit unless $test_parameter;
111 my $timeneeded = time() - $starttime;
112 print "$count MARC record done in $timeneeded seconds\n";
113 if ( scalar(@errors) > 0 ) {
114     print "Some biblionumber could not be processed though: ", join( " ", @errors );
115 }