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