--- db/prgsrc/db.cgi 2003/04/04 10:01:27 1.94 +++ db/prgsrc/db.cgi 2003/04/09 23:31:54 1.95 @@ -8,14 +8,26 @@ use POSIX qw(locale_h); use locale; open STDERR, ">/var/tmp/errors1"; my $newsurl='http://news.chgk.info/'; +my $usewas=0; my $cashednumber=500; -my $outputbumber=10; +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/ ) { + my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs"; + Redirect ($u); + exit; +} my $thislocale; if ($^O =~ /win/i) { $thislocale = "Russian_Russia.20866"; @@ -52,8 +64,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"; @@ -80,6 +92,29 @@ sub NewEnough { } # 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); @@ -98,6 +133,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) = @_; @@ -123,7 +203,6 @@ sub GetQuestion { sub GetTourQuestions { my ($dbh, $ParentId) = @_; my (@arr, @Questions); - my ($sth) = $dbh->prepare("SELECT QuestionId FROM Questions WHERE ParentId=$ParentId order by Number"); @@ -170,13 +249,17 @@ sub count sub printform { - my $qnumber=(" "x10)."Выводить по ". textfield(-name=>'kvo', - -default=>param('kvo')||$outputbumber, - -size=>3, - -maxlength=>5)." вопросов"; + 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], @@ -189,7 +272,7 @@ sub printform $checked{'all'}=$all?"checked":""; $checked{'any'}=$all?"":"checked"; $checked{lc param('metod')}="checked"; - $checked{'russian'}=1 unless $checked{'russian'} || $checked{'old'}; + $checked{'rus'}=1 unless $checked{'rus'} || $checked{'old'}; ################################################# return @@ -323,7 +406,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]}=(); @@ -354,7 +436,6 @@ print "$query",br if $printqueries; my $kvo=0; push @$allnf, @{$_} foreach @nf; - print "nf=@$allnf" if $printqueries; foreach $i (0..$#w) #запросы в базу... { @@ -364,11 +445,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; @@ -380,7 +460,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; @@ -428,7 +507,6 @@ print "$query\n",br if $printqueries; } } #foreach $i -#print "keys tasksof", join ' ', keys %{$tasksof{0}}; #Ищем пересечение или объединение списков вопросов (значений %tasksof) foreach $sf (keys %tasksof) { @@ -438,7 +516,6 @@ print "$query\n",br if $printqueries; keys %count) ; -print "\n\$#tasks=",$#tasks,br if $printqueries; ############ Сортировка найденных вопросов foreach (keys %wordsof) @@ -451,11 +528,7 @@ foreach (keys %wordsof) ############ -print "tasks=@tasks" if $printqueries; -#print "$_ $relevance{$_} | " foreach @tasks; -#print br; -print "allnf=@$allnf",br if $printqueries; return @tasks; } @@ -544,7 +617,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 @@ -576,7 +648,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) @@ -585,7 +656,6 @@ print $query if $printqueries; push @Questions, $arr[0] unless $forbidden{$arr[0]}; } $sth->finish; -print "@Questions" if $printqueries; return @Questions; } @@ -620,13 +690,12 @@ sub NoCase { sub PrintList { my ($dbh,$Questions,$shablon,$was)=@_; - + my $Output; my $first=param('first') ||1; - my $kvo=param('kvo') ||$outputbumber; - $first=$first-($first-1)%$kvo; + $first=$first-($first-1)%$outputkvo; my $fkvo=param('fkvo')||($#$Questions+1); - my $last=$first+$kvo-1; + my $last=$first+$outputkvo-1; $last=$fkvo if $fkvo<$last; my($f,$l); my $nav=''; @@ -635,93 +704,98 @@ sub PrintList { $qs=~s/\&first\=[^\&]+//g; my $sstr=param('sstr')||''; $qs=~s/sstr=[^\&]+/sstr=$sstr/; - $qs=~s/\&was=[^\&]+//; - $qs.="&was=$was" if $was; - $qs.="&fkvo=$fkvo" if $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=".($first-$outputkvo))},"<").(" "x4) } else {$nav.=' 'x15;} my ($fprint,$lprint); - my $llprint=$fkvo- ($fkvo)%$kvo+1; # - if ($fkvo<=$kvo*7) + my $llprint=$fkvo- ($fkvo)%$outputkvo+1; # + if ($fkvo<=$outputkvo*7) { $fprint=1; $lprint=$llprint; } - elsif ($first>$kvo*3 && $fkvo-$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; +# 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")."] ";} } - if ($lprint+$kvo<$fkvo) + if ($lprint+$outputkvo<$fkvo) { $nav.= (" "x4). - a({href=>(url."?".$qs."\&first=".($first+$kvo))},">").(" "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..$last]; + 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; $was=0 if $metod eq 'proxy'; - if ($was && ($metod ne 'proxy')) + if ($usewas && $was && ($metod ne 'proxy')) { my $sth=$dbh->prepare ("select sstr,questions,allnf from lastqueries where id=".param('was')); $sth->execute; @@ -731,25 +805,28 @@ sub PrintSearch { @allnf=unpack 'L*',$nf; $sth->finish; } - if (!$was || ($metod eq 'proxy') || (param('first')+param('kvo')>$cashednumber)) + if (!$was || ($metod eq 'proxy') || (param('first')+$outputkvo>$cashednumber)) { @Questions=&Search($dbh, \$sstr,$metod,$all,\@allnf); + $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; @@ -760,7 +837,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; @@ -773,7 +849,6 @@ print "$query" if $printqueries; $shablon=~s/[её]/\[ЕЁ\]/gi; # $shablon=~s/([йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ])/&NoCase($1)/ge; $shablon=qr/$shablon/i; - print "!$shablon!",br if $printqueries; } @@ -787,7 +862,7 @@ print "$query" if $printqueries; $suffix = 'я'; } - print p({align=>"center"}, "Результаты поиска на " . strong($sstr) + $Output.= p({align=>"center"}, "Результаты поиска на " . strong($sstr) . " : $hits попадани$suffix."); if (param('word')) { @@ -804,13 +879,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); @@ -820,19 +898,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; } } @@ -1010,6 +1093,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'} !~ /Т/); @@ -1026,10 +1110,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'}) { @@ -1097,24 +1183,37 @@ 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'}; + 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=>url . "?tour=$fname"}, $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=>url . "?tour=$fname.$Question{TourNumber}#$qnum"}, $Question{'TourTitle'}))); } $output .= dl(strong($titles)); } @@ -1132,38 +1231,38 @@ sub PrintQuestion { if ($Question{'Authors'} ) { my $q=$Question{'Authors'}; ###АВТОРА!! - 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; - } - } - } +# 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); } @@ -1253,9 +1352,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; @@ -1378,6 +1479,7 @@ sub PrintQOfAuthor { my ($dbh, $id) = @_; + my $Output=''; unless ($id=~/^\d+$/) { $id=$dbh->quote($id); my $sth = $dbh->prepare("SELECT Id FROM Authors WHERE CharId=$id"); @@ -1408,9 +1510,8 @@ sub PrintQOfAuthor } else { $suffix = 'я'; } -# print h2("Поиск в базе вопросов"); - print printform; - print p({align=>"center"}, "Автор ".strong("$name $surname. ") + $Output.= printform; + $Output.= p({align=>"center"}, "Автор ".strong("$name $surname. ") . " : $hits попадани$suffix."); @@ -1418,7 +1519,7 @@ sub PrintQOfAuthor # $output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1); # print $output; # } - PrintList($dbh,\@Questions,'gdfgdfgdfgdfg'); + $Output.=PrintList($dbh,\@Questions,'gdfgdfgdfgdfg'); } @@ -1463,11 +1564,10 @@ a({href=>url."?authors=surname"},"фамили { my ($id,$name,$surname,$kvo)=@$arr; - if (!$name || !$surname) {#print "Opanki at $id\n" + if (!$name || !$surname) { } else { my $add=Tr(td([a({href=>url."?qofauthor=$id"},"$name $surname"), $kvo]))."\n"; - print STDERR $add; $output.=$add; } } @@ -1634,15 +1734,22 @@ MAIN: setlocale(LC_CTYPE,'russian'); my($i, $tour); my($text) = (param('text')) ? 1 : 0; + if (param('qid') && (param('qid')=~/^\d+$/)) { + my $destination='http://db.chgk.info/search.html'; +# print header (-'Content-Type' => 'text/html', +# -'Location:'=> 'http:\\db.chgk.info'); + Redirect($destination); + exit + } + if ($text) { 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"); + print &Include_virtual("../dimrub/db/reklama.html") if $url!~/localhost/; print end_html; die "Can't connect to DB chgk\n"; }; @@ -1650,10 +1757,11 @@ MAIN: if (param('qid')) { my $sth; my $qid=param('qid'); - if ($qid !~ /^[0-9]*$/) { +# if ($qid !~ /^[0-9]+$/) + { my ($fname,$t,$n)= split /\./ , $qid; $n=$t,$t='' unless $n; - if ($n) + if ($t) { $sth = $dbh->prepare( "SELECT t2.Id FROM Tournaments as t1, @@ -1676,18 +1784,19 @@ MAIN: Questions.Number=$n"); $sth->execute; $qid = ($sth->fetchrow)[0]; - } - my $query="SELECT Question, Answer from Questions where QuestionId=$qid"; - $sth=$dbh->prepare($query); - $sth->execute; - $sstr= join ' ',$sth->fetchrow; - $sth->finish; - $searchin{'Question'}=1; - $searchin{'Answer'}=1; - $sstr=~tr/ёЁ/еЕ/; - $sstr=~s/[^йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮa-zA-Z0-9]/ /gi; - $proxysstr=$sstr; - $proxysstr=makeproxysstr($dbh,\$proxysstr); + my $query="SELECT Question, Answer from Questions where QuestionId=$qid"; + $sth=$dbh->prepare($query); + $sth->execute; + $sstr= join ' ',$sth->fetchrow; + $sth->finish; + $searchin{'Question'}=1; + $searchin{'Answer'}=1; + $sstr=~tr/ёЁ/еЕ/; + $sstr=~s/[^йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮa-zA-Z0-9]/ /gi; + $proxysstr=$sstr; + $proxysstr=makeproxysstr($dbh,\$proxysstr); + } + } @@ -1696,18 +1805,30 @@ MAIN: if ($proxysstr||$sstr); $title||="База вопросов"; - print start_html(-"title"=>$title, + $globaloutput.=start_html(-"title"=>$title, -author=>'dimrub@icomverse.com', -bgcolor=>'#fff0e0', -vlink=>'#800020'); -print "\n"; - print &Include_virtual("../dimrub/db/reklama.html"); + $globaloutput.=&Include_virtual("../dimrub/db/reklama.html"); } + if (length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)/i) { + my $sth=$dbh->prepare("SELECT page,times,t from hash where query=".$dbh->quote($qs)); + $sth->execute(); + my ($p,$times,$t)=$sth->fetchrow(); + $sth->finish; + if ($p) { + print ".$p"; + $dbh->disconnect; + exit ; + } + } + @@ -1765,52 +1886,46 @@ Content-type: text/$mime_type; charset=" EOT print F &PrintRandom($dbh, $type, $qnum, $text); close F; - print "Пакет случайно выбранных вопросов послан по адресу $Email. Нажмите + $globaloutput.= "Пакет случайно выбранных вопросов послан по адресу $Email. Нажмите на Reload для получения еще одного пакета"; } else { - print &PrintRandom($dbh, $type, $qnum, $text); + $globaloutput.= &PrintRandom($dbh, $type, $qnum, $text); } } elsif (param('authors')){ - print &PrintAuthors($dbh,param('authors')); + $globaloutput.= &PrintAuthors($dbh,param('authors')); } elsif (param('qofauthor')){ - &PrintQOfAuthor($dbh,param('qofauthor')); + $globaloutput.= &PrintQOfAuthor($dbh,param('qofauthor')); } elsif (param('sstr')||param('was')) { - &PrintSearch($dbh, $sstr||' ', param('metod')||'',param('was')); + $globaloutput.=&PrintSearch($dbh, $sstr||' ', param('metod')||'',param('was')); $dbh->do("delete from lastqueries where (TO_DAYS(NOW()) - TO_DAYS(t) >= 2) OR - (time_to_sec(now())-time_to_sec(t) >3600)") + (time_to_sec(now())-time_to_sec(t) >3600)") if $usewas && random(30)==0; } elsif (param('qid')) { - &PrintSearch($dbh, $sstr||'', 'proxy'); + $globaloutput.=&PrintSearch($dbh, $sstr||'', 'proxy'); } elsif (param('getfile')){ - print &writefile + $globaloutput.=&writefile } elsif (param('all')) { - print &PrintAll($dbh, 0); + $globaloutput.=&PrintAll($dbh, 0); } elsif (param('from_year') && param('to_year')) { - print &PrintDates($dbh); + $globaloutput.=&PrintDates($dbh); } elsif (param('comp')) { - 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"; -# print header( -# -'Content-Type' => 'application/x-zip-compressed; name="db.zip"', -# -'Content-Type' => 'application/zip', -# -'Content-Disposition' => 'attachment; filename="db.zip"' -# ); + $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"; $tour ||= 0; my (@files) = &PrintArchive($dbh, $tour); WriteFile($dbh,$_) foreach @files; -# open F, "$ZIP -j - $SRCPATH/COPYRIGHT @files |"; open F, "$ZIP -j - @files |"; binmode(F); binmode(STDOUT); - print (); + $globaloutput.= join "",; close F; $dbh->disconnect; exit; @@ -1835,15 +1950,15 @@ EOT $QuestionNumber=($sth->fetchrow)[0]||0; } if ($QuestionNumber) { - print &PrintQuestion($dbh, $QuestionNumber, param('answer')||0, $qnum, 1); + $globaloutput.= &PrintQuestion($dbh, $QuestionNumber, param('answer')||0, $qnum, 1,0,0); # $dbh, $Id, $answer, $qnum, $title, $text } else { - print &PrintTournament($dbh, $tour, param('answer')); + $globaloutput.=&PrintTournament($dbh, $tour, param('answer')); } } if (!$text) { - print &Include_virtual("../dimrub/db/footer.html"); - print p."
Обновление: ".&Include_virtual("../dimrub/db/date")."
"; + $globaloutput.=&Include_virtual("../dimrub/db/footer.html"); + $globaloutput.=p."
Обновление: ".&Include_virtual("../dimrub/db/date")."
"; print < function toggle(e) { @@ -1856,7 +1971,15 @@ function toggle(e) { EEE ; -# print end_html; + print end_html; + } + 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; }