Bug 30430: UNIMARC XSLT add field B214 display
[koha.git] / misc / migration_tools / 22_to_30 / move_marc_to_biblioitems.pl
1 #!/usr/bin/perl
2 use Modern::Perl;
3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
5 use C4::Context;
6 use MARC::Record;
7 use MARC::File::XML ( BinaryEncoding => 'utf8' );
8
9 print "moving MARC record to biblioitems table\n";
10
11 my $dbh = C4::Context->dbh();
12
13 #
14 # moving MARC data from marc_subfield_table to biblioitems.marc
15 #
16
17 # changing marc field type
18 $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` LONGBLOB NULL DEFAULT NULL ');
19 # adding marc xml, just for convenience
20 $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
21 # moving data from marc_subfield_value to biblio
22 my $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
23 $sth->execute;
24 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
25 my $totaldone=0;
26
27 $|=1;
28
29 while (my ($bibid,$biblionumber) = $sth->fetchrow) {
30     my $record = LocalMARCgetbiblio($dbh,$bibid);
31     #Force UTF-8 in record leader
32     $record->encoding('UTF-8');
33     my $marcflavour;
34     if (C4::Context->preference("marcflavour")=~/unimarc/i){
35       $marcflavour="UNIMARC";
36     } else {
37      $marcflavour="USMARC";
38     }
39     $sth_update->execute($record->as_usmarc(),$record->as_xml_record($marcflavour),$biblionumber);
40     $totaldone++;
41     print ".";
42     print "\r$totaldone" unless ($totaldone % 100);
43 }
44 print "\rdone\n";
45
46
47 #
48 # this sub is a copy of Biblio.pm, version 2.2.4
49 # It is useful only once, for moving from 2.2 to 3.0
50 # the MARCgetbiblio in Biblio.pm
51 # is still here, but uses other tables
52 # (the ones that are filled by updatedatabase !)
53 #
54
55 sub LocalMARCgetbiblio {
56
57     # Returns MARC::Record of the biblio passed in parameter.
58     my ( $dbh, $bibid ) = @_;
59     my $record = MARC::Record->new();
60 #    warn "". $bidid;
61
62     my $sth =
63       $dbh->prepare(
64 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
65                   from marc_subfield_table
66                   where bibid=? order by tag,tagorder,subfieldorder
67               "
68     );
69     my $sth2 =
70       $dbh->prepare(
71         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
72     $sth->execute($bibid);
73     my $prevtagorder = 1;
74     my $prevtag      = 'XXX';
75     my $previndicator;
76     my $field;        # for >=10 tags
77     my $prevvalue;    # for <10 tags
78     while ( my $row = $sth->fetchrow_hashref ) {
79
80         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
81             $sth2->execute( $row->{'valuebloblink'} );
82             my $row2 = $sth2->fetchrow_hashref;
83             $sth2->finish;
84             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
85         }
86         if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
87             $previndicator .= "  ";
88             if ( $prevtag < 10 ) {
89                 if ($prevtag ne '000') {
90                     $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX";    # ignore the 1st loop
91                 } else {
92                     $record->leader(sprintf("%24s",$prevvalue));
93                 }
94             }
95             else {
96                 $record->add_fields($field) unless $prevtag eq "XXX";
97             }
98             undef $field;
99             $prevtagorder  = $row->{tagorder};
100             $prevtag       = $row->{tag};
101             $previndicator = $row->{tag_indicator};
102             if ( $row->{tag} < 10 ) {
103                 $prevvalue = $row->{subfieldvalue};
104             }
105             else {
106                 $field = MARC::Field->new(
107                     ( sprintf "%03s", $prevtag ),
108                     substr( $row->{tag_indicator} . '  ', 0, 1 ),
109                     substr( $row->{tag_indicator} . '  ', 1, 1 ),
110                     $row->{'subfieldcode'},
111                     $row->{'subfieldvalue'}
112                 );
113             }
114         }
115         else {
116             if ( $row->{tag} < 10 ) {
117                 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
118                     $row->{'subfieldvalue'} );
119             }
120             else {
121                 $field->add_subfields( $row->{'subfieldcode'},
122                     $row->{'subfieldvalue'} );
123             }
124             $prevtag       = $row->{tag};
125             $previndicator = $row->{tag_indicator};
126         }
127     }
128
129     # the last has not been included inside the loop... do it now !
130     if ( $prevtag ne "XXX" )
131     { # check that we have found something. Otherwise, prevtag is still XXX and we
132          # must return an empty record, not make MARC::Record fail because we try to
133          # create a record with XXX as field :-(
134         if ( $prevtag < 10 ) {
135             $record->add_fields( $prevtag, $prevvalue );
136         }
137         else {
138
139             #          my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
140             $record->add_fields($field);
141         }
142     }
143     if (C4::Context->preference('marcflavour')=~/unimarc/i){
144       $record->leader('     nac  22     1u 4500');
145       my $string;
146       if ($record->field(100)) {
147         $string = substr($record->subfield(100,"a")."                                   ",0,35);
148         my $f100 = $record->field(100);
149         $record->delete_field($f100);
150       } else {
151         $string = POSIX::strftime("%Y%m%d", localtime);
152         $string=~s/\-//g;
153         $string = sprintf("%-*s",35, $string);
154       }
155       substr($string,22,6,"frey50");
156       unless ($record->subfield(100,"a")){
157         $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>"$string"));
158       }
159     }
160
161     return $record;
162 }