More test files
[koha.git] / misc / rebuild_marc_newframework.pl
1 #!/usr/bin/perl 
2 #-----------------------------------
3 # Script Name: rebuild_marc_newframework.pl
4 # Script Version: 0.1.0
5 # Date:  20/04/2006
6 ##If you change your framework for marc mapping use this script to recreate marc records in your db
7 ## Then drop the old framework and install the new one.New frameworks are being introduced with Koha3.0
8 ##Re-export all your marc and recreate Zebra db.
9 ##Writen by Tumer Garip tgarip@neu.edu.tr
10
11
12
13 use strict;
14
15 use C4::Context;
16 use C4::Biblio;
17 use MARC::Record;
18 use MARC::File::USMARC;
19 use MARC::File::XML;
20
21 my $dbh=C4::Context->dbh;
22 use Time::HiRes qw(gettimeofday);
23
24 ##Write the corresponding new mappings below. this one maps old 090$c$d to new 09o$c$d and  952 holdings of NEU to 95k values
25 ##Adjust this mapping list to your own needs
26 my %mapping_list = (
27         '090cd' =>'09ocd',
28         '952abcdefpruvxyz'=>'95kkbcfazpw9d4ye',
29         );
30
31 my $starttime = gettimeofday;
32 my $sth=$dbh->prepare("SELECT biblionumber,marc FROM biblioitems ");
33 $sth->execute;
34
35 my $update=$dbh->prepare("update biblioitems set marc=?,marcxml=? where biblionumber=?");
36
37 my $b=0;
38 my $timeneeded;
39 while (my ($biblionumber, $marc) = $sth->fetchrow) {
40          
41 my $record=MARC::File::USMARC::decode($marc);
42
43 foreach my $key (keys %mapping_list){
44 my $tag=substr($key,0,3);
45 my $newtag=substr($mapping_list{$key},0,3);
46 my @subf;
47 my @newsub;
48         for (my $i=3; $i<length($key); $i++){
49         push @subf,substr($key,$i,1);
50         push @newsub,substr($mapping_list{$key},$i,1);
51         }##
52
53 foreach my $field ($record->field($tag)){
54 my $notnew=1;
55 my $addedfield;
56         for  (my $r=0; $r<@subf; $r++){
57                 if ($field->subfield($subf[$r]) && $notnew){
58                 $addedfield=MARC::Field->new($newtag,$field->indicator(1),$field->indicator(2),$newsub[$r]=>$field->subfield($subf[$r]));
59                 $notnew=0;
60                 }elsif ($field->subfield($subf[$r])){
61                 $addedfield->update($newsub[$r]=>$field->subfield($subf[$r]));
62                 }## a subfield exists
63         }## all subfields added
64 $record->delete_field($field);  
65 $record->add_fields($addedfield);
66 }##foreach field found  
67 ##Now update-db
68 $update->execute($record->as_usmarc,$record->as_xml_record,$biblionumber);
69         
70 }##foreach $key
71         $timeneeded = gettimeofday - $starttime unless ($b % 10000);
72         print "$b in $timeneeded s\n" unless ($b % 10000);
73         print "." unless ($b % 500);
74         $b++;
75 }##while biblionumber
76
77 ##Dont forget to export all new marc records and build your zebra db
78
79         
80 #       $timeneeded = gettimeofday - $starttime unless ($i % 30000);
81 #       print "$i in $timeneeded s\n" unless ($i % 30000);
82 #       print "." unless ($i % 500);
83 #       $i++;
84
85
86 $dbh->disconnect();