Bug 2365 : Inner counter not properly set for serials subscriptions not starting...
[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::Record;
14 use C4::Context;
15 use C4::Biblio;
16 use Time::HiRes qw(gettimeofday);
17
18 use Getopt::Long;
19 my ( $input_marc_file, $number) = ('',0);
20 my ($version, $confirm,$test_parameter);
21 GetOptions(
22     'c' => \$confirm,
23     'h' => \$version,
24     't' => \$test_parameter,
25 );
26
27 if ($version || (!$confirm)) {
28     print <<EOF
29 This script rebuilds the non-MARC DB from the MARC values.
30 You can/must use it when you change your mapping.
31 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
32 results in OPAC !
33 syntax :
34 \t./rebuildnonmarc.pl -h (or without arguments => shows this screen)
35 \t./rebuildnonmarc.pl -c (c like confirm => rebuild non marc DB (may be long)
36 \t-t => test only, change nothing in DB
37 EOF
38 ;
39 die;
40 }
41
42 my $dbh = C4::Context->dbh;
43 my $i=0;
44 my $starttime = time();
45
46 $|=1; # flushes output
47 $starttime = gettimeofday;
48
49 #1st of all, find item MARC tag.
50 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
51 # $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");
52 my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
53 $sth->execute;
54 # my ($biblionumbermax) =  $sth->fetchrow;
55 # warn "$biblionumbermax <<==";
56 while (my ($biblionumber)= $sth->fetchrow) {
57     #now, parse the record, extract the item fields, and store them in somewhere else.
58     my $record = GetMarcBiblio($biblionumber);
59     my @fields = $record->field($tagfield);
60     my @items;
61     my $nbitems=0;
62     print ".";
63     my $timeneeded = gettimeofday - $starttime;
64     print "$i in $timeneeded s\n" unless ($i % 50);
65     $i++;
66     foreach my $field (@fields) {
67         my $item = MARC::Record->new();
68         $item->append_fields($field);
69         push @items,$item;
70         $record->delete_field($field);
71         $nbitems++;
72     }
73 #     print "$biblionumber\n";
74     my $frameworkcode = GetFrameworkCode($biblionumber);
75     localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
76 }
77 # $dbh->do("unlock tables");
78 my $timeneeded = time() - $starttime;
79 print "$i MARC record done in $timeneeded seconds\n";
80
81 # modified NEWmodbiblio to jump the MARC part of the biblio modif
82 # highly faster
83 sub localNEWmodbiblio {
84     my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
85     $frameworkcode="" unless $frameworkcode;
86     my $oldbiblio = TransformMarcToKoha($dbh,$record,$frameworkcode);
87     C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio );
88     C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
89     return 1;
90 }