Making sure fix makes it into the main branch as well
[koha.git] / updater / thesaurus_create.pl
1 #!/usr/bin/perl
2
3 use strict;
4 # This script generates and fill the thesaurus table
5 # with the data in bibliothesaurus
6
7 use C4::Database;
8 use C4::Catalogue;
9 use DBI;
10 use C4::Acquisitions;
11 use C4::Output;
12
13 my $dbh=C4Connect;
14
15 sub dosql {
16         my ($dbh,$sql_cmd)=@_;
17         my $sti=$dbh->prepare($sql_cmd);
18         $sti->execute;
19         if ($sti->err) {
20                 print "error : ".$sti->errstr." \n tried to execute : $sql_cmd\n";
21                 $sti->finish;
22         }
23 }
24
25 my $sth=$dbh->prepare("show tables");
26 $sth->execute;
27 my %tables;
28 while (my ($table) = $sth->fetchrow) {
29     $tables{$table}=1;
30 #    print "table $table\n";
31 }
32
33 print "creating thesaurus...\n";
34 dosql($dbh,"CREATE TABLE bibliothesaurus (code BIGINT not null AUTO_INCREMENT, freelib CHAR (255) not null , stdlib CHAR (255) not null , type CHAR (80) not null , PRIMARY KEY (code), INDEX (freelib),index(stdlib),index(type))");
35         my $sti=$dbh->prepare("select count(*) as tot from bibliosubject");
36         $sti->execute;
37         my $total = $sti->fetchrow_hashref;
38         my $sti=$dbh->prepare("select subject from bibliosubject");
39         $sti->execute;
40         my $i;
41         while (my $line =$sti->fetchrow_hashref) {
42                 $i++;
43                 if ($i % 1000==0) {
44                         print "$i / $total->{'tot'}\n";
45                 }
46 #               print "$i $line->{'subject'}\n";
47                 my $sti2=$dbh->prepare("select count(*) as t from bibliothesaurus where freelib=".$dbh->quote($line->{'subject'}));
48                 $sti2->execute;
49                 if ($sti2->err) {
50                         print "error : ".$sti2->errstr."\n";
51                         die;
52                 }
53                 my $line2=$sti2->fetchrow_hashref;
54                 if ($line2->{'t'} ==0) {
55                         dosql($dbh,"insert into bibliothesaurus (freelib,stdlib) values (".$dbh->quote($line->{'subject'}).",".$dbh->quote($line->{'subject'}).")");
56 #               } else {
57 #                       print "pas ecriture pour : $line->{'subject'}\n";
58                 }
59
60         }
61