--- db/prgsrc/db.cgi 2003/04/09 23:31:54 1.95 +++ db/prgsrc/db.cgi 2004/02/08 00:35:29 1.129 @@ -6,14 +6,41 @@ use strict; use Time::Local; use POSIX qw(locale_h); use locale; +use vars qw($opt_z); +use Getopt::Std; +getopts('z'); +$opt_z||=param("makehtml"); +my $timestamp="_timestamp.tmp"; +my $paramtour; +my $withanswers=param('answer')||param('answers'); open STDERR, ">/var/tmp/errors1"; my $newsurl='http://news.chgk.info/'; +my $reklama="../dimrub/db/reklama.html"; +my $footer="../dimrub/db/footer.html"; + +my $datefooter="../dimrub/db/date"; + +my $fname; +$reklama="../reklama.html" if $opt_z; +$footer="../footer.html" if $opt_z; +$datefooter="../date" if $opt_z; +my $HTMLDIR="/znatoki/dimrub/db/baza/"; +$HTMLDIR="/files/"; +my $realHTMLDIR; +if ($^O =~ /win/i) { + $realHTMLDIR="/html/znatoki/baza/"; +} else +{ + $realHTMLDIR="/home/piataev/public_html/dimrub/db/files/"; +} +my $usehtml=$opt_z||0; +$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,11 +50,16 @@ $outputkvo=100 if $outputkvo>100; if (param('debug')) {$debug=1; $printqueries=1} *STDERR=*STDOUT if $debug; -if ($url !~ /db\.chgk\.info/ && $url !~ /localhost/ ) { +if ($url !~ /db\.chgk\.info/ && $url !~ /localhost/ && $url !~ /bilbo/) { my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs"; Redirect ($u); exit; } +#if (!param('sstr') && param('all')) { +# my $destination='http://db.chgk.info/all.html'; +# Redirect($destination); +# exit; +#} my $thislocale; if ($^O =~ /win/i) { $thislocale = "Russian_Russia.20866"; @@ -61,6 +93,14 @@ my %TypeName=('children'=>'Д', 'game'=>' 'chgk'=>'Ч', 'brain'=>'Б', 'beskrylka'=>'Л','ehruditka'=>'Э'); +sub countz { + my ($dbh,$type)=@_; + + my $sth=$dbh->prepare("select count(*) from Questions where Type LIKE '%$type%'"); + $sth->execute(); + my ($tmp)=$sth->fetchrow(); + return $tmp; +} my $all=param('all'); $all=0 if lc $all eq 'no'; @@ -87,7 +127,8 @@ my @months=('000','Jan',"Feb","Mar","Apr sub NewEnough { my ($a) = @_; my ($year, $month, $day) = split('-', $a); - + $month=1 if ($month<=0); + $day=1 if ($day<=0); return (time - timelocal(0, 0, 0, $day, $month -1, $year) < $TMSECS); } @@ -95,6 +136,12 @@ sub NewEnough { sub Redirect { my ($destination) = @_; +print header.< +EndOfHTML +; +=head + print <here< EndOfHTML ; +=cut } @@ -136,12 +184,12 @@ sub GetTournament { sub fetchquestion { my ($sth,$q,$WithTour)=@_; if ($WithTour) { - ($$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, + ($$q{'QuestionId'}, $$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{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'},$$q{'Sources'}, $$q{'Number'})= $sth->fetchrow; } @@ -149,24 +197,27 @@ sub fetchquestion { + sub SelectQuestions { my ($dbh,$q,$WithTour) = @_; my %q=(); - $_ = "QuestionId=$_" foreach @$q; - my $where=join " OR ",@$q; +# $_ = "QuestionId=$_" foreach @$q; +# my $where=join " OR ",@$q; + my $where=join ',',@$q; + $where &&= "QuestionId IN (".(join ',',$where).")"; $where||=1; $where="($where) AND Questions.ParentId=t1.Id AND t1.ParentId=t2.Id" if $WithTour; my $query; if ($WithTour) { - $query="SELECT Questions.Question, Answer, Comments, Authors, + $query="SELECT QuestionId, Questions.Question, Answer, Comments, Authors, Sources, 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, + $query="SELECT QuestionId, Questions.Question, Answer, Comments, Authors, Sources, Questions.Number from Questions WHERE $where"; } @@ -199,6 +250,31 @@ sub GetQuestion { return %Question; } +sub tourhref { + my ($t,$a,$gr)=@_; + my $res; + if ($usehtml) { + $res=$t; + $res.=$a?"-a":"-q" unless $gr; + $res.=".html"; + $res=~s/(\#\d+)(.*)$/$2$1/; + my $t=$res; + $t=~s/\#.*$//; + $res=~s/\.1// unless -e "$realHTMLDIR$t"; + $t=$res; + $t=~s/\#.*$//; + $res=~s/\.html/-q\.html/ unless -e "$realHTMLDIR$t"; + $res="$HTMLDIR$res" unless $opt_z; + 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) = @_; @@ -348,6 +424,7 @@ sub proxy sub makeproxysstr { my ($dbh,$ptext)=@_; my $text=$$ptext; + POSIX::setlocale( &POSIX::LC_ALL, $thislocale ); $text=~tr/ёЁ/еЕ/; $text=~s/(${RLrl})p(${RLrl})/$1p$2/gom; $text=~s/p(${RLrl})/р$1/gom; @@ -356,6 +433,7 @@ sub makeproxysstr { $text=~s/[^йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮQWERTYUIOPASDFGHJKLZXCVBNM0-9]/ /g; $text=uc $text; my @list= $text=~m/(?:(?:${RLrl})+)|(?:[A-Za-z0-9]+)/gom; + my (%c, %good,$sstr); foreach (@list) { @@ -364,7 +442,6 @@ sub makeproxysstr { my @words=sort {$c{$a}<=> $c{$b}} @list; # $good{$words[$_]}=1 foreach 0..4; - foreach (@words) { $good{$_}=1 if $c{$_}<200; @@ -394,9 +471,12 @@ sub russearch { my %relevance; my @blob; my %count; - + POSIX::setlocale( &POSIX::LC_ALL, $thislocale ); $sstr=~tr/йцукенгшщзхъфывапролджэячсмитьбю/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/; - @qw=@w =split (' ', uc $sstr); +# @qw=@w =split (' ', uc $sstr); + my $ts=uc $sstr; + @qw=@w= $ts=~m/(?:(?:${RLrl})+)|(?:[A-Za-z0-9]+)/gom; + #----------- foreach $i (0..$#w) # заполняем массив @nf начальных форм @@ -525,7 +605,6 @@ foreach (keys %wordsof) @tasks=sort {$relevance{$b}<=>$relevance{$a}} @tasks; - ############ @@ -667,7 +746,7 @@ sub makewhere { $type .= ($_=$TypeName{$_}) foreach @type; my $where=' 0 '; foreach (@type) { - $where.= " OR (Type ='$_') OR (Type ='$_Д') "; + $where.= " OR (Type ='$_') OR (Type ='$_Д') OR (Type ='Д$_') "; } $where.= "OR (Type='ЧБ')" if ($type=~/Ч|Б/); return $where; @@ -692,7 +771,6 @@ sub PrintList { my ($dbh,$Questions,$shablon,$was)=@_; my $Output; my $first=param('first') ||1; - $first=$first-($first-1)%$outputkvo; my $fkvo=param('fkvo')||($#$Questions+1); my $last=$first+$outputkvo-1; @@ -713,8 +791,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;} @@ -756,27 +834,34 @@ 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..$last]; + my @q=@$Questions[$first-1..$last-1]; my %q=(); my $sth=SelectQuestions($dbh,\@q,1); + my $qq; + my @Q; + for (0..$#q) { + %{$Q[$_]}=(); + fetchquestion($sth,$Q[$_],1); + $q{$Q[$_]{'QuestionId'}}=$Q[$_]; + } + for (my $i = $first; $i <= $last; $i++) { - fetchquestion($sth,\%q,1); + my $q=$q{$$Questions[$i-1]}; my $output; - $output = &PrintQuestion($dbh, \%q, 1, 0, 1,0,1 ); - if (param('metod') && (param('metod') eq 'rus' || param('metod') eq 'proxy')) + $output = &PrintQuestion($dbh, $q, 1, 0, 1,0,1 ); +# if (param('metod') && (param('metod') eq 'rus' || param('metod') eq 'proxy')) { $output=~s/\b($shablon)\b/\$1\<\/strong\>/gi; - } else { $output=~s/($shablon)/\$1\<\/strong\>/gi; } $Output.= $output; @@ -889,9 +974,11 @@ sub PrintRandom { my %q; my $answer=$razd?0:1; my @answers; +#my $t=time; my (@Questions) = &Get12Random($dbh, $type, $num); - my ($output, $i) = ('', 0); + my ($output, $i) = ('', 0); +#$output.="time=".(time-$t).p; if ($text) { $output .= " $num случайных вопросов.\n\n"; } else { @@ -937,9 +1024,9 @@ sub PrintTournament { %Tournament = &GetTournament($dbh, $Id) if ($Id); - my ($URL) = $Tournament{'URL'}; - $URL=~s/http:\/znatoki\/boris\/reports\//$newsurl/ if url=~/kulichki/; - $URL=~s/\/znatoki\/boris\/reports\//$newsurl/ if url=~/kulichki/;; + my ($URL) = $Tournament{'URL'}||''; + $URL=~s/http:\/znatoki\/boris\/reports\//$newsurl/ if $url=~/kulichki/ || $url=~/db.chgk.info/; + $URL=~s/\/znatoki\/boris\/reports\//$newsurl/ if url=~/kulichki/ || $url=~/db.chgk.info/; my ($Info) = $Tournament{'Info'}; my ($Copyright) = $Tournament{'Copyright'}; my $fname=$Tournament{'FileName'}; @@ -973,9 +1060,20 @@ sub PrintTournament { }; } } else { - my ($qnum) = GetQNum($dbh); - $output .= h2("Банк Вопросов: $qnum вопрос".&Suffix($qnum)) - . p . "\n"; + my ($qnum) = GetQNum($dbh,0); + my ($qnum1) = GetQNum($dbh,1); + $output .= h2("База вопросов"). + h3("$qnum запис".&Suffix2($qnum). + " (уникальных $qnum1)"); + +# h4("". +# Tr(td("Из них:"), td("Вопросов ЧГК: ".countz($dbh,'Ч'))). +# Tr(td(" "), td("Вопросов для брейна: ".countz($dbh,'Б'))). +# Tr(td(" "), td("Вопросов для ЧГК и брейна: ".countz($dbh,'ЧБ'))). +# Tr(td(" "), td("Интернет-вопросов: ".countz($dbh,'И'))). +# Tr(td(" "), td("Бескрылок: ".countz($dbh,'Л'))). +# Tr(td(" "),td("Заданий для Своей Игры: ".countz($dbh,'Я'))). +# Tr(td(" "),td("Эрудиток: ".countz($dbh,'Э'))))."
" } for ($i = 0; $i <= $#Tours; $i++) { @@ -993,7 +1091,7 @@ sub PrintTournament { } else { $qnum = ''; } - if ($Tournament{'Type'} =~ /Г/) { + if ($Tournament{'Type'} !~ /[ТЧ]/) { $SingleTour=0; $imgsrc = "/icons/folder.gif"; $alt = "[*]"; @@ -1015,30 +1113,32 @@ sub PrintTournament { else {$textid=$Tournament{'Id'}}; - if ($SingleTour or $Tournament{'Type'} =~ /Т/) { + if ($SingleTour or ($Tournament{'Type'} =~ /Т/)) { $list .= dd(img({src=>$imgsrc, alt=>$alt}) . " " . $Tournament{'Title'} . " " . - $Tournament{'PlayedAt'} . $qnum) . + ($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"}, - img({src=>'/icons/compressed.gif', alt=>'[ZIP]', border=>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); + ($Tournament{'PlayedAt'}||'')) . $qnum); } } $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)); @@ -1064,6 +1164,23 @@ sub Suffix { return $suffix; } + +sub Suffix1 { + my ($qnum) = @_; + my ($suffix) = 'я' if $qnum =~ /[234]$/; + $suffix = 'е' if $qnum =~ /1$/; + $suffix = 'й' if $qnum =~ /[567890]$/ || $qnum =~ /1.$/; + return $suffix; +} + +sub Suffix2 { + my ($qnum) = @_; + my ($suffix) = 'и' if $qnum =~ /[234]$/; + $suffix = 'ь' if $qnum =~ /1$/; + $suffix = 'ей' if $qnum =~ /[567890]$/ || $qnum =~ /1.$/; + return $suffix; +} + sub IsTour { my ($dbh, $Id,$n) = @_; @@ -1134,23 +1251,27 @@ sub PrintTour { my $n=$Tour{'Number'}; if ($answer == 0) { + my $nn=".$n"; + $nn="" if ($n==1 && !&IsTour($dbh, $Tour{'ParentId'}, $n + 1)); + $bottom .= - "[" . a({href=>url . "?tour=$fname.$n&answer=1"}, "ответы") . "] " . br; + "[" . a({href=>tourhref("$fname$nn",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)}, "следующий тур с ответами") . "] "; } @@ -1166,8 +1287,10 @@ sub PrintField { $value =~ s/<[\/\w]*?>//sg; } else { $value =~ s/^\s+/
    /mg; + $value =~ s/(\s+)-+(\s+)/$1–$2/mg; + $value =~ s/\s+\–/ \–/mg + if $value !~ /^\|/; $value =~ s/^\|([^\n]*)/
$1<\/pre>/mg;
-	    $value =~ s/\s+-+\s+/ – /mg;
 	    $value =~ s/(http:\/\/\S+[^\s\)\(\,\.])/$1<\/a>/g if $header !~ /^Авто/;
 #	    $value =~ s/(http:\/\/(?:\w+.)+[\w\\\~]+(\?[^\s.]+)?)/$1<\/a>/g if $header !~ /^Авто/;
 #	    $value =~ s/(\s)"/$1“/mg;
@@ -1210,10 +1333,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));
 	}
@@ -1309,9 +1434,10 @@ $output.=""
 
 	}
 	$output=~s/\(pic: ([^\)]*)\)/

/g unless $text; - my $qid=param('tour') ? (param('tour').".$Question{'Number'}" ): ''; + $paramtour||=param("tour"); + my $qid=$paramtour ? ($paramtour.".$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; @@ -1319,12 +1445,22 @@ qid=$qid"}, 'Близкие вопросы').p # Returns the total number of questions currently in the DB. sub GetQNum { - my ($dbh) = @_; + my ($dbh,$x) = @_; my ($sth) = $dbh->prepare("SELECT COUNT(*) FROM Questions"); $sth->execute; my $tmp=($sth->fetchrow)[0]; $sth->finish; - return $tmp; + ($sth)= $dbh -> prepare("select distinct count(first) FROM equalto"); + $sth -> execute; + my ($c)=$sth->fetchrow; + +# $i++ while ( my ($first, $second)=$sth -> fetchrow) +# { +# $forbidden{$first}=1; +# } + $sth->finish; + + return $tmp-($x?$c:0); } sub GetMaxQId { my ($dbh) = @_; @@ -1345,7 +1481,8 @@ sub Get12Random { srand; my $where=0; my $r=int (rand(10000)); - + my $w1=$r<5000? "QuestionId<5000 ":"QuestionId>=5000"; + $w1=1 if $type!~/Ч/; foreach (split '', $type) { $where.= " OR (Type ='$_') OR (Type ='$_Д') "; @@ -1355,7 +1492,7 @@ sub Get12Random { # $q="select QuestionId, QuestionId/$r-floor(QuestionId/$r) as val # from Questions where $where order by val limit $num"; # Когда на куличках появится mysql >=3.23 надо заменить на order by rand(); - $q="select QuestionId from Questions where $where order by rand() limit $num"; + $q="select QuestionId from Questions where ($w1) AND ($where) order by rand() limit $num"; $sth=$dbh->prepare($q); @@ -1374,20 +1511,20 @@ sub Get12Random { sub Include_virtual { my ($fn, $output) = (@_, ''); - + return "\n\n" if ($opt_z); open F , $fn - or return; #die "Can't open the file $fn: $!\n"; + or return ""; #die "Can't open the file $fn: $!\n"; while () { if (//&Include_virtual($1)/e; + s//&Include_virtual($1)/e; } if (//`$1`/e; } $output .= $_; } - return $output; + return $output||""; } sub PrintArchive { @@ -1412,6 +1549,8 @@ sub PrintAll { my (%Tournament) = &GetTournament($dbh, $Id); my (@Tours) = &GetTours($dbh, $Id); + my $SingleTour = $#Tours == 0; + my ($New) = ($Id and $Tournament{'Type'} eq 'Ч' and &NewEnough($Tournament{"CreatedAt"})) ? img({src=>"/znatoki/dimrub/db/new-sml.gif", alt=>"NEW!"}) : ""; @@ -1433,7 +1572,7 @@ sub PrintAll { $output .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) . - " " . a({href=>url . "?tour=$textid&answer=0"}, + " " . a({href=>tourhref($textid,0,!$SingleTour)}, $Tournament{'Title'}) ." " . ($Tournament{'PlayedAt'}||'') . " $New"); } if ($Id == 0 or $Tournament{'Type'} =~ /Г/ or $Tournament{'Type'} eq '') { @@ -1466,8 +1605,19 @@ sub PrintDates { next if (!$array[0]); %Tournament = &GetTournament($dbh, $array[0]); + my $textid; + if ($textid=$Tournament{'FileName'}) + { + $textid=~s/\.txt//; + } + elsif ($textid=$Tournament{'Number'}) + { + $fname=~s/\.txt//; + $textid="$fname.$textid"; + } + else {$textid=$Tournament{'Id'}}; $list .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) . - " " . a({href=>url . "?tour=$Tournament{'Id'}&answer=0"}, + " " . a({href=>tourhref($textid,0,1)}, $Tournament{'Title'}, $Tournament{'PlayedAt'}||'')); } $sth->finish; @@ -1548,10 +1698,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=''; @@ -1567,7 +1717,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; } } @@ -1586,7 +1736,8 @@ sub WriteFile { my $query= "SELECT Id, Title, Copyright, Info, URL, Editors, EnteredBy, PlayedAt, CreatedAt - from Tournaments where FileName=".$dbh->quote("$fname.txt"); + from Tournaments where FileName= + '$fname' OR FileName=".$dbh->quote("$fname.txt"); my $sth=$dbh->prepare($query); my (%Question,%editor,%qnumber,%copyright,%author,%vid,%tourtitle); $sth->execute; @@ -1727,14 +1878,48 @@ sub WriteFile { } +sub Bottom +{ + my $output.=&Include_virtual("$footer")||""; + $output.=p."

