--- db/prgsrc/updateindex.pl 2000/10/18 18:48:58 1.1 +++ db/prgsrc/updateindex.pl 2000/10/19 01:06:18 1.3 @@ -16,6 +16,16 @@ Upadets metainformation in the B d An example of the index file follows: + Авторские вопросы + Виктор Байрак + bayrak.txt Вопросы В.Байрака + Борис Бурда + burda.txt Вопросы Бориса Бурды + burda1.txt Тренировки Бориса Бурды 1 + burda10.txt Тренировки Бориса Бурды 10 + burda11.txt Тренировки Бориса Бурды 11 + burda12.txt Тренировки Бориса Бурды 12 + =head1 BUGS @@ -29,47 +39,85 @@ createindex.pl(1) Boris Veytsman -=head1 $Id: updateindex.pl,v 1.1 2000/10/18 18:48:58 boris Exp $ +=head1 $Id: updateindex.pl,v 1.3 2000/10/19 01:06:18 boris Exp $ =cut use strict; -use vars qw($opt_m); +use vars qw($opt_i $opt_h); use Getopt::Std; use DBI; MAIN: { - getopts('m:') or die "Wrong options"; - my($source) = $opt_m || 'meta'; - my($depth, @depthId); - my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") - or die "Can't connect to DB chgk\n"; - - open INFD, $source or die "Can't open input file: $!\n"; - while () { - my $MetaId; - s/^\[(\d*)\]\t//; - $MetaId = $1; - s/^(\t*)//; - chomp; - $depth = length($1); - my $title = $dbh->quote($_); - my $ParentId = ($depth) ? $depthId[$depth - 1] : 0; - my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments WHERE - MetaId=$MetaId"); - $sth->execute; - if ($sth->fetchrow) { - print "$title is already in the DB!\n"; - next; - } - $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, ParentId, MetaId, Type) - VALUES ($title, $ParentId, $MetaId, 'Г')"); - $sth->execute; - my $Id = $sth->{'mysql_insertid'}; - $depthId[$depth] = $Id; + my $USAGE="Usage: updateindex.pl -i indexfile\n"; + getopts('hi:') or die $USAGE; + if ($opt_h) { + print $USAGE; + exit 0; + } + my($source) = $opt_i || 'index'; + my($depth, @depthId); + my $filename; + my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") + or die "Can't connect to DB chgk\n"; + + open INFD, $source or die "Can't open input file: $!\n"; + while () { + chomp; + s/ //; + next if (/^\s*$/); + if (s/^(\S+) *//) { # File found + $filename = $1; + $depth = -1; + } else { # Group found + $filename = ''; + $depth = -2; + } + s/^(\t*)//; + $depth += length($1); + if ($depth < 0) { + die "Wrong line $_\n"; } - $dbh->disconnect; + s/^\s*//; + s/\s$//; + my $title = $dbh->quote($_); + my $ParentId = ($depth) ? $depthId[$depth - 1] : 0; + my $sth; + my $type; + if ($filename) { + $type=$dbh->quote('Ч'); + $filename = $dbh->quote($filename); + $sth = $dbh->prepare("SELECT Id FROM Tournaments + WHERE FileName=$filename"); + $sth->execute; + if ($sth->fetchrow) { + print "$filename is already in the DB!\n"; + next; + } + $sth = $dbh->prepare("INSERT INTO Tournaments + (Title, ParentId, FileName, Type) + VALUES ($title, $ParentId, $filename, $type)"); + $sth->execute; + } else { + $type=$dbh->quote('Г'); + $sth = $dbh->prepare("SELECT Id FROM Tournaments + WHERE Title=$title"); + + $sth->execute; + if ($sth->fetchrow) { + print "$title is already in the DB!\n"; + next; + } + $sth = $dbh->prepare("INSERT INTO Tournaments + (Title, ParentId, Type) + VALUES ($title, $ParentId, $type)"); + $sth->execute; + my $Id = $sth->{'mysql_insertid'}; + $depthId[$depth] = $Id; + } + + } + $dbh->disconnect; }