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

(-)a/misc/batchRebuildItemsTables.pl (-35 / +54 lines)
Lines 1-4 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
use Modern::Perl;
3
use Modern::Perl;
3
use Getopt::Long;
4
use Getopt::Long;
4
use C4::Context;
5
use C4::Context;
Lines 34-40 my $errorcount = 0; Link Here
34
my $starttime = gettimeofday;
35
my $starttime = gettimeofday;
35
my @errors;
36
my @errors;
36
my %opt;
37
my %opt;
37
my ($confirm, $help, $test_parameter, $where);
38
my ( $confirm, $help, $test_parameter, $where );
38
GetOptions(
39
GetOptions(
39
    'c' => \$confirm,
40
    'c' => \$confirm,
40
    'help|h' => \$help,
41
    'help|h' => \$help,
Lines 42-106 GetOptions( Link Here
42
    'where:s' => \$where,
43
    'where:s' => \$where,
43
) or pod2usage(2);
44
) or pod2usage(2);
44
45
45
pod2usage(1) if ($help || (!$confirm));
46
pod2usage(1) if $help ;# || !$confirm;
46
print "### Database will not be modified ###\n" if $test_parameter;
47
print "### Database will not be modified ###\n" if $test_parameter;
48
47
#dbh
49
#dbh
48
my $dbh = C4::Context->dbh;
50
my $dbh = C4::Context->dbh;
49
$dbh->{AutoCommit} = 0;
51
$dbh->{AutoCommit} = 0;
52
50
#sysprefs
53
#sysprefs
51
C4::Context->disable_syspref_cache() if ( defined( C4::Context->disable_syspref_cache() ) );
54
C4::Context->disable_syspref_cache() if ( defined( C4::Context->disable_syspref_cache() ) );
52
my $CataloguingLog = C4::Context->preference('CataloguingLog');
55
my $CataloguingLog = C4::Context->preference('CataloguingLog');
53
my $dontmerge = C4::Context->preference('dontmerge');
56
my $dontmerge = C4::Context->preference('dontmerge');
54
$dontmerge="0" unless defnonull($dontmerge);
57
$dontmerge = "0" unless defnonull( $dontmerge );
55
$dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'");
58
$dbh->do( "UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'" );
56
$dbh->do("UPDATE systempreferences SET value=1 where variable='dontmerge'");
59
$dbh->do( "UPDATE systempreferences SET value=1 where variable='dontmerge'" );
57
$dbh->commit() unless $test_parameter;
60
$dbh->commit() unless $test_parameter;
58
my ($itemfield,$itemnumbersubfield) = &GetMarcFromKohaField("items.itemnumber",'');
61
62
my ( $itemfield, $itemnumbersubfield ) = GetMarcFromKohaField( "items.itemnumber", '' );
63
59
#dbh query init
64
#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};
65
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);
66
$query .= qq{ WHERE $where } if $where;
62
67
63
my $sth = $dbh->prepare($query);
68
my $sth = $dbh->prepare( $query );
64
$sth->execute();
69
$sth->execute();
65
while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow )
70
while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow )
66
{
71
{
67
    $count++;
72
    $count++;
68
    warn $count unless $count %1000;
73
    warn $count unless $count %1000;
69
    my $extkey;
74
    my $record = GetMarcBiblio( $biblionumber, 1 );
70
    my $record = GetMarcBiblio($biblionumber,1);
75
    unless( $record ) {
71
    unless ($record){ push @errors, "bad record biblionumber $biblionumber"; next; }
76
        push @errors, "bad record biblionumber $biblionumber";
72
    my ($tmptestfields,$tmptestdirectory,$reclen,$tmptestbaseaddress) = MARC::File::USMARC::_build_tag_directory($record);
77
        next;
73
    #print "\n################################ record before ##################################\n".$record->as_formatted;#!test
78
    }
74
    unless ($test_parameter) {
79
    unless ( $test_parameter ) {
75
        my $rqitemnumber=$dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?");
80
        my $rqitemnumber = $dbh->prepare( "SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?" );
76
        foreach my $itemfield ($record->field($itemfield)){
81
        foreach my $itemfield ( $record->field($itemfield) ) {
77
            my $marcitem=MARC::Record->new();
82
            my $marcitem = MARC::Record->new;
78
            $marcitem->encoding('UTF-8');
83
            $marcitem->encoding( 'UTF-8' );
79
            $marcitem->append_fields($itemfield);
84
            $marcitem->append_fields( $itemfield );
80
            my $itemnum; my @itemnumbers = $itemfield->subfield($itemnumbersubfield);
85
            my $itemnum;
81
            foreach my $itemnumber ( @itemnumbers ){
86
            my @itemnumbers = $itemfield->subfield( $itemnumbersubfield );
82
                $rqitemnumber->execute($itemnumber, $biblionumber);
87
            foreach my $itemnumber ( @itemnumbers ) {
83
                if( my $row = $rqitemnumber->fetchrow_hashref ){ $itemnum = $row->{itemnumber};}
88
                $rqitemnumber->execute( $itemnumber, $biblionumber );
89
                if( my $row = $rqitemnumber->fetchrow_hashref ) {
90
                    $itemnum = $row->{itemnumber};
91
                }
92
            }
93
            eval {
94
                if( $itemnum ) {
95
                    ModItemFromMarc( $marcitem, $biblionumber, $itemnum );
96
                } else {
97
                    die "$biblionumber";
98
                }
99
            };
100
            if( $@ ) {
101
                warn "Problem with $biblionumber: $@";
102
                warn $record->as_formatted;
84
            }
103
            }
85
            eval{ if($itemnum){ ModItemFromMarc($marcitem,$biblionumber,$itemnum) }else{ die("$biblionumber"); } };
86
            if ($@){ warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; }
87
        }
104
        }
88
    }
105
    }
89
    unless ($test_parameter) {
106
    unless( $test_parameter ) {
90
        $dbh->commit() unless $count %1000;
107
        $dbh->commit() unless $count %1000;
91
    }
108
    }
92
}
109
}
93
110
94
my $sthCataloguingLog = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'");
111
my $sthCataloguingLog = $dbh->prepare( "UPDATE systempreferences SET value=? WHERE variable='CataloguingLog'" );
95
$sthCataloguingLog->execute($CataloguingLog);
112
$sthCataloguingLog->execute( $CataloguingLog );
96
my $sthdontmerge = $dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='dontmerge'");
113
my $sthdontmerge = $dbh->prepare( "UPDATE systempreferences SET value=? WHERE variable='dontmerge'" );
97
$sthdontmerge->execute($dontmerge);
114
$sthdontmerge->execute( $dontmerge );
98
$dbh->commit() unless $test_parameter;
115
$dbh->commit() unless $test_parameter;
99
my $timeneeded = time() - $starttime;
116
my $timeneeded = time() - $starttime;
100
print "$count MARC record done in $timeneeded seconds\n";
117
print "$count MARC record done in $timeneeded seconds\n";
101
if (scalar(@errors) > 0) {
118
if( scalar(@errors) > 0 ) {
102
    print "Some biblionumber could not be processed though: ", join(" ", @errors);
119
    print "Some biblionumber could not be processed though: ",
120
        join( " ", @errors );
103
}
121
}
104
122
105
sub defnonull { my $var = shift; defined $var and $var ne "" }
123
sub defnonull {
106
__END__
124
    my $var = shift;
125
    return defined $var and $var ne "";
126
}
107
- 

Return to bug 17447