3 # script to shift marc to biblioitems
4 # scraped from updatedatabase for dev week by chris@katipo.co.nz
7 use C4::AuthoritiesMarc;
9 use MARC::File::XML ( BinaryEncoding => 'utf8' );
12 print "moving MARC record to marc_header table\n";
14 my $dbh = C4::Context->dbh();
15 # changing marc field type
16 $dbh->do('ALTER TABLE auth_header CHANGE marc marc BLOB NULL DEFAULT NULL ');
18 # adding marc xml, just for convenience
20 'ALTER TABLE auth_header ADD marcxml LONGTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL '
23 $|=1; # flushes output
25 # moving data from marc_subfield_value to biblio
26 my $sth = $dbh->prepare('select authid,authtypecode from auth_header');
30 'update auth_header set marc=?,marcxml=? where authid=?');
32 while ( my ( $authid,$authtypecode ) = $sth->fetchrow ) {
33 # my $authtypecode = AUTHfind_authtypecode($dbh,$authid);
34 my $record = old_AUTHgetauthority( $dbh, $authid );
35 $record->leader(' nac 22 1u 4500');
38 $string = sprintf("%-*s",26, $string);
39 substr($string,9,6,"frey50");
40 unless ($record->subfield(100,"a")){
41 $record->insert_fields_ordered(MARC::Field->new(100,"","","a"=>$string));
43 if ($record->field(152)){
44 if ($record->subfield('152','b')){
46 $record->field('152')->add_subfields("b"=>$authtypecode);
49 $record->insert_fields_ordered(MARC::Field->new(152,"","","b"=>$authtypecode));
51 unless ($record->field('001')){
52 $record->insert_fields_ordered(MARC::Field->new('001',$authid));
56 #Force UTF-8 in record leaded
57 $record->encoding('UTF-8');
58 # warn "REC : ".$record->as_formatted;
59 $sth_update->execute( $record->as_usmarc(),$record->as_xml("UNIMARCAUTH"),
62 print "\r$totaldone" unless ( $totaldone % 100 );
67 # copying the 2.2 getauthority function, to retrieve authority correctly
68 # before moving it to marcxml field.
70 sub old_AUTHgetauthority {
71 # Returns MARC::Record of the biblio passed in parameter.
73 my $record = MARC::Record->new();
74 #---- TODO : the leader is missing
76 my $sth=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
77 from auth_subfield_table
78 where authid=? order by tag,tagorder,subfieldorder
80 $sth->execute($authid);
84 my $field; # for >=10 tags
85 my $prevvalue; # for <10 tags
86 while (my $row=$sth->fetchrow_hashref) {
87 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
90 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
92 $record->add_fields($field) unless $prevtag eq "XXX";
95 $prevtagorder=$row->{tagorder};
96 $prevtag = $row->{tag};
97 $previndicator=$row->{tag_indicator};
99 $prevvalue = $row->{subfieldvalue};
101 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
104 if ($row->{tag} <10) {
105 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
107 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
109 $prevtag= $row->{tag};
110 $previndicator=$row->{tag_indicator};
113 # the last has not been included inside the loop... do it now !
114 if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
115 # must return an empty record, not make MARC::Record fail because we try to
116 # create a record with XXX as field :-(
118 $record->add_fields($prevtag,$prevvalue);
120 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
121 $record->add_fields($field);