File:  [Local Repository] / db / prgsrc / writefile.pl
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Tue Jan 15 03:34:31 2002 UTC (22 years, 4 months ago) by roma7
Branches: MAIN
CVS tags: HEAD
*** empty log message ***

    1: #!perl 
    2: use DBI;
    3: @months=('000','Jan',"Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct",
    4:           "Nov","Dec");
    5: 
    6: $fname=shift;
    7: $fname=~s/\.txt$//;
    8: $TMPDIR="new";
    9:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
   10: 	or die "Can't connect to DB chgk\n";
   11: 
   12:    if (WriteFile($dbh,$fname)==-1) {print "File?";}
   13: 
   14:    $dbh->disconnect;
   15: 
   16: 
   17: sub PrintDate {
   18:   my $date=shift;
   19:   my ($year,$month,$day)=split /-/, $date;
   20: #  $month=0,$date=0 if $year && $month==1 && $day==1;
   21:   return sprintf("%02d-%3s-%04d",$day,$months[$month],$year);
   22: }
   23: 
   24: sub WriteFile {
   25:   my ($dbh,$fname) = @_;
   26:   $fname=~s/\.txt$//;
   27:   $fname=~s/.*\/(\w+)/$1/;
   28:   my $query= "SELECT Id, Title, Copyright, Info, URL, 
   29:                       Editors, EnteredBy, PlayedAt, CreatedAt 
   30:                      from Tournaments where FileName=".$dbh->quote("$fname.txt");
   31:   my $sth=$dbh->prepare($query);
   32:   my (%Question,%editor,%qnumber,%copyright,%author,%vid,%tourtitle);
   33:   $sth->execute;
   34:   my ($Id, $Title, $Copyright, $Info, $URL, 
   35:    $Editors, $EnteredBy, $PlayedAt, $CreatedAt)=
   36:       $sth->fetchrow;
   37:   return -1 unless $Id;
   38:   open (OUT, ">$TMPDIR/$fname.txt");
   39:   print OUT "Чемпионат:\n$Title\n\n";
   40: 
   41:   print OUT "URL:\n$URL\n\n" if $URL;
   42: 
   43:   print OUT "Инфо:\n$Info\n\n" if $Info;
   44: 
   45:   print OUT "Копирайт:\n$Copyright\n\n" if $Copyright;
   46: 
   47:   print OUT "Редактор:\n$Editors\n\n" if $Editors;
   48: 
   49:   print OUT "Дата:\n".PrintDate($PlayedAt)."\n\n" if $PlayedAt;
   50: 
   51: 
   52:   $query= "SELECT Id, Title, Copyright, Editors, PlayedAt from Tournaments where ParentId=$Id order by Id";
   53:   $sth=$dbh->prepare($query);
   54:   $sth->execute;
   55:   my ($tourid,$tourtitle,$cright,$editor,@tours,$vid,$author,$tourauthor);
   56: 
   57: 
   58:   while (($tourid,$tourtitle,$cright,$editor,$date)=$sth->fetchrow,$tourid)
   59:   {
   60: #    $text{$tourid}="Тур:\n$tourtitle\n\n";
   61:     $query= "SELECT * from Questions where ParentId=$tourid order by QuestionId";
   62:     my $sth1=$dbh->prepare($query);
   63:     $sth1->execute;
   64:     push(@tours,$tourid);
   65:     $tourtitle{$tourid}=$tourtitle;
   66:     $copyright{$tourid}=$cright;
   67:     $editor{$tourid}=$editor;
   68:     $date{$tourid}=$date;
   69:     $vid='';
   70:     my $author='';
   71:     my $eqauthor=1;
   72:     my $qnumber=0;
   73:     my @arr;
   74:     while ( @arr=$sth1->fetchrow, $arr[0])
   75:     {
   76: 	my $i = 0;
   77: 	$qnumber++;
   78: 
   79: 	foreach my $name (@{$sth1->{NAME}}) {
   80: 	        $arr[$i]=~s/^(.*?)\s*$/$1/ if $arr[$i];
   81: 		$Question{$tourid}[$qnumber]{$name} = $arr[$i++];
   82: 	}
   83: 	if ($vid)
   84:         {
   85:           if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print STDERR "Warning: Different types for Tournament $tourid\n"}
   86:         } else 
   87:         {
   88:             $vid=$Question{$tourid}[$qnumber]{'Type'};
   89:         } 
   90: 
   91: 
   92: 	if ($author)
   93:         {
   94:           if ($author ne $Question{$tourid}[$qnumber]{'Authors'})  
   95:           {
   96:              $eqauthor=0;
   97:           }
   98:         } else 
   99:         {
  100:             $author=$Question{$tourid}[$qnumber]{'Authors'};
  101:             $eqauthor=0 unless $author;
  102:         } 
  103:     }
  104:     $vid{$tourid}=$vid;
  105:     $qnumber{$tourid}=$qnumber;
  106:     $author{$tourid}=$eqauthor ? $author : '';
  107:   }
  108: 
  109: 
  110:   $vid='';
  111:   my $eqvid=1;
  112:   my $eqauthor=1;
  113:   my $eqdate=1;
  114:   $date='';
  115: 
  116: 
  117:   foreach (@tours)
  118:   {
  119:      $vid||=$vid{$_};
  120:      if ($vid{$_} ne $vid)
  121:      {
  122:         $eqvid=0;
  123:      }
  124:      $author||=$author{$_};
  125:      if (!$author{$_} || ($author{$_} ne $author))
  126:      {
  127:         $eqauthor=0;
  128:      }
  129:      $date||=$date{$_};
  130:      if (!$date{$_} || ($date{$_} ne $date))
  131:      {
  132:         $eqdate=0;
  133:      }
  134:   }
  135:   print OUT "Вид:\n$vid\n\n" if $eqvid;
  136:   print OUT "Автор:\n$author\n\n" if $eqauthor;
  137: 
  138:   foreach my $tour(@tours)
  139:   {
  140:      print OUT "Тур:\n$tourtitle{$tour}\n\n";
  141:      print OUT "Вид:\n$vid{$tour}\n\n" if  !$eqvid;
  142:      print OUT "Копирайт:\n$copyright{$tour}\n\n" if $copyright{$tour} && ($copyright{$tour} ne $Copyright);
  143:      print OUT "Редактор:\n$editor{$tour}\n\n" if $editor{$tour} && ($editor{$tour} ne $Editors);
  144:      print OUT "Дата:\n".PrintDate($date{$tour})."\n\n" if !$eqdate && $date{$tour} || !$PlayedAt;
  145:      $tourauthor=0;
  146:      if (!$eqauthor && $author{$tour})
  147:      { 
  148:        print OUT "Автор:\n$author{$tour}\n\n";
  149:        $tourauthor=1;
  150:      }
  151:      foreach my $q(1..$qnumber{$tour})
  152:      {
  153:         print OUT "Вопрос $q:\n".$Question{$tour}[$q]{'Question'}."\n\n";
  154: 	print OUT  "Ответ:\n".$Question{$tour}[$q]{'Answer'}."\n\n";
  155: 	print OUT  "Автор:\n".$Question{$tour}[$q]{'Authors'}."\n\n" 
  156:                if !$tourauthor && !$eqauthor && $Question{$tour}[$q]{'Authors'};
  157: 	print OUT  "Комментарий:\n".$Question{$tour}[$q]{'Comments'}."\n\n" 
  158:                if $Question{$tour}[$q]{'Comments'};
  159: 	print OUT "Источник:\n".$Question{$tour}[$q]{'Sources'}."\n\n" 
  160:                if $Question{$tour}[$q]{'Sources'};
  161: 	print OUT "Рейтинг:\n".$Question{$tour}[$q]{'Rating'}."\n\n" 
  162:                if $Question{$tour}[$q]{'Rating'};
  163: 
  164:      }
  165:   }
  166: 
  167:   close OUT;
  168: 
  169: 
  170: 
  171: }
  172: 
  173: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>