--- db/prgsrc/updatedb.pl 2000/10/18 01:52:16 1.5 +++ db/prgsrc/updatedb.pl 2000/10/20 00:39:27 1.9 @@ -22,7 +22,7 @@ The database, user and password are hard Dmitry Rubinstein -=head1 $Id: updatedb.pl,v 1.5 2000/10/18 01:52:16 boris Exp $ +=head1 $Id: updatedb.pl,v 1.9 2000/10/20 00:39:27 boris Exp $ =cut @@ -70,7 +70,7 @@ sub getField { chomp $key; if ($key eq 'Дата') { $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/; - my($month) = $RevMonths{$2}; + my($month) = $RevMonths{$2} || '000'; $value =~ s/$2/$month/; } $value = $dbh->quote($value); @@ -78,7 +78,7 @@ sub getField { } next if (/^\s*$/); - if (/^(.*):\s*$/ && !$key) { + if (/^(.*)[:\.]\s*$/ && !$key) { $key = $1; next; } @@ -95,18 +95,34 @@ sub getField { } sub SelectGroup { - my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_; + my ($dbh, $source, $TourName) = @_; + my ($sth, $ParentId, $i, @arr); $sth = $dbh->prepare("SELECT Id, Title FROM Tournaments WHERE Type = 'Г'"); $sth->execute; - print "Выберите группу для турнира:\n$TourName\n\n"; + print "Выберите группу для турнира:\n$TourName, файл $source\n\n"; while (@arr=$sth->fetchrow) { print "[$arr[0]] $arr[1]\n"; } $ParentId = ; - print "Вы выбрали турнир: $ParentId\n"; - return $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); + } + + } sub UpdateTournament { @@ -122,6 +138,63 @@ sub UpdateQuestion { 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 = ; + if ($answer !~ /^[yY]/) { + return (0,0); + } else { + DeleteTournament($dbh,$Id,0); + } + } + return($Id,$ParentId); +} + + +sub DeleteTournament { + my ($dbh,$Id,$DeleteMyself) = @_; + my (@Tours) = &GetTours($dbh, $Id); + foreach my $Tour (@Tours) { + DeleteTournament($dbh,$Tour,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; +} + + MAIN: { my($key, $value, $addition); @@ -139,37 +212,33 @@ MAIN: $d[1]++; $d[0]+=1900; my ($CreatedAt) = $dbh->quote( join('-', @d)); - print STDERR "File created on: $CreatedAt\n"; - + open INFD, $source or die "Can't open input file: $!\n"; $source =~ s/^.*\/([^\/]*)$/$1/; $source = $dbh->quote($source); - print STDERR "Processing file: $source \n"; + print STDERR "Файл: $source, дата: $CreatedAt "; while (($key, $value) = getField(\*INFD, $dbh)) { last if (!$key); if ($key =~ /Мета/) { - $value =~ s/[^\d]*//g; - $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE - MetaId=$value"); - $sth->execute - or die "Invalid Meta field: $value"; - $ParentId = ($sth->fetchrow)[0]; - next; + next; # This is obsolete } - if ($key =~ /Чемпионат/) { - $ParentId = &SelectGroup($dbh, $value) - unless ($ParentId); - $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, Type, ParentId, FileName, - CreatedAt) - VALUES ($value, 'Ч', $ParentId, - $source, $CreatedAt)"); + 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; - $TournamentId = $sth->{mysql_insertid}; next; } if ($key =~ /Тур/) { @@ -187,7 +256,7 @@ MAIN: $TourId = $sth->{mysql_insertid}; next; } - if ($key =~ /Вид/) { + if ($key =~ /Вид/ || $key =~ /Тип/) { $qtype = $value; $qtype =~ s/\'//g; next; @@ -204,32 +273,64 @@ MAIN: $all_qnum++; next; } - &UpdateQuestion($dbh, $QuestionId, "Answer", $value) - if ($key =~ /Ответ/); + + if ($key =~ /Ответ/) { + &UpdateQuestion($dbh, $QuestionId, "Answer", $value); + next; + } + + if ($key =~ /Рейтинг/) { + &UpdateQuestion($dbh, $QuestionId, "Rating", $value); + next; + } - &UpdateQuestion($dbh, $QuestionId, "Authors", $value) - if ($key =~ /Автор/); + + if ($key =~ /Автор/) { + &UpdateQuestion($dbh, $QuestionId, "Authors", $value); + next; + } - &UpdateQuestion($dbh, $QuestionId, "Sources", $value) - if ($key =~ /Источник/); + + if ($key =~ /Источник/) { + &UpdateQuestion($dbh, $QuestionId, "Sources", $value); + next; + } - &UpdateQuestion($dbh, $QuestionId, "Comments", $value) - if ($key =~ /Комментарий/); + + if ($key =~ /Комментари/) { + &UpdateQuestion($dbh, $QuestionId, "Comments", $value); + next; + } - &UpdateTournament($dbh, $TournamentId, "URL", $value) - if ($key =~ /URL/); + + if ($key =~ /URL/ || $key =~ /Ссылка/) { + &UpdateTournament($dbh, $TournamentId, "URL", $value); + next; + } - &UpdateTournament($dbh, $TournamentId, "Copyright", $value) - if ($key =~ /Копирайт/); + + if ($key =~ /Копирайт/) { + &UpdateTournament($dbh, $TournamentId, "Copyright", $value); + next; + } - &UpdateTournament($dbh, $TournamentId, "Info", $value) - if ($key =~ /Инфо/); + + if ($key =~ /Инфо/) { + &UpdateTournament($dbh, $TournamentId, "Info", $value); + next; + } - &UpdateTournament($dbh, $TournamentId, "Editors", $value) - if ($key =~ /Редактор/); + + if ($key =~ /Редактор/) { + &UpdateTournament($dbh, $TournamentId, "Editors", $value); + next; + } - &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value) - if ($key =~ /Обработан/); + + if ($key =~ /Обработан/) { + &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value); + next; + } if ($key =~ /Дата/) { if ($TourId) { @@ -237,14 +338,21 @@ MAIN: } else { &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value); } + next; } + + # + # If we are here, something got wrong! + # + 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); - print STDERR "Total number of questions: $all_qnum \n"; + print STDERR "Всего вопросов: $all_qnum \n"; } $dbh->disconnect; }