Bug 21899: Update MARC21 frameworks to Update 27 (November 2018)
[koha.git] / misc / batchRebuildBiblioTables.pl
blobf9bd21800b4d592fca2a0ff965d003b0e9ece558
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 Koha::Script;
17 use MARC::Record;
18 use C4::Context;
19 use C4::Biblio;
20 use Time::HiRes qw(gettimeofday);
22 use Getopt::Long;
23 my ( $input_marc_file, $number) = ('', 0);
24 my ($version, $confirm, $test_parameter);
25 GetOptions(
26 'c' => \$confirm,
27 'h' => \$version,
28 't' => \$test_parameter,
31 if ($version || (!$confirm)) {
32 print <<EOF
33 This script rebuilds the non-MARC DB from the MARC values.
34 You can/must use it when you change your mapping.
36 Example: you decide to map biblio.title to 200\$a (it was previously mapped to 610\$a).
37 Run this script or you will have strange results in OPAC !
39 Syntax:
40 \t./batchRebuildBiblioTables.pl -h (or without arguments => shows this screen)
41 \t./batchRebuildBiblioTables.pl -c (c like confirm => rebuild non marc DB (may be long)
42 \t-t => test only, change nothing in DB
43 EOF
45 exit;
48 my $dbh = C4::Context->dbh;
49 my $i=0;
50 my $starttime = time();
52 $|=1; # flushes output
53 $starttime = gettimeofday;
55 #1st of all, find item MARC tag.
56 my ($tagfield,$tagsubfield) = &GetMarcFromKohaField("items.itemnumber",'');
57 # $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");
58 my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
59 $sth->execute;
60 # my ($biblionumbermax) = $sth->fetchrow;
61 # warn "$biblionumbermax <<==";
62 my @errors;
63 while (my ($biblionumber)= $sth->fetchrow) {
64 #now, parse the record, extract the item fields, and store them in somewhere else.
65 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
66 if (not defined $record) {
67 push @errors, $biblionumber;
68 next;
70 my @fields = $record->field($tagfield);
71 my @items;
72 my $nbitems=0;
73 print ".";
74 my $timeneeded = gettimeofday - $starttime;
75 print "$i in $timeneeded s\n" unless ($i % 50);
76 $i++;
77 foreach my $field (@fields) {
78 my $item = MARC::Record->new();
79 $item->append_fields($field);
80 push @items,$item;
81 $record->delete_field($field);
82 $nbitems++;
84 # print "$biblionumber\n";
85 my $frameworkcode = GetFrameworkCode($biblionumber);
86 localNEWmodbiblio($dbh,$record,$biblionumber,$frameworkcode) unless $test_parameter;
88 # $dbh->do("unlock tables");
89 my $timeneeded = time() - $starttime;
90 print "$i MARC record done in $timeneeded seconds\n";
91 if (scalar(@errors) > 0) {
92 print "Some biblionumber could not be processed though: ", join(" ", @errors);
95 # modified NEWmodbiblio to jump the MARC part of the biblio modif
96 # highly faster
97 sub localNEWmodbiblio {
98 my ($dbh,$record,$biblionumber,$frameworkcode) =@_;
99 $frameworkcode="" unless $frameworkcode;
100 my $oldbiblio = TransformMarcToKoha($record,$frameworkcode);
101 C4::Biblio::_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
102 C4::Biblio::_koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
103 return 1;