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