Bug 18999: Modified SQL query in GetBudgetSpent() in C4/Budgets.pm
[koha.git] / misc / batchRebuildBiblioTables.pl
blob5db801bbc10d15512b6155d05948c08c7e5a18a3
1 #!/usr/bin/perl
2 # Small script that rebuilds the non-MARC DB
3 # Formerly named rebuildnonmarc.pl
5 use strict;
6 #use warnings; FIXME - Bug 2505
8 BEGIN {
9 # find Koha's Perl modules
10 # test carefully before changing this
11 use FindBin;
12 eval { require "$FindBin::Bin/kohalib.pl" };
15 # Koha modules used
16 use MARC::Record;
17 use C4::Context;
18 use C4::Biblio;
19 use Time::HiRes qw(gettimeofday);
21 use Getopt::Long;
22 my ( $input_marc_file, $number) = ('', 0);
23 my ($version, $confirm, $test_parameter);
24 GetOptions(
25 'c' => \$confirm,
26 'h' => \$version,
27 't' => \$test_parameter,
30 if ($version || (!$confirm)) {
31 print <<EOF
32 This script rebuilds the non-MARC DB from the MARC values.
33 You can/must use it when you change your mapping.
35 Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a).
36 Run this script or you will have strange results in OPAC !
38 Syntax:
39 \t./batchRebuildBiblioTables.pl -h (or without arguments => shows this screen)
40 \t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long)
41 \t-t => test only, change nothing in DB
42 EOF
44 exit;
47 my $dbh = C4::Context->dbh;
48 my $i=0;
49 my $starttime = time();
51 $|=1; # flushes output
52 $starttime = gettimeofday;
54 #1st of all, find item MARC tag.
55 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
56 # $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");
57 my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
58 $sth->execute;
59 # my ($biblionumbermax) = $sth->fetchrow;
60 # warn "$biblionumbermax <<==";
61 my @errors;
62 while (my ($biblionumber)= $sth->fetchrow) {
63 #now, parse the record, extract the item fields, and store them in somewhere else.
64 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
65 if (not defined $record) {
66 push @errors, $biblionumber;
67 next;
69 my @fields = $record->field($tagfield);
70 my @items;
71 my $nbitems=0;
72 print ".";
73 my $timeneeded = gettimeofday - $starttime;
74 print "$i in $timeneeded s\n" unless ($i % 50);
75 $i++;
76 foreach my $field (@fields) {
77 my $item = MARC::Record->new();
78 $item->append_fields($field);
79 push @items,$item;
80 $record->delete_field($field);
81 $nbitems++;
83 # print "$biblionumber\n";
84 my $frameworkcode = GetFrameworkCode($biblionumber);
85 localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
87 # $dbh->do("unlock tables");
88 my $timeneeded = time() - $starttime;
89 print "$i MARC record done in $timeneeded seconds\n";
90 if (scalar(@errors) > 0) {
91 print "Some biblionumber could not be processed though: ", join(" ", @errors);
94 # modified NEWmodbiblio to jump the MARC part of the biblio modif
95 # highly faster
96 sub localNEWmodbiblio {
97 my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
98 $frameworkcode="" unless $frameworkcode;
99 my $oldbiblio = TransformMarcToKoha($record,$frameworkcode);
100 C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
101 C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
102 return 1;