View | Details | Raw Unified | Return to bug 11399
Collapse All | Expand All

(-)a/misc/batchRebuildItemsTables.pl (-1 / +107 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
use 5.10.0;
3
use strict;
4
use warnings;
5
use Getopt::Long;
6
use C4::Context;
7
use C4::Biblio;
8
use C4::Items;
9
use MARC::Record;
10
use MARC::Field;
11
use Data::Dumper;
12
use Time::HiRes qw(gettimeofday);
13
use Pod::Usage;
14
15
=head1 NAME
16
17
batchRebuildBiblioTables.pl - rebuilds the non-MARC DB items table from the MARC values
18
19
You can/must use it when you change items mapping.
20
21
=head1 SYNOPSIS
22
23
batchRebuildItemsTables.pl [ -h ][ -c ][ -t ][ --where ]
24
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')
30
31
=cut
32
33
34
my $count = 0;
35
my $errorcount = 0;
36
my $starttime = gettimeofday;
37
my @errors;
38
my %opt;
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));
48
print "### Database will not be modified ###\n" if $test_parameter;
49
#dbh
50
my $dbh = C4::Context->dbh;
51
$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 $dontmerge = C4::Context->preference('dontmerge');
56
$dontmerge="0" unless defnonull($dontmerge);
57
$dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
58
$dbh->do("UPDATE systempreferences SET value=1 where variable='dontmerge'");
59
$dbh->commit() unless $test_parameter;
60
my ($itemfield,$itemnumbersubfield) = &GetMarcFromKohaField("items.itemnumber",'');
61
#dbh query init
62
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};
63
$query.=qq{ WHERE $where } if ($where);
64
65
my $sth = $dbh->prepare($query);
66
$sth->execute();
67
while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow )
68
{
69
    $count++;
70
    warn $count unless $count %1000;
71
    my $extkey;
72
    my $record = GetMarcBiblio($biblionumber,1);
73
    unless ($record){ push @errors, "bad record biblionumber $biblionumber"; next; }
74
    my ($tmptestfields,$tmptestdirectory,$reclen,$tmptestbaseaddress) = MARC::File::USMARC::_build_tag_directory($record);
75
    if ($reclen > 99999) { push @errors,  "bad record length for biblionumber $biblionumber (length : $reclen) "; next; }
76
    #print "\n################################ record before ##################################\n".$record->as_formatted;#!test
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; 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};}
87
            }
88
            eval{ if($itemnum){ ModItemFromMarc($marcitem,$biblionumber,$itemnum) }else{ die("$biblionumber"); } };
89
            if ($@){ warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }    
90
        }
91
    }
92
    unless ($test_parameter) {
93
        $dbh->commit() unless $count %1000;
94
    }
95
}
96
97
$dbh->do("UPDATE systempreferences SET value=$CataloguingLog WHERE variable='CataloguingLog'");
98
$dbh->do("UPDATE systempreferences SET value=$dontmerge where variable='dontmerge'");
99
$dbh->commit() unless $test_parameter;
100
my $timeneeded = time() - $starttime;
101
print "$count MARC record done in $timeneeded seconds\n";
102
if (scalar(@errors) > 0) {
103
    print "Some biblionumber could not be processed though: ", join(" ", @errors);
104
}
105
106
sub defnonull { my $var = shift; defined $var and $var ne "" }
107
__END__

Return to bug 11399