2 # small script that rebuilds the non-MARC DB
7 # use MARC::File::USMARC;
12 use Time::HiRes qw(gettimeofday);
15 my ( $input_marc_file, $number) = ('',0);
16 my ($version, $confirm,$test_parameter);
20 't' => \$test_parameter,
23 if ($version || (!$confirm)) {
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
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
38 my $dbh = C4::Context->dbh;
40 my $starttime = time();
42 $|=1; # flushes output
43 my $starttime = gettimeofday;
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");
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);
59 my $timeneeded = gettimeofday - $starttime;
60 print "$i in $timeneeded s\n" unless ($i % 50);
62 foreach my $field (@fields) {
63 my $item = MARC::Record->new();
64 $item->append_fields($field);
66 $record->delete_field($field);
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";
82 # $dbh->do("unlock tables");
83 my $timeneeded = time() - $starttime;
84 print "$i MARC record done in $timeneeded seconds\n";
86 # modified NEWmodbiblio to jump the MARC part of the biblio modif
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]);
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]);
113 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject",$frameworkcode);
114 my @subj = $record->field($tagfield);
116 foreach my $subject (@subj) {
117 my @subjsubfield = $subject->subfield($tagsubfield);
118 foreach my $subfieldcount (0..$#subjsubfield) {
119 push @subjects,$subjsubfield[$subfieldcount];
122 C4::Biblio::OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
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 );