Adding XSLTreultsFilename and XSLTDetailFilename
[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 my @errors;
57 while (my ($biblionumber)= $sth->fetchrow) {
58     #now, parse the record, extract the item fields, and store them in somewhere else.
59     my $record = GetMarcBiblio($biblionumber);
60     if (not defined $record) {
61         push @errors, $biblionumber;
62         next;
63     }
64     my @fields = $record->field($tagfield);
65     my @items;
66     my $nbitems=0;
67     print ".";
68     my $timeneeded = gettimeofday - $starttime;
69     print "$i in $timeneeded s\n" unless ($i % 50);
70     $i++;
71     foreach my $field (@fields) {
72         my $item = MARC::Record->new();
73         $item->append_fields($field);
74         push @items,$item;
75         $record->delete_field($field);
76         $nbitems++;
77     }
78 #     print "$biblionumber\n";
79     my $frameworkcode = GetFrameworkCode($biblionumber);
80     localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
81 }
82 # $dbh->do("unlock tables");
83 my $timeneeded = time() - $starttime;
84 print "$i MARC record done in $timeneeded seconds\n";
85 if (scalar(@errors) > 0) {
86     print "Some biblionumber could not be processed though: ", join(" ", @errors);
87 }
88
89 # modified NEWmodbiblio to jump the MARC part of the biblio modif
90 # highly faster
91 sub localNEWmodbiblio {
92     my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
93     $frameworkcode="" unless $frameworkcode;
94     my $oldbiblio = TransformMarcToKoha($dbh,$record,$frameworkcode);
95     C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
96     C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
97     return 1;
98 }