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 utf8; |
14 |
#use open qw/ :std :utf8 /; |
15 |
my $count = 0; |
16 |
my $errorcount = 0; |
17 |
my $starttime = gettimeofday; |
18 |
my @errors; |
19 |
my %opt; |
20 |
my ($confirm, $help, $test_parameter, $where); |
21 |
GetOptions( |
22 |
'c' => \$confirm, |
23 |
'h' => \$help, |
24 |
't' => \$test_parameter, |
25 |
'where:s' => \$where, |
26 |
); |
27 |
|
28 |
if ($help || (!$confirm)) { |
29 |
print <<EOF |
30 |
This script rebuilds the non-MARC DB items table from the MARC values. |
31 |
You can/must use it when you change your mapping. |
32 |
|
33 |
Syntax: |
34 |
\t./batchRebuildItemsTables.pl -h (or without arguments => shows this screen) |
35 |
\t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long) |
36 |
\t-t => test only, change nothing in DB |
37 |
\t-where => optionnal where condition on default query (eg. -where 'biblio.biblionumber < 100') |
38 |
EOF |
39 |
; |
40 |
exit; |
41 |
} |
42 |
#dbh |
43 |
my $dbh = C4::Context->dbh; |
44 |
$dbh->{AutoCommit} = 0; |
45 |
#sysprefs |
46 |
C4::Context->disable_syspref_cache() if ( defined( C4::Context->disable_syspref_cache() ) ); |
47 |
my $CataloguingLog = C4::Context->preference('CataloguingLog'); |
48 |
my $dontmerge = C4::Context->preference('dontmerge'); |
49 |
$dontmerge="0" unless defnonull($dontmerge); |
50 |
$dbh->do("UPDATE systempreferences SET value=0 WHERE variable='CataloguingLog'"); |
51 |
$dbh->do("UPDATE systempreferences SET value=1 where variable='dontmerge'"); |
52 |
$dbh->commit() unless $test_parameter; |
53 |
my ($itemfield,$itemnumbersubfield) = &GetMarcFromKohaField("items.itemnumber",''); |
54 |
#dbh query init |
55 |
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}; |
56 |
$query.=qq{ WHERE $where } if ($where); |
57 |
|
58 |
#warn $query; |
59 |
#sub defnonull { my $var = shift; defined $var and $var ne "" } |
60 |
#__END__ |
61 |
my $sth = $dbh->prepare($query); |
62 |
$sth->execute(); |
63 |
while ( my ( $biblionumber, $biblioitemnumber, $frameworkcode ) = $sth->fetchrow ) |
64 |
{ |
65 |
$count++; |
66 |
warn $count unless $count %1000; |
67 |
my $extkey; |
68 |
my $record = GetMarcBiblio($biblionumber,1); |
69 |
unless ($record){ push @errors, "bad record biblionumber $biblionumber"; next; } |
70 |
my ($tmptestfields,$tmptestdirectory,$reclen,$tmptestbaseaddress) = MARC::File::USMARC::_build_tag_directory($record); |
71 |
if ($reclen > 99999) { push @errors, "bad record length for biblionumber $biblionumber (length : $reclen) "; next; } |
72 |
#print "\n################################ record before ##################################\n".$record->as_formatted;#!test |
73 |
unless ($test_parameter) { |
74 |
my $rqitemnumber=$dbh->prepare("SELECT itemnumber, biblionumber from items where itemnumber = ? and biblionumber = ?"); |
75 |
foreach my $itemfield ($record->field($itemfield)){ |
76 |
my $marcitem=MARC::Record->new(); |
77 |
$marcitem->encoding('UTF-8'); |
78 |
$marcitem->append_fields($itemfield); |
79 |
my $itemnum; my @itemnumbers = $itemfield->subfield($itemnumbersubfield); |
80 |
foreach my $itemnumber ( @itemnumbers ){ |
81 |
$rqitemnumber->execute($itemnumber, $biblionumber); |
82 |
if( my $row = $rqitemnumber->fetchrow_hashref ){ $itemnum = $row->{itemnumber};} |
83 |
} |
84 |
eval{ if($itemnum){ ModItemFromMarc($marcitem,$biblionumber,$itemnum) }else{ die("$biblionumber"); } }; |
85 |
if ($@){ warn "Problem with : $biblionumber : $@"; warn $record->as_formatted; } |
86 |
} |
87 |
} |
88 |
unless ($test_parameter) { |
89 |
$dbh->commit() unless $count %1000; |
90 |
} |
91 |
} |
92 |
|
93 |
$dbh->do("UPDATE systempreferences SET value=$CataloguingLog WHERE variable='CataloguingLog'"); |
94 |
$dbh->do("UPDATE systempreferences SET value=$dontmerge where variable='dontmerge'"); |
95 |
$dbh->commit() unless $test_parameter; |
96 |
my $timeneeded = time() - $starttime; |
97 |
print "$count MARC record done in $timeneeded seconds\n"; |
98 |
if (scalar(@errors) > 0) { |
99 |
print "Some biblionumber could not be processed though: ", join(" ", @errors); |
100 |
} |
101 |
|
102 |
sub defnonull { my $var = shift; defined $var and $var ne "" } |
103 |
__END__ |