--- db/prgsrc/db.cgi 2002/12/19 23:55:55 1.80 +++ db/prgsrc/db.cgi 2003/04/22 10:50:37 1.115 @@ -6,13 +6,58 @@ use strict; use Time::Local; use POSIX qw(locale_h); use locale; +use vars qw($opt_z); +use Getopt::Std; +getopts('z'); +my $withanswers=param('answer')||param('answers'); open STDERR, ">/var/tmp/errors1"; my $newsurl='http://news.chgk.info/'; +my $reklama="../dimrub/db/reklama.html"; +$reklama="../reklama.html" if $opt_z; +my $HTMLDIR="/znatoki/dimrub/db/baza/"; +my $realHTMLDIR; +if ($^O =~ /win/i) { + $realHTMLDIR="/html/znatoki/baza/"; +} else +{ + $realHTMLDIR="/home/znatoki/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 $qs=query_string; +my $globaloutput; my %forbidden=(); my $debug=0; #added by R7 +my $outputkvo=param('kvo') ||$outputnumber; +$outputkvo=100 if $outputkvo>100; + if (param('debug')) {$debug=1; $printqueries=1} *STDERR=*STDOUT if $debug; +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"; +} else { + $thislocale = "ru_RU.KOI8-R"; +} +POSIX::setlocale( &POSIX::LC_ALL, $thislocale ); + +if ((uc 'а') ne 'А') {print STDERR "Koi8-r locale not installed!\n"}; + my %fieldname= (0,'Question', 1, 'Answer', 2, 'Comments', 3, 'Authors', 4, 'Sources'); my %rusfieldname=('Question','Вопрос', 'Answer', 'Ответ', 'Comments', 'Комментарии', 'Authors', 'Автор', @@ -30,7 +75,6 @@ my %metodchar=('rus',1,'old',2); -my $thislocale; $searchin{$_}=1 foreach param('searchin'); my %TypeName=('children'=>'Д', 'game'=>'Я', 'igp'=>'И', @@ -40,8 +84,8 @@ my %TypeName=('children'=>'Д', 'game'=>' my $all=param('all'); $all=0 if lc $all eq 'no'; -my ($PWD) = `pwd`; -chomp $PWD; +my ($PWD) = `pwd` if $^O!~/win/i; +chomp $PWD if $PWD; my ($SRCPATH) = "/home/piataev/public_html/dimrub/src"; my ($ZIP) = "/usr/local/bin/zip"; my $DUMPFILE = "/tmp/chgkdump"; @@ -63,11 +107,35 @@ 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); } # Reads one question from the DB. Gets DB handler and Question ID. + +sub Redirect { +my ($destination) = @_; +print < +Redirection + +

Redirection

+

It appears that your browser cannot handle redirections +automatically. You can proceed to the randomly-selected page +by clicking here.

+ + +EndOfHTML +; +} + + + + sub GetTournament { my ($dbh, $Id) = @_; my (%Tournament, $field, @arr); @@ -86,6 +154,51 @@ sub GetTournament { return %Tournament; } +sub fetchquestion { + my ($sth,$q,$WithTour)=@_; + if ($WithTour) { + ($$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{'Number'})= + $sth->fetchrow; + } +} + + + +sub SelectQuestions { + my ($dbh,$q,$WithTour) = @_; + my %q=(); + $_ = "QuestionId=$_" foreach @$q; + my $where=join " OR ",@$q; + $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, + 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, + Questions.Number from Questions + WHERE $where"; + } + + my $sth; + $sth=$dbh->prepare($query); + $sth->execute; + return $sth; +} + + # Reads one question from the DB. Gets DB handler and Question ID. sub GetQuestion { my ($dbh, $QuestionId) = @_; @@ -107,11 +220,29 @@ 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) = @_; my (@arr, @Questions); - my ($sth) = $dbh->prepare("SELECT QuestionId FROM Questions WHERE ParentId=$ParentId order by Number"); @@ -145,13 +276,11 @@ sub GetTours { sub count { my ($dbh,$word)=@_; -print "timeb=".time.br if $debug; $word=$dbh->quote(uc $word); my $query="SELECT number from nests,nf where $word=w1 AND w2=nf.id"; my $sth=$dbh->prepare($query); $sth->execute; my @a=$sth->fetchrow; -print "timee0=".time.br if $debug; $sth->finish; $a[0]||0; } @@ -160,60 +289,104 @@ print "timee0=".time.br if $debug; sub printform { - my $submit=submit(-value=>'Поиск'); - my $inputstring=textfield(-name=>'sstr', - -default=>param('sstr')||'', - -size=>30, - -maxlength=>50); - my $qnumber="Выводить по".br. textfield(-name=>'kvo', - -default=>param('kvo')||'150', - -size=>3, - -maxlength=>5). br."вопросов"; - + my $qnumber=(" "x10)."Выводить по + "; +#textfield(-name=>'kvo', +# -default=>6, +# -size=>3, +# -maxlength=>5)." вопросов"; + my $sstr=param('sstr'); my @df=keys %searchin; + my %checked; + $checked{lc $_}="" foreach ('Question','Answer','Comments','Authors','Sources','old','rus', + 'chgk','brain','igp','game','ehruditka','beskrylka'); @df=('Question', 'Answer') unless @df; + $checked{lc $_}="checked" foreach @df; my $fields=checkbox_group('searchin',['Question','Answer','Comments','Authors','Sources'], [@df], 'false',\%rusfieldname); @df=param('type'); @df=('chgk','brain','igp','game','ehruditka','beskrylka') unless @df; + $checked{lc $_}="checked" foreach @df; + my $all=param('all') && param('all') eq 'yes'; - my $types=checkbox_group('type',['chgk','brain','igp','game','ehruditka','beskrylka'], [@df], - 'false',\%rusfieldname); - my $metod=radio_group(-name=>'metod',-values=>['old','rus'], - -default=>(param('metod')||'rus'), - -labels=>\%rusfieldname); - my $all=radio_group(-name=>'all',-values=>['yes','no'], - -default=>(param('all')||'no'), - -labels=>{'yes'=>'Все','no'=>'Любое'}); + $checked{'all'}=$all?"checked":""; + $checked{'any'}=$all?"":"checked"; + $checked{lc param('metod')}="checked"; + $checked{'rus'}=1 unless $checked{'rus'} || $checked{'old'}; ################################################# -return start_form(-method=>'get', - -action=>url, - -enctype=> - "application/x-www-form-urlencoded" -).br. -table(Tr -( - td({-valign=>'TOP'},$inputstring.$submit.p."Метод: $metod".p."Слова: $all"), - td({-valign=>'TOP'},(' 'x 8).'Поля:'), - td({-valign=>'TOP'},$fields), - td({-valign=>'TOP'},(' 'x 1).'Типы:'), - td({-valign=>'TOP'},$types), td(" "x5), - td({-valign=>'TOP'},$qnumber) -) -) - -#$fields. -#$inputstring.$submit.br.$metod.$all +return +< +

Поиск в базе вопросов

+ + + $qnumber +

+ + + + + + + + + + + + + +
Вариант поиска: + + Простой (старый) + + "Что? Где? Когда?" + + "Брейн-Ринг" + + "Интернет" +
+ "Своя игра" + + "Эрудитка" + + "Бескрылка" +
Расширенный (с учетом грамматики, в вопросах всех типов) +
Искать: + +Все слова + +Любое слово +
Поля для поиска: + +Вопрос + +Ответ
+
+Комментарии
+
+Автор
+
+Источник
+
+ + +EOT .endform .hr } sub proxy -{ -#print "time0=".time.br if $debug; +{ my ($dbh,$ptext,$allnf)=@_; + my $sstr=makeproxysstr($dbh,$ptext,$allnf); + return russearch($dbh,$sstr,0,$allnf); +} + +sub makeproxysstr { + my ($dbh,$ptext)=@_; my $text=$$ptext; $text=~tr/ёЁ/еЕ/; $text=~s/(${RLrl})p(${RLrl})/$1p$2/gom; @@ -239,18 +412,9 @@ sub proxy $good{$words[$_]}=0 foreach 16..$#words; -# foreach (@list) -# { -# if ($good{$_}) -# { -# $good{$_}=0; -# $sstr.=" $_"; -# } -# } $sstr.=" $_" foreach grep {$good{$_}} @list; -print "time05=".time.br if $debug; $$ptext=$sstr; - return russearch($dbh,$sstr,0,$allnf); + return $sstr; } @@ -282,7 +446,6 @@ $sstr=~tr/йцукенгшщзхъфывапролджэячсмить $qw= $dbh->quote (uc $w[$i]); $query=" select distinct w2 from nests where w1=$qw"; -print "$query",br if $printqueries; $sth=$dbh -> prepare($query); $sth -> execute; @{$nf[$i]}=(); @@ -313,7 +476,6 @@ print "$query",br if $printqueries; my $kvo=0; push @$allnf, @{$_} foreach @nf; - print "nf=@$allnf" if $printqueries; foreach $i (0..$#w) #запросы в базу... { @@ -323,11 +485,10 @@ print "$query",br if $printqueries; - $_= " word2question.word=".$_. ' ' foreach @arr; + $_= " word2question.word=$_" foreach @arr; $_= " nf.id=".$_. ' ' foreach @arr1; # @arr=(0) unless @arr; - $query="select questions from word2question where". (join ' OR ', @arr); -print STDERR "!$query\n",br if $printqueries; + $query="select questions from word2question where (". (join ' OR ', @arr).") AND length(questions)<80000"; $sth=$dbh -> prepare($query); $sth->execute; @@ -339,7 +500,6 @@ print STDERR "!$query\n",br if $printque } $sth->finish; $query="select number from nf where ".(join ' OR ', @arr1); -print "$query\n",br if $printqueries; $sth=$dbh -> prepare($query); $sth->execute; @@ -387,7 +547,6 @@ print "$query\n",br if $printqueries; } } #foreach $i -#print "keys tasksof", join ' ', keys %{$tasksof{0}}; #Ищем пересечение или объединение списков вопросов (значений %tasksof) foreach $sf (keys %tasksof) { @@ -397,7 +556,6 @@ print "$query\n",br if $printqueries; keys %count) ; -print "\n\$#tasks=",$#tasks,br if $printqueries; ############ Сортировка найденных вопросов foreach (keys %wordsof) @@ -410,11 +568,7 @@ foreach (keys %wordsof) ############ -print "tasks=@tasks" if $printqueries; -#print "$_ $relevance{$_} | " foreach @tasks; -#print br; -print "allnf=@$allnf",br if $printqueries; return @tasks; } @@ -469,17 +623,6 @@ sub Search { my $sstr=$$s; my (@arr, @Questions, @fields); my (@sar, $i, $sth,$where,$query); -# my $ip=$ENV{'REMOTE_ADDR'}; - -# $ip=$dbh->quote($ip); -# $query= -# "INSERT into queries (query,metod,searchin,ip) -# values (". $dbh->quote($sstr).', '. -# $dbh->quote($metod) . ', ' . -# $dbh->quote(join ' ', grep $searchin{$_}, keys %searchin) . -# ", $ip)"; -#print $query if $printqueries; -# $dbh -> do ($query); if ($metod eq 'rus') { my @tasks=russearch($dbh,$sstr,$all,$allnf); @@ -487,10 +630,7 @@ sub Search { } elsif ($metod eq 'proxy') { -# $searchin{'question'}=1; -# $searchin{'answer'}=1; my @task=proxy($dbh,$s,$allnf); -# $$s=$sstr; return @task } @@ -517,7 +657,6 @@ sub Search { $where= $$q{'matchexp'}; $query= "SELECT Questionid FROM Questions WHERE $where"; - print br."Query is: $query".br if $debug; $sth = $dbh->prepare($query); } else @@ -549,7 +688,6 @@ sub Search { WHERE ($f LIKE $sstr) AND (".&makewhere.") ORDER BY QuestionId"; -print $query if $printqueries; $sth = $dbh->prepare($query) } #else -- processing old-style query (R7) @@ -558,7 +696,6 @@ print $query if $printqueries; push @Questions, $arr[0] unless $forbidden{$arr[0]}; } $sth->finish; -print "@Questions" if $printqueries; return @Questions; } @@ -593,13 +730,13 @@ sub NoCase { sub PrintList { my ($dbh,$Questions,$shablon,$was)=@_; - + my $Output; my $first=param('first') ||1; - my $kvo=param('kvo') ||150; - $first=$first-($first-1)%$kvo; - my $last=$first+$kvo-1; - $last=scalar @$Questions if scalar @$Questions <$last; + $first=$first-($first-1)%$outputkvo; + my $fkvo=param('fkvo')||($#$Questions+1); + my $last=$first+$outputkvo-1; + $last=$fkvo if $fkvo<$last; my($f,$l); my $nav=''; my $qs=query_string; @@ -607,92 +744,98 @@ sub PrintList { $qs=~s/\&first\=[^\&]+//g; my $sstr=param('sstr')||''; $qs=~s/sstr=[^\&]+/sstr=$sstr/; - $qs=~s/\&was=[^\&]+//; - $qs.="&was=$was"||''; - if ($first>$kvo*3+1) + if ($usewas) { + $qs=~s/\&was=[^\&]+//; + $qs.="&was=$was" if $was; + $qs.="&fkvo=$fkvo" if $was; + } + if ($first>$outputkvo*3+1) { $nav.= (" "x4). - a({href=>url."?".$qs."\&first=1"},"<<").(" "x4). - a({href=>(url."?".$qs."\&first=".($first-$kvo))},"<").(" "x4) - } - + a({href=>$url."?".$qs."\&first=1"},"<<").(" "x4). + a({href=>($url."?".$qs."\&first=".($first-$outputkvo))},"<").(" "x4) + } else {$nav.=' 'x15;} my ($fprint,$lprint); - my $llprint=$#$Questions- ($#$Questions+1)%$kvo+2; - if ($#$Questions+1<=$kvo*7) + my $llprint=$fkvo- ($fkvo)%$outputkvo+1; # + if ($fkvo<=$outputkvo*7) { $fprint=1; $lprint=$llprint; } - elsif ($first>$kvo*3 && $#$Questions+1-$first>$kvo*3) + elsif ($first>$outputkvo*3 && $fkvo-$first>$outputkvo*3) { - $fprint=$first-$kvo*3; - $lprint=$first+$kvo*3; + $fprint=$first-$outputkvo*3; + $lprint=$first+$outputkvo*3; } - elsif ($first<=$kvo*3) + elsif ($first<=$outputkvo*3) { - $fprint=1; $lprint=6*$kvo+1; + $fprint=1; $lprint=6*$outputkvo+1; } else { $lprint=$llprint; - $fprint=$lprint-$kvo*6 + $fprint=$lprint-$outputkvo*6 } -# my $fprint=($first>$kvo*3) ? $first-$kvo*3 : 1; -# my $lprint=$#$Questions+1-$fprint>$kvo*7 ? $kvo*7 :$#$Questions+1; -# if ($lprint-$fprint<$kvo*6 && $fprint>1) +# my $fprint=($first>$outputkvo*3) ? $first-$outputkvo*3 : 1; +# my $lprint=$#$Questions+1-$fprint>$outputkvo*7 ? $outputkvo*7 :$#$Questions+1; +# if ($lprint-$fprint<$outputkvo*6 && $fprint>1) # { -# $fprint=$lprint-$kvo*6; +# $fprint=$lprint-$outputkvo*6; # $fprint=1 if ($fprint<=0) # } - for($f=$fprint; $f<=$lprint; $f+=$kvo) + for($f=$fprint; $f<=$lprint; $f+=$outputkvo) { -# next if $first-$f>$kvo*3; - $l=$f+$kvo-1; - $l=$#$Questions+1 if $l>$#$Questions+1; +# next if $first-$f>$outputkvo*3; + $l=$f+$outputkvo-1; + $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+$kvo<$#$Questions) + if ($lprint+$outputkvo<$fkvo) { $nav.= (" "x4). - a({href=>(url."?".$qs."\&first=".($first+$kvo))},">").(" "x4). - a({href=>url."?".$qs."\&first=$llprint"},">>").(" "x4) + a({href=>($url."?".$qs."\&first=".($first+$outputkvo))},">").(" "x4). + a({href=>$url."?".$qs."\&first=$llprint"},">>").(" "x4) } - - - print "$nav".br."\n"; + $Output.= "$nav".br."\n"; + my @q=@$Questions[$first-1..$last-1]; + my %q=(); + my $sth=SelectQuestions($dbh,\@q,1); for (my $i = $first; $i <= $last; $i++) { - my $output = &PrintQuestion($dbh, $$Questions[$i-1], 1, 0, 1); + fetchquestion($sth,\%q,1); + my $output; + $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; } - print $output; + $Output.= $output; } + $sth->finish; - - print "$nav".br."\n"; - + $Output.= "$nav".br."\n"; + return $Output; } sub PrintSearch { + my $Output=''; my ($dbh, $sstr, $metod,$was) = @_; my $t=time; - print h2("Поиск в базе вопросов"); - print printform; + $Output.= printform; my @allnf; my @Questions; - if ($was) + $was=0 if $metod eq 'proxy'; + if ($usewas && $was && ($metod ne 'proxy')) { my $sth=$dbh->prepare ("select sstr,questions,allnf from lastqueries where id=".param('was')); $sth->execute; @@ -701,25 +844,29 @@ sub PrintSearch { @Questions=unpack 'L*',$q; @allnf=unpack 'L*',$nf; $sth->finish; - } else + } + if (!$was || ($metod eq 'proxy') || (param('first')+$outputkvo>$cashednumber)) { @Questions=&Search($dbh, \$sstr,$metod,$all,\@allnf); - my $tmp=$dbh->quote(pack("L*",@Questions)); + $cashednumber=$#Questions if $cashednumber>$#Questions; + my $tmp=$dbh->quote(pack("L*",@Questions[0..$cashednumber])); my $qsstr=$dbh->quote($sstr); my $nf=$dbh->quote(pack("L*", @allnf)); my $ss=200; - do - { - $was=int rand(32000); - } - while (--$ss && (!$dbh->do ("insert into lastqueries (id,sstr,questions,allnf) + if ($usewas) { + do + { + $was=int rand(32000); + } + while (--$ss && (!$dbh->do ("insert into lastqueries (id,sstr,questions,allnf) values ($was, $qsstr,$tmp,$nf)"))); - print "Something is wrong...".br unless $ss; + $Output.= "Something is wrong...".br unless $ss; + } } - print p. "Время поиска: " . (time-$t) ." сек.".p; + $Output.= p. "Время поиска: " . (time-$t) ." сек.".p; my ($output, $i, $suffix, $hits) = ('', 0, '', $#Questions + 1); my $shablon; @@ -730,7 +877,6 @@ sub PrintSearch { $where.= " or w2=$_ " foreach @allnf; my $query="select w1 from nests where $where"; my $sth=$dbh->prepare($query); -print "$query" if $printqueries; $sth->execute; my @shablon; @@ -743,11 +889,10 @@ print "$query" if $printqueries; $shablon=~s/[её]/\[ЕЁ\]/gi; # $shablon=~s/([йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ])/&NoCase($1)/ge; $shablon=qr/$shablon/i; - print "!$shablon!",br if $printqueries; } - + $hits=param("fkvo")||$hits; if ($hits =~ /1.$/ || $hits =~ /[5-90]$/) { $suffix = 'й'; @@ -757,7 +902,7 @@ print "$query" if $printqueries; $suffix = 'я'; } - print p({align=>"center"}, "Результаты поиска на " . strong($sstr) + $Output.= p({align=>"center"}, "Результаты поиска на " . strong($sstr) . " : $hits попадани$suffix."); if (param('word')) { @@ -774,13 +919,16 @@ print "$query" if $printqueries; s/(\W)/\\$1/g foreach (@sar); $shablon=join "|",@sar; } - PrintList($dbh,\@Questions,$shablon,$was); + $Output.=PrintList($dbh,\@Questions,$shablon,$was); + return $Output; } sub PrintRandom { my ($dbh, $type, $num, $text) = @_; my $razd=param('razd'); + my %q; my $answer=$razd?0:1; + my @answers; my (@Questions) = &Get12Random($dbh, $type, $num); my ($output, $i) = ('', 0); @@ -790,19 +938,24 @@ sub PrintRandom { $output .= h2({align=>"center"}, "$num случайных вопросов."); } - + my $sth=SelectQuestions($dbh,\@Questions,0); for ($i = 0; $i <= $#Questions; $i++) { - # Passing DB handler, question ID, print answer, question - # number, print title, print text/html + fetchquestion($sth,\%q,0); $output .= - &PrintQuestion($dbh, $Questions[$i], $answer, $i + 1, 0, $text); + &PrintQuestion($dbh, \%q, $answer, $i + 1, 0, $text,1); + push @answers, $q{'Answer'}; } + $sth->finish; unless ($answer ) { $output.=$text?"\n".('-'x 20)."\nОтветы\n~~~~~~\n\n":h2('Ответы'); + $sth=SelectQuestions($dbh,\@Questions,0); for ($i = 0; $i <= $#Questions; $i++) { - $output .= - &PrintQuestion($dbh, $Questions[$i], -1, $i + 1, 0, $text); +# fetchquestion($sth,\%q,0); +# $output .= +# &PrintQuestion($dbh, \%q, -1, $i + 1, 0, $text,1); + $output.=$text?("Ответ ". ($i+1).": $answers[$i]\n\n"): + b("Ответ ". ($i+1).": "). $answers[$i].p; } } @@ -825,7 +978,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'}; @@ -880,7 +1033,7 @@ sub PrintTournament { } else { $qnum = ''; } - if ($Tournament{'Type'} =~ /Г/) { + if ($Tournament{'Type'} !~ /[ТЧ]/) { $SingleTour=0; $imgsrc = "/icons/folder.gif"; $alt = "[*]"; @@ -902,30 +1055,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)); @@ -980,6 +1135,7 @@ sub PrintTour { my (%Tour) = &GetTournament($dbh, $Id); my (@Tours) = &GetTours($dbh, $Tour{'ParentId'}); my (%Tournament) = &GetTournament($dbh, $Tour{'ParentId'}); + my %q; return 0 if ($Tour{'Type'} !~ /Т/); @@ -996,10 +1152,12 @@ sub PrintTour { $output .=&PrintEditor(\%Tour); my (@Questions) = &GetTourQuestions($dbh, $Id); + my $sth=SelectQuestions($dbh,\@Questions,0); for ($q = 0; $q <= $#Questions; $q++) { - $output .= &PrintQuestion($dbh, $Questions[$q], $answer, 0); + fetchquestion($sth,\%q,0); + $output .= &PrintQuestion($dbh, \%q, $answer, 0,0,0,1); } - + $sth->finish; $output .= hr({-'align'=>'center', -'width'=>'80%'}); if ($Tournament{'URL'}) { @@ -1019,22 +1177,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)}, "следующий тур с ответами") . "] "; } @@ -1067,25 +1226,39 @@ sub PrintField { # Gets a DB handler (ofcourse) and a question Id. Prints # that question, according to the options. sub PrintQuestion { - my ($dbh, $Id, $answer, $qnum, $title, $text) = @_; + my ($dbh, $Id, $answer, $qnum, $title, $text,$h) = @_; my ($output, $titles) = ('', ''); - my (%Question) = &GetQuestion($dbh, $Id); - $qnum = $Question{'Number'} + my (%Question); + if ($h) { + %Question=%$Id; + } else { + %Question = &GetQuestion($dbh, $Id); + if ($title) { + my (%Tour) = GetTournament($dbh, $Question{'ParentId'}); + my (%Tournament) = GetTournament($dbh, $Tour{'ParentId'}); + $Question{'FileName'}=$Tournament{'FileName'}; + $Question{'Title'}=$Tournament{'Title'}; + $Question{'PlayedAt'}=$Tournament{'PlayedAt'}; + $Question{'TourNumber'}=$Tour{'Number'}; + $Question{'TourTitle'}=$Tour{'Title'}; + } + + } + $qnum = $Question{'Number'} if ($qnum == 0); if (!$text) { $output .= hr({width=>"50%"}) if $answer>=0; if ($title) { - my (%Tour) = GetTournament($dbh, $Question{'ParentId'}); - my (%Tournament) = GetTournament($dbh, $Tour{'ParentId'}); - my $fname=$Tournament{'FileName'}; -#return "" if $fname=~/mgp0203/; + my $fname=$Question{'FileName'}; $fname=~s/\.txt//; $titles .= dd(img({src=>"/icons/folder.open.gif"}) . " " . - a({href=>url . "?tour=$fname"}, $Tournament{'Title'}, $Tournament{'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.$Tour{Number}#$qnum"}, $Tour{'Title'}))); + a({href=>tourhref("$fname.$Question{'TourNumber'}#$qnum",1)}, + $Question{'TourTitle'}))); } $output .= dl(strong($titles)); } @@ -1103,32 +1276,38 @@ sub PrintQuestion { if ($Question{'Authors'} ) { my $q=$Question{'Authors'}; ###АВТОРА!! - my $sth=$dbh->prepare("select Authors.Id,Name, Surname, Nicks from Authors, A2Q - where Authors.Id=Author And Question=$Id"); - $sth->execute; - my ($AuthorId,$Name, $Surname,$other,$Nicks); - if (!$text) { - while ((($AuthorId,$Name, $Surname,$Nicks)=$sth->fetchrow),$AuthorId) - { - my ($firstletter)=$Name=~m/^./g; - $Name=~s/\./\\\./g; - my $sha="(?:$Name\\s+$Surname)|(?:$Surname\\s+$Name)|(?:$firstletter\\.\\s*$Surname)|(?:$Surname\\s+$firstletter\\.)|(?:$Surname)|(?:$Name)"; - if ($Nicks) - { - $Nicks=~s/^\|//; - foreach (split /\|/, $Nicks) - { - s/\s+/ /g; - s/\s+$//; - s/ /\\s+/g; - s/\./\\\./g; - if (s/>$//) {$sha="$sha|(?:$_)"} - else {$sha="(?:$_)|$sha"} - } - } - $q=~s/($sha)/a({href=>url."?qofauthor=$AuthorId"},$1)/ei; - } - } +# my $sth=$dbh->prepare("select Authors.CharId,Name, Surname, Nicks from Authors, A2Q +# where Authors.Id=Author And Question=$Id"); +# $sth->execute; +# my ($AuthorId,$Name, $Surname,$other,$Nicks); +# if (!$text) { +# while ((($AuthorId,$Name, $Surname,$Nicks)=$sth->fetchrow),$AuthorId) +# { +# my ($firstletter)=$Name=~m/^./g; +# $Name=~s/\./\\\./g; +# $Name=~s/ё/[её]/g; +# $Surname=~s/ё/[её]/g; +# my $sha="(?:$Name\\s+$Surname)|(?:$Surname\\s+$Name)|(?:$firstletter\\.\\s*$Surname)|(?:$Surname\\s+$firstletter\\.)|(?:$Surname)"; +# if ($Nicks) +# { +# $Nicks=~s/^\|//; +# foreach (split /\|/, $Nicks) +# { +# s/\s+/ /g; +# s/\s+$//; +# s/ /\\s+/g; +# s/\./\\\./g; +# if (s/>$//) {$sha="$sha|(?:$_)"} +# else {$sha="(?:$_)|$sha"} +# } +# } +# $q=~s/($sha)/a({href=>url."?qofauthor=$AuthorId"},$1)/ei; +# unless ($1) +# { +# $q=~s/$Name/a({href=>url."?qofauthor=$AuthorId"},$1)/ei; +# } +# } +# } $output .= &PrintField("Автор(ы)", $q, $text); } @@ -1170,11 +1349,16 @@ EOTT } + $output.="" } - $output.=br.a({href=> url."?metod=proxy&qid=$Id"}, 'Близкие вопросы').p - if $answer>0 && !$text; + $output=~s/\(pic: ([^\)]*)\)/

/g unless $text; + my $qid=param('tour') ? (param('tour').".$Question{'Number'}" ): ''; + + $output.=br.a({href=> $url."?metod=proxy& +qid=$qid"}, 'Близкие вопросы').p + if $answer>0 && !$text && $qid; return $output; } @@ -1213,9 +1397,11 @@ sub Get12Random { } $where.= "OR (Type='ЧБ')" if ($type=~/Ч|Б/); - $q="select QuestionId, QuestionId/$r-floor(QuestionId/$r) as val - from Questions where $where order by val limit $num"; +# $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"; + $sth=$dbh->prepare($q); $sth->execute; @@ -1233,9 +1419,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 (/