New script:
[koha.git] / misc / rebuildthesaurus.pl
1 #!/usr/bin/perl
2 # script that rebuild thesaurus from biblio table.
3
4 use strict;
5
6 # Koha modules used
7 use MARC::File::USMARC;
8 use MARC::Record;
9 use MARC::Batch;
10 use C4::Context;
11 use C4::Biblio;
12 use C4::Authorities;
13 use Time::HiRes qw(gettimeofday);
14
15 use Getopt::Long;
16 my ( $input_marc_file, $number) = ('',0);
17 my ($version, $verbose, $test_parameter, $field,$delete,$category,$subfields);
18 GetOptions(
19     'h' => \$version,
20     'd' => \$delete,
21     't' => \$test_parameter,
22     's:s' => \$subfields,
23     'v' => \$verbose,
24     'c:s' => \$category,
25 );
26
27 if ($version || ($category eq '')) {
28     print <<EOF
29 small script to recreate a authority table into Koha.
30 parameters :
31 \th : this version/help screen
32 \tc : thesaurus category
33 \tv : verbose mode.
34 \tt : test mode : parses the file, saying what he would do, but doing nothing.
35 \ts : the subfields
36 \d : delete every entry of the selected category before doing work.
37
38 SAMPLES :
39  ./rebuildthesaurus.pl -c NP -s "##700#a, ##700#b (##700#c ; ##700#d)" => will build authority file NP with value constructed with 700 field \$a, \$b, \$c & \$d subfields In UNIMARC this rebuild author authority file.
40  ./rebuildthesaurus.pl -c EDITORS -s "##210#c -- ##225#a" => will build authority for editor and collection. The EDITORS authority category is used with plugins for 210 & 225 in UNIMARC.
41 EOF
42 ;#
43 die;
44 }
45
46 my $dbh = C4::Context->dbh;
47 my @subf = $subfields =~ /(##\d\d\d##.)/g;
48 if ($delete) {
49     print "deleting thesaurus\n";
50     my $sth = $dbh->prepare("delete from bibliothesaurus where category=?");
51     $sth->execute($category);
52 }
53 if ($test_parameter) {
54     print "TESTING MODE ONLY\n    DOING NOTHING\n===============\n";
55 }
56 $|=1; # flushes output
57
58 my $starttime = gettimeofday;
59 my $sth = $dbh->prepare("select bibid from marc_biblio");
60 $sth->execute;
61 my $i=1;
62 while (my ($bibid) = $sth->fetchrow) {
63     my $record = GetMarcBiblio($bibid);
64     print ".";
65     my $timeneeded = gettimeofday - $starttime;
66     print "$i in $timeneeded s\n" unless ($i % 50);
67
68 #    warn $record->as_formatted;
69     my $resultstring = $subfields;
70     foreach my $fieldwanted ($record->fields) {
71         next if $fieldwanted->tag()<=10;
72         foreach my $pair ( $fieldwanted->subfields() ) {
73             my $fieldvalue = $fieldwanted->tag();
74 #            warn "$fieldvalue ==> #$fieldvalue#$pair->[0]/$pair->[1]";
75             $resultstring =~ s/##$fieldvalue##$pair->[0]/$pair->[1]/g;
76         }
77     }
78         # deals empty subfields
79         foreach my $empty (@subf) {
80             $resultstring =~ s/$empty//g;
81         }
82         if ($resultstring ne $subfields && $resultstring) {
83             &newauthority($dbh,$category,$resultstring);
84         }
85         $i++;
86 }
87 my $timeneeded = gettimeofday - $starttime;
88 print "$i entries done in $timeneeded seconds (".($i/$timeneeded)." per second)\n";