Bug 28617: Remove kohalib.pl and rely on PERL5LIB
[koha.git] / misc / migration_tools / 22_to_30 / move_marc_to_authheader.pl
1 #!/usr/bin/perl
2
3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
5 use strict;
6 #use warnings; FIXME - Bug 2505
7 use C4::Context;
8 use C4::AuthoritiesMarc;
9 use MARC::Record;
10 use MARC::File::XML ( BinaryEncoding => 'utf8' );
11
12 print "moving MARC record to marc_header table\n";
13
14 my $dbh = C4::Context->dbh();
15 # changing marc field type
16 $dbh->do('ALTER TABLE auth_header CHANGE marc marc BLOB NULL DEFAULT NULL ');
17
18 # adding marc xml, just for convenience
19 $dbh->do(
20 'ALTER TABLE auth_header ADD marcxml LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL '
21 );
22
23 $|=1; # flushes output
24
25 # moving data from marc_subfield_value to biblio
26 my $sth = $dbh->prepare('select authid,authtypecode from auth_header');
27 $sth->execute;
28 my $sth_update =
29   $dbh->prepare(
30     'update auth_header set marc=?,marcxml=? where authid=?');
31 my $totaldone = 0;
32 while ( my ( $authid,$authtypecode ) = $sth->fetchrow ) {
33 #     my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
34     my $record = old_AUTHgetauthority( $dbh, $authid );
35     $record->leader('     nac  22     1u 4500');
36     my $string;
37     $string=~s/\-//g;
38     $string = sprintf("%-*s",26, $string);
39     substr($string,9,6,"frey50");
40     unless ($record->subfield(100,"a") and length($record->subfield(100,"a")) == 26 ){
41       $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>$string));
42     }
43     if ($record->field(152)){
44       if ($record->subfield('152','b')){
45       } else {
46         $record->field('152')->add_subfields("b"=>$authtypecode);
47       }
48     } else {
49       $record->insert_fields_ordered(MARC::Field->new(152,"","","b"=>$authtypecode));
50     }
51     unless ($record->field('001')){
52       $record->insert_fields_ordered(MARC::Field->new('001',$authid));
53     }
54                                                                                                                                                                                 
55
56     #Force UTF-8 in record leaded
57     $record->encoding('UTF-8');
58 #     warn "REC : ".$record->as_formatted;
59     $sth_update->execute( $record->as_usmarc(),$record->as_xml("UNIMARCAUTH"),
60         $authid );
61     $totaldone++;
62     print "\r$totaldone" unless ( $totaldone % 100 );
63 }
64 print "\rdone\n";
65
66 #
67 # copying the 2.2 getauthority function, to retrieve authority correctly
68 # before moving it to marcxml field.
69 #
70 sub old_AUTHgetauthority {
71 # Returns MARC::Record of the biblio passed in parameter.
72     my ($dbh,$authid)=@_;
73     my $record = MARC::Record->new();
74 #---- TODO : the leader is missing
75         $record->leader('                        ');
76     my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
77                                  from auth_subfield_table
78                                  where authid=? order by tag,tagorder,subfieldorder
79                          ");
80         $sth->execute($authid);
81         my $prevtagorder=1;
82         my $prevtag='XXX';
83         my $previndicator;
84         my $field; # for >=10 tags
85         my $prevvalue; # for <10 tags
86         while (my $row=$sth->fetchrow_hashref) {
87                 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
88                         $previndicator.="  ";
89                         if ($prevtag <10) {
90                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
91                         } else {
92                                 $record->add_fields($field) unless $prevtag eq "XXX";
93                         }
94                         undef $field;
95                         $prevtagorder=$row->{tagorder};
96                         $prevtag = $row->{tag};
97                         $previndicator=$row->{tag_indicator};
98                         if ($row->{tag}<10) {
99                                 $prevvalue = $row->{subfieldvalue};
100                         } else {
101                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
102                         }
103                 } else {
104                         if ($row->{tag} <10) {
105                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
106                         } else {
107                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
108                         }
109                         $prevtag= $row->{tag};
110                         $previndicator=$row->{tag_indicator};
111                 }
112         }
113         # the last has not been included inside the loop... do it now !
114         if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
115                                                 # must return an empty record, not make MARC::Record fail because we try to
116                                                 # create a record with XXX as field :-(
117                 if ($prevtag <10) {
118                         $record->add_fields($prevtag,$prevvalue);
119                 } else {
120         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
121                         $record->add_fields($field);
122                 }
123         }
124         return $record;
125 }
126