--- db/prgsrc/writefile.pl 2002/01/12 05:00:30 1.1 +++ db/prgsrc/writefile.pl 2002/01/15 03:34:31 1.2 @@ -1,37 +1,42 @@ +#!perl use DBI; @months=('000','Jan',"Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct", "Nov","Dec"); -open WARN ,"warn"; $fname=shift; $fname=~s/\.txt$//; - +$TMPDIR="new"; my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") or die "Can't connect to DB chgk\n"; - if (WriteFile($fname)==-1) {print "File?";} + if (WriteFile($dbh,$fname)==-1) {print "File?";} + + $dbh->disconnect; +sub PrintDate { + my $date=shift; + my ($year,$month,$day)=split /-/, $date; +# $month=0,$date=0 if $year && $month==1 && $day==1; + return sprintf("%02d-%3s-%04d",$day,$months[$month],$year); +} + sub WriteFile { - my ($fname) = @_; + my ($dbh,$fname) = @_; + $fname=~s/\.txt$//; + $fname=~s/.*\/(\w+)/$1/; my $query= "SELECT Id, Title, Copyright, Info, URL, Editors, EnteredBy, PlayedAt, CreatedAt from Tournaments where FileName=".$dbh->quote("$fname.txt"); - $sth=$dbh->prepare($query); - + my $sth=$dbh->prepare($query); + my (%Question,%editor,%qnumber,%copyright,%author,%vid,%tourtitle); $sth->execute; - ($Id, $Title, $Copyright, $Info, $URL, + my ($Id, $Title, $Copyright, $Info, $URL, $Editors, $EnteredBy, $PlayedAt, $CreatedAt)= $sth->fetchrow; return -1 unless $Id; - open (OUT, ">new/$fname.txt"); + open (OUT, ">$TMPDIR/$fname.txt"); print OUT "Чемпионат:\n$Title\n\n"; - my $date=$PlayedAt; - ($year,$month,$day)=split /-/, $date; -# $month=0,$date=0 if $year && $month==1 && $day==1; - $pdate=sprintf("%02d-%3s-%4d",$day,$months[$month],$year); - - print OUT "Дата:\n$pdate\n\n" if $date; print OUT "URL:\n$URL\n\n" if $URL; @@ -41,40 +46,49 @@ sub WriteFile { print OUT "Редактор:\n$Editors\n\n" if $Editors; + print OUT "Дата:\n".PrintDate($PlayedAt)."\n\n" if $PlayedAt; - $query= "SELECT Id, Title, Copyright, Editors from Tournaments where ParentId=$Id order by Id"; + + $query= "SELECT Id, Title, Copyright, Editors, PlayedAt from Tournaments where ParentId=$Id order by Id"; $sth=$dbh->prepare($query); $sth->execute; - while (($tourid,$tourtitle,$cright,$editor)=$sth->fetchrow,$tourid) + my ($tourid,$tourtitle,$cright,$editor,@tours,$vid,$author,$tourauthor); + + + while (($tourid,$tourtitle,$cright,$editor,$date)=$sth->fetchrow,$tourid) { # $text{$tourid}="Тур:\n$tourtitle\n\n"; $query= "SELECT * from Questions where ParentId=$tourid order by QuestionId"; - $sth1=$dbh->prepare($query); + my $sth1=$dbh->prepare($query); $sth1->execute; push(@tours,$tourid); $tourtitle{$tourid}=$tourtitle; $copyright{$tourid}=$cright; $editor{$tourid}=$editor; + $date{$tourid}=$date; $vid=''; - $author=''; - $eqauthor=1; - $qnumber=0; - while (@arr=$sth1->fetchrow, $arr[0]) + my $author=''; + my $eqauthor=1; + my $qnumber=0; + my @arr; + while ( @arr=$sth1->fetchrow, $arr[0]) { - my($i, $name) = 0; + my $i = 0; $qnumber++; - foreach $name (@{$sth1->{NAME}}) { - $arr[$i]=~s/^(.*?)\s*$/$1/; + + foreach my $name (@{$sth1->{NAME}}) { + $arr[$i]=~s/^(.*?)\s*$/$1/ if $arr[$i]; $Question{$tourid}[$qnumber]{$name} = $arr[$i++]; } if ($vid) { - if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print WARN "Warning: Different types for Tournament $tourid\n"} + if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print STDERR "Warning: Different types for Tournament $tourid\n"} } else { $vid=$Question{$tourid}[$qnumber]{'Type'}; } + if ($author) { if ($author ne $Question{$tourid}[$qnumber]{'Authors'}) @@ -94,8 +108,12 @@ sub WriteFile { $vid=''; - $eqvid=1; - $eqauthor=1; + my $eqvid=1; + my $eqauthor=1; + my $eqdate=1; + $date=''; + + foreach (@tours) { $vid||=$vid{$_}; @@ -108,24 +126,29 @@ sub WriteFile { { $eqauthor=0; } + $date||=$date{$_}; + if (!$date{$_} || ($date{$_} ne $date)) + { + $eqdate=0; + } } - print OUT "Вид:\n$vid\n\n" if $eqvid; print OUT "Автор:\n$author\n\n" if $eqauthor; - foreach $tour(@tours) + foreach my $tour(@tours) { print OUT "Тур:\n$tourtitle{$tour}\n\n"; print OUT "Вид:\n$vid{$tour}\n\n" if !$eqvid; print OUT "Копирайт:\n$copyright{$tour}\n\n" if $copyright{$tour} && ($copyright{$tour} ne $Copyright); print OUT "Редактор:\n$editor{$tour}\n\n" if $editor{$tour} && ($editor{$tour} ne $Editors); + print OUT "Дата:\n".PrintDate($date{$tour})."\n\n" if !$eqdate && $date{$tour} || !$PlayedAt; $tourauthor=0; if (!$eqauthor && $author{$tour}) { print OUT "Автор:\n$author{$tour}\n\n"; $tourauthor=1; } - foreach $q(1..$qnumber{$tour}) + foreach my $q(1..$qnumber{$tour}) { print OUT "Вопрос $q:\n".$Question{$tour}[$q]{'Question'}."\n\n"; print OUT "Ответ:\n".$Question{$tour}[$q]{'Answer'}."\n\n"; @@ -145,4 +168,6 @@ sub WriteFile { -} \ No newline at end of file +} + +