1: #!/usr/local/bin/perl -w
2:
3: =head1 NAME
4:
5: updateindex.pl - a script for creation of new database.
6:
7: =head1 SYNOPSIS
8:
9: updateind.pl [B<-i> I<indexfile>]
10:
11:
12: =head1 DESCRIPTION
13:
14: Upadets metainformation in the B<chgk> databse. Uses file
15: L<./index> unless [B<-i>] option is used
16:
17: An example of the index file follows:
18:
19: Авторские вопросы
20: Виктор Байрак
21: bayrak.txt Вопросы В.Байрака
22: Борис Бурда
23: burda.txt Вопросы Бориса Бурды
24: burda1.txt Тренировки Бориса Бурды 1
25: burda10.txt Тренировки Бориса Бурды 10
26: burda11.txt Тренировки Бориса Бурды 11
27: burda12.txt Тренировки Бориса Бурды 12
28:
29:
30: =head1 BUGS
31:
32: The database, user and password are hardcoded.
33:
34: =head1 SEE ALSO
35:
36: createindex.pl(1)
37:
38: =head1 AUTHOR
39:
40: Boris Veytsman
41:
42: =head1 $Id: updateindex.pl,v 1.3 2000/10/19 01:06:18 boris Exp $
43:
44: =cut
45:
46: use strict;
47: use vars qw($opt_i $opt_h);
48:
49: use Getopt::Std;
50: use DBI;
51:
52: MAIN:
53: {
54: my $USAGE="Usage: updateindex.pl -i indexfile\n";
55: getopts('hi:') or die $USAGE;
56: if ($opt_h) {
57: print $USAGE;
58: exit 0;
59: }
60: my($source) = $opt_i || 'index';
61: my($depth, @depthId);
62: my $filename;
63: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
64: or die "Can't connect to DB chgk\n";
65:
66: open INFD, $source or die "Can't open input file: $!\n";
67: while (<INFD>) {
68: chomp;
69: s/
//;
70: next if (/^\s*$/);
71: if (s/^(\S+) *//) { # File found
72: $filename = $1;
73: $depth = -1;
74: } else { # Group found
75: $filename = '';
76: $depth = -2;
77: }
78: s/^(\t*)//;
79: $depth += length($1);
80: if ($depth < 0) {
81: die "Wrong line $_\n";
82: }
83: s/^\s*//;
84: s/\s$//;
85: my $title = $dbh->quote($_);
86: my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;
87: my $sth;
88: my $type;
89: if ($filename) {
90: $type=$dbh->quote('Ч');
91: $filename = $dbh->quote($filename);
92: $sth = $dbh->prepare("SELECT Id FROM Tournaments
93: WHERE FileName=$filename");
94: $sth->execute;
95: if ($sth->fetchrow) {
96: print "$filename is already in the DB!\n";
97: next;
98: }
99: $sth = $dbh->prepare("INSERT INTO Tournaments
100: (Title, ParentId, FileName, Type)
101: VALUES ($title, $ParentId, $filename, $type)");
102: $sth->execute;
103: } else {
104: $type=$dbh->quote('Г');
105: $sth = $dbh->prepare("SELECT Id FROM Tournaments
106: WHERE Title=$title");
107:
108: $sth->execute;
109: if ($sth->fetchrow) {
110: print "$title is already in the DB!\n";
111: next;
112: }
113: $sth = $dbh->prepare("INSERT INTO Tournaments
114: (Title, ParentId, Type)
115: VALUES ($title, $ParentId, $type)");
116: $sth->execute;
117: my $Id = $sth->{'mysql_insertid'};
118: $depthId[$depth] = $Id;
119: }
120:
121: }
122: $dbh->disconnect;
123: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>