--- db/prgsrc/db.cgi 2003/04/20 12:21:13 1.109 +++ db/prgsrc/db.cgi 2003/05/01 20:46:59 1.118 @@ -9,17 +9,30 @@ 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 $HTMLDIR=""; +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/znatoki/public_html/dimrub/db/baza/"; + $realHTMLDIR="/home/piataev/public_html/dimrub/db/baza/"; } -my $usehtml=1; +my $usehtml=$opt_z||0; my $usewas=0; my $cashednumber=500; my $outputnumber=10; @@ -40,11 +53,11 @@ if ($url !~ /db\.chgk\.info/ && $url !~ Redirect ($u); exit; } -if (!param('sstr') && param('all')) { - my $destination='http://db.chgk.info/all.html'; - Redirect($destination); - 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"; @@ -154,12 +167,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{'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{'Number'})= $sth->fetchrow; } @@ -167,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, + $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, + $query="SELECT QuestionId, Questions.Question, Answer, Comments, Authors, Questions.Number from Questions WHERE $where"; } @@ -225,11 +241,12 @@ sub tourhref { $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":""; + $res.=$a?"&answers=1":""; return $res; } @@ -384,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; @@ -392,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) { @@ -400,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; @@ -430,7 +448,7 @@ sub russearch { my %relevance; my @blob; my %count; - + POSIX::setlocale( &POSIX::LC_ALL, $thislocale ); $sstr=~tr/ÊÃÕËÅÎÇÛÝÚÈßÆÙ×ÁÐÒÏÌÄÖÜÑÞÓÍÉÔØÂÀ/êãõëåîçûýúèÿæù÷áðòïìäöüñþóíéôøâà/; @qw=@w =split (' ', uc $sstr); @@ -561,7 +579,6 @@ foreach (keys %wordsof) @tasks=sort {$relevance{$b}<=>$relevance{$a}} @tasks; - ############ @@ -728,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; @@ -805,14 +821,21 @@ sub PrintList { 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; @@ -973,9 +996,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'}; @@ -1065,8 +1088,8 @@ sub PrintTournament { } else { $list .= dd(a({href=> $url . "?tour=$textid&comp=1"}, - img({src=>'/icons/compressed.gif', alt=>'[ZIP]', border=>1})). - " " . + img({src=>'/icons/compressed.gif', alt=>'[ZIP]', border=>1})) . + " " . img({src=>$imgsrc, alt=>$alt}) . " " . a({href=>tourhref($textid,0,1)}, $Tournament{'Title'}. " ". @@ -1173,7 +1196,7 @@ sub PrintTour { my $n=$Tour{'Number'}; if ($answer == 0) { $bottom .= - "[" . a({href=>tourhref($fname,1)}, + "[" . a({href=>tourhref("$fname.$n",1)}, "ÏÔ×ÅÔÙ") . "] " . br; } if ($n>1) { @@ -1350,7 +1373,8 @@ $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& qid=$qid"}, 'âÌÉÚËÉÅ ×ÏÐÒÏÓÙ').p @@ -1415,7 +1439,7 @@ 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"; @@ -1629,7 +1653,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; @@ -1772,8 +1797,8 @@ sub WriteFile { sub Bottom { - my $output.=&Include_virtual("../dimrub/db/footer.html")||""; - $output.=p."

ïÂÎÏ×ÌÅÎÉÅ: ".&Include_virtual("../dimrub/db/date")."
"; + my $output.=&Include_virtual("$footer")||""; + $output.=p."
ïÂÎÏ×ÌÅÎÉÅ: ".&Include_virtual("$datefooter")."
"; $output.=< function toggle(e) { @@ -1795,6 +1820,7 @@ 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+$/)) { @@ -1805,18 +1831,19 @@ MAIN: exit } - if ($text) { + if ($text && !param ('comp')) { print header('text/plain'); - } else {print header;} + } elsif (!param('comp')) {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 &Include_virtual("$reklama") if $url!~/localhost/; print end_html; die "Can't connect to DB chgk\n"; }; my $sstr=param('sstr'); + $opt_z||=param("makehtml"); if (param('qid')) { my $sth; my $qid=param('qid'); @@ -1824,19 +1851,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; @@ -1877,10 +1905,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)|(all=)/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(); @@ -1914,7 +1942,7 @@ th {font-size: x-small; font-family : sa $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=$n"); } else @@ -1974,25 +2002,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); +# 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=db.zip\n"; + print "Content-Disposition: attachment; filename=db.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; @@ -2008,7 +2036,7 @@ EOT exit; } - elsif (!$opt_z) { + elsif (!$opt_z && !param("makehtml")) { my $QuestionNumber=0; my $qnum; if ($qnum=param('qnumber')){ @@ -2018,13 +2046,15 @@ 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; @@ -2056,8 +2086,9 @@ my ($sth) = $dbh->prepare("SELECT t1.Id, $fname=~s/\.txt$//; if ($type=~/ô/ || $c<=1) { - open FF, ">$realHTMLDIR$fname-q.html" or die "ERROR! - $HTMLDIR$fname-q.html\n"; + 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; @@ -2070,7 +2101,7 @@ my ($sth) = $dbh->prepare("SELECT t1.Id, close FF; } else { - open FF, ">$realHTMLDIR$fname.html" or die "ERROR! - $HTMLDIR$fname-q.html\n"; + open FF, ">$realHTMLDIR$fname.html" or die "ERROR! - $fname-q.html\n"; my $o=$globaloutput; $o.=&PrintTournament($dbh, $Id, 0); $o.=&Bottom;