e900d02daab9371b8e6c0088e0e9a61d8fc03acc
[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 Time::HiRes qw(gettimeofday);
13 my $timeneeded;
14 my $starttime = gettimeofday;
15
16
17 my $dbh = C4::Context->dbh;
18 my $sthcols=$dbh->prepare("show columns from auth_header");
19 $sthcols->execute();
20 my %columns;
21 while (( my $cols)=$sthcols->fetchrow){
22 $columns{$cols}=1;
23 }
24
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 ");
29 $sth->execute();
30 }
31 unless ($columns{'marc'}){
32 my $sth=$dbh->prepare("ALTER TABLE auth_header  ADD COLUMN `marc` BLOB  NOT NULL DEFAULT 0 ");
33 $sth->execute();
34 }
35
36 my $sth=$dbh->prepare("select authid,authtypecode from auth_header  ");
37         $sth->execute();
38  
39 my $i=0;
40 my $sth2 = $dbh->prepare("UPDATE auth_header  set marc=? where authid=?" );
41    
42
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);
49         $i++;
50 }
51 $dbh->do("UNLOCK TABLES ");
52
53 sub AUTHgetauthority {
54 # Returns MARC::Record of the biblio passed in parameter.
55     my ($dbh,$authid)=@_;
56     my $record = MARC::Record->new();
57 #---- TODO : the leader is missing
58         $record->leader('                        ');
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
62                          ");
63         $sth3->execute($authid);
64         my $prevtagorder=1;
65         my $prevtag='XXX';
66         my $previndicator;
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) {
71                         $previndicator.="  ";
72                         if ($prevtag <10) {
73                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
74                         } else {
75                                 $record->add_fields($field) unless $prevtag eq "XXX";
76                         }
77                         undef $field;
78                         $prevtagorder=$row->{tagorder};
79                         $prevtag = $row->{tag};
80                         $previndicator=$row->{tag_indicator};
81                         if ($row->{tag}<10) {
82                                 $prevvalue = $row->{subfieldvalue};
83                         } else {
84                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
85                         }
86                 } else {
87                         if ($row->{tag} <10) {
88                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
89                         } else {
90                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
91                         }
92                         $prevtag= $row->{tag};
93                         $previndicator=$row->{tag_indicator};
94                 }
95         }
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 :-(
100                 if ($prevtag <10) {
101                         $record->add_fields($prevtag,$prevvalue);
102                 } else {
103         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
104                         $record->add_fields($field);
105                 }
106         }
107         return $record;
108 }
109
110 END;