--- db/prgsrc/updatedb.pl 2000/10/22 05:10:12 1.14 +++ db/prgsrc/updatedb.pl 2003/01/09 19:53:23 1.33 @@ -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,18 @@ The database, user and password are hard Dmitry Rubinstein -=head1 $Id: updatedb.pl,v 1.14 2000/10/22 05:10:12 boris Exp $ +=head1 $Id: updatedb.pl,v 1.33 2003/01/09 19:53:23 roma7 Exp $ =cut +use vars qw($opt_i); + +use Getopt::Std; +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,7 +58,7 @@ my ($sth); use DBI; use strict; - +my $isunsorted=0; sub UpdateParents { my ($dbh, $ParentId, $all_qnum) = @_; if ($ParentId) { @@ -67,20 +81,22 @@ sub getField { s/[ ]//g; if ($key && /^\s*$/) { chomp $value; + $value =~ s/\s+$//; chomp $key; if ($key eq 'Дата') { $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/; - my($month) = $RevMonths{$2} || '000'; - $value =~ s/$2/$month/; + my($month) = $RevMonths{$2} || '01'; + $value =~ s/-(.*)-/-$month-/; + $value =~ s/-00*$/-01/; } $value = $dbh->quote($value); return ($key, $value); } next if (/^\s*$/); - if (s/^(.*?)[:\.]\s*// && !$key) { + if (!$key && /^(.*?)[:\.]\s*(.*)$/s) { $key = $1; - $value=$_; + $value=$2; next; } if ($key) { @@ -89,6 +105,7 @@ sub getField { } } if ($key && $value) { + $value=~s/\s+$//; $value = $dbh->quote($value); return ($key, $value); } @@ -98,30 +115,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); + } } @@ -199,10 +235,10 @@ sub GetTours { } sub CreateTour { - my ($dbh,$title,$ParentId,$rh_defaults)=@_; + my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_; my $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, Type, ParentId) - VALUES ($title, 'Т', $ParentId)"); + (Title, Type, ParentId, Number) + VALUES ($title, 'Т', $ParentId, $TourNum)"); $sth->execute; my $TourId = $sth->{mysql_insertid}; while (my ($key,$value)=each %$rh_defaults) { @@ -227,16 +263,22 @@ MAIN: # Inherited fields for a Question # my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type', - 'Автор' => 'Authors', - 'Источник' => 'Sources'); + 'Автор' => 'Authors', 'Рейтинг'=>'Rating', + 'Источник' => 'Sources', + 'Тема' => 'Topic'); my($source); my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") or die "Can't connect to DB chgk\n"; - + my @sources; + open UNSORTED, ">$DUMPDIR/unsorted"; 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); @@ -281,7 +323,9 @@ MAIN: WHERE Id=$TourId"); } $qnum = 0; - $TourId=CreateTour($dbh,$value,$TournamentId,\%TourDefaults); + $TourNum++; + $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum, + \%TourDefaults); %QuestionDefaults=%QuestionGlobalDefaults; $QuestionId=0; next; @@ -289,7 +333,8 @@ MAIN: if ($key =~ /Вопрос/) { if (!$TourId) { $qnum = 0; - $TourId=CreateTour($dbh,'1',$TournamentId, + $TourNum++; + $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum, \%TourDefaults); %QuestionDefaults=%QuestionGlobalDefaults; } @@ -318,7 +363,7 @@ MAIN: next; } - my @Fields = grep /$key/, keys %QuestionFields; + my @Fields = grep { $key =~ /$_/ } keys %QuestionFields; if (scalar @Fields) { my $word = shift @Fields; @@ -333,7 +378,7 @@ MAIN: next; } - @Fields = grep /$key/, keys %TourFields; + @Fields = grep { $key =~ /$_/ } keys %TourFields; if (scalar @Fields) { my $word = shift @Fields; @@ -364,5 +409,7 @@ MAIN: &UpdateParents($dbh, $ParentId, $all_qnum); print STDERR "Всего вопросов: $all_qnum \n"; } + close UNSORTED; + unlink "unsorted" unless $isunsorted; $dbh->disconnect; }