--- db/prgsrc/db.cgi 2003/04/19 22:50:06 1.99 +++ db/prgsrc/db.cgi 2003/04/29 22:21:26 1.117 @@ -6,14 +6,39 @@ 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 $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="/baza/"; +my $realHTMLDIR; +if ($^O =~ /win/i) { + $realHTMLDIR="/html/znatoki/baza/"; +} else +{ + $realHTMLDIR="/home/piataev/public_html/dimrub/db/baza/"; +} +my $usehtml=$opt_z||0; 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 +48,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/ && $url !~ /bilbo/) { my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs"; Redirect ($u); exit; @@ -92,7 +117,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); } @@ -141,14 +167,12 @@ sub GetTournament { sub fetchquestion { my ($sth,$q,$WithTour)=@_; if ($WithTour) { - ($$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, - $$q{'Sources'}, + ($$q{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, $$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{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, $$q{'Number'})= $sth->fetchrow; } @@ -156,24 +180,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, Sources, + $query="SELECT QuestionId, 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 QuestionId, Questions.Question, Answer, Comments, Authors, Questions.Number from Questions WHERE $where"; } @@ -184,6 +211,7 @@ sub SelectQuestions { return $sth; } + # Reads one question from the DB. Gets DB handler and Question ID. sub GetQuestion { my ($dbh, $QuestionId) = @_; @@ -205,6 +233,25 @@ 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/; + $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) = @_; @@ -354,6 +401,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; @@ -362,6 +410,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) { @@ -370,7 +419,6 @@ sub makeproxysstr { my @words=sort {$c{$a}<=> $c{$b}} @list; # $good{$words[$_]}=1 foreach 0..4; - foreach (@words) { $good{$_}=1 if $c{$_}<200; @@ -400,7 +448,7 @@ sub russearch { my %relevance; my @blob; my %count; - + POSIX::setlocale( &POSIX::LC_ALL, $thislocale ); $sstr=~tr/йцукенгшщзхъфывапролджэячсмитьбю/ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ/; @qw=@w =split (' ', uc $sstr); @@ -531,7 +579,6 @@ foreach (keys %wordsof) @tasks=sort {$relevance{$b}<=>$relevance{$a}} @tasks; - ############ @@ -698,7 +745,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; @@ -719,8 +765,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,27 +808,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-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; @@ -944,8 +997,8 @@ 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/;; + $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'}; @@ -999,7 +1052,7 @@ sub PrintTournament { } else { $qnum = ''; } - if ($Tournament{'Type'} =~ /Г/) { + if ($Tournament{'Type'} !~ /[ТЧ]/) { $SingleTour=0; $imgsrc = "/icons/folder.gif"; $alt = "[*]"; @@ -1021,30 +1074,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)); @@ -1141,22 +1196,23 @@ sub PrintTour { my $n=$Tour{'Number'}; if ($answer == 0) { $bottom .= - "[" . a({href=>url . "?tour=$fname.$n&answer=1"}, "ответы") . "] " . br; + "[" . a({href=>tourhref("$fname.$n",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 +1272,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)); } @@ -1315,9 +1373,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; @@ -1380,9 +1439,9 @@ sub Get12Random { sub Include_virtual { my ($fn, $output) = (@_, ''); - + return "" 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 (/