--- db/prgsrc/db.cgi 2000/10/19 22:34:56 1.4
+++ db/prgsrc/db.cgi 2001/05/16 23:49:01 1.14
@@ -5,11 +5,12 @@ use CGI ':all';
use strict;
use Time::Local;
use POSIX qw(locale_h);
-
+my $debug=1; #added by R7
my ($PWD) = `pwd`;
chomp $PWD;
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 ($TMSECS) = 30*24*60*60;
my (%RevMonths) =
@@ -104,32 +105,64 @@ sub GetTours {
# Returns list of QuestionId's, that have the search string in them.
sub Search {
- my ($dbh, $sstr) = @_;
+ my ($dbh, $sstr, $metod) = @_;
my (@arr, @Questions, @fields);
- my (@sar, $i, $sth);
+ my (@sar, $i, $sth,$where);
+
+# push @fields, 'Question';
- push @fields, 'Question';
- foreach (qw/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($_)) {
- push @fields, "IFNULL($_, '')";
+ push @fields, $_;
}
}
- @sar = split " ", $sstr;
- for $i (0 .. $#sar) {
+ @fields=(qw/Question Answer Sources Authors Comments/) unless scalar @fields;
+ 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]}%");
- }
+ }
- my($f) = "CONCAT(" . join(',', @fields) . ")";
- if (param('all') eq 'yes') {
+ my($f) = "CONCAT(" . join(',', @fields) . ")";
+ if (param('all') eq 'yes') {
$sstr = join " AND $f LIKE ", @sar;
- } else {
+ } else {
$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");
+ } #else -- processing old-style query (R7)
+
$sth->execute;
while (@arr = $sth->fetchrow) {
push @Questions, $arr[0];
@@ -154,8 +187,8 @@ sub NoCase {
}
sub PrintSearch {
- my ($dbh, $sstr) = @_;
- my (@Questions) = &Search($dbh, $sstr);
+ my ($dbh, $sstr, $metod) = @_;
+ my (@Questions) = &Search($dbh, $sstr,$metod);
my ($output, $i, $suffix, $hits) = ('', 0, '', $#Questions + 1);
if ($hits =~ /1.$/ || $hits =~ /[5-90]$/) {
@@ -175,7 +208,7 @@ sub PrintSearch {
$sstr =~ s/(.)/&NoCase($1)/ge;
- my(@sar) = split(/\s/, $sstr);
+ my(@sar) = split(' ', $sstr);
for ($i = 0; $i <= $#Questions; $i++) {
$output = &PrintQuestion($dbh, $Questions[$i], 1, $i + 1, 1);
foreach (@sar) {
@@ -224,15 +257,21 @@ sub PrintTournament {
for ($Tournament{'Type'}) {
/ç/ && do {
$output .= h2({align=>"center"},
- "çÒÕÐÐÁ: $Tournament{'Title'}") . p . "\n";
+ "çÒÕÐÐÁ: $Tournament{'Title'} ",
+ "$Tournament{'PlayedAt'}") . p . "\n";
last;
};
/þ/ && do {
return &PrintTour($dbh, $Tours[0], $answer)
if ($#Tours == 0);
+
+ my $title="ðÁËÅÔ: $Tournament{'Title'}";
+ if ($Tournament{'PlayedAt'}) {
+ $title .= " $Tournament{'PlayedAt'}";
+ }
$output .= h2({align=>"center"},
- "þÅÍÐÉÏÎÁÔ: $Tournament{'Title'}") . p . "\n";
+ "$title") . p . "\n";
last;
};
/ô/ && do {
@@ -269,7 +308,8 @@ sub PrintTournament {
if ($SingleTour or $Tournament{'Type'} =~ /ô/) {
$list .= dd(img({src=>$imgsrc, alt=>$alt})
- . " " . $Tournament{'Title'} . $qnum) .
+ . " " . $Tournament{'Title'} . " " .
+ $Tournament{'PlayedAt'} . $qnum) .
dl(
dd("["
. a({href=>url . "?tour=$Tournament{'Id'}&answer=0"},
@@ -282,7 +322,8 @@ sub PrintTournament {
img({src=>'/icons/compressed.gif', alt=>'[ZIP]', border=>1}))
. " " . img({src=>$imgsrc, alt=>$alt})
. " " . a({href=>url . "?tour=$Tournament{'Id'}&answer=0"},
- $Tournament{'Title'}) . $qnum);
+ $Tournament{'Title'}. " ".
+ $Tournament{'PlayedAt'}) . $qnum);
}
}
$output .= dl($list);
@@ -337,6 +378,7 @@ sub PrintTour {
my ($suffix) = &Suffix($qnum);
$output .= h2({align=>"center"}, $Tournament{"Title"},
+ $Tournament{'PlayedAt'},
"
", $Tour{"Title"} .
" ($qnum ×ÏÐÒÏÓ$suffix)\n") . p;
@@ -392,7 +434,10 @@ sub PrintTour {
sub PrintField {
my ($header, $value, $text) = @_;
if ($text) {
- $value =~ s/<[\/\w]*>//sg;
+ $value =~ s/<[\/\w]*>//sg;
+ } else {
+ $value =~ s/^\s+/
/mg;
+ $value =~ s/^\|([^\n]*)/
$1<\/pre>/mg; } return $text ? "$header:\n$value\n\n" : strong("$header: ") . $value . p . "\n"; @@ -412,7 +457,7 @@ sub PrintQuestion { my (%Tournament) = GetTournament($dbh, $Tour{'ParentId'}); $titles .= dd(img({src=>"/icons/folder.open.gif"}) . " " . - a({href=>url . "?tour=$Tournament{'Id'}"}, $Tournament{'Title'})); + a({href=>url . "?tour=$Tournament{'Id'}"}, $Tournament{'Title'}, $Tournament{'PlayedAt'})); $titles .= dl(dd(img({src=>"/icons/folder.open.gif"}) . " " . a({href=>url . "?tour=$Tour{'Id'}"}, $Tour{'Title'}))); @@ -452,27 +497,33 @@ sub GetQNum { $sth->execute; 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 sub Get12Random { my ($dbh, $type, $num) = @_; my ($i, @questions, $q, $t, $sth); - my ($qnum) = &GetQNum($dbh); + my ($qnum) = &GetMaxQId($dbh); my (%chosen); srand; - for ($i = 0; $i < $num; $i++) { - do { - $q = int(rand($qnum)); - $sth = $dbh->prepare("SELECT Type FROM Questions + 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/; - $chosen{$q} = 'y'; - push @questions, $q; - } - return @questions; + $sth->execute; + $t = ($sth->fetchrow)[0]; + } until !$chosen{$q} && $t && $type =~ /[$t]/; + $chosen{$q} = 'y'; + push @questions, $q; + } + return @questions; } sub Include_virtual { @@ -524,7 +575,7 @@ sub PrintAll { } else { $output .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) . " " . a({href=>url . "?tour=$Tournament{'Id'}&answer=0"}, - $Tournament{'Title'}) . " $New"); + $Tournament{'Title'}) ." " . $Tournament{'PlayedAt'} . " $New"); } if ($Id == 0 or $Tournament{'Type'} =~ /ç/) { for ($i = 0; $i <= $#Tours; $i++) { @@ -558,7 +609,7 @@ sub PrintDates { %Tournament = &GetTournament($dbh, $array[0]); $list .= dd(img({src=>"/icons/folder.gif", alt=>"[*]"}) . " " . a({href=>url . "?tour=$Tournament{'Id'}&answer=0"}, - $Tournament{'Title'})); + $Tournament{'Title'}, $Tournament{'PlayedAt'})); } $output .= dl($list); return $output; @@ -577,7 +628,7 @@ MAIN: print end_html; die "Can't connect to DB chgk\n"; }; - if (!param('comp') and !$text) { + if (!param('comp') and !param('sqldump') and !$text) { print header; print start_html(-"title"=>'Database of the questions', -author=>'dimrub@icomverse.com', @@ -591,16 +642,18 @@ MAIN: } if (param('rand')) { - my ($type, $qnum) = ('þ', 12); - $type = 'â' if (param('brain')); + my ($type, $qnum) = ('', 12); + $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 ($mime_type) = $text ? "plain" : "html"; print F <'application/x-zip-compressed; name="db.zip"', - -'Content-Disposition' => 'attachment; filename="db.zip"' - ); - $tour = (param('tour')) ? param('tour') : 0; - my (@files) = &PrintArchive($dbh, $tour); - open F, "$ZIP -j - $SRCPATH/COPYRIGHT @files |"; - print ( ); - close F; - $dbh->disconnect; - exit; + print header( + -'Content-Type' => 'application/x-zip-compressed; name="db.zip"', + -'Content-Disposition' => 'attachment; filename="db.zip"' + ); + $tour = (param('tour')) ? param('tour') : 0; + my (@files) = &PrintArchive($dbh, $tour); + open F, "$ZIP -j - $SRCPATH/COPYRIGHT @files |"; + print ( ); + close F; + $dbh->disconnect; + 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 ( ); + close F; + $dbh->disconnect; + exit; + } else { $tour = (param('tour')) ? param('tour') : 0; if ($tour !~ /^[0-9]*$/) {