fixed variable masking warnings found by perl -w
[koha.git] / misc / batchRebuildBiblioTables.pl
1 #!/usr/bin/perl
2 # small script that rebuilds the non-MARC DB
3
4 use strict;
5 BEGIN {
6     # find Koha's Perl modules
7     # test carefully before changing this
8     use FindBin;
9     eval { require "$FindBin::Bin/kohalib.pl" };
10 }
11
12 # Koha modules used
13 # use MARC::File::USMARC;
14 use MARC::Record;
15 use MARC::Batch;
16 use C4::Context;
17 use C4::Biblio;
18 use Time::HiRes qw(gettimeofday);
19
20 use Getopt::Long;
21 my ( $input_marc_file, $number) = ('',0);
22 my ($version, $confirm,$test_parameter);
23 GetOptions(
24     'c' => \$confirm,
25     'h' => \$version,
26     't' => \$test_parameter,
27 );
28
29 if ($version || (!$confirm)) {
30     print <<EOF
31 This script rebuilds the non-MARC DB from the MARC values.
32 You can/must use it when you change your mapping.
33 For example : you decide to map biblio.title to 200$a (it was previously mapped to 610$a) : run this script or you will have strange
34 results in OPAC !
35 syntax :
36 \t./rebuildnonmarc.pl -h (or without arguments => shows this screen)
37 \t./rebuildnonmarc.pl -c (c like confirm => rebuild non marc DB (may be long)
38 \t-t => test only, change nothing in DB
39 EOF
40 ;
41 die;
42 }
43
44 my $dbh = C4::Context->dbh;
45 my $i=0;
46 my $starttime = time();
47
48 $|=1; # flushes output
49 $starttime = gettimeofday;
50
51 #1st of all, find item MARC tag.
52 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
53 # $dbh->do("lock tables biblio write, biblioitems write, items write, marc_biblio write, marc_subfield_table write, marc_blob_subfield write, marc_word write, marc_subfield_structure write, stopwords write");
54 my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
55 $sth->execute;
56 # my ($biblionumbermax) =  $sth->fetchrow;
57 # warn "$biblionumbermax <<==";
58 while (my ($biblionumber)= $sth->fetchrow) {
59     #now, parse the record, extract the item fields, and store them in somewhere else.
60     my $record = GetMarcBiblio($biblionumber);
61     my @fields = $record->field($tagfield);
62     my @items;
63     my $nbitems=0;
64     print ".";
65     my $timeneeded = gettimeofday - $starttime;
66     print "$i in $timeneeded s\n" unless ($i % 50);
67     $i++;
68     foreach my $field (@fields) {
69         my $item = MARC::Record->new();
70         $item->append_fields($field);
71         push @items,$item;
72         $record->delete_field($field);
73         $nbitems++;
74     }
75 #     print "$biblionumber\n";
76     my $frameworkcode = GetFrameworkCode($biblionumber);
77     localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
78 }
79 # $dbh->do("unlock tables");
80 my $timeneeded = time() - $starttime;
81 print "$i MARC record done in $timeneeded seconds\n";
82
83 # modified NEWmodbiblio to jump the MARC part of the biblio modif
84 # highly faster
85 sub localNEWmodbiblio {
86     my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
87     $frameworkcode="" unless $frameworkcode;
88     my $oldbiblio = TransformMarcToKoha($dbh,$record,$frameworkcode);
89     C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio );
90     C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
91     return 1;
92 }