--- db/prgsrc/db.cgi 2003/07/05 00:32:03 1.122 +++ db/prgsrc/db.cgi 2004/03/11 07:14:14 1.130 @@ -8,9 +8,16 @@ use POSIX qw(locale_h); use locale; use vars qw($opt_z); use Getopt::Std; +my ($dbuser,$dbname,$dbpass,$dbhost); +require "dbdefs.pl"; +$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"; @@ -33,28 +40,39 @@ if ($^O =~ /win/i) { { $realHTMLDIR="/home/piataev/public_html/dimrub/db/files/"; } +my $url=url||''; + 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 ($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 +111,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'; @@ -176,12 +202,12 @@ sub GetTournament { sub fetchquestion { my ($sth,$q,$WithTour)=@_; if ($WithTour) { - ($$q{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, + ($$q{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'},$$q{'Sources'}, $$q{'Number'}, $$q{'Title'}, $$q{'TourTitle'}, $$q{'FileName'},$$q{'PlayedAt'},$$q{'TourNumber'}) = $sth->fetchrow; } else { - ($$q{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'}, + ($$q{'QuestionId'}, $$q{'Question'},$$q{'Answer'},$$q{'Comments'},$$q{'Authors'},$$q{'Sources'}, $$q{'Number'})= $sth->fetchrow; } @@ -203,13 +229,13 @@ sub SelectQuestions { my $query; if ($WithTour) { - $query="SELECT QuestionId, Questions.Question, Answer, Comments, Authors, + $query="SELECT QuestionId, Questions.Question, Answer, Comments, Authors, Sources, 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 QuestionId, Questions.Question, Answer, Comments, Authors, + $query="SELECT QuestionId, Questions.Question, Answer, Comments, Authors, Sources, Questions.Number from Questions WHERE $where"; } @@ -738,7 +764,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 +992,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 +1078,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 +1182,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 +1305,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 +1463,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 +1499,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 +1511,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);
@@ -1554,8 +1624,19 @@ sub PrintDates {
 		next
 			if (!$array[0]);
 		%Tournament = &GetTournament($dbh, $array[0]);
+	         my $textid;
+		 if ($textid=$Tournament{'FileName'})
+		 {
+		   $textid=~s/\.txt//;
+		 }
+		 elsif ($textid=$Tournament{'Number'})
+		 {
+		      $fname=~s/\.txt//;
+		      $textid="$fname.$textid";
+		 }
+	         else {$textid=$Tournament{'Id'}};
       $list .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) .
-      " " . a({href=>tourhref($Tournament{'FileName'},0,1)},
+      " " . a({href=>tourhref($textid,0,1)},
       $Tournament{'Title'}, $Tournament{'PlayedAt'}||''));
 	}
         $sth->finish;
@@ -1847,9 +1928,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;
@@ -1858,14 +1941,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$//;        
@@ -1914,7 +1997,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')) {
@@ -1981,7 +2065,7 @@ th	{font-size: x-small; font-family : sa
 		$globaloutput.=&Include_virtual("$reklama")||'';
 	}
 
-       if (!$opt_z && length ($qs)<=255 && $qs !~ /(sstr)|(rand)|(comp)|(all=)/i) {
+       if ($usehash && !$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();
@@ -2127,7 +2211,7 @@ EOT
 #                     FROM Tournaments as t1, Tournaments as t2
 #		     WHERE t1.CreatedAt>$d AND t2.ParentId=t1.Id GROUP BY t1.Id");
 my ($sth) = $dbh->prepare("SELECT t1.Id, t1.FileName, t1.Type, count(t2.Id)  FROM Tournaments as t1  LEFT JOIN Tournaments as t2 
- ON t2.ParentId=t1.id WHERE t1.CreatedAt>$d GROUP BY t1.Id");
+ ON t2.ParentId=t1.id WHERE t1.CreatedAt>=$d GROUP BY t1.Id");
 		  $sth->execute;
 		  my ($Id,$fname,$type,$c);
 		  while (($Id,$fname,$type,$c)=$sth->fetchrow,$Id) {
@@ -2166,7 +2250,7 @@ my ($sth) = $dbh->prepare("SELECT t1.Id,
 	}
         if (!$opt_z){ 
 	  print $globaloutput;
-	  if (($qs!~ /(rand)|(sstr)|(comp)/i) && (length $qs<=255)) {
+	  if (($qs!~ /(rand)|(sstr)|(comp)/i) && (length $qs<=255) && $usehash) {
 		$globaloutput=  $dbh->quote($globaloutput);
 		$dbh->do("insert into hash (query,page) values (".
 		$dbh->quote($qs).