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_structure 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 ");
36 ###Chechk auth_subfield_structure as well. User may have forgotten to update database
37 my $sthcols=$dbh->prepare("show columns from auth_subfield_structure");
40 while (( my $cols)=$sthcols->fetchrow){
43 ##Update the database if missing fields;
44 unless ($columns{'link'}){
45 my $sth=$dbh->prepare("ALTER TABLE auth_subfield_structure ADD COLUMN `link` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 ");
48 unless ($columns{'isurl'}){
49 my $sth=$dbh->prepare("ALTER TABLE auth_subfield_structure ADD COLUMN `isurl` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 ");
52 unless ($columns{'hidden'}){
53 my $sth=$dbh->prepare("ALTER TABLE auth_subfield_structure ADD COLUMN `hidden` TINYINT(3) UNSIGNED NOT NULL ZEROFILL DEFAULT 000 ");
56 unless ($columns{'kohafield'}){
57 my $sth=$dbh->prepare("ALTER TABLE auth_subfield_structure ADD COLUMN `kohafield` VARCHAR(45) NOT NULL ");
60 $dbh->do("UNLOCK TABLES ");
61 my $sth=$dbh->prepare("select authid,authtypecode from auth_header ");
65 my $sth2 = $dbh->prepare("UPDATE auth_header set marc=? where authid=?" );
68 while (my ($authid,$authtypecode)=$sth->fetchrow ){
69 my $record = AUTHgetauthorityold($dbh,$authid);
70 ##Add authid and authtypecode to record. Old records did not have these fields
71 my ($authidfield,$authidsubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authid",$authtypecode);
72 my ($authidfield,$authtypesubfield)=AUTHfind_marc_from_kohafield($dbh,"auth_header.authtypecode",$authtypecode);
73 ##Both authid and authtypecode is expected to be in the same field. Modify if other requirements arise
74 $record->add_fields($authidfield,'','',$authidsubfield=>$authid,$authtypesubfield=>$authtypecode);
75 $sth2->execute($record->as_usmarc,$authid);
76 $timeneeded = gettimeofday - $starttime unless ($i % 1000);
77 print "$i in $timeneeded s\n" unless ($i % 1000);
78 print "." unless ($i % 500);
83 sub AUTHgetauthorityold {
84 # Returns MARC::Record of the biblio passed in parameter.
86 my $record = MARC::Record->new();
87 #---- TODO : the leader is missing
89 my $sth3=$dbh->prepare("select authid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue
90 from auth_subfield_table
91 where authid=? order by tag,tagorder,subfieldorder
93 $sth3->execute($authid);
97 my $field; # for >=10 tags
98 my $prevvalue; # for <10 tags
99 while (my $row=$sth3->fetchrow_hashref) {
100 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
103 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
105 $record->add_fields($field) unless $prevtag eq "XXX";
108 $prevtagorder=$row->{tagorder};
109 $prevtag = $row->{tag};
110 $previndicator=$row->{tag_indicator};
111 if ($row->{tag}<10) {
112 $prevvalue = $row->{subfieldvalue};
114 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
117 if ($row->{tag} <10) {
118 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
120 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
122 $prevtag= $row->{tag};
123 $previndicator=$row->{tag_indicator};
126 # the last has not been included inside the loop... do it now !
127 if ($prevtag ne "XXX") { # 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 :-(
131 $record->add_fields($prevtag,$prevvalue);
133 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
134 $record->add_fields($field);