--- db/prgsrc/db.cgi 2003/08/29 21:52:25 1.124 +++ db/prgsrc/db.cgi 2004/03/17 10:26:31 1.132 @@ -2,23 +2,34 @@ use DBI; use CGI ':all'; -use strict; +#use strict; use Time::Local; +my $proxyredirect=1; use POSIX qw(locale_h); use locale; use vars qw($opt_z); use Getopt::Std; +#my ($dbuser,$dbname,$dbpass,$dbhost); +require "dbdefs.pl"; +my $url=url||''; +$dbuser||="piataev"; +$dbname||="chgk"; +$dbpass||=""; +$dbhost||="localhost"; getopts('z'); $opt_z||=param("makehtml"); my $timestamp="_timestamp.tmp"; +my $usehash=0; 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"; - +$footer="../../chgk/footer.html" if $url=~/zaba/; +$reklama="../../chgk/reklama.html" if $url=~/zaba/; my $datefooter="../dimrub/db/date"; +$datefooter="../../chgk/date" if $url=~/zaba/; my $fname; $reklama="../reklama.html" if $opt_z; @@ -33,28 +44,39 @@ if ($^O =~ /win/i) { { $realHTMLDIR="/home/piataev/public_html/dimrub/db/files/"; } + + my $usehtml=$opt_z||0; $usehtml=1; +$usehtml=0 if $url=~/zaba/; + 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 $metod=param('metod')||''; 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/) { +if ($url !~ /db\.chgk\.info/ && $url !~ /localhost/ && $url !~ /bilbo/ && $url !~ /zaba/) { my $u="http://db.chgk.info/cgi-bin/db.cgi?$qs"; Redirect ($u); exit; } + +if ($proxyredirect && $metod=~/proxy/ && $url !~ /localhost/ && $url !~ /bilbo/ && $url !~ /zaba/) { + my $u="http://chgk.zaba.ru/cgi-bin/db.cgi?$qs"; + Redirect ($u); + exit; +} + #if (!param('sstr') && param('all')) { # my $destination='http://db.chgk.info/all.html'; # Redirect($destination); @@ -93,6 +115,14 @@ my %TypeName=('children'=>'Д', 'game'=>' 'chgk'=>'Ч', 'brain'=>'Б', 'beskrylka'=>'Л','ehruditka'=>'Э'); +sub countz { + my ($dbh,$type)=@_; + + my $sth=$dbh->prepare("select count(*) from Questions where Type LIKE '%$type%'"); + $sth->execute(); + my ($tmp)=$sth->fetchrow(); + return $tmp; +} my $all=param('all'); $all=0 if lc $all eq 'no'; @@ -260,8 +290,9 @@ sub tourhref { return $res; } else { $res=$url; - $res.="?tour=$t"; - $res.=$a?"&answers=1":""; + $res.=$a?"?answers=1&":"?"; + $res.="tour=$t"; + return $res; } @@ -398,6 +429,9 @@ action="/znatoki/cgi-bin/db.cgi"> +

