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 = MARCgetbiblio($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 # those 2 subs are a copy of Biblio.pm, version 2.2.4
45 # they are useful only once, for moving from 2.2 to 3.0
46 # the MARCgetbiblio & MARCgetitem subs in Biblio.pm
47 # are still here, but uses other tables
48 # (the ones that are filled by updatedatabase !)
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);
144 # Returns MARC::Record of the biblio passed in parameter.
145 my ( $dbh, $bibid, $itemnumber ) = @_;
146 my $record = MARC::Record->new();
148 # search MARC tagorder
151 "select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?"
153 $sth2->execute( $bibid, $itemnumber );
154 my ($tagorder) = $sth2->fetchrow_array();
156 #---- TODO : the leader is missing
159 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
160 from marc_subfield_table
161 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
166 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
167 $sth->execute( $bibid, $tagorder );
168 while ( my $row = $sth->fetchrow_hashref ) {
169 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
170 $sth2->execute( $row->{'valuebloblink'} );
171 my $row2 = $sth2->fetchrow_hashref;
173 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
175 if ( $record->field( $row->{'tag'} ) ) {
178 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
179 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
180 if ( length( $row->{'tag'} ) < 3 ) {
181 $row->{'tag'} = "0" . $row->{'tag'};
183 $field = $record->field( $row->{'tag'} );
186 $field->add_subfields( $row->{'subfieldcode'},
187 $row->{'subfieldvalue'} );
188 $record->delete_field($field);
189 $record->add_fields($field);
193 if ( length( $row->{'tag'} ) < 3 ) {
194 $row->{'tag'} = "0" . $row->{'tag'};
197 MARC::Field->new( $row->{'tag'}, " ", " ",
198 $row->{'subfieldcode'} => $row->{'subfieldvalue'} );
199 $record->add_fields($temp);