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