Old authorities did not contain authid in marc record. Add it
[koha.git] / misc / marc_into_authority.pl
1 #!/usr/bin/perl
2 # script that populates the authorities table with marc  
3 #  Written by TG on 10/04/2006
4 use strict;
5
6 # Koha modules used
7
8 use C4::Context;
9 use MARC::Record;
10 use MARC::File::USMARC;
11 use MARC::File::XML;
12 use C4::AuthoritiesMarc;
13 use Time::HiRes qw(gettimeofday);
14 my $timeneeded;
15 my $starttime = gettimeofday;
16
17
18 my $dbh = C4::Context->dbh;
19 my $sthcols=$dbh->prepare("show columns from auth_header");
20 $sthcols->execute();
21 my %columns;
22 while (( my $cols)=$sthcols->fetchrow){
23 $columns{$cols}=1;
24 }
25
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 ");
30 $sth->execute();
31 }
32 unless ($columns{'marc'}){
33 my $sth=$dbh->prepare("ALTER TABLE auth_header  ADD COLUMN `marc` BLOB  NOT NULL DEFAULT 0 ");
34 $sth->execute();
35 }
36
37 my $sth=$dbh->prepare("select authid,authtypecode from auth_header  ");
38         $sth->execute();
39  
40 my $i=0;
41 my $sth2 = $dbh->prepare("UPDATE auth_header  set marc=? where authid=?" );
42    
43
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);
55         $i++;
56 }
57 $dbh->do("UNLOCK TABLES ");
58
59 sub AUTHgetauthorityold {
60 # Returns MARC::Record of the biblio passed in parameter.
61     my ($dbh,$authid)=@_;
62     my $record = MARC::Record->new();
63 #---- TODO : the leader is missing
64         $record->leader('                        ');
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
68                          ");
69         $sth3->execute($authid);
70         my $prevtagorder=1;
71         my $prevtag='XXX';
72         my $previndicator;
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) {
77                         $previndicator.="  ";
78                         if ($prevtag <10) {
79                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
80                         } else {
81                                 $record->add_fields($field) unless $prevtag eq "XXX";
82                         }
83                         undef $field;
84                         $prevtagorder=$row->{tagorder};
85                         $prevtag = $row->{tag};
86                         $previndicator=$row->{tag_indicator};
87                         if ($row->{tag}<10) {
88                                 $prevvalue = $row->{subfieldvalue};
89                         } else {
90                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
91                         }
92                 } else {
93                         if ($row->{tag} <10) {
94                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
95                         } else {
96                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
97                         }
98                         $prevtag= $row->{tag};
99                         $previndicator=$row->{tag_indicator};
100                 }
101         }
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 :-(
106                 if ($prevtag <10) {
107                         $record->add_fields($prevtag,$prevvalue);
108                 } else {
109         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
110                         $record->add_fields($field);
111                 }
112         }
113         return $record;
114 }
115
116 END;