version 1.1, 2005/03/07 02:37:51
|
version 1.2, 2005/03/07 02:52:12
|
Line 2
|
Line 2
|
|
|
=head1 NAME |
=head1 NAME |
|
|
updatedb.pl - a script for creation of new database. |
validate.pl - a script to validate files for db |
|
|
=head1 SYNOPSIS |
=head1 SYNOPSIS |
|
|
updatedb.pl B<[-i]> I<file1> I<file2>.... |
validate.pl I<file1> I<file2>.... |
|
|
|
|
=head1 DESCRIPTION |
=head1 DESCRIPTION |
|
|
Updates information in the B<chgk> databse. Uses file |
Validates files |
|
|
=head1 OPTIONS |
=head1 OPTIONS |
|
|
=item B<-i> |
=item B<-i> |
|
|
Ask about ParentId. |
|
|
|
|
|
=head1 BUGS |
|
|
|
The database, user and password are hardcoded. |
|
|
|
=head1 AUTHOR |
=head1 AUTHOR |
|
|
Dmitry Rubinstein |
Boris Veytsman, based on a script by Dmitry Rubinstein |
|
|
=head1 $Id$ |
=head1 $Id$ |
|
|
=cut |
=cut |
|
|
use vars qw($opt_i); |
|
|
|
use Getopt::Std; |
|
my $unsortedname="../dump/unsorted"; |
|
getopts('i'); |
|
#open STDERR, ">errors"; |
|
my $Interactive=$opt_i || 0; |
|
my $DUMPDIR = $ENV{DUMPDIR} || "../dump"; |
|
|
|
my (%RevMonths) = |
my (%RevMonths) = |
('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6', |
('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6', |
Line 57 my ($sth);
|
Line 42 my ($sth);
|
|
|
|
|
|
|
use DBI; |
|
use strict; |
use strict; |
my $isunsorted=0; |
|
sub UpdateParents { |
|
my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_; |
|
if ($ParentId) { |
|
my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId, CreatedAt |
|
FROM Tournaments WHERE Id = $ParentId"); |
|
$sth1->execute; |
|
my ($q, $p,$c) = ($sth1->fetchrow)[0, 1, 2]; |
|
$c=$CreatedAt if $CreatedAt && ($CreatedAt gt $c); |
|
my $qc=$dbh->quote($c); |
|
$dbh->do("UPDATE Tournaments SET |
|
QuestionsNum=$q + $all_qnum, CreatedAt=$qc |
|
WHERE Id = $ParentId"); |
|
&UpdateParents($dbh, $p, $all_qnum,$c); |
|
} |
|
} |
|
|
|
|
|
sub getField { |
sub getField { |
my($desc, $dbh) = @_; |
my($desc) = @_; |
my($key); |
my($key); |
my($value) = (''); |
my($value) = (''); |
while (<$desc>) { |
while (<$desc>) { |
Line 94 sub getField {
|
Line 62 sub getField {
|
$value =~ s/-00*$/-01/; |
$value =~ s/-00*$/-01/; |
} |
} |
if ($key eq 'Автор') {$value=~s/\.$//;} |
if ($key eq 'Автор') {$value=~s/\.$//;} |
$value = $dbh->quote($value); |
|
return ($key, $value); |
return ($key, $value); |
} |
} |
next if (/^\s*$/); |
next if (/^\s*$/); |
Line 111 sub getField {
|
Line 78 sub getField {
|
} |
} |
if ($key && $value) { |
if ($key && $value) { |
$value=~s/\s+$//; |
$value=~s/\s+$//; |
$value = $dbh->quote($value); |
|
return ($key, $value); |
return ($key, $value); |
} |
} |
return (0, 0); |
return (0, 0); |
} |
} |
|
|
sub SelectGroup { |
|
my ($dbh, $source, $TourName) = @_; |
|
my ($sth, $ParentId, $i, @arr); |
|
|
|
if ($Interactive) { |
|
$sth = $dbh->prepare("SELECT Id, Title FROM |
|
Tournaments WHERE Type = 'Г'"); |
|
$sth->execute; |
|
print "Выберите группу для турнира:\n$TourName, файл $source\n\n"; |
|
while (@arr=$sth->fetchrow) { |
|
print "[$arr[0]] $arr[1]\n"; |
|
} |
|
$ParentId = <STDIN>; |
|
chomp $ParentId; |
|
if (!$ParentId) { |
|
print "Пропускаем файл $source\n"; |
|
print STDERR "Файл $source отвергнут оператором\n"; |
|
return (0,0); |
|
} else { |
|
print "Вы выбрали турнир: $ParentId\n"; |
|
$sth = $dbh->prepare("INSERT INTO Tournaments |
|
(Title, Type, ParentId, FileName) |
|
VALUES ($TourName, 'Ч', $ParentId, |
|
$source)"); |
|
$sth->execute; |
|
my $TournamentId = $sth->{mysql_insertid}; |
|
return ($TournamentId,$ParentId); |
|
} |
|
} else { |
|
# Теперь, если файла нет в дереве турниров, никаких вопросов не |
|
# задаётся, а вместо этого он добавляется в группу 9999 |
|
$ParentId = 9999; |
|
my $tempsource=$source; |
|
my $temptname=$TourName; |
|
$tempsource=~s/^\'(.*)\'$/$1/; |
|
$temptname=~s/^\'(.*)\'$/$1/; |
|
print UNSORTED "$tempsource".((12 -length($source))x' ')."\t$temptname\n"; |
|
$isunsorted=1; |
|
$sth = $dbh->prepare("INSERT INTO Tournaments |
|
(Title, Type, ParentId, FileName) |
|
VALUES ($TourName, 'Ч', $ParentId, |
|
$source)"); |
|
$sth->execute; |
|
my $TournamentId = $sth->{mysql_insertid}; |
|
return ($TournamentId,$ParentId); |
|
} |
|
|
|
|
|
} |
|
|
|
sub UpdateTournament { |
|
my ($dbh, $TournamentId, $field, $value) = @_; |
|
$dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId") |
|
or die $dbh->errstr; |
|
} |
|
|
|
sub UpdateQuestion { |
|
my ($dbh, $QuestionId, $field, $value) = @_; |
|
|
|
if (($field eq 'Type') && ($value eq "'Д'")) { |
|
$value = "'ЧД'"; |
|
} |
|
$dbh->do("UPDATE Questions SET $field=$value |
|
WHERE QuestionId=$QuestionId") |
|
or die $dbh->errstr; |
|
} |
|
|
|
sub CheckFile { |
|
my ($dbh, $source, $title) = @_; |
|
my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments |
|
WHERE FileName=$source AND Type='Ч'"); |
|
$sth->execute or die $dbh->errstr; |
|
my @arr = $sth->fetchrow; |
|
if (! scalar @arr) { |
|
return SelectGroup($dbh,$source,$title); |
|
} |
|
my($Id,$ParentId,$QuestionsNum)=@arr; |
|
if($QuestionsNum) { |
|
print "Файл $source с данными $title уже существует. ", |
|
"Заменить?[y/N]\n"; |
|
my $answer = <STDIN>; |
|
if ($answer !~ /^[yY]/) { |
|
return (0,0); |
|
} else { |
|
DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0); |
|
} |
|
} |
|
return($Id,$ParentId); |
|
} |
|
|
|
|
|
sub DeleteTournament { |
|
my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_; |
|
if ($QuestionsNum) { |
|
UpdateParents($dbh,$ParentId,-$QuestionsNum); |
|
} |
|
my (@Tours) = &GetTours($dbh, $Id); |
|
foreach my $Tour (@Tours) { |
|
DeleteTournament($dbh,$Tour,$Id,0,1); |
|
} |
|
my $sth = $dbh->prepare("DELETE FROM Questions |
|
WHERE ParentId=$Id"); |
|
$sth->execute or die $dbh->errstr; |
|
if($DeleteMyself) { |
|
$sth = $dbh->prepare("DELETE FROM Tournaments |
|
WHERE Id=$Id"); |
|
$sth->execute or die $dbh->errstr; |
|
} |
|
} |
|
|
|
sub GetTours { |
|
my ($dbh, $ParentId) = @_; |
|
my (@arr, @Tours); |
|
|
|
my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments |
|
WHERE ParentId=$ParentId ORDER BY Id"); |
|
|
|
$sth->execute; |
|
|
|
while (@arr = $sth->fetchrow) { |
|
push @Tours, $arr[0]; |
|
} |
|
|
|
return @Tours; |
|
} |
|
|
|
sub CreateTour { |
|
my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_; |
|
my $sth = $dbh->prepare("INSERT INTO Tournaments |
|
(Title, Type, ParentId, Number) |
|
VALUES ($title, 'Т', $ParentId, $TourNum)"); |
|
$sth->execute; |
|
my $TourId = $sth->{mysql_insertid}; |
|
while (my ($key,$value)=each %$rh_defaults) { |
|
&UpdateTournament($dbh, $TourId, $key, $value); |
|
} |
|
return $TourId; |
|
} |
|
|
|
|
|
MAIN: |
MAIN: |
Line 279 MAIN:
|
Line 107 MAIN:
|
|
|
my($source); |
my($source); |
|
|
my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") |
|
or die "Can't connect to DB chgk\n"; |
|
my @sources; |
my @sources; |
open UNSORTED, ">$unsortedname"; |
|
while ($source = shift) { |
while ($source = shift) { |
push @sources,glob($source); |
push @sources,glob($source); |
} |
} |
Line 296 MAIN:
|
Line 121 MAIN:
|
$d[1]=sprintf("%02d",$d[1]); |
$d[1]=sprintf("%02d",$d[1]); |
$d[2]=sprintf("%02d",$d[2]); |
$d[2]=sprintf("%02d",$d[2]); |
$d[0]+=1900; |
$d[0]+=1900; |
my $UnquotedCreated=join('-', @d); |
my $CreatedAt=join('-', @d); |
my ($CreatedAt) = $dbh->quote($UnquotedCreated); |
|
|
|
open INFD, $source |
open INFD, $source |
or die "Can't open input file: $!\n"; |
or die "Can't open input file: $!\n"; |
|
|
$source =~ s/^.*\/([^\/]*)$/$1/; |
$source =~ s/^.*\/([^\/]*)$/$1/; |
my $unquotedsource=$source; |
|
$unquotedsource=~s/\.txt\s*$//; |
|
$source = $dbh->quote($source); |
|
print STDERR "Файл: $source, дата: $CreatedAt "; |
print STDERR "Файл: $source, дата: $CreatedAt "; |
my %TourDefaults=('CreatedAt'=>$CreatedAt); |
while (($key, $value) = getField(\*INFD)) { |
my %QuestionDefaults=(); |
|
my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч')); |
|
while (($key, $value) = getField(\*INFD, $dbh)) { |
|
last if (!$key); |
last if (!$key); |
|
|
if ($key =~ /Мета/) { |
if ($key =~ /Мета/) { |
next; # This is obsolete |
next; # This is obsolete |
} |
} |
if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { |
if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { |
($TournamentId, $ParentId) = CheckFile($dbh,$source,$value); |
|
if (!$TournamentId) { |
|
last; |
|
} |
|
$sth = $dbh->prepare("UPDATE Tournaments SET |
|
Title=$value, Type='Ч', |
|
ParentId=$ParentId, |
|
FileName=$source, |
|
CreatedAt=$CreatedAt |
|
WHERE |
|
Id=$TournamentId"); |
|
$sth->execute; |
|
next; |
next; |
} |
} |
if ($key =~ /Тур/) { |
if ($key =~ /Тур/) { |
if ($TourId) { |
|
|
|
$dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum |
|
WHERE Id=$TourId"); |
|
} |
|
$qnum = 0; |
$qnum = 0; |
$TourNum++; |
$TourNum++; |
$TourDefaults{'FileName'}=$dbh->quote( |
|
"$unquotedsource.$TourNum" |
|
); |
|
$TourId=CreateTour($dbh,$value,$TournamentId,$TourNum, |
|
\%TourDefaults); |
|
%QuestionDefaults=%QuestionGlobalDefaults; |
|
$QuestionId=0; |
$QuestionId=0; |
next; |
next; |
} |
} |
if ($key =~ /Вопрос/) { |
if ($key =~ /Вопрос/) { |
if (!$TourId) { |
|
$qnum = 0; |
|
$TourNum++; |
|
$TourId=CreateTour($dbh,'1',$TournamentId,$TourNum, |
|
\%TourDefaults); |
|
%QuestionDefaults=%QuestionGlobalDefaults; |
|
} |
|
my $query = "INSERT INTO Questions |
|
(ParentId, Number) |
|
VALUES ($TourId, $qnum+1)"; |
|
$sth = $dbh->prepare($query); |
|
$sth->execute or print $query;; |
|
$QuestionId = $sth->{mysql_insertid}; |
|
&UpdateQuestion($dbh, $QuestionId, "Question", $value); |
|
while (my ($key,$value)=each %QuestionDefaults) { |
|
&UpdateQuestion($dbh, $QuestionId, $key, $value); |
|
} |
|
$qnum++; |
$qnum++; |
$all_qnum++; |
$all_qnum++; |
next; |
next; |
} |
} |
|
|
if ($key =~ /Ответ/) { |
if ($key =~ /Ответ/) { |
&UpdateQuestion($dbh, $QuestionId, "Answer", $value); |
|
next; |
next; |
} |
} |
|
|
|
|
if ($key =~ /Зач[её]т/) { |
if ($key =~ /Зач[её]т/) { |
&UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value); |
|
next; |
next; |
} |
} |
|
|
|
|
|
|
if ($key =~ /Комментари/) { |
if ($key =~ /Комментари/) { |
&UpdateQuestion($dbh, $QuestionId, "Comments", $value); |
|
next; |
next; |
} |
} |
|
|
Line 394 MAIN:
|
Line 169 MAIN:
|
my @Fields = grep { $key =~ /$_/ } keys %QuestionFields; |
my @Fields = grep { $key =~ /$_/ } keys %QuestionFields; |
|
|
if (scalar @Fields) { |
if (scalar @Fields) { |
my $word = shift @Fields; |
|
my $field = $QuestionFields{$word}; |
|
if ($QuestionId) { |
|
&UpdateQuestion($dbh, $QuestionId, $field, $value); |
|
} elsif ($TourId) { |
|
$QuestionDefaults{$field}=$value; |
|
} else { |
|
$QuestionGlobalDefaults{$field}=$value; |
|
} |
|
next; |
next; |
} |
} |
|
|
@Fields = grep { $key =~ /$_/ } keys %TourFields; |
@Fields = grep { $key =~ /$_/ } keys %TourFields; |
|
|
if (scalar @Fields) { |
if (scalar @Fields) { |
my $word = shift @Fields; |
|
my $field = $TourFields{$word}; |
|
if ($QuestionId) { |
|
print STDERR "ОШИБКА: $key $value недопустимы после", |
|
" начала вопросов\n"; |
|
} elsif ($TourId) { |
|
&UpdateTournament($dbh, $TourId, $field, $value); |
|
} else { |
|
&UpdateTournament($dbh, $TournamentId, $field, $value); |
|
$TourDefaults{$field}=$value; |
|
} |
|
next; |
next; |
} |
} |
|
|
|
|
|
|
# |
# |
# If we are here, something got wrong! |
# If we are here, something got wrong! |
Line 430 MAIN:
|
Line 186 MAIN:
|
print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n"; |
print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n"; |
|
|
} |
} |
$dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum |
|
WHERE Id=$TourId"); |
|
$dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum |
|
WHERE Id=$TournamentId"); |
|
&UpdateParents($dbh, $ParentId, $all_qnum,$UnquotedCreated); |
|
print STDERR "Всего вопросов: $all_qnum \n"; |
print STDERR "Всего вопросов: $all_qnum \n"; |
} |
} |
close UNSORTED; |
|
unlink $unsortedname unless $isunsorted; |
|
$dbh->disconnect; |
|
} |
} |