--- db/prgsrc/db.cgi 2001/12/05 01:55:21 1.44
+++ db/prgsrc/db.cgi 2001/12/21 11:54:37 1.53
@@ -2,15 +2,17 @@
use DBI;
use CGI ':all';
-use Text::Query;
+#use Text::Query;
use strict;
use Time::Local;
use POSIX qw(locale_h);
use locale;
-#open STDERR, ">errors1";
+open STDERR, ">errors1";
my $printqueries=0;
my %forbidden=();
my $debug=0; #added by R7
+if (param('debug')) {$debug=1; $printqueries=1}
+*STDERR=*STDOUT if $debug;
my %fieldname= (0,'Question', 1, 'Answer', 2, 'Comments', 3, 'Authors', 4, 'Sources');
my %rusfieldname=('Question','Вопрос', 'Answer', 'Ответ',
'Comments', 'Комментарии', 'Authors', 'Автор',
@@ -28,11 +30,11 @@ my $Ll=qr/(?:[A-Z])|(?:${RL})/;
my $thislocale;
$searchin{$_}=1 foreach param('searchin');
-#$searchin{'Question'}=param('Question');
-#$searchin{'Answer'}=param('Answer');
-#$searchin{'Comments'}=param('Comments');
-#$searchin{'Authors'}=param('Authors');
-#$searchin{'Sources'}=param('Sources');
+my %TypeName=('children'=>'Д', 'game'=>'И',
+ 'chgk'=>'Ч', 'brain'=>'Б', 'beskrylka'=>'Л','ehruditka'=>'Э');
+
+
+
my $all=param('all');
$all=0 if lc $all eq 'no';
my ($PWD) = `pwd`;
@@ -73,7 +75,7 @@ sub GetTournament {
foreach $name (@{$sth->{NAME}}) {
$Tournament{$name} = $arr[$i++];
}
-
+ $sth->finish;
return %Tournament;
}
@@ -94,6 +96,7 @@ sub GetQuestion {
$Question{$name} = $arr[$i++];
}
+ $sth->finish;
return %Question;
}
@@ -103,7 +106,7 @@ sub GetTourQuestions {
my (@arr, @Questions);
my ($sth) = $dbh->prepare("SELECT QuestionId FROM Questions
- WHERE ParentId=$ParentId ORDER BY QuestionId");
+ WHERE ParentId=$ParentId");
$sth->execute;
@@ -111,6 +114,7 @@ sub GetTourQuestions {
push @Questions, $arr[0];
}
+ $sth->finish;
return @Questions;
}
@@ -127,7 +131,7 @@ sub GetTours {
while (@arr = $sth->fetchrow) {
push @Tours, $arr[0];
}
-
+ $sth->finish;
return @Tours;
}
@@ -141,6 +145,7 @@ print "timeb=".time.br if $debug;
$sth->execute;
my @a=$sth->fetchrow;
print "timee0=".time.br if $debug;
+ $sth->finish;
$a[0]||0;
}
@@ -151,7 +156,13 @@ sub printform
my $submit=submit(-value=>'Поиск');
my $inputstring=textfield(-name=>'sstr',
-default=>param('sstr')||'',
- -size=>50);
+ -size=>30,
+ -maxlength=>30);
+ my $qnumber="Выводить по".br. textfield(-name=>'kvo',
+ -default=>param('kvo')||'150',
+ -size=>3,
+ -maxlength=>5). br."вопросов";
+
my @df=keys %searchin;
@df=('Question', 'Answer') unless @df;
my $fields=checkbox_group('searchin',['Question','Answer','Comments','Authors','Sources'], [@df],
@@ -174,7 +185,8 @@ table(Tr
(
td({-valign=>'TOP'},$inputstring.$submit.p."Метод: $metod".p."Слова: $all"),
td({-valign=>'TOP'},(' 'x 8).'Поля:'),
- td({-valign=>'TOP'},$fields)
+ td({-valign=>'TOP'},$fields), td(" "x5),
+ td({-valign=>'TOP'},$qnumber)
)
)
@@ -265,6 +277,7 @@ print "$query",br if $printqueries;
{
push (@{$nf[$i]},$arr[0])
}
+ $sth->finish;
}
@@ -311,6 +324,7 @@ print STDERR "!$query\n",br if $printque
{
@blob=(@blob,unpack 'C*',$arr[0]);
}
+ $sth->finish;
$query="select number from nf where ".(join ' OR ', @arr1);
print "$query\n",br if $printqueries;
$sth=$dbh -> prepare($query);
@@ -320,7 +334,7 @@ print "$query\n",br if $printqueries;
{
$frequence[$i]+=$arr[0];
}
-
+ $sth->finish;
if (@blob < 4)
@@ -441,17 +455,17 @@ sub Search {
my $sstr=$$s;
my (@arr, @Questions, @fields);
my (@sar, $i, $sth,$where,$query);
- my $ip=$ENV{'REMOTE_ADDR'};
+# 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);
+# $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);
@@ -527,7 +541,7 @@ print $query if $printqueries;
while (@arr = $sth->fetchrow) {
push @Questions, $arr[0] unless $forbidden{$arr[0]};
}
-
+ $sth->finish;
print "@Questions" if $printqueries;
return @Questions;
}
@@ -537,7 +551,7 @@ print "@Questions" if $printqueries;
пП рР сС тТ уУ фФ хХ цЦ чЧ шШ щЩ ьЬ ыЫ эЭ юЮ яЯ/;
sub NoCase {
- my ($sstr) = shift;
+ my ($sstr) = shift;
my ($res);
if (($res) = grep(/$sstr/, @letters)) {
@@ -551,7 +565,7 @@ sub PrintList {
my ($dbh,$Questions,$shablon)=@_;
my $first=param('first') ||1;
- my $kvo=param('kvo') ||30;
+ my $kvo=param('kvo') ||150;
$first=$first-($first-1)%$kvo;
my $last=$first+$kvo-1;
@@ -561,8 +575,8 @@ sub PrintList {
my $qs=query_string;
$qs=~s/\;/\&/g;
$qs=~s/\&first\=[^\&]+//g;
-
-
+ my $sstr=param('sstr');
+ $qs=~s/sstr=[^\&]+/sstr=$sstr/;
if ($first>$kvo*3+1)
{
$nav.=
@@ -719,6 +733,13 @@ sub PrintRandom {
return $output;
}
+sub PrintEditor {
+ my $t=shift; #ссылка на Хэш с полями
+ my $ed=$$t{'Editors'};
+ my $edname=($ed=~/\,/ ) ? "Редакторы" : "Редактор" ;
+ return h4({align=>"center"},"$edname: $ed" );
+}
+
sub PrintTournament {
my ($dbh, $Id, $answer) = @_;
my (%Tournament, @Tours, $i, $list, $qnum, $imgsrc, $alt,
@@ -732,7 +753,7 @@ sub PrintTournament {
my ($Copyright) = $Tournament{'Copyright'};
@Tours = &GetTours($dbh, $Id);
-
+ $list='';
if ($Id) {
for ($Tournament{'Type'}) {
/Г/ && do {
@@ -752,6 +773,7 @@ sub PrintTournament {
$output .= h2({align=>"center"},
"$title") . p . "\n";
+ $output.=&PrintEditor(\%Tournament);
last;
};
/Т/ && do {
@@ -818,10 +840,11 @@ sub PrintTournament {
$output .= p("Копирайт: " . $Copyright);
}
+
+
if ($Info) {
$output .= p($Info);
}
-
return $output;
}
@@ -861,6 +884,7 @@ sub PrintTour {
$Tournament{'PlayedAt'},
"
", $Tour{"Title"} .
" ($qnum вопрос$suffix)\n") . p;
+ $output .=&PrintEditor(\%Tour);
my (@Questions) = &GetTourQuestions($dbh, $Id);
for ($q = 0; $q <= $#Questions; $q++) {
@@ -958,31 +982,31 @@ 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);
-
- 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.Id,Name, Surname, Nicks from Authors, A2Q
+# where Authors.Id=Author And Question=$Id");
+# $sth->execute;
+# my ($AuthorId,$Name, $Surname,$other,$Nicks);
+
+# 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;
+# }
$output .= &PrintField("Автор(ы)", $q, $text);
@@ -997,8 +1021,8 @@ sub PrintQuestion {
$output .= &PrintField("Комментарии", $Question{'Comments'}, $text);
}
}
- $output.=br.a({href=> url."?metod=proxy&qid=$Id"}, 'Близкие вопросы').p
- if $answer;
+# $output.=br.a({href=> url."?metod=proxy&qid=$Id"}, 'Близкие вопросы').p
+# if $answer;
return $output;
}
@@ -1024,17 +1048,31 @@ sub Get12Random {
my (%chosen);
srand;
- for ($i = 0; $i < $num; $i++) {
- do {
- $q = int(rand($qnum));
- $sth = $dbh->prepare("SELECT Type FROM Questions
- WHERE QuestionId=$q");
- $sth->execute;
- $t = ($sth->fetchrow)[0];
- } until !$chosen{$q} && $t && $type =~ /[$t]/;
- $chosen{$q} = 'y';
- push @questions, $q;
+ my $where=0;
+ my $r=int (rand(10000));
+
+ foreach (split '', $type)
+ {
+ $where.= " OR (Type ='$_') OR (Type ='$_Д') ";
+ }
+ $where.= "OR (Type='ЧБ')" if ($type=~/Ч|Б/);
+
+ $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();
+
+ $sth=$dbh->prepare($q);
+ $sth->execute;
+ while (($i)=$sth->fetchrow)
+ {
+ push @questions,$i;
}
+
+ for ($i=@questions; --$i;){
+ my $j=rand ($i+1);
+ @questions[$i,$j]=@questions[$j,$i] unless $i==$j;
+ }
return @questions;
}
@@ -1123,6 +1161,7 @@ sub PrintDates {
" " . a({href=>url . "?tour=$Tournament{'Id'}&answer=0"},
$Tournament{'Title'}, $Tournament{'PlayedAt'}));
}
+ $sth->finish;
$output .= dl($list);
return $output;
}
@@ -1142,6 +1181,7 @@ sub PrintQOfAuthor
my @Questions;
while (($q)=$sth->fetchrow,$q)
{push @Questions,$q unless $forbidden{$q}}
+ $sth->finish;
my ($output, $i, $suffix, $hits) = ('', 0, '', $#Questions + 1);
@@ -1215,6 +1255,7 @@ a({href=>url."?authors=surname"},"фамили
}
}
$output.="";
+ $sth->finish;
return $output;
}
@@ -1271,17 +1312,18 @@ if ((uc 'а') ne 'А') {print "Koi8-r loca
if (param('rand')) {
my ($type, $qnum) = ('', 12);
- $type .= 'Б' if (param('brain'));
- $type .= 'Ч' if (param('chgk'));
+ $type.=$TypeName{$_} foreach param('type');
+# $type .= 'Б' if (param('brain'));
+# $type .= 'Ч' if (param('chgk'));
$qnum = param('qnum') if (param('qnum') =~ /^\d+$/);
$qnum = 0 if (!$type);
- if (param('email') && -x $SENDMAIL &&
- open(F, "| $SENDMAIL -t -n")) {
- my ($Email) = param('email');
+ my $Email;
+ if (($Email=param('email')) && -x $SENDMAIL &&
+ open(F, "| $SENDMAIL $Email")) {
my ($mime_type) = $text ? "plain" : "html";
print F <Reload для получения еще одного пакета";
} else {
print &PrintRandom($dbh, $type, $qnum, $text);
@@ -1311,6 +1353,7 @@ print $query if $printqueries;
my $sth=$dbh->prepare($query);
$sth->execute;
my $sstr= join ' ',$sth->fetchrow;
+ $sth->finish;
$searchin{'Question'}=1;
$searchin{'Answer'}=1;
$sstr=~tr/ёЁ/еЕ/;
@@ -1353,6 +1396,7 @@ $sstr=~s/[^йцукенгшщзхъфывапролджэячсмит
WHERE FileName = '$tour.txt'");
$sth->execute;
$tour = ($sth->fetchrow)[0];
+ $sth->finish;
}
print &PrintTournament($dbh, $tour, param('answer'));
}