3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
9 use MARC::File::XML ( BinaryEncoding => 'utf8' );
11 print "moving MARC record to biblioitems table\n";
13 my $dbh = C4::Context->dbh();
16 # moving MARC data from marc_subfield_table to biblioitems.marc
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');
26 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
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);
38 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
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 !)
51 sub LocalMARCgetbiblio {
53 # Returns MARC::Record of the biblio passed in parameter.
54 my ( $dbh, $bibid ) = @_;
55 my $record = MARC::Record->new();
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
67 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
68 $sth->execute($bibid);
72 my $field; # for >=10 tags
73 my $prevvalue; # for <10 tags
74 while ( my $row = $sth->fetchrow_hashref ) {
76 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
77 $sth2->execute( $row->{'valuebloblink'} );
78 my $row2 = $sth2->fetchrow_hashref;
80 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
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
88 $record->leader(sprintf("%24s",$prevvalue));
92 $record->add_fields($field) unless $prevtag eq "XXX";
95 $prevtagorder = $row->{tagorder};
96 $prevtag = $row->{tag};
97 $previndicator = $row->{tag_indicator};
98 if ( $row->{tag} < 10 ) {
99 $prevvalue = $row->{subfieldvalue};
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'}
112 if ( $row->{tag} < 10 ) {
113 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
114 $row->{'subfieldvalue'} );
117 $field->add_subfields( $row->{'subfieldcode'},
118 $row->{'subfieldvalue'} );
120 $prevtag = $row->{tag};
121 $previndicator = $row->{tag_indicator};
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 );
135 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
136 $record->add_fields($field);