--- db/prgsrc/updatedb.pl 2004/01/10 21:07:38 1.38 +++ db/prgsrc/updatedb.pl 2010/04/24 18:13:03 1.48 @@ -28,17 +28,18 @@ The database, user and password are hard Dmitry Rubinstein -=head1 $Id: updatedb.pl,v 1.38 2004/01/10 21:07:38 boris Exp $ +=head1 $Id: updatedb.pl,v 1.48 2010/04/24 18:13:03 roma7 Exp $ =cut -use vars qw($opt_i); +use vars qw($opt_i, $opt_n); use Getopt::Std; my $unsortedname="../dump/unsorted"; -getopts('i'); +getopts('in'); #open STDERR, ">errors"; my $Interactive=$opt_i || 0; +my $newOnly = $opt_n ||0; my $DUMPDIR = $ENV{DUMPDIR} || "../dump"; my (%RevMonths) = @@ -59,6 +60,7 @@ my ($sth); use DBI; use strict; +use Data::Dumper; my $isunsorted=0; sub UpdateParents { my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_; @@ -76,25 +78,39 @@ FROM Tournaments WHERE Id = $ParentId"); } } +sub parseDate { + my $value = shift; + my ($from, $to) = split /\s+\-+\s+/, $value; + $from =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/; + my($month) = $RevMonths{$2} || '01'; + $from =~ s/-(.*)-/-$month-/; + $from =~ s/-00*$/-01/; + if ($to) { + $to =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/; + $month = $RevMonths{$2} || '01'; + $to =~ s/-(.*)-/-$month-/; + $to =~ s/-00*$/-01/; + } + return ($from, $to); + +} sub getField { my($desc, $dbh) = @_; my($key); my($value) = (''); while (<$desc>) { - s/[ ]//g; + s/[ +]//g; if ($key && /^\s*$/) { chomp $value; $value =~ s/\s+$//; chomp $key; if ($key eq 'Дата') { - $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/; - my($month) = $RevMonths{$2} || '01'; - $value =~ s/-(.*)-/-$month-/; - $value =~ s/-00*$/-01/; + my ($from, $to) = parseDate($value); + $value = {'PlayedAt'=>$from, 'PlayedAt2'=>$to}; } if ($key eq 'Автор') {$value=~s/\.$//;} - $value = $dbh->quote($value); return ($key, $value); } next if (/^\s*$/); @@ -105,14 +121,13 @@ sub getField { next; } if ($key) { - $value .= $_; + $value .= $_."\n"; next; } } if ($key && $value) { - $value=~s/\s+$//; - $value = $dbh->quote($value); - return ($key, $value); + $value=~s/\s+$//sm; + return ($key, $value); } return (0, 0); } @@ -156,10 +171,10 @@ sub SelectGroup { 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; + (Title, Type, ParentId, FileName,CreatedAt) + VALUES (".$dbh->quote($TourName).", 'Ч', $ParentId, + ?,NOW())"); + $sth->execute($source); my $TournamentId = $sth->{mysql_insertid}; return ($TournamentId,$ParentId); } @@ -169,13 +184,28 @@ sub SelectGroup { sub UpdateTournament { my ($dbh, $TournamentId, $field, $value) = @_; - $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId") - or die $dbh->errstr; + if (ref $value eq 'HASH') { + # It means that $value contains information about several fields + foreach my $k(keys %$value) { + if ($value->{$k}) { + &UpdateTournament($dbh, $TournamentId, $k, $value->{$k}); + } + } + } else { + my $v = $dbh->quote($value); + $dbh->do("UPDATE Tournaments SET $field=$v WHERE Id=$TournamentId") + or die $dbh->errstr; + } } sub UpdateQuestion { my ($dbh, $QuestionId, $field, $value) = @_; - $dbh->do("UPDATE Questions SET $field=$value + + if (($field eq 'Type') && ($value eq "Д")) { + $value = "ЧД"; + } + my $v = $dbh->quote($value); + $dbh->do("UPDATE Questions SET $field=$v WHERE QuestionId=$QuestionId") or die $dbh->errstr; } @@ -183,14 +213,17 @@ sub UpdateQuestion { 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; + WHERE FileName=? AND Type='Ч'"); + $sth->execute($source) or die $dbh->errstr; my @arr = $sth->fetchrow; if (! scalar @arr) { return SelectGroup($dbh,$source,$title); } my($Id,$ParentId,$QuestionsNum)=@arr; if($QuestionsNum) { + if ($newOnly) { + return (0,0); + } print "Файл $source с данными $title уже существует. ", "Заменить?[y/N]\n"; my $answer = ; @@ -240,14 +273,14 @@ sub GetTours { } sub CreateTour { - my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_; + my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_; my $sth = $dbh->prepare("INSERT INTO Tournaments - (Title, Type, ParentId, Number) - VALUES ($title, 'Т', $ParentId, $TourNum)"); - $sth->execute; + (Title, Type, ParentId, Number,CreatedAt) + VALUES (?, 'Т', $ParentId, $TourNum,NOW())"); + $sth->execute($title); my $TourId = $sth->{mysql_insertid}; while (my ($key,$value)=each %$rh_defaults) { - &UpdateTournament($dbh, $TourId, $key, $value); + &UpdateTournament($dbh, $TourId, $key, $value); } return $TourId; } @@ -263,7 +296,7 @@ MAIN: 'Инфо' => 'Info', 'URL' => 'URL', 'Ссылка' => 'URL', 'Редактор' => 'Editors', 'Обработан'=> 'EnteredBy', - 'Дата'=>'PlayedAt'); + 'Дата'=>'PlayedAt', 'PlayedAt2'=>'PlayedAt2'); # # Inherited fields for a Question # @@ -277,6 +310,7 @@ 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) { @@ -293,7 +327,7 @@ MAIN: $d[2]=sprintf("%02d",$d[2]); $d[0]+=1900; my $UnquotedCreated=join('-', @d); - my ($CreatedAt) = $dbh->quote($UnquotedCreated); + my ($CreatedAt) = $UnquotedCreated; open INFD, $source or die "Can't open input file: $!\n"; @@ -301,131 +335,121 @@ MAIN: $source =~ s/^.*\/([^\/]*)$/$1/; my $unquotedsource=$source; $unquotedsource=~s/\.txt\s*$//; - $source = $dbh->quote($source); print STDERR "Файл: $source, дата: $CreatedAt "; my %TourDefaults=('CreatedAt'=>$CreatedAt); my %QuestionDefaults=(); - my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч')); + my %QuestionGlobalDefaults=('Type'=>'Ч'); while (($key, $value) = getField(\*INFD, $dbh)) { - last if (!$key); + + last if (!$key); - if ($key =~ /Мета/) { - next; # This is obsolete - } - if ($key =~ /Чемпионат/ || $key =~ /Пакет/) { - ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value); - if (!$TournamentId) { + if ($key =~ /Мета/) { + next; # This is obsolete + } + 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; - } - if ($key =~ /Тур/) { - if ($TourId) { + } + $sth = $dbh->prepare("UPDATE Tournaments SET + Title=?, Type='Ч', + ParentId=?, + FileName=?, + CreatedAt=? + WHERE + Id=?"); + $sth->execute($value,$ParentId,$source,$CreatedAt,$TournamentId); + next; + } + if ($key =~ /Тур/) { + if ($TourId) { $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, + } + $qnum = 0; + $TourNum++; + $TourDefaults{'FileName'}= "$unquotedsource.$TourNum"; + + + $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum, \%TourDefaults); - %QuestionDefaults=%QuestionGlobalDefaults; - $QuestionId=0; - next; - } - if ($key =~ /Вопрос/) { - if (!$TourId) { + %QuestionDefaults=%QuestionGlobalDefaults; + $QuestionId=0; + next; + } + if ($key =~ /Вопрос/) { + if (!$TourId) { $qnum = 0; - $TourNum++; + $TourNum++; $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum, \%TourDefaults); - %QuestionDefaults=%QuestionGlobalDefaults; - } - my $query = "INSERT INTO Questions + %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) { + $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++; - $all_qnum++; - next; - } - - if ($key =~ /Ответ/) { - &UpdateQuestion($dbh, $QuestionId, "Answer", $value); - next; - } - - - if ($key =~ /Зач[её]т/) { - &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value); - next; - } - - - - if ($key =~ /Комментари/) { - &UpdateQuestion($dbh, $QuestionId, "Comments", $value); - next; - } - - if ($key =~ /Вид/) { - if ($value =~ /^\s*Д\s*$/) {#Questions Д are really ЧД... - $value = "ДЧ"; - } - &UpdateQuestion($dbh, $QuestionId, "Type", $value); - next; - } + } + $qnum++; + $all_qnum++; + next; + } + if ($key =~ /Ответ/) { + &UpdateQuestion($dbh, $QuestionId, "Answer", $value); + next; + } + if ($key =~ /Зач[её]т/) { + &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value); + next; + } - - my @Fields = grep { $key =~ /$_/ } keys %QuestionFields; + if ($key =~ /Комментари/) { + &UpdateQuestion($dbh, $QuestionId, "Comments", $value); + next; + } + + my @Fields = grep { $key =~ /$_/ } keys %QuestionFields; - if (scalar @Fields) { - my $word = shift @Fields; - my $field = $QuestionFields{$word}; - if ($QuestionId) { - &UpdateQuestion($dbh, $QuestionId, $field, $value); - } elsif ($TourId) { + if (scalar @Fields) { + my $word = shift @Fields; + my $field = $QuestionFields{$word}; + if ($QuestionId) { + &UpdateQuestion($dbh, $QuestionId, $field, $value); + } elsif ($TourId) { $QuestionDefaults{$field}=$value; - } else { + } else { $QuestionGlobalDefaults{$field}=$value; - } - next; - } + } + next; + } - @Fields = grep { $key =~ /$_/ } keys %TourFields; + @Fields = grep { $key =~ /$_/ } keys %TourFields; - if (scalar @Fields) { - my $word = shift @Fields; - my $field = $TourFields{$word}; - if ($QuestionId) { + if (scalar @Fields) { + my $word = shift @Fields; + my $field = $TourFields{$word}; + my $updateId; + if ($QuestionId) { print STDERR "ОШИБКА: $key $value недопустимы после", " начала вопросов\n"; - } elsif ($TourId) { - &UpdateTournament($dbh, $TourId, $field, $value); - } else { - &UpdateTournament($dbh, $TournamentId, $field, $value); - $TourDefaults{$field}=$value; - } - next; - } + } else { + if ($TourId) { + $updateId = $TourId; + } else { + $updateId = $TournamentId; + $TourDefaults{$field}=$value; + } + &UpdateTournament($dbh, $updateId, $field, $value); + } + next; + } #