Bug 31272: Use TT plugins for pickup library and due date in opac-reserve.pl
[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( GetMarcFromKohaField );
14 use C4::Items qw( ModItemFromMarc );
15 use Koha::Biblios;
16
17 =head1 NAME
18
19 batchRebuildBiblioTables.pl - rebuilds the non-MARC DB items table from the MARC values
20
21 You can/must use it when you change items mapping.
22
23 =head1 SYNOPSIS
24
25 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
26
27  Options:
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')
32
33 =cut
34
35 my $count      = 0;
36 my $errorcount = 0;
37 my $starttime  = gettimeofday;
38 my @errors;
39 my ( $confirm, $help, $test_parameter, $where );
40 GetOptions(
41     'c'       => \$confirm,
42     'help|h'  => \$help,
43     't'       => \$test_parameter,
44     'where:s' => \$where,
45 ) or pod2usage(2);
46
47 pod2usage(1) if $help || ( !$confirm && !$test_parameter );
48 print "### Database will not be modified ###\n" if $test_parameter;
49
50 my $dbh = C4::Context->dbh;
51 my $schema = Koha::Database->schema;
52 $schema->txn_begin;
53
54 #sysprefs
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){
61     $schema->txn_commit;
62     $schema->txn_begin;
63 }
64 my ( $itemfield, $itemnumbersubfield ) = &GetMarcFromKohaField( "items.itemnumber" );
65
66 #dbh query init
67 my $query =
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);
70
71 my $sth = $dbh->prepare($query);
72 $sth->execute();
73 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) {
74     $count++;
75     warn $count unless $count % 1000;
76     my $biblio = Koha::Biblios->find($biblionumber);
77     my $record = $biblio->metadata->record({ embed_items => 1 });
78
79     unless ($record) { push @errors, "bad record biblionumber $biblionumber"; next; }
80
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);
87             my $itemnum;
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}; }
92             }
93             eval {
94                 if ($itemnum) { ModItemFromMarc( $marcitem, $biblionumber, $itemnum ) }
95                 else          { die("$biblionumber"); }
96             };
97             if ($@) { warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
98         }
99     }
100
101     unless ($test_parameter || $count % 1000) {
102         $schema->txn_commit;
103         $schema->txn_begin;
104     }
105 }
106
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 );
116 }