3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
7 use MARC::File::XML ( BinaryEncoding => 'utf8' );
9 print "moving MARC record to biblioitems table\n";
11 my $dbh = C4::Context->dbh();
14 # moving MARC data from marc_subfield_table to biblioitems.marc
17 # changing marc field type
18 $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` LONGBLOB NULL DEFAULT NULL ');
19 # adding marc xml, just for convenience
20 $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
21 # moving data from marc_subfield_value to biblio
22 my $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
24 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
29 while (my ($bibid,$biblionumber) = $sth->fetchrow) {
30 my $record = LocalMARCgetbiblio($dbh,$bibid);
31 #Force UTF-8 in record leader
32 $record->encoding('UTF-8');
34 if (C4::Context->preference("marcflavour")=~/unimarc/i){
35 $marcflavour="UNIMARC";
37 $marcflavour="USMARC";
39 $sth_update->execute($record->as_usmarc(),$record->as_xml_record($marcflavour),$biblionumber);
42 print "\r$totaldone" unless ($totaldone % 100);
48 # this sub is a copy of Biblio.pm, version 2.2.4
49 # It is useful only once, for moving from 2.2 to 3.0
50 # the MARCgetbiblio in Biblio.pm
51 # is still here, but uses other tables
52 # (the ones that are filled by updatedatabase !)
55 sub LocalMARCgetbiblio {
57 # Returns MARC::Record of the biblio passed in parameter.
58 my ( $dbh, $bibid ) = @_;
59 my $record = MARC::Record->new();
64 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
65 from marc_subfield_table
66 where bibid=? order by tag,tagorder,subfieldorder
71 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
72 $sth->execute($bibid);
76 my $field; # for >=10 tags
77 my $prevvalue; # for <10 tags
78 while ( my $row = $sth->fetchrow_hashref ) {
80 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
81 $sth2->execute( $row->{'valuebloblink'} );
82 my $row2 = $sth2->fetchrow_hashref;
84 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
86 if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
87 $previndicator .= " ";
88 if ( $prevtag < 10 ) {
89 if ($prevtag ne '000') {
90 $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX"; # ignore the 1st loop
92 $record->leader(sprintf("%24s",$prevvalue));
96 $record->add_fields($field) unless $prevtag eq "XXX";
99 $prevtagorder = $row->{tagorder};
100 $prevtag = $row->{tag};
101 $previndicator = $row->{tag_indicator};
102 if ( $row->{tag} < 10 ) {
103 $prevvalue = $row->{subfieldvalue};
106 $field = MARC::Field->new(
107 ( sprintf "%03s", $prevtag ),
108 substr( $row->{tag_indicator} . ' ', 0, 1 ),
109 substr( $row->{tag_indicator} . ' ', 1, 1 ),
110 $row->{'subfieldcode'},
111 $row->{'subfieldvalue'}
116 if ( $row->{tag} < 10 ) {
117 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
118 $row->{'subfieldvalue'} );
121 $field->add_subfields( $row->{'subfieldcode'},
122 $row->{'subfieldvalue'} );
124 $prevtag = $row->{tag};
125 $previndicator = $row->{tag_indicator};
129 # the last has not been included inside the loop... do it now !
130 if ( $prevtag ne "XXX" )
131 { # check that we have found something. Otherwise, prevtag is still XXX and we
132 # must return an empty record, not make MARC::Record fail because we try to
133 # create a record with XXX as field :-(
134 if ( $prevtag < 10 ) {
135 $record->add_fields( $prevtag, $prevvalue );
139 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
140 $record->add_fields($field);
143 if (C4::Context->preference('marcflavour')=~/unimarc/i){
144 $record->leader(' nac 22 1u 4500');
146 if ($record->field(100)) {
147 $string = substr($record->subfield(100,"a")." ",0,35);
148 my $f100 = $record->field(100);
149 $record->delete_field($f100);
151 $string = POSIX::strftime("%Y%m%d", localtime);
153 $string = sprintf("%-*s",35, $string);
155 substr($string,22,6,"frey50");
156 unless ($record->subfield(100,"a")){
157 $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>"$string"));