version 1.6, 2000/10/20 04:06:54
|
version 1.20, 2001/05/17 23:31:08
|
Line 2
|
Line 2
|
|
|
use DBI; |
use DBI; |
use CGI ':all'; |
use CGI ':all'; |
|
use Text::Query; |
use strict; |
use strict; |
use Time::Local; |
use Time::Local; |
use POSIX qw(locale_h); |
use POSIX qw(locale_h); |
|
my $debug=1; #added by R7 |
|
open STDERR, ">/tmp/errors" if $debug; |
my ($PWD) = `pwd`; |
my ($PWD) = `pwd`; |
chomp $PWD; |
chomp $PWD; |
my ($SRCPATH) = "$PWD/../dimrub/src"; |
my ($SRCPATH) = "$PWD/../dimrub/src"; |
my ($ZIP) = "/usr/bin/zip"; |
my ($ZIP) = "/home/piataev/bin/zip"; |
|
my $DUMPFILE = "/tmp/chgkdump"; |
my ($SENDMAIL) = "/usr/sbin/sendmail"; |
my ($SENDMAIL) = "/usr/sbin/sendmail"; |
my ($TMSECS) = 30*24*60*60; |
my ($TMSECS) = 30*24*60*60; |
my (%RevMonths) = |
my (%RevMonths) = |
Line 104 sub GetTours {
|
Line 107 sub GetTours {
|
|
|
# Returns list of QuestionId's, that have the search string in them. |
# Returns list of QuestionId's, that have the search string in them. |
sub Search { |
sub Search { |
my ($dbh, $sstr) = @_; |
my ($dbh, $sstr, $metod) = @_; |
my (@arr, @Questions, @fields); |
my (@arr, @Questions, @fields); |
my (@sar, $i, $sth); |
my (@sar, $i, $sth,$where); |
|
my $btime=time; |
|
|
# push @fields, 'Question'; |
# push @fields, 'Question'; |
foreach (qw/Question Answer Sources Authors Comments/) { |
|
|
###Simple and advanced query processing. Added by R7 |
|
if ($metod eq 'simple' || $metod eq 'advanced') |
|
{ |
|
foreach (qw/Question Answer Sources Authors Comments/) { |
if (param($_)) { |
if (param($_)) { |
push @fields, "IFNULL($_, '')"; |
push @fields, $_; |
} |
} |
} |
} |
|
|
@sar = split " ", $sstr; |
@fields=(qw/Question Answer Sources Authors Comments/) unless scalar @fields; |
for $i (0 .. $#sar) { |
my $fields=join ",", @fields; |
|
my $q=new Text::Query($sstr, |
|
-parse => 'Text::Query::'. |
|
(($metod eq 'simple') ? 'ParseSimple':'ParseAdvanced'), |
|
-solve => 'Text::Query::SolveSQL', |
|
-build => 'Text::Query::BuildSQLMySQL', |
|
-fields_searched => $fields); |
|
|
|
$where= $$q{'matchexp'}; |
|
my $query= "SELECT Questionid FROM Questions |
|
WHERE $where"; |
|
print br."Query is: $query".br if $debug; |
|
|
|
$sth = $dbh->prepare($query); |
|
} else |
|
###### |
|
{ |
|
|
|
|
|
foreach (qw/Question Answer Sources Authors Comments/) { |
|
if (param($_)) { |
|
push @fields, "IFNULL($_, '')"; |
|
} |
|
} |
|
|
|
@sar = split " ", $sstr; |
|
for $i (0 .. $#sar) { |
$sar[$i] = $dbh->quote("%${sar[$i]}%"); |
$sar[$i] = $dbh->quote("%${sar[$i]}%"); |
} |
} |
|
|
my($f) = "CONCAT(" . join(',', @fields) . ")"; |
my($f) = "CONCAT(" . join(',', @fields) . ")"; |
if (param('all') eq 'yes') { |
if (param('all') eq 'yes') { |
$sstr = join " AND $f LIKE ", @sar; |
$sstr = join " AND $f LIKE ", @sar; |
} else { |
} else { |
$sstr = join " OR $f LIKE ", @sar; |
$sstr = join " OR $f LIKE ", @sar; |
} |
} |
|
|
$sth = $dbh->prepare("SELECT QuestionId FROM Questions |
$sth = $dbh->prepare("SELECT QuestionId FROM Questions |
WHERE $f LIKE $sstr ORDER BY QuestionId"); |
WHERE $f LIKE $sstr ORDER BY QuestionId"); |
|
|
|
} #else -- processing old-style query (R7) |
|
|
$sth->execute; |
$sth->execute; |
while (@arr = $sth->fetchrow) { |
while (@arr = $sth->fetchrow) { |
push @Questions, $arr[0]; |
push @Questions, $arr[0]; |
} |
} |
|
print br, "Search time: ",time-$btime," sec",br if $debug; |
return @Questions; |
return @Questions; |
} |
} |
|
|
Line 154 sub NoCase {
|
Line 190 sub NoCase {
|
} |
} |
|
|
sub PrintSearch { |
sub PrintSearch { |
my ($dbh, $sstr) = @_; |
my ($dbh, $sstr, $metod) = @_; |
my (@Questions) = &Search($dbh, $sstr); |
my (@Questions) = &Search($dbh, $sstr,$metod); |
my ($output, $i, $suffix, $hits) = ('', 0, '', $#Questions + 1); |
my ($output, $i, $suffix, $hits) = ('', 0, '', $#Questions + 1); |
|
|
if ($hits =~ /1.$/ || $hits =~ /[5-90]$/) { |
if ($hits =~ /1.$/ || $hits =~ /[5-90]$/) { |
Line 175 sub PrintSearch {
|
Line 211 sub PrintSearch {
|
|
|
$sstr =~ s/(.)/&NoCase($1)/ge; |
$sstr =~ s/(.)/&NoCase($1)/ge; |
|
|
my(@sar) = split(/\s/, $sstr); |
my(@sar) = split(' ', $sstr); |
for ($i = 0; $i <= $#Questions; $i++) { |
for ($i = 0; $i <= $#Questions; $i++) { |
$output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1); |
$output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1); |
foreach (@sar) { |
foreach (@sar) { |
Line 464 sub GetQNum {
|
Line 500 sub GetQNum {
|
$sth->execute; |
$sth->execute; |
return ($sth->fetchrow)[0]; |
return ($sth->fetchrow)[0]; |
} |
} |
|
sub GetMaxQId { |
|
my ($dbh) = @_; |
|
my ($sth) = $dbh->prepare("SELECT MAX(QuestionId) FROM Questions"); |
|
$sth->execute; |
|
return ($sth->fetchrow)[0]; |
|
} |
|
|
# Returns Id's of 12 random questions |
# Returns Id's of 12 random questions |
sub Get12Random { |
sub Get12Random { |
my ($dbh, $type, $num) = @_; |
my ($dbh, $type, $num) = @_; |
my ($i, @questions, $q, $t, $sth); |
my ($i, @questions, $q, $t, $sth); |
my ($qnum) = &GetQNum($dbh); |
my ($qnum) = &GetMaxQId($dbh); |
my (%chosen); |
my (%chosen); |
srand; |
srand; |
|
|
for ($i = 0; $i < $num; $i++) { |
for ($i = 0; $i < $num; $i++) { |
do { |
do { |
$q = int(rand($qnum)); |
$q = int(rand($qnum)); |
$sth = $dbh->prepare("SELECT Type FROM Questions |
$sth = $dbh->prepare("SELECT Type FROM Questions |
WHERE QuestionId=$q"); |
WHERE QuestionId=$q"); |
$sth->execute; |
$sth->execute; |
$t = ($sth->fetchrow)[0]; |
$t = ($sth->fetchrow)[0]; |
} until !$chosen{$q} && $t =~ /$type/; |
} until !$chosen{$q} && $t && $type =~ /[$t]/; |
$chosen{$q} = 'y'; |
$chosen{$q} = 'y'; |
push @questions, $q; |
push @questions, $q; |
} |
} |
return @questions; |
return @questions; |
} |
} |
|
|
sub Include_virtual { |
sub Include_virtual { |
Line 589 MAIN:
|
Line 631 MAIN:
|
print end_html; |
print end_html; |
die "Can't connect to DB chgk\n"; |
die "Can't connect to DB chgk\n"; |
}; |
}; |
if (!param('comp') and !$text) { |
if (!param('comp') and !param('sqldump') and !$text) { |
print header; |
print header; |
print start_html(-"title"=>'Database of the questions', |
print start_html(-"title"=>'Database of the questions', |
-author=>'dimrub@icomverse.com', |
-author=>'dimrub@icomverse.com', |
Line 603 MAIN:
|
Line 645 MAIN:
|
} |
} |
|
|
if (param('rand')) { |
if (param('rand')) { |
my ($type, $qnum) = ('þ', 12); |
my ($type, $qnum) = ('', 12); |
$type = 'â' if (param('brain')); |
$type .= 'â' if (param('brain')); |
|
$type .= 'þ' if (param('chgk')); |
$qnum = param('qnum') if (param('qnum') =~ /^\d+$/); |
$qnum = param('qnum') if (param('qnum') =~ /^\d+$/); |
|
$qnum = 0 if (!$type); |
if (param('email') && -x $SENDMAIL && |
if (param('email') && -x $SENDMAIL && |
open(F, "| $SENDMAIL -t -n")) { |
open(F, "| $SENDMAIL -t -n")) { |
my ($Email) = param('email'); |
my ($Email) = param('email'); |
my ($mime_type) = $text ? "plain" : "html"; |
my ($mime_type) = $text ? "plain" : "html"; |
print F <<EOT; |
print F <<EOT; |
To: $Email |
To: $Email |
From: dimrub\@icomverse.com |
From: olegstemanov\@mail.ru |
Subject: Sluchajnij Paket Voprosov "Chto? Gde? Kogda?" |
Subject: Sluchajnij Paket Voprosov "Chto? Gde? Kogda?" |
MIME-Version: 1.0 |
MIME-Version: 1.0 |
Content-type: text/$mime_type; charset="koi8-r" |
Content-type: text/$mime_type; charset="koi8-r" |
Line 626 EOT
|
Line 670 EOT
|
print &PrintRandom($dbh, $type, $qnum, $text); |
print &PrintRandom($dbh, $type, $qnum, $text); |
} |
} |
} elsif (param('sstr')) { |
} elsif (param('sstr')) { |
&PrintSearch($dbh, param('sstr')); |
&PrintSearch($dbh, param('sstr'), param('metod')); |
} elsif (param('all')) { |
} elsif (param('all')) { |
print &PrintAll($dbh, 0); |
print &PrintAll($dbh, 0); |
} elsif (param('from_year') && param('to_year')) { |
} elsif (param('from_year') && param('to_year')) { |
print &PrintDates($dbh); |
print &PrintDates($dbh); |
} elsif (param('comp')) { |
} elsif (param('comp')) { |
print header( |
print header( |
-'Content-Type' => 'application/x-zip-compressed; name="db.zip"', |
-'Content-Type' => 'application/x-zip-compressed; name="db.zip"', |
-'Content-Disposition' => 'attachment; filename="db.zip"' |
-'Content-Disposition' => 'attachment; filename="db.zip"' |
); |
); |
$tour = (param('tour')) ? param('tour') : 0; |
$tour = (param('tour')) ? param('tour') : 0; |
my (@files) = &PrintArchive($dbh, $tour); |
my (@files) = &PrintArchive($dbh, $tour); |
open F, "$ZIP -j - $SRCPATH/COPYRIGHT @files |"; |
open F, "$ZIP -j - $SRCPATH/COPYRIGHT @files |"; |
print (<F>); |
print (<F>); |
close F; |
close F; |
$dbh->disconnect; |
$dbh->disconnect; |
exit; |
exit; |
|
} elsif (param('sqldump')) { |
|
print header( |
|
-'Content-Type' => 'application/x-zip-compressed; name="dump.zip"', |
|
-'Content-Disposition' => 'attachment; filename="dump.zip"' |
|
); |
|
open F, "$ZIP -j - $DUMPFILE |"; |
|
print (<F>); |
|
close F; |
|
$dbh->disconnect; |
|
exit; |
|
|
} else { |
} else { |
$tour = (param('tour')) ? param('tour') : 0; |
$tour = (param('tour')) ? param('tour') : 0; |
if ($tour !~ /^[0-9]*$/) { |
if ($tour !~ /^[0-9]*$/) { |