fixing typo error
[koha.git] / misc / rebuildnonmarc.pl
1 #!/usr/bin/perl
2 # small script that rebuilds the non-MARC DB
3
4 use strict;
5
6 # Koha modules used
7 # use MARC::File::USMARC;
8 use MARC::Record;
9 use MARC::Batch;
10 use C4::Context;
11 use C4::Biblio;
12 use Time::HiRes qw(gettimeofday);
13
14 use Getopt::Long;
15 my ( $input_marc_file, $number) = ('',0);
16 my ($version, $confirm,$test_parameter);
17 GetOptions(
18         'c' => \$confirm,
19         'h' => \$version,
20         't' => \$test_parameter,
21 );
22
23 if ($version || (!$confirm)) {
24         print <<EOF
25 This script rebuilds the non-MARC DB from the MARC values.
26 You can/must use it when you change your mapping.
27 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
28 results in OPAC !
29 syntax :
30 \t./rebuildnonmarc.pl -h (or without arguments => shows this screen)
31 \t./rebuildnonmarc.pl -c (c like confirm => rebuild non marc DB (may be long)
32 \t-t => test only, change nothing in DB
33 EOF
34 ;
35 die;
36 }
37
38 my $dbh = C4::Context->dbh;
39 my $i=0;
40 my $starttime = time();
41
42 $|=1; # flushes output
43 my $starttime = gettimeofday;
44
45 #1st of all, find item MARC tag.
46 my ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,"items.itemnumber",'');
47 # $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");
48 my $sth = $dbh->prepare("select bibid from marc_biblio");
49 $sth->execute;
50 # my ($bibidmax) =  $sth->fetchrow;
51 # warn "$bibidmax <<==";
52 while (my ($bibid)= $sth->fetchrow) {
53         #now, parse the record, extract the item fields, and store them in somewhere else.
54         my $record = MARCgetbiblio($dbh,$bibid);
55         my @fields = $record->field($tagfield);
56         my @items;
57         my $nbitems=0;
58         print ".";
59         my $timeneeded = gettimeofday - $starttime;
60         print "$i in $timeneeded s\n" unless ($i % 50);
61         $i++;
62         foreach my $field (@fields) {
63                 my $item = MARC::Record->new();
64                 $item->append_fields($field);
65                 push @items,$item;
66                 $record->delete_field($field);
67                 $nbitems++;
68         }
69 #       print "$bibid\n";
70         # now, create biblio and items with NEWnewXX call.
71         my $frameworkcode = MARCfind_frameworkcode($dbh,$bibid);
72         localNEWmodbiblio($dbh,$record,$bibid,$frameworkcode) unless $test_parameter;
73 #       warn 'B=>'.$record->as_formatted;
74 #       print "biblio done\n";
75         for (my $i=0;$i<=$#items;$i++) {
76                 my $tmp = MARCmarc2koha($dbh,$items[$i],$frameworkcode) unless $test_parameter; # finds the itemnumber
77 #               warn "    I=> ".$items[$i]->as_formatted;
78                 localNEWmoditem($dbh,$items[$i],$bibid,$tmp->{itemnumber},0) unless $test_parameter;
79 #               print "1 item done\n";
80         }
81 }
82 # $dbh->do("unlock tables");
83 my $timeneeded = time() - $starttime;
84 print "$i MARC record done in $timeneeded seconds\n";
85
86 # modified NEWmodbiblio to jump the MARC part of the biblio modif
87 # highly faster
88 sub localNEWmodbiblio {
89         my ($dbh,$record,$bibid,$frameworkcode) =@_;
90         $frameworkcode="" unless $frameworkcode;
91 #       &MARCmodbiblio($dbh,$bibid,$record,$frameworkcode,0);
92         my $oldbiblio = MARCmarc2koha($dbh,$record,$frameworkcode);
93         my $oldbiblionumber = C4::Biblio::OLDmodbiblio($dbh,$oldbiblio);
94         C4::Biblio::OLDmodbibitem($dbh,$oldbiblio);
95         # now, modify addi authors, subject, addititles.
96         my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author",$frameworkcode);
97         my @addiauthfields = $record->field($tagfield);
98         $dbh->do("delete from bibliosubtitle where biblionumber=$oldbiblionumber");
99         foreach my $addiauthfield (@addiauthfields) {
100                 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
101                 foreach my $subfieldcount (0..$#addiauthsubfields) {
102                         C4::Biblio::OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
103                 }
104         }
105         ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle",$frameworkcode);
106         my @subtitlefields = $record->field($tagfield);
107         foreach my $subtitlefield (@subtitlefields) {
108                 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
109                 foreach my $subfieldcount (0..$#subtitlesubfields) {
110                         C4::Biblio::OLDnewsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
111                 }
112         }
113         ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject",$frameworkcode);
114         my @subj = $record->field($tagfield);
115         my @subjects;
116         foreach my $subject (@subj) {
117                 my @subjsubfield = $subject->subfield($tagsubfield);
118                 foreach my $subfieldcount (0..$#subjsubfield) {
119                         push @subjects,$subjsubfield[$subfieldcount];
120                 }
121         }
122         C4::Biblio::OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
123         return 1;
124 }
125
126 sub localNEWmoditem {
127     my ( $dbh, $record, $bibid, $itemnumber, $delete ) = @_;
128 #       warn "NEWmoditem $bibid / $itemnumber / $delete ".$record->as_formatted;
129 #     &MARCmoditem( $dbh, $record, $bibid, $itemnumber, $delete );
130         my $frameworkcode=MARCfind_frameworkcode($dbh,$bibid);
131     my $olditem = MARCmarc2koha( $dbh, $record,$frameworkcode );
132     C4::Biblio::OLDmoditem( $dbh, $olditem );
133 }