Translation updates for Koha 3.22.0 release
[koha.git] / misc / batchRebuildItemsTables.pl
blob61431c417d5cb6b7d8f23f84a046af452cd85a62
1 #!/usr/bin/perl
2 use Modern::Perl;
3 use Getopt::Long;
4 use C4::Context;
5 use C4::Biblio;
6 use C4::Items;
7 use MARC::Record;
8 use MARC::Field;
9 use Data::Dumper;
10 use Time::HiRes qw(gettimeofday);
11 use Pod::Usage;
13 =head1 NAME
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.
19 =head1 SYNOPSIS
21 batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
23 Options:
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')
29 =cut
32 my $count = 0;
33 my $errorcount = 0;
34 my $starttime = gettimeofday;
35 my @errors;
36 my %opt;
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));
46 print "### Database will not be modified ###\n" if $test_parameter;
47 #dbh
48 my $dbh = C4::Context->dbh;
49 $dbh->{AutoCommit} = 0;
50 #sysprefs
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",'');
59 #dbh query init
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);
64 $sth->execute();
65 while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow )
67 $count++;
68 warn $count unless $count %1000;
69 my $extkey;
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 #print "\n################################ record before ##################################\n".$record->as_formatted;#!test
74 unless ($test_parameter) {
75 my $rqitemnumber=$dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
76 foreach my $itemfield ($record->field($itemfield)){
77 my $marcitem=MARC::Record->new();
78 $marcitem->encoding('UTF-8');
79 $marcitem->append_fields($itemfield);
80 my $itemnum; my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
81 foreach my $itemnumber ( @itemnumbers ){
82 $rqitemnumber->execute($itemnumber, $biblionumber);
83 if( my $row = $rqitemnumber->fetchrow_hashref ){ $itemnum = $row->{itemnumber};}
85 eval{ if($itemnum){ ModItemFromMarc($marcitem,$biblionumber,$itemnum) }else{ die("$biblionumber"); } };
86 if ($@){ warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
89 unless ($test_parameter) {
90 $dbh->commit() unless $count %1000;
94 my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
95 $sthCataloguingLog->execute($CataloguingLog);
96 my $sthdontmerge = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='dontmerge'");
97 $sthdontmerge->execute($dontmerge);
98 $dbh->commit() unless $test_parameter;
99 my $timeneeded = time() - $starttime;
100 print "$count MARC record done in $timeneeded seconds\n";
101 if (scalar(@errors) > 0) {
102 print "Some biblionumber could not be processed though: ", join(" ", @errors);
105 sub defnonull { my $var = shift; defined $var and $var ne "" }
106 __END__