--- db/prgsrc/db.cgi 2003/04/19 22:50:06 1.99 +++ db/prgsrc/db.cgi 2003/04/20 07:59:41 1.100 @@ -6,14 +6,20 @@ use strict; use Time::Local; use POSIX qw(locale_h); use locale; +use vars qw($opt_z); +use Getopt::Std; +getopts('z'); open STDERR, ">/var/tmp/errors1"; my $newsurl='http://news.chgk.info/'; +my $HTMLDIR="/!!!/"; +my $realHTMLDIR="c:/html/!!!/"; +my $usehtml=1; my $usewas=0; my $cashednumber=500; my $outputnumber=10; my ($proxyptext,$proxysstr); my $printqueries=0; -my $url=url; +my $url=url||''; my $qs=query_string; my $globaloutput; my %forbidden=(); @@ -23,7 +29,7 @@ $outputkvo=100 if $outputkvo>100; if (param('debug')) {$debug=1; $printqueries=1} *STDERR=*STDOUT if $debug; -if ($url !~ /db\.chgk\.info/ && $url !~ /(localhost)||(bilbo)/ ) { +if ($url !~ /db\.chgk\.info/ && $url !~ /localhost/ ) { my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs"; Redirect ($u); exit; @@ -142,13 +148,11 @@ sub fetchquestion { my ($sth,$q,$WithTour)=@_; if ($WithTour) { ($$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, - $$q{'Sources'}, $$q{'Number'}, $$q{'Title'}, $$q{'TourTitle'}, $$q{'FileName'},$$q{'PlayedAt'},$$q{'TourNumber'}) = $sth->fetchrow; } else { ($$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, - $$q{'Sources'}, $$q{'Number'})= $sth->fetchrow; } @@ -167,13 +171,13 @@ sub SelectQuestions { my $query; if ($WithTour) { - $query="SELECT Questions.Question, Answer, Comments, Authors, Sources, + $query="SELECT Questions.Question, Answer, Comments, Authors, Questions.Number , t2.Title, t1.Title, t2.FileName, t2.PlayedAt,t1.Number from Questions,Tournaments as t1, Tournaments as t2 WHERE $where"; } else { - $query="SELECT Questions.Question, Answer, Comments, Authors,Sources, + $query="SELECT Questions.Question, Answer, Comments, Authors, Questions.Number from Questions WHERE $where"; } @@ -184,6 +188,7 @@ sub SelectQuestions { return $sth; } + # Reads one question from the DB. Gets DB handler and Question ID. sub GetQuestion { my ($dbh, $QuestionId) = @_; @@ -205,6 +210,23 @@ sub GetQuestion { return %Question; } +sub tourhref { + my ($t,$a,$gr)=@_; + my $res; + if ($usehtml) { + $res=$t; + $res.=$a?"-a":"-q" unless $gr; + $res.=".html"; + return $res; + } else { + $res=$url; + $res.="?tour=$t"; + $res.=$a?"?answers=1":""; + return $res; + } + +} + # Gets numbers of all the questions from the given tour. sub GetTourQuestions { my ($dbh, $ParentId) = @_; @@ -719,8 +741,8 @@ sub PrintList { { $nav.= (" "x4). - a({href=>url."?".$qs."\&first=1"},"<<").(" "x4). - a({href=>(url."?".$qs."\&first=".($first-$outputkvo))},"<").(" "x4) + a({href=>$url."?".$qs."\&first=1"},"<<").(" "x4). + a({href=>($url."?".$qs."\&first=".($first-$outputkvo))},"<").(" "x4) } else {$nav.=' 'x15;} @@ -762,14 +784,14 @@ sub PrintList { $l=$fkvo if $l>$fkvo+1; if ($f==$first) {$nav.="[$f-$l] ";} else { - $nav.= "[".a({href=>(url."?".$qs."\&first=$f")},"$f-$l")."] ";} + $nav.= "[".a({href=>($url."?".$qs."\&first=$f")},"$f-$l")."] ";} } if ($lprint+$outputkvo<$fkvo) { $nav.= (" "x4). - a({href=>(url."?".$qs."\&first=".($first+$outputkvo))},">").(" "x4). - a({href=>url."?".$qs."\&first=$llprint"},">>").(" "x4) + a({href=>($url."?".$qs."\&first=".($first+$outputkvo))},">").(" "x4). + a({href=>$url."?".$qs."\&first=$llprint"},">>").(" "x4) } $Output.= "$nav".br."\n"; my @q=@$Questions[$first-1..$last-1]; @@ -944,7 +966,7 @@ sub PrintTournament { %Tournament = &GetTournament($dbh, $Id) if ($Id); my ($URL) = $Tournament{'URL'}; - $URL=~s/http:\/znatoki\/boris\/reports\//$newsurl/ if url=~/kulichki/; + $URL=~s/http:\/znatoki\/boris\/reports\//$newsurl/ if $url=~/kulichki/; $URL=~s/\/znatoki\/boris\/reports\//$newsurl/ if url=~/kulichki/;; my ($Info) = $Tournament{'Info'}; my ($Copyright) = $Tournament{'Copyright'}; @@ -999,7 +1021,7 @@ sub PrintTournament { } else { $qnum = ''; } - if ($Tournament{'Type'} =~ /Г/) { + if ($Tournament{'Type'} !~ /[ТЧ]/) { $SingleTour=0; $imgsrc = "/icons/folder.gif"; $alt = "[*]"; @@ -1027,16 +1049,17 @@ sub PrintTournament { $Tournament{'PlayedAt'} . $qnum) . dl( dd("[" - . a({href=>url . "?tour=$textid&answer=0"}, + . a({href=>tourhref($textid,0)}, "вопросы") . "] [" - . a({href=>url . "?tour=$textid&answer=1"}, + . a({href=>tourhref($textid,1)}, "вопросы + ответы") . "]") ); } else { - $list .= dd(a({href=>url . "?tour=$textid&comp=1"}, + $list .= dd(a({href=> + $url . "?tour=$textid&comp=1"}, img({src=>'/icons/compressed.gif', alt=>'[ZIP]', border=>1})). " " . img({src=>$imgsrc, alt=>$alt}) - . " " . a({href=>url . "?tour=$textid&answer=0"}, + . " " . a({href=>tourhref($textid,0,1)}, $Tournament{'Title'}. " ". $Tournament{'PlayedAt'}||'') . $qnum); } @@ -1044,7 +1067,7 @@ sub PrintTournament { $output .= dl($list); if ($URL) { - if (url=~/zaba\.ru/ && $URL=~/^\//){$URL="http://info.chgk.info$URL"} + if ($url=~/zaba\.ru/ && $URL=~/^\//){$URL="http://info.chgk.info$URL"} $output .= p("Дополнительная информация об этом турнире - по адресу " . a({-'href'=>$URL}, $URL)); @@ -1141,22 +1164,23 @@ sub PrintTour { my $n=$Tour{'Number'}; if ($answer == 0) { $bottom .= - "[" . a({href=>url . "?tour=$fname.$n&answer=1"}, "ответы") . "] " . br; + "[" . a({href=>tourhref($fname,1)}, + "ответы") . "] " . br; } if ($n>1) { $bottom .= - "[" . a({href=>url . "?tour=$fname." . ($n - 1) . "&answer=0"}, + "[" . a({href=>tourhref("$fname.".($n-1),0)}, "предыдущий тур") . "] "; $bottom .= - "[" . a({href=>url . "?tour=$fname." . ($n - 1) . "&answer=1"}, + "[" . a({href=>tourhref("$fname.".($n-1),1)}, "предыдущий тур с ответами") . "] " . br; } if (&IsTour($dbh, $Tour{'ParentId'}, $n + 1)) { $bottom .= - "[" . a({href=>url . "?tour=$fname." . ($n + 1) . "&answer=0"}, + "[" . a({href=>tourhref("$fname.".($n+1),0)}, "следующий тур") . "] "; $bottom .= - "[" . a({href=>url . "?tour=$fname." . ($n + 1) . "&answer=1"}, + "[" . a({href=>tourhref("$fname.".($n+1),1)}, "следующий тур с ответами") . "] "; } @@ -1216,10 +1240,12 @@ sub PrintQuestion { $fname=~s/\.txt//; $titles .= dd(img({src=>"/icons/folder.open.gif"}) . " " . - a({href=>url . "?tour=$fname"}, $Question{'Title'}, $Question{'PlayedAt'}||'')); + a({href=>tourhref($fname,0,1)}, + $Question{'Title'}, $Question{'PlayedAt'}||'')); $titles .= dl(dd(img({src=>"/icons/folder.open.gif"}) . " " . - a({href=>url . "?tour=$fname.$Question{TourNumber}#$qnum"}, $Question{'TourTitle'}))); + a({href=>tourhref("$fname.$Question{'TourNumber'}#$qnum",1)}, + $Question{'TourTitle'}))); } $output .= dl(strong($titles)); } @@ -1317,7 +1343,7 @@ $output.="" $output=~s/\(pic: ([^\)]*)\)/

/g unless $text; my $qid=param('tour') ? (param('tour').".$Question{'Number'}" ): ''; - $output.=br.a({href=> url."?metod=proxy& + $output.=br.a({href=> $url."?metod=proxy& qid=$qid"}, 'Близкие вопросы').p if $answer>0 && !$text && $qid; return $output; @@ -1439,7 +1465,7 @@ sub PrintAll { $output .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) . - " " . a({href=>url . "?tour=$textid&answer=0"}, + " " . a({href=>tourhref($textid,0,1)}, $Tournament{'Title'}) ." " . ($Tournament{'PlayedAt'}||'') . " $New"); } if ($Id == 0 or $Tournament{'Type'} =~ /Г/ or $Tournament{'Type'} eq '') { @@ -1473,7 +1499,7 @@ sub PrintDates { if (!$array[0]); %Tournament = &GetTournament($dbh, $array[0]); $list .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) . - " " . a({href=>url . "?tour=$Tournament{'Id'}&answer=0"}, + " " . a({href=>tourhref($Tournament{'FileName'},0,1)}, $Tournament{'Title'}, $Tournament{'PlayedAt'}||'')); } $sth->finish; @@ -1554,10 +1580,10 @@ sub PrintAuthors $sth->execute; - $output.=Tr(th[a({href=>url."?authors=name"},"Имя") + $output.=Tr(th[a({href=>$url."?authors=name"},"Имя") .", ". -a({href=>url."?authors=surname"},"фамилия") - , a({href=>url."?authors=kvo"},"Количество вопросов")]); +a({href=>$url."?authors=surname"},"фамилия") + , a({href=>$url."?authors=kvo"},"Количество вопросов")]); $out1=''; @@ -1573,7 +1599,7 @@ a({href=>url."?authors=surname"},"фамили if (!$name || !$surname) { } else { - my $add=Tr(td([a({href=>url."?qofauthor=$id"},"$name $surname"), $kvo]))."\n"; + my $add=Tr(td([a({href=>$url."?qofauthor=$id"},"$name $surname"), $kvo]))."\n"; $output.=$add; } } @@ -1733,6 +1759,26 @@ sub WriteFile { } +sub Bottom +{ + my $output.=&Include_virtual("../dimrub/db/footer.html"); + $output.=p."

Обновление: ".&Include_virtual("../dimrub/db/date")."
"; + $output.=< +function toggle(e) { + if (e.style.display == "none") { + e.style.display=""; + } else { + e.style.display = "none"; + } +} + +EEE +; + $output.=end_html; + return $output; +} + MAIN: { @@ -1820,7 +1866,7 @@ td {font-size: x-small; font-family : sa th {font-size: x-small; font-family : sans-serif} \n"; - $globaloutput.=&Include_virtual("../dimrub/db/reklama.html"); + $globaloutput.=&Include_virtual("../dimrub/db/reklama.html")||''; } if (length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)|(all=)/i) { @@ -1950,7 +1996,8 @@ EOT $dbh->disconnect; exit; - } else { + } + elsif (!$opt_z) { my $QuestionNumber=0; my $qnum; if ($qnum=param('qnumber')){ @@ -1966,29 +2013,57 @@ EOT $globaloutput.=&PrintTournament($dbh, $tour, param('answer')); } } + else { + open TS, "timestamp"; + my $d=$dbh->quote(); + close TS; + my ($sth) = $dbh->prepare("SELECT t1.Id, t1.FileName, t1.Type, + count(t2.Id) + FROM Tournaments as t1, Tournaments as t2 + WHERE t1.CreatedAt>$d AND t2.ParentId=t1.Id GROUP BY t1.Id"); + $sth->execute; + my ($Id,$fname,$type,$c); + while (($Id,$fname,$type,$c)=$sth->fetchrow,$Id) { + print "$fname\n"; + $fname=~s/\.txt$//; +print " c=$c\n"; + if ($type=~/Т/ || $c<=1) + { + open FF, ">$realHTMLDIR$fname-q.html" or die "ERROR! - $HTMLDIR$fname-q.html\n"; + my $o=$globaloutput; + $o.=&PrintTournament($dbh, $Id, 0); + $o.=&Bottom; + print FF $o; + close FF; + open FF, ">$realHTMLDIR$fname-a.html"; + $o=$globaloutput; + $o.=&PrintTournament($dbh, $Id, 1); + $o.=&Bottom; + print FF $o; + close FF; + } + else { + open FF, ">$realHTMLDIR$fname.html" or die "ERROR! - $HTMLDIR$fname-q.html\n"; + my $o=$globaloutput; + $o.=&PrintTournament($dbh, $Id, 0); + $o.=&Bottom; + print FF $o; + close FF; + + } + } + } if (!$text) { - $globaloutput.=&Include_virtual("../dimrub/db/footer.html"); - $globaloutput.=p."
Обновление: ".&Include_virtual("../dimrub/db/date")."
"; -print < -function toggle(e) { - if (e.style.display == "none") { - e.style.display=""; - } else { - e.style.display = "none"; - } -} - -EEE -; - print end_html; + $globaloutput.=&Bottom; } - print $globaloutput; - if (($qs!~ /(rand)|(sstr)|(comp)/i) && (length $qs<=255)) { + if (!$opt_z){ + print $globaloutput; + if (($qs!~ /(rand)|(sstr)|(comp)/i) && (length $qs<=255)) { $globaloutput= $dbh->quote($globaloutput); $dbh->do("insert into hash (query,page) values (". $dbh->quote($qs). ",$globaloutput)"); + } } $dbh->disconnect;