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) = &GetMarcFromKohaField("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 = GetMarcBiblio($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 = GetFrameworkCode($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 = TransformMarcToKoha($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 my $oldbiblio = TransformMarcToKoha($dbh,$record,$frameworkcode);
97 my ( $dbh, $record, $bibid, $itemnumber, $delete ) = @_;
98 # warn "NEWmoditem $bibid / $itemnumber / $delete ".$record->as_formatted;
99 my $frameworkcode=GetFrameworkCode($bibid);
100 my $olditem = TransformMarcToKoha( $dbh, $record,$frameworkcode );
101 C4::Biblio::_koha_modify_item( $dbh, $olditem );