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