3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
6 # find Koha's Perl modules
7 # test carefully before changing this
9 eval { require "$FindBin::Bin/../../kohalib.pl" };
14 use MARC::File::XML ( BinaryEncoding => 'utf8' );
16 print "moving MARC record to biblioitems table\n";
18 my $dbh = C4::Context->dbh();
21 # moving MARC data from marc_subfield_table to biblioitems.marc
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');
31 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
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);
43 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
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 !)
56 sub LocalMARCgetbiblio {
58 # Returns MARC::Record of the biblio passed in parameter.
59 my ( $dbh, $bibid ) = @_;
60 my $record = MARC::Record->new();
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
72 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
73 $sth->execute($bibid);
77 my $field; # for >=10 tags
78 my $prevvalue; # for <10 tags
79 while ( my $row = $sth->fetchrow_hashref ) {
81 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
82 $sth2->execute( $row->{'valuebloblink'} );
83 my $row2 = $sth2->fetchrow_hashref;
85 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
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
93 $record->leader(sprintf("%24s",$prevvalue));
97 $record->add_fields($field) unless $prevtag eq "XXX";
100 $prevtagorder = $row->{tagorder};
101 $prevtag = $row->{tag};
102 $previndicator = $row->{tag_indicator};
103 if ( $row->{tag} < 10 ) {
104 $prevvalue = $row->{subfieldvalue};
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'}
117 if ( $row->{tag} < 10 ) {
118 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
119 $row->{'subfieldvalue'} );
122 $field->add_subfields( $row->{'subfieldcode'},
123 $row->{'subfieldvalue'} );
125 $prevtag = $row->{tag};
126 $previndicator = $row->{tag_indicator};
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 );
140 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
141 $record->add_fields($field);