Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / updater / thesaurus_create.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 # This script generates and fill the thesaurus table
23 # with the data in bibliothesaurus
24
25 use C4::Context;
26 use C4::Catalogue;
27 use DBI;
28 use C4::Acquisitions;
29 use C4::Output;
30
31 my $dbh = C4::Context->dbh;
32
33 sub dosql {
34         my ($dbh,$sql_cmd)=@_;
35         my $sti=$dbh->prepare($sql_cmd);
36         $sti->execute;
37         if ($sti->err) {
38                 print "error : ".$sti->errstr." \n tried to execute : $sql_cmd\n";
39                 $sti->finish;
40         }
41 }
42
43 my $sth=$dbh->prepare("show tables");
44 $sth->execute;
45 my %tables;
46 while (my ($table) = $sth->fetchrow) {
47     $tables{$table}=1;
48 #    print "table $table\n";
49 }
50
51 print "creating thesaurus...\n";
52 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))");
53         my $sti=$dbh->prepare("select count(*) as tot from bibliosubject");
54         $sti->execute;
55         my $total = $sti->fetchrow_hashref;
56         # FIXME - There's already a $sti in this scope.
57         my $sti=$dbh->prepare("select subject from bibliosubject");
58         $sti->execute;
59         my $i;
60         while (my $line =$sti->fetchrow_hashref) {
61                 $i++;
62                 if ($i % 1000==0) {
63                         print "$i / $total->{'tot'}\n";
64                 }
65 #               print "$i $line->{'subject'}\n";
66                 my $sti2=$dbh->prepare("select count(*) as t from bibliothesaurus where freelib=".$dbh->quote($line->{'subject'}));
67                 $sti2->execute;
68                 if ($sti2->err) {
69                         print "error : ".$sti2->errstr."\n";
70                         die;
71                 }
72                 my $line2=$sti2->fetchrow_hashref;
73                 if ($line2->{'t'} ==0) {
74                         dosql($dbh,"insert into bibliothesaurus (freelib,stdlib) values (".$dbh->quote($line->{'subject'}).",".$dbh->quote($line->{'subject'}).")");
75 #               } else {
76 #                       print "pas ecriture pour : $line->{'subject'}\n";
77                 }
78
79         }
80