installer: command-line scripts improve finding C4 modules
[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 BEGIN {
15     # find Koha's Perl modules
16     # test carefully before changing this
17     use FindBin;
18     eval { require "$FindBin::Bin/kohalib.pl" };
19 }
20
21 use C4::Context;
22 use C4::Biblio;
23 use MARC::Record;
24 use MARC::File::USMARC;
25 use MARC::File::XML;
26
27 my $dbh=C4::Context->dbh;
28 use Time::HiRes qw(gettimeofday);
29
30 ##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
31 ##Adjust this mapping list to your own needs
32 my %mapping_list = (
33         '090cd' =>'09ocd',
34         '952abcdefpruvxyz'=>'95kkbcfazpw9d4ye',
35         );
36
37 my $starttime = gettimeofday;
38 my $sth=$dbh->prepare("SELECT biblionumber,marc FROM biblioitems ");
39 $sth->execute;
40
41 my $update=$dbh->prepare("update biblioitems set marc=?,marcxml=? where biblionumber=?");
42
43 my $b=0;
44 my $timeneeded;
45 while (my ($biblionumber, $marc) = $sth->fetchrow) {
46          
47 my $record=MARC::File::USMARC::decode($marc);
48
49 foreach my $key (keys %mapping_list){
50 my $tag=substr($key,0,3);
51 my $newtag=substr($mapping_list{$key},0,3);
52 my @subf;
53 my @newsub;
54         for (my $i=3; $i<length($key); $i++){
55         push @subf,substr($key,$i,1);
56         push @newsub,substr($mapping_list{$key},$i,1);
57         }##
58
59 foreach my $field ($record->field($tag)){
60 my $notnew=1;
61 my $addedfield;
62         for  (my $r=0; $r<@subf; $r++){
63                 if ($field->subfield($subf[$r]) && $notnew){
64                 $addedfield=MARC::Field->new($newtag,$field->indicator(1),$field->indicator(2),$newsub[$r]=>$field->subfield($subf[$r]));
65                 $notnew=0;
66                 }elsif ($field->subfield($subf[$r])){
67                 $addedfield->update($newsub[$r]=>$field->subfield($subf[$r]));
68                 }## a subfield exists
69         }## all subfields added
70 $record->delete_field($field);  
71 $record->add_fields($addedfield);
72 }##foreach field found  
73 ##Now update-db
74 $update->execute($record->as_usmarc,$record->as_xml_record,$biblionumber);
75         
76 }##foreach $key
77         $timeneeded = gettimeofday - $starttime unless ($b % 10000);
78         print "$b in $timeneeded s\n" unless ($b % 10000);
79         print "." unless ($b % 500);
80         $b++;
81 }##while biblionumber
82
83 ##Dont forget to export all new marc records and build your zebra db
84
85         
86 #       $timeneeded = gettimeofday - $starttime unless ($i % 30000);
87 #       print "$i in $timeneeded s\n" unless ($i % 30000);
88 #       print "." unless ($i % 500);
89 #       $i++;
90
91
92 $dbh->disconnect();