Если при попытке поиска выдаётся сообщение об ошибке, +попробуйте воспользоваться +зеркалом базы EOT @@ -436,7 +470,7 @@ sub makeproxysstr { # $good{$words[$_]}=1 foreach 0..4; foreach (@words) { - $good{$_}=1 if $c{$_}<200; + $good{$_}=1 if $c{$_}<200 && length $_>2; } $good{$words[$_]}=0 foreach 16..$#words; @@ -738,7 +772,7 @@ sub makewhere { $type .= ($_=$TypeName{$_}) foreach @type; my $where=' 0 '; foreach (@type) { - $where.= " OR (Type ='$_') OR (Type ='$_Д') "; + $where.= " OR (Type ='$_') OR (Type ='$_Д') OR (Type ='Д$_') "; } $where.= "OR (Type='ЧБ')" if ($type=~/Ч|Б/); return $where; @@ -966,9 +1000,11 @@ sub PrintRandom { my %q; my $answer=$razd?0:1; my @answers; +#my $t=time; my (@Questions) = &Get12Random($dbh, $type, $num); - my ($output, $i) = ('', 0); + my ($output, $i) = ('', 0); +#$output.="time=".(time-$t).p; if ($text) { $output .= " $num случайных вопросов.\n\n"; } else { @@ -1050,9 +1086,20 @@ sub PrintTournament { }; } } else { - my ($qnum) = GetQNum($dbh); - $output .= h2("Банк Вопросов: $qnum вопрос".&Suffix($qnum)) - . p . "\n"; + my ($qnum) = GetQNum($dbh,0); + my ($qnum1) = GetQNum($dbh,1); + $output .= h2("База вопросов"). + h3("$qnum запис".&Suffix2($qnum). + " (уникальных $qnum1)"); + +# h4("". +# Tr(td("Из них:"), td("Вопросов ЧГК: ".countz($dbh,'Ч'))). +# Tr(td(" "), td("Вопросов для брейна: ".countz($dbh,'Б'))). +# Tr(td(" "), td("Вопросов для ЧГК и брейна: ".countz($dbh,'ЧБ'))). +# Tr(td(" "), td("Интернет-вопросов: ".countz($dbh,'И'))). +# Tr(td(" "), td("Бескрылок: ".countz($dbh,'Л'))). +# Tr(td(" "),td("Заданий для Своей Игры: ".countz($dbh,'Я'))). +# Tr(td(" "),td("Эрудиток: ".countz($dbh,'Э'))))."
" } for ($i = 0; $i <= $#Tours; $i++) { @@ -1143,6 +1190,23 @@ sub Suffix { return $suffix; } + +sub Suffix1 { + my ($qnum) = @_; + my ($suffix) = 'я' if $qnum =~ /[234]$/; + $suffix = 'е' if $qnum =~ /1$/; + $suffix = 'й' if $qnum =~ /[567890]$/ || $qnum =~ /1.$/; + return $suffix; +} + +sub Suffix2 { + my ($qnum) = @_; + my ($suffix) = 'и' if $qnum =~ /[234]$/; + $suffix = 'ь' if $qnum =~ /1$/; + $suffix = 'ей' if $qnum =~ /[567890]$/ || $qnum =~ /1.$/; + return $suffix; +} + sub IsTour { my ($dbh, $Id,$n) = @_; @@ -1249,8 +1313,10 @@ sub PrintField { $value =~ s/<[\/\w]*?>//sg; } else { $value =~ s/^\s+/
    /mg; + $value =~ s/(\s+)-+(\s+)/$1–$2/mg; + $value =~ s/\s+\–/ \–/mg + if $value !~ /^\|/; $value =~ s/^\|([^\n]*)/

$1<\/pre>/mg;
-	    $value =~ s/\s+-+\s+/ – /mg;
 	    $value =~ s/(http:\/\/\S+[^\s\)\(\,\.])/$1<\/a>/g if $header !~ /^Авто/;
 #	    $value =~ s/(http:\/\/(?:\w+.)+[\w\\\~]+(\?[^\s.]+)?)/$1<\/a>/g if $header !~ /^Авто/;
 #	    $value =~ s/(\s)"/$1“/mg;
@@ -1405,12 +1471,22 @@ qid=$qid"}, 'Близкие вопросы').p
 
 # Returns the total number of questions currently in the DB.
 sub GetQNum {
-	my ($dbh) = @_;
+	my ($dbh,$x) = @_;
 	my ($sth) = $dbh->prepare("SELECT COUNT(*) FROM Questions");
 	$sth->execute;
 	my $tmp=($sth->fetchrow)[0];
         $sth->finish;
- 	return $tmp;
+	($sth)=  $dbh -> prepare("select distinct count(first) FROM equalto");
+	           $sth -> execute;
+	my ($c)=$sth->fetchrow;
+
+#	           $i++  while ( my  ($first, $second)=$sth -> fetchrow)
+#                  {
+#                       $forbidden{$first}=1;
+#                  }
+                  $sth->finish;
+
+ 	return $tmp-($x?$c:0);
 }
 sub GetMaxQId {
 	my ($dbh) = @_;
@@ -1431,7 +1507,9 @@ sub Get12Random {
 	srand;
 	my $where=0;
 	my $r=int (rand(10000));
-
+	my $w1=$r<5000? "QuestionId<50000 ":"QuestionId>=50000";
+	$w1=1 if $url=~/zaba/;
+	$w1=1 if $type!~/Ч/;
 	foreach (split '', $type)
         {
  	   $where.= " OR (Type ='$_') OR (Type ='$_Д') ";
@@ -1441,7 +1519,7 @@ sub Get12Random {
 #   $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";
+   $q="select QuestionId from Questions where ($w1) AND ($where) order by rand() limit $num";
 
 
    $sth=$dbh->prepare($q);
@@ -1858,9 +1936,11 @@ MAIN:
 	$tour = (param('tour')) ? param('tour') : 0;
 	my $texttour=$tour;
 	my ($sth,$dbh);
-	$dbh = DBI->connect("DBI:mysql:chgk", "piataev", "")
+	my($dsn) = "DBI:mysql:database=$dbname;host=$dbhost";
+        $dbh = DBI->connect($dsn, $dbuser, $dbpass) 
+#	$dbh = DBI->connect("DBI:mysql:$dbname", $username, $dbpass)
 		or do {
-			print h1("Временные проблемы") . "База вопросов временно не
+			print header.h1("Временные проблемы") . "База вопросов временно не
 			работает. Заходите попозже.";
 			print &Include_virtual("$reklama") if $url!~/localhost/;
 		    print end_html;
@@ -1869,14 +1949,14 @@ MAIN:
 
 
 	if (param('qid') && (param('qid')=~/^\d+$/) || $tour && $tour=~/^\d+$/) {
-	   my $destination='http://db.chgk.info/search.html';
+	   my $destination='http://chgk.zaba.ru/search.html';
 #		print header (-'Content-Type' => 'text/html',
 #                -'Location:'=> 'http:\\db.chgk.info');
 		Redirect($destination);
                 exit
 	}
 
-	if ($tour && (!param('answers')||!param('answers')<=1))
+	if ($tour && !param('qnumber') && (!param('answers')||(param('answers')<=1)))
 	{       
 	        my $n=param('tour');
 	        $n=~s/.txt$//;        
@@ -1885,14 +1965,13 @@ MAIN:
 		my $d=$destination;
 		$d=~s/$HTMLDIR/$realHTMLDIR/;
 #		print header.$destination;
-                print header."$d|".(-e "$realHTMLDIR$n.html");
+#                print header."$d|".(-e "$realHTMLDIR$n.html");
                 if (-e $d) {
 			Redirect($destination);
 			exit
 		}
 		$d=~s/\.\d+//;
 		$destination=~s/\.\d+//;
-print br.br.$d;
                 if (-e $d) {
 			Redirect($destination);
 			exit
@@ -1925,7 +2004,8 @@ print br.br.$d;
 
 	if ($text && !param ('comp')) {
 		print header('text/plain');
-	} elsif (!param('comp')) {print header;}
+	} elsif (!param('comp')) {
+		print header(-charset =>'koi8-r')}
 	  my $sstr=param('sstr');
           $opt_z||=param("makehtml");
 	  if (param('qid')) {
@@ -1979,11 +2059,10 @@ print br.br.$d;
 	   my $title="Результаты поиска на \"". ($proxysstr||$sstr) .'"' 
                                                     if ($proxysstr||$sstr);
            $title||="База вопросов";
-
 	   $globaloutput.=start_html(-"title"=>$title,
 	           -author=>'dimrub@icomverse.com',
 	           -bgcolor=>'#fff0e0',
-				  -vlink=>'#800020');
+				  -vlink=>'#800020');#}
 $globaloutput.="