--- db/prgsrc/db.cgi 2000/10/20 04:06:54 1.6 +++ db/prgsrc/db.cgi 2000/10/24 18:03:49 1.12 @@ -9,7 +9,8 @@ use POSIX qw(locale_h); 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) = @@ -464,27 +465,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 { @@ -589,7 +596,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', @@ -603,16 +610,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]*$/) {