2 # script that populates the authorities table with marc
3 # Written by TG on 10/04/2006
10 use MARC::File::USMARC;
12 use Time::HiRes qw(gettimeofday);
14 my $starttime = gettimeofday;
17 my $dbh = C4::Context->dbh;
18 my $sthcols=$dbh->prepare("show columns from auth_header");
21 while (( my $cols)=$sthcols->fetchrow){
25 ##Update the database if missing fields;
26 $dbh->do("LOCK TABLES auth_header WRITE auth_subfield_table READ");
27 unless ($columns{'linkid'}){
28 my $sth=$dbh->prepare("ALTER TABLE auth_header ADD COLUMN `linkid` BIGINT(20) UNSIGNED NOT NULL DEFAULT 0 ");
31 unless ($columns{'marc'}){
32 my $sth=$dbh->prepare("ALTER TABLE auth_header ADD COLUMN `marc` BLOB NOT NULL DEFAULT 0 ");
36 my $sth=$dbh->prepare("select authid,authtypecode from auth_header ");
40 my $sth2 = $dbh->prepare("UPDATE auth_header set marc=? where authid=?" );
43 while (my ($authid,$authtypecode)=$sth->fetchrow ){
44 my $record = AUTHgetauthority($dbh,$authid);
45 $sth2->execute($record->as_usmarc,$authid);
46 $timeneeded = gettimeofday - $starttime unless ($i % 1000);
47 print "$i in $timeneeded s\n" unless ($i % 1000);
48 print "." unless ($i % 500);
51 $dbh->do("UNLOCK TABLES ");
53 sub AUTHgetauthority {
54 # Returns MARC::Record of the biblio passed in parameter.
56 my $record = MARC::Record->new();
57 #---- TODO : the leader is missing
59 my $sth3=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
60 from auth_subfield_table
61 where authid=? order by tag,tagorder,subfieldorder
63 $sth3->execute($authid);
67 my $field; # for >=10 tags
68 my $prevvalue; # for <10 tags
69 while (my $row=$sth3->fetchrow_hashref) {
70 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
73 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
75 $record->add_fields($field) unless $prevtag eq "XXX";
78 $prevtagorder=$row->{tagorder};
79 $prevtag = $row->{tag};
80 $previndicator=$row->{tag_indicator};
82 $prevvalue = $row->{subfieldvalue};
84 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
87 if ($row->{tag} <10) {
88 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
90 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
92 $prevtag= $row->{tag};
93 $previndicator=$row->{tag_indicator};
96 # the last has not been included inside the loop... do it now !
97 if ($prevtag ne "XXX") { # check that we have found something. Otherwise, prevtag is still XXX and we
98 # must return an empty record, not make MARC::Record fail because we try to
99 # create a record with XXX as field :-(
101 $record->add_fields($prevtag,$prevvalue);
103 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
104 $record->add_fields($field);