Annotation of db/prgsrc/updateindex.pl, revision 1.3
1.1 boris 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:
1.3 ! boris 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:
1.1 boris 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:
1.3 ! boris 42: =head1 $Id: updateindex.pl,v 1.2 2000/10/18 21:50:48 boris Exp boris $
1.1 boris 43:
44: =cut
45:
46: use strict;
1.2 boris 47: use vars qw($opt_i $opt_h);
1.1 boris 48:
49: use Getopt::Std;
50: use DBI;
51:
52: MAIN:
53: {
1.2 boris 54: my $USAGE="Usage: updateindex.pl -i indexfile\n";
1.3 ! boris 55: getopts('hi:') or die $USAGE;
1.2 boris 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*$/);
1.3 ! boris 71: if (s/^(\S+) *//) { # File found
1.2 boris 72: $filename = $1;
73: $depth = -1;
74: } else { # Group found
1.3 ! boris 75: $filename = '';
1.2 boris 76: $depth = -2;
77: }
78: s/^(\t*)//;
79: $depth += length($1);
80: if ($depth < 0) {
81: die "Wrong line $_\n";
1.1 boris 82: }
1.2 boris 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;
1.3 ! boris 89: if ($filename) {
1.2 boris 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)
1.3 ! boris 101: VALUES ($title, $ParentId, $filename, $type)");
1.2 boris 102: $sth->execute;
103: } else {
1.3 ! boris 104: $type=$dbh->quote('Г');
1.2 boris 105: $sth = $dbh->prepare("SELECT Id FROM Tournaments
106: WHERE Title=$title");
1.3 ! boris 107:
! 108: $sth->execute;
1.2 boris 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)
1.3 ! boris 115: VALUES ($title, $ParentId, $type)");
1.2 boris 116: $sth->execute;
117: my $Id = $sth->{'mysql_insertid'};
118: $depthId[$depth] = $Id;
119: }
120:
121: }
122: $dbh->disconnect;
1.1 boris 123: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>