installer: command-line scripts improve finding C4 modules
[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 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` BLOB 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     $sth_update->execute($record->as_usmarc(),$record->as_xml_record(),$biblionumber);
41     $totaldone++;
42     print ".";
43     print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
44 }
45 print "\rdone\n";
46
47
48 #
49 # this sub is a copy of Biblio.pm, version 2.2.4
50 # It is useful only once, for moving from 2.2 to 3.0
51 # the MARCgetbiblio in Biblio.pm
52 # is still here, but uses other tables
53 # (the ones that are filled by updatedatabase !)
54 #
55
56 sub LocalMARCgetbiblio {
57
58     # Returns MARC::Record of the biblio passed in parameter.
59     my ( $dbh, $bibid ) = @_;
60     my $record = MARC::Record->new();
61 #    warn "". $bidid;
62
63     my $sth =
64       $dbh->prepare(
65 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
66                   from marc_subfield_table
67                   where bibid=? order by tag,tagorder,subfieldorder
68               "
69     );
70     my $sth2 =
71       $dbh->prepare(
72         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
73     $sth->execute($bibid);
74     my $prevtagorder = 1;
75     my $prevtag      = 'XXX';
76     my $previndicator;
77     my $field;        # for >=10 tags
78     my $prevvalue;    # for <10 tags
79     while ( my $row = $sth->fetchrow_hashref ) {
80
81         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
82             $sth2->execute( $row->{'valuebloblink'} );
83             my $row2 = $sth2->fetchrow_hashref;
84             $sth2->finish;
85             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
86         }
87         if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
88             $previndicator .= "  ";
89             if ( $prevtag < 10 ) {
90                 if ($prevtag ne '000') {
91                     $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX";    # ignore the 1st loop
92                 } else {
93                     $record->leader(sprintf("%24s",$prevvalue));
94                 }
95             }
96             else {
97                 $record->add_fields($field) unless $prevtag eq "XXX";
98             }
99             undef $field;
100             $prevtagorder  = $row->{tagorder};
101             $prevtag       = $row->{tag};
102             $previndicator = $row->{tag_indicator};
103             if ( $row->{tag} < 10 ) {
104                 $prevvalue = $row->{subfieldvalue};
105             }
106             else {
107                 $field = MARC::Field->new(
108                     ( sprintf "%03s", $prevtag ),
109                     substr( $row->{tag_indicator} . '  ', 0, 1 ),
110                     substr( $row->{tag_indicator} . '  ', 1, 1 ),
111                     $row->{'subfieldcode'},
112                     $row->{'subfieldvalue'}
113                 );
114             }
115         }
116         else {
117             if ( $row->{tag} < 10 ) {
118                 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
119                     $row->{'subfieldvalue'} );
120             }
121             else {
122                 $field->add_subfields( $row->{'subfieldcode'},
123                     $row->{'subfieldvalue'} );
124             }
125             $prevtag       = $row->{tag};
126             $previndicator = $row->{tag_indicator};
127         }
128     }
129
130     # the last has not been included inside the loop... do it now !
131     if ( $prevtag ne "XXX" )
132     { # check that we have found something. Otherwise, prevtag is still XXX and we
133          # must return an empty record, not make MARC::Record fail because we try to
134          # create a record with XXX as field :-(
135         if ( $prevtag < 10 ) {
136             $record->add_fields( $prevtag, $prevvalue );
137         }
138         else {
139
140             #          my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
141             $record->add_fields($field);
142         }
143     }
144     return $record;
145 }