2 # script that populates the authorities table with marc
3 # Written by TG on 10/04/2006
10 use MARC::File::USMARC;
12 use C4::AuthoritiesMarc;
13 use Time::HiRes qw(gettimeofday);
15 my $starttime = gettimeofday;
18 my $dbh = C4::Context->dbh;
19 my $sthcols=$dbh->prepare("show columns from auth_header");
22 while (( my $cols)=$sthcols->fetchrow){
26 ##Update the database if missing fields;
27 $dbh->do("LOCK TABLES auth_header WRITE auth_subfield_table READ");
28 unless ($columns{'linkid'}){
29 my $sth=$dbh->prepare("ALTER TABLE auth_header ADD COLUMN `linkid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 ");
32 unless ($columns{'marc'}){
33 my $sth=$dbh->prepare("ALTER TABLE auth_header ADD COLUMN `marc` BLOB NOT NULL DEFAULT 0 ");
37 my $sth=$dbh->prepare("select authid,authtypecode from auth_header ");
41 my $sth2 = $dbh->prepare("UPDATE auth_header set marc=? where authid=?" );
44 while (my ($authid,$authtypecode)=$sth->fetchrow ){
45 my $record = AUTHgetauthorityold($dbh,$authid);
46 ##Add authid and authtypecode to record. Old records did not have these fields
47 my ($authidfield,$authidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
48 my ($authidfield,$authtypesubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authtypecode",$authtypecode);
49 ##Both authid and authtypecode is expected to be in the same field. Modify if other requirements arise
50 $record->add_fields($authfield,'','',$authidsubfield=>$authid,$authtypesubfield=>$authtypecode);
51 $sth2->execute($record->as_usmarc,$authid);
52 $timeneeded = gettimeofday - $starttime unless ($i % 1000);
53 print "$i in $timeneeded s\n" unless ($i % 1000);
54 print "." unless ($i % 500);
57 $dbh->do("UNLOCK TABLES ");
59 sub AUTHgetauthorityold {
60 # Returns MARC::Record of the biblio passed in parameter.
62 my $record = MARC::Record->new();
63 #---- TODO : the leader is missing
65 my $sth3=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
66 from auth_subfield_table
67 where authid=? order by tag,tagorder,subfieldorder
69 $sth3->execute($authid);
73 my $field; # for >=10 tags
74 my $prevvalue; # for <10 tags
75 while (my $row=$sth3->fetchrow_hashref) {
76 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
79 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
81 $record->add_fields($field) unless $prevtag eq "XXX";
84 $prevtagorder=$row->{tagorder};
85 $prevtag = $row->{tag};
86 $previndicator=$row->{tag_indicator};
88 $prevvalue = $row->{subfieldvalue};
90 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
93 if ($row->{tag} <10) {
94 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
96 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
98 $prevtag= $row->{tag};
99 $previndicator=$row->{tag_indicator};
102 # the last has not been included inside the loop... do it now !
103 if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
104 # must return an empty record, not make MARC::Record fail because we try to
105 # create a record with XXX as field :-(
107 $record->add_fields($prevtag,$prevvalue);
109 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
110 $record->add_fields($field);