--- db/prgsrc/updatedb.pl 2002/01/15 03:34:31 1.24 +++ db/prgsrc/updatedb.pl 2006/09/25 00:09:43 1.42 @@ -6,13 +6,19 @@ updatedb.pl - a script for creation of n =head1 SYNOPSIS -updatedb.pl I I.... +updatedb.pl B<[-i]> I I.... =head1 DESCRIPTION Updates information in the B databse. Uses file +=head1 OPTIONS + +=item B<-i> + +Ask about ParentId. + =head1 BUGS @@ -22,10 +28,19 @@ The database, user and password are hard Dmitry Rubinstein -=head1 $Id: updatedb.pl,v 1.24 2002/01/15 03:34:31 roma7 Exp $ +=head1 $Id: updatedb.pl,v 1.42 2006/09/25 00:09:43 roma7 Exp $ =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) = ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6', 'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11', @@ -44,21 +59,24 @@ my ($sth); use DBI; use strict; - +my $isunsorted=0; sub UpdateParents { - my ($dbh, $ParentId, $all_qnum) = @_; + my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_; if ($ParentId) { - my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId + my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId, CreatedAt FROM Tournaments WHERE Id = $ParentId"); $sth1->execute; - my ($q, $p) = ($sth1->fetchrow)[0, 1]; + 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 + QuestionsNum=$q + $all_qnum, CreatedAt=$qc WHERE Id = $ParentId"); - &UpdateParents($dbh, $p, $all_qnum); + &UpdateParents($dbh, $p, $all_qnum,$c); } } + sub getField { my($desc, $dbh) = @_; my($key); @@ -75,6 +93,7 @@ sub getField { $value =~ s/-(.*)-/-$month-/; $value =~ s/-00*$/-01/; } + if ($key eq 'Автор') {$value=~s/\.$//;} $value = $dbh->quote($value); return ($key, $value); } @@ -101,30 +120,49 @@ sub getField { sub SelectGroup { my ($dbh, $source, $TourName) = @_; my ($sth, $ParentId, $i, @arr); - - $sth = $dbh->prepare("SELECT Id, Title FROM + + 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 = ; - chomp $ParentId; - if (!$ParentId) { - print "Пропускаем файл $source\n"; - print STDERR "Файл $source отвергнут оператором\n"; - return (0,0); + $sth->execute; + print "Выберите группу для турнира:\n$TourName, файл $source\n\n"; + while (@arr=$sth->fetchrow) { + print "[$arr[0]] $arr[1]\n"; + } + $ParentId = ; + 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 { - print "Вы выбрали турнир: $ParentId\n"; - $sth = $dbh->prepare("INSERT INTO Tournaments +# Теперь, если файла нет в дереве турниров, никаких вопросов не +# задаётся, а вместо этого он добавляется в группу 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); - } + $sth->execute; + my $TournamentId = $sth->{mysql_insertid}; + return ($TournamentId,$ParentId); + } } @@ -137,6 +175,10 @@ sub UpdateTournament { 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; @@ -239,21 +281,31 @@ MAIN: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") or die "Can't connect to DB chgk\n"; - +if ($dbh->get_info( 18 )=~/^(5|(4.1))/) {$dbh->do("SET NAMES 'koi8r'");} + my @sources; + open UNSORTED, ">$unsortedname"; while ($source = shift) { + push @sources,glob($source); + } + foreach $source(@sources) { my $TourNum=0; my($PlayedAt) = ''; my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0); my($tournum, $qnum, $all_qnum) = (0, 0, 0); my (@d) = (localtime((stat($source))[9]))[5,4,3]; $d[1]++; + $d[1]=sprintf("%02d",$d[1]); + $d[2]=sprintf("%02d",$d[2]); $d[0]+=1900; - my ($CreatedAt) = $dbh->quote( join('-', @d)); + my $UnquotedCreated=join('-', @d); + my ($CreatedAt) = $dbh->quote($UnquotedCreated); open INFD, $source or die "Can't open input file: $!\n"; $source =~ s/^.*\/([^\/]*)$/$1/; + my $unquotedsource=$source; + $unquotedsource=~s/\.txt\s*$//; $source = $dbh->quote($source); print STDERR "Файл: $source, дата: $CreatedAt "; my %TourDefaults=('CreatedAt'=>$CreatedAt); @@ -282,11 +334,15 @@ MAIN: } if ($key =~ /Тур/) { if ($TourId) { - $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum + + $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum WHERE Id=$TourId"); } $qnum = 0; $TourNum++; + $TourDefaults{'FileName'}=$dbh->quote( + "$unquotedsource.$TourNum" + ); $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum, \%TourDefaults); %QuestionDefaults=%QuestionGlobalDefaults; @@ -321,10 +377,20 @@ MAIN: next; } + + if ($key =~ /Зач[её]т/) { + &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value); + next; + } + + + if ($key =~ /Комментари/) { &UpdateQuestion($dbh, $QuestionId, "Comments", $value); next; } + + my @Fields = grep { $key =~ /$_/ } keys %QuestionFields; @@ -369,8 +435,10 @@ MAIN: WHERE Id=$TourId"); $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum WHERE Id=$TournamentId"); - &UpdateParents($dbh, $ParentId, $all_qnum); + &UpdateParents($dbh, $ParentId, $all_qnum,$UnquotedCreated); print STDERR "Всего вопросов: $all_qnum \n"; } + close UNSORTED; + unlink $unsortedname unless $isunsorted; $dbh->disconnect; }