Added copyright statement to all .pl and .pm files
[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::Database;
26 use C4::Catalogue;
27 use DBI;
28 use C4::Acquisitions;
29 use C4::Output;
30
31 my $dbh=C4Connect;
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         my $sti=$dbh->prepare("select subject from bibliosubject");
57         $sti->execute;
58         my $i;
59         while (my $line =$sti->fetchrow_hashref) {
60                 $i++;
61                 if ($i % 1000==0) {
62                         print "$i / $total->{'tot'}\n";
63                 }
64 #               print "$i $line->{'subject'}\n";
65                 my $sti2=$dbh->prepare("select count(*) as t from bibliothesaurus where freelib=".$dbh->quote($line->{'subject'}));
66                 $sti2->execute;
67                 if ($sti2->err) {
68                         print "error : ".$sti2->errstr."\n";
69                         die;
70                 }
71                 my $line2=$sti2->fetchrow_hashref;
72                 if ($line2->{'t'} ==0) {
73                         dosql($dbh,"insert into bibliothesaurus (freelib,stdlib) values (".$dbh->quote($line->{'subject'}).",".$dbh->quote($line->{'subject'}).")");
74 #               } else {
75 #                       print "pas ecriture pour : $line->{'subject'}\n";
76                 }
77
78         }
79