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" };
13 use MARC::File::XML ( BinaryEncoding => 'utf8' );
15 print "moving MARC record to biblioitems table\n";
17 my $dbh = C4::Context->dbh();
20 # moving MARC data from marc_subfield_table to biblioitems.marc
23 # changing marc field type
24 $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` LONGBLOB NULL DEFAULT NULL ');
25 # adding marc xml, just for convenience
26 $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
27 # moving data from marc_subfield_value to biblio
28 my $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
30 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
35 while (my ($bibid,$biblionumber) = $sth->fetchrow) {
36 my $record = LocalMARCgetbiblio($dbh,$bibid);
37 #Force UTF-8 in record leader
38 $record->encoding('UTF-8');
40 if (C4::Context->preference("marcflavour")=~/unimarc/i){
41 $marcflavour="UNIMARC";
43 $marcflavour="USMARC";
45 $sth_update->execute($record->as_usmarc(),$record->as_xml_record($marcflavour),$biblionumber);
48 print "\r$totaldone" unless ($totaldone % 100);
54 # this sub is a copy of Biblio.pm, version 2.2.4
55 # It is useful only once, for moving from 2.2 to 3.0
56 # the MARCgetbiblio in Biblio.pm
57 # is still here, but uses other tables
58 # (the ones that are filled by updatedatabase !)
61 sub LocalMARCgetbiblio {
63 # Returns MARC::Record of the biblio passed in parameter.
64 my ( $dbh, $bibid ) = @_;
65 my $record = MARC::Record->new();
70 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
71 from marc_subfield_table
72 where bibid=? order by tag,tagorder,subfieldorder
77 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
78 $sth->execute($bibid);
82 my $field; # for >=10 tags
83 my $prevvalue; # for <10 tags
84 while ( my $row = $sth->fetchrow_hashref ) {
86 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
87 $sth2->execute( $row->{'valuebloblink'} );
88 my $row2 = $sth2->fetchrow_hashref;
90 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
92 if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
93 $previndicator .= " ";
94 if ( $prevtag < 10 ) {
95 if ($prevtag ne '000') {
96 $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX"; # ignore the 1st loop
98 $record->leader(sprintf("%24s",$prevvalue));
102 $record->add_fields($field) unless $prevtag eq "XXX";
105 $prevtagorder = $row->{tagorder};
106 $prevtag = $row->{tag};
107 $previndicator = $row->{tag_indicator};
108 if ( $row->{tag} < 10 ) {
109 $prevvalue = $row->{subfieldvalue};
112 $field = MARC::Field->new(
113 ( sprintf "%03s", $prevtag ),
114 substr( $row->{tag_indicator} . ' ', 0, 1 ),
115 substr( $row->{tag_indicator} . ' ', 1, 1 ),
116 $row->{'subfieldcode'},
117 $row->{'subfieldvalue'}
122 if ( $row->{tag} < 10 ) {
123 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
124 $row->{'subfieldvalue'} );
127 $field->add_subfields( $row->{'subfieldcode'},
128 $row->{'subfieldvalue'} );
130 $prevtag = $row->{tag};
131 $previndicator = $row->{tag_indicator};
135 # the last has not been included inside the loop... do it now !
136 if ( $prevtag ne "XXX" )
137 { # check that we have found something. Otherwise, prevtag is still XXX and we
138 # must return an empty record, not make MARC::Record fail because we try to
139 # create a record with XXX as field :-(
140 if ( $prevtag < 10 ) {
141 $record->add_fields( $prevtag, $prevvalue );
145 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
146 $record->add_fields($field);
149 if (C4::Context->preference('marcflavour')=~/unimarc/i){
150 $record->leader(' nac 22 1u 4500');
152 if ($record->field(100)) {
153 $string = substr($record->subfield(100,"a")." ",0,35);
154 my $f100 = $record->field(100);
155 $record->delete_field($f100);
157 $string = POSIX::strftime("%Y%m%d", localtime);
159 $string = sprintf("%-*s",35, $string);
161 substr($string,22,6,"frey50");
162 unless ($record->subfield(100,"a")){
163 $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>"$string"));