Обновление: ".&Include_virtual("$datefooter")."
"; + $output.=< +function toggle(e) { + if (e.style.display == "none") { + e.style.display=""; + } else { + e.style.display = "none"; + } +} + +EEE +; + $output.=end_html; + return $output; +} + MAIN: { setlocale(LC_CTYPE,'russian'); + POSIX::setlocale( &POSIX::LC_ALL, $thislocale ); my($i, $tour); my($text) = (param('text')) ? 1 : 0; - if (param('qid') && (param('qid')=~/^\d+$/)) { + $tour = (param('tour')) ? param('tour') : 0; + my $texttour=$tour; + my ($sth,$dbh); + $dbh = DBI->connect("DBI:mysql:chgk", "piataev", "") + or do { + print h1("Временные проблемы") . "База вопросов временно не + работает. Заходите попозже."; + print &Include_virtual("$reklama") if $url!~/localhost/; + print end_html; + die "Can't connect to DB chgk\n"; + }; + + + if (param('qid') && (param('qid')=~/^\d+$/) || $tour && $tour=~/^\d+$/) { my $destination='http://db.chgk.info/search.html'; # print header (-'Content-Type' => 'text/html', # -'Location:'=> 'http:\\db.chgk.info'); @@ -1742,18 +1927,58 @@ MAIN: exit } - if ($text) { + if ($tour && !param('qnumber') && (!param('answers')||(param('answers')<=1))) + { + my $n=param('tour'); + $n=~s/.txt$//; + my $gr=($n=~/^[A-Z]/) || (-e "$realHTMLDIR$n.html"); + my $destination=tourhref($tour,param('answers')||0,$gr); + my $d=$destination; + $d=~s/$HTMLDIR/$realHTMLDIR/; +# print header.$destination; + print header."$d|".(-e "$realHTMLDIR$n.html"); + if (-e $d) { + Redirect($destination); + exit + } + $d=~s/\.\d+//; + $destination=~s/\.\d+//; +print br.br.$d; + if (-e $d) { + Redirect($destination); + exit + } + + } + + if ($tour !~ /^[0-9]*$/) { + if ($tour=~/\./) + { + my ($fname,$n)= split /\./ , $tour; + + $sth = $dbh->prepare( + "SELECT t2.Id FROM Tournaments as t1, + Tournaments as t2 + WHERE (t1.FileName = '$fname.txt' OR t1.FileName='$fname') + AND t1.Id=t2.ParentId AND t2.Number=$n"); + } + else + { + $sth = $dbh->prepare("SELECT Id FROM Tournaments + WHERE FileName = '$tour.txt' OR + FileName = '$tour'"); + } + $sth->execute; + $tour = ($sth->fetchrow)[0]; + $sth->finish; + } + + + if ($text && !param ('comp')) { print header('text/plain'); - } else {print header;} - my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") - or do { - print h1("Временные проблемы") . "База вопросов временно не - работает. Заходите попозже."; - print &Include_virtual("../dimrub/db/reklama.html") if $url!~/localhost/; - print end_html; - die "Can't connect to DB chgk\n"; - }; + } elsif (!param('comp')) {print header;} my $sstr=param('sstr'); + $opt_z||=param("makehtml"); if (param('qid')) { my $sth; my $qid=param('qid'); @@ -1761,19 +1986,20 @@ MAIN: { my ($fname,$t,$n)= split /\./ , $qid; $n=$t,$t='' unless $n; + $t||=1; if ($t) { $sth = $dbh->prepare( "SELECT t2.Id FROM Tournaments as t1, Tournaments as t2 - WHERE t1.FileName = '$fname.txt' + WHERE (t1.FileName = '$fname.txt' OR t1.FileName='$fname') AND t1.Id=t2.ParentId AND t2.Number=$t"); } - else - { - $sth = $dbh->prepare("SELECT Id FROM Tournaments - WHERE FileName = '$fname.txt'"); - } +# else +# { +# $sth = $dbh->prepare("SELECT Id FROM Tournaments +# WHERE FileName = '$fname.txt' OR FileName = '$fname'" ); +# } $sth->execute; $tour = ($sth->fetchrow)[0]; $sth->finish; @@ -1814,10 +2040,10 @@ 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("$reklama")||''; } - if (length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)/i) { + if (!$opt_z && length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)|(all=)/i) { my $sth=$dbh->prepare("SELECT page,times,t from hash where query=".$dbh->quote($qs)); $sth->execute(); my ($p,$times,$t)=$sth->fetchrow(); @@ -1841,28 +2067,6 @@ th {font-size: x-small; font-family : sa } $sth->finish; } - $tour = (param('tour')) ? param('tour') : 0; - my $sth; - if ($tour !~ /^[0-9]*$/) { - if ($tour=~/\./) - { - my ($fname,$n)= split /\./ , $tour; - - $sth = $dbh->prepare( - "SELECT t2.Id FROM Tournaments as t1, - Tournaments as t2 - WHERE t1.FileName = '$fname.txt' - AND t1.Id=t2.ParentId AND t2.Number=$n"); - } - else - { - $sth = $dbh->prepare("SELECT Id FROM Tournaments - WHERE FileName = '$tour.txt'"); - } - $sth->execute; - $tour = ($sth->fetchrow)[0]; - $sth->finish; - } if (param('rand')) { @@ -1910,22 +2114,25 @@ EOT elsif (param('getfile')){ $globaloutput.=&writefile } elsif (param('all')) { +# my $destination='http://db.chgk.info/all.html'; +# Redirect($destination); +# exit; $globaloutput.=&PrintAll($dbh, 0); } elsif (param('from_year') && param('to_year')) { $globaloutput.=&PrintDates($dbh); } elsif (param('comp')) { - $globaloutput.="Content-Type: application/octet-stream\n"; - $globaloutput.="Content-Type: application/force-download\n"; - $globaloutput.="Content-Type: application/download\n"; - $globaloutput.="Content-Type: application/x-zip-compressed; name=db.zip\n"; - $globaloutput.="Content-Disposition: attachment; filename=db.zip \n\n"; + print "Content-Type: application/octet-stream\n"; + print "Content-Type: application/force-download\n"; + print "Content-Type: application/download\n"; + print "Content-Type: application/x-zip-compressed; name=$texttour.zip\n"; + print "Content-Disposition: attachment; filename=$texttour.zip \n\n"; $tour ||= 0; my (@files) = &PrintArchive($dbh, $tour); WriteFile($dbh,$_) foreach @files; open F, "$ZIP -j - @files |"; binmode(F); binmode(STDOUT); - $globaloutput.= join "",; + print join "",; close F; $dbh->disconnect; exit; @@ -1940,7 +2147,8 @@ EOT $dbh->disconnect; exit; - } else { + } + elsif (!$opt_z && !param("makehtml")) { my $QuestionNumber=0; my $qnum; if ($qnum=param('qnumber')){ @@ -1950,35 +2158,82 @@ EOT $QuestionNumber=($sth->fetchrow)[0]||0; } if ($QuestionNumber) { - $globaloutput.= &PrintQuestion($dbh, $QuestionNumber, param('answer')||0, $qnum, 1,0,0); + $globaloutput.= &PrintQuestion($dbh, $QuestionNumber, $withanswers||0, $qnum, 1,0,0); # $dbh, $Id, $answer, $qnum, $title, $text } else { - $globaloutput.=&PrintTournament($dbh, $tour, param('answer')); + $globaloutput.=&PrintTournament($dbh, $tour, $withanswers); } } + else { + $opt_z=1; + $url="http://db.chgk.info/cgi-bin/db.cgi"; + open TS, $timestamp; + my $d=$dbh->quote(); + close TS; + open FF, ">${realHTMLDIR}index.html" or die "ERROR! - ${HTMLDIR}index.html\n"; + my $o=$globaloutput; + $o.=&PrintTournament($dbh, 0, 0); + $o.=&Bottom; + print FF $o; + close FF; + open FF, ">${realHTMLDIR}all.html" or die "ERROR! - ${HTMLDIR}all.html\n"; + $o=$globaloutput; + $o.=&PrintAll($dbh,0); + $o.=&Bottom; + print FF $o; + close FF; + + +# 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"); +my ($sth) = $dbh->prepare("SELECT t1.Id, t1.FileName, t1.Type, count(t2.Id) FROM Tournaments as t1 LEFT JOIN Tournaments as t2 + ON t2.ParentId=t1.id WHERE t1.CreatedAt>=$d GROUP BY t1.Id"); + $sth->execute; + my ($Id,$fname,$type,$c); + while (($Id,$fname,$type,$c)=$sth->fetchrow,$Id) { + next unless $fname; + print "$fname\n"; + $fname=~s/\.txt$//; + if ($type=~/Т/ || $c<=1) + { + open FF, ">$realHTMLDIR$fname-q.html" or die "ERROR! - $fname-q.html\n"; + my $o=$globaloutput; + $paramtour=$fname; + $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! - $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;