re introducing TotalPaid function as circ/stat.pl use 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_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 ");
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 ###Chechk auth_subfield_structure as well. User may have forgotten to update database
37 my $sthcols=$dbh->prepare("show columns from auth_subfield_structure");
38 $sthcols->execute();
39 my %columns;
40 while (( my $cols)=$sthcols->fetchrow){
41 $columns{$cols}=1;
42 }
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 ");
46 $sth->execute();
47 }
48 unless ($columns{'isurl'}){
49 my $sth=$dbh->prepare("ALTER TABLE auth_subfield_structure ADD COLUMN `isurl` TINYINT(1) UNSIGNED NOT NULL DEFAULT 0 ");
50 $sth->execute();
51 }
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 ");
54 $sth->execute();
55 }
56 unless ($columns{'kohafield'}){
57 my $sth=$dbh->prepare("ALTER TABLE auth_subfield_structure  ADD COLUMN `kohafield` VARCHAR(45)  NOT NULL  ");
58 $sth->execute();
59 }
60 $dbh->do("UNLOCK TABLES ");
61 my $sth=$dbh->prepare("select authid,authtypecode from auth_header  ");
62         $sth->execute();
63  
64 my $i=0;
65 my $sth2 = $dbh->prepare("UPDATE auth_header  set marc=? where authid=?" );
66    
67
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("auth_header.authid",$authtypecode);
72 my ($authidfield,$authtypesubfield)=AUTHfind_marc_from_kohafield("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);
79         $i++;
80 }
81
82
83 sub AUTHgetauthorityold {
84 # Returns MARC::Record of the biblio passed in parameter.
85     my ($dbh,$authid)=@_;
86     my $record = MARC::Record->new();
87 #---- TODO : the leader is missing
88         $record->leader('                        ');
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
92                          ");
93         $sth3->execute($authid);
94         my $prevtagorder=1;
95         my $prevtag='XXX';
96         my $previndicator;
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) {
101                         $previndicator.="  ";
102                         if ($prevtag <10) {
103                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
104                         } else {
105                                 $record->add_fields($field) unless $prevtag eq "XXX";
106                         }
107                         undef $field;
108                         $prevtagorder=$row->{tagorder};
109                         $prevtag = $row->{tag};
110                         $previndicator=$row->{tag_indicator};
111                         if ($row->{tag}<10) {
112                                 $prevvalue = $row->{subfieldvalue};
113                         } else {
114                                 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.'  ',0,1), substr($row->{tag_indicator}.'  ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
115                         }
116                 } else {
117                         if ($row->{tag} <10) {
118                                 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
119                         } else {
120                                 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
121                         }
122                         $prevtag= $row->{tag};
123                         $previndicator=$row->{tag_indicator};
124                 }
125         }
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 :-(
130                 if ($prevtag <10) {
131                         $record->add_fields($prevtag,$prevvalue);
132                 } else {
133         #               my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
134                         $record->add_fields($field);
135                 }
136         }
137         return $record;
138 }
139
140 END;