removing some useless tables from updatedatabase
[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
29 my $dbh = C4::Context->dbh;
30
31 sub dosql {
32         my ($dbh,$sql_cmd)=@_;
33         my $sti=$dbh->prepare($sql_cmd);
34         $sti->execute;
35         if ($sti->err) {
36                 print "error : ".$sti->errstr." \n tried to execute : $sql_cmd\n";
37                 $sti->finish;
38         }
39 }
40
41 my $sth=$dbh->prepare("show tables");
42 $sth->execute;
43 my %tables;
44 while (my ($table) = $sth->fetchrow) {
45     $tables{$table}=1;
46 #    print "table $table\n";
47 }
48
49 #print "creating thesaurus...\n";
50 #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))");
51 $dbh->do("delete from bibliothesaurus");
52 my $sti=$dbh->prepare("select count(*) from bibliosubject");
53 $sti->execute;
54 my ($total) = $sti->fetchrow_array;
55 $sti=$dbh->prepare("select subject from bibliosubject");
56 $sti->execute;
57 my $i;
58 my $search_sth = $dbh->prepare("select id,level,hierarchy from bibliothesaurus where stdlib=?");
59 my $insert_sth = $dbh->prepare("insert into bibliothesaurus (freelib,stdlib,category,level,hierarchy) values (?,?,?,?,?)");
60 while (my $line =$sti->fetchrow_hashref) {
61         $i++;
62         if ($i % 1000==0) {
63                 print "$i / $total\n";
64         }
65         my @hierarchy = split / - /,$line->{'subject'};
66         my $rebuild = "";
67         my $top_hierarchy = "";
68         #---- if not a main authority field, search where to link
69         for (my $hier=0; $hier<$#hierarchy+1 ; $hier++) {
70                 $rebuild .=$hierarchy[$hier];
71                 $search_sth->execute($rebuild);
72                 my ($id,$level,$hierarchy) = $search_sth->fetchrow_array;
73 #               warn "/($line->{'subject'}) : $rebuild/";
74 # if father not found, create father and son
75                 if (!$id) {
76                         $insert_sth->execute($rebuild,$rebuild,"",$hier,"$top_hierarchy");
77                         # search again, to find $id and buiild $top_hierarchy
78                         $search_sth->execute($rebuild);
79                         my ($id,$level,$hierarchy) = $search_sth->fetchrow_array;
80                         $top_hierarchy .="|" if ($top_hierarchy);
81                         $top_hierarchy .= "$id";
82 # else create only son
83                 } else {
84                         $top_hierarchy .="|" if ($top_hierarchy);
85                         $top_hierarchy .= "$id";
86 #                       $insert_sth->execute($rebuild,$rebuild,"",$hier,"$top_hierarchy");
87                 }
88                 $rebuild .=" - ";
89         }
90 #       my $sti2=$dbh->prepare("select count(*) as t from bibliothesaurus where freelib=".$dbh->quote($line->{'subject'}));
91 #       $sti2->execute;
92 #       if ($sti2->err) {
93 #               print "error : ".$sti2->errstr."\n";
94 #               die;
95 #       }
96 #       my $line2=$sti2->fetchrow_hashref;
97 #       if ($line2->{'t'} ==0) {
98 #               dosql($dbh,"insert into bibliothesaurus (freelib,stdlib,category) values (".$dbh->quote($line->{'subject'}).",".$dbh->quote($line->{'subject'}).")");
99 #       }
100 }