File:  [Local Repository] / db / prgsrc / writefile.pl
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Jan 17 08:00:24 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,%info);
   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:   print OUT "Обработан:\n$EnteredBy\n\n" if $EnteredBy;
   52: 
   53: 
   54:   $query= "SELECT Id, Title, Copyright, Editors, PlayedAt, Info from Tournaments where ParentId=$Id order by Id";
   55:   $sth=$dbh->prepare($query);
   56:   $sth->execute;
   57:   my ($tourid,$tourtitle,$cright,$editor,@tours,$vid,$author,$tourauthor);
   58: 
   59: 
   60:   while (($tourid,$tourtitle,$cright,$editor,$date,$info)=$sth->fetchrow,$tourid)
   61:   {
   62: #    $text{$tourid}="Тур:\n$tourtitle\n\n";
   63:     $query= "SELECT * from Questions where ParentId=$tourid order by QuestionId";
   64:     my $sth1=$dbh->prepare($query);
   65:     $sth1->execute;
   66:     push(@tours,$tourid);
   67:     $tourtitle{$tourid}=$tourtitle;
   68:     $copyright{$tourid}=$cright;
   69:     $info{$tourid}=$info;
   70:     $editor{$tourid}=$editor;
   71:     $date{$tourid}=$date if $date;
   72:     $vid='';
   73:     my $author='';
   74:     my $eqauthor=1;
   75:     my $qnumber=0;
   76:     my @arr;
   77:     while ( @arr=$sth1->fetchrow, $arr[0])
   78:     {
   79: 	my $i = 0;
   80: 	$qnumber++;
   81: 
   82: 	foreach my $name (@{$sth1->{NAME}}) {
   83: 	        $arr[$i]=~s/^(.*?)\s*$/$1/ if $arr[$i];
   84: 		$Question{$tourid}[$qnumber]{$name} = $arr[$i++];
   85: 	}
   86: 	if ($vid)
   87:         {
   88:           if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print STDERR "Warning: Different types for Tournament $tourid\n"}
   89:         } else 
   90:         {
   91:             $vid=$Question{$tourid}[$qnumber]{'Type'};
   92:         } 
   93: 
   94: 
   95: 	if ($author)
   96:         {
   97:           if ($author ne $Question{$tourid}[$qnumber]{'Authors'})  
   98:           {
   99:              $eqauthor=0;
  100:           }
  101:         } else 
  102:         {
  103:             $author=$Question{$tourid}[$qnumber]{'Authors'};
  104:             $eqauthor=0 unless $author;
  105:         } 
  106:     }
  107:     $vid{$tourid}=$vid;
  108:     $qnumber{$tourid}=$qnumber;
  109:     $author{$tourid}=$eqauthor ? $author : '';
  110:   }
  111: 
  112: 
  113:   $vid='';
  114:   my $eqvid=1;
  115:   my $eqauthor=1;
  116:   my $eqdate=1;
  117:   my $eqinfo=1;
  118:   $date='';
  119: 
  120: 
  121:   foreach (@tours)
  122:   {
  123:      $vid||=$vid{$_};
  124:      if ($vid{$_} ne $vid)
  125:      {
  126:         $eqvid=0;
  127:      }
  128:      $author||=$author{$_};
  129:      if (!$author{$_} || ($author{$_} ne $author))
  130:      {
  131:         $eqauthor=0;
  132:      }
  133:      $date||=$date{$_};
  134:      if (!$date{$_} || ($date{$_} ne $date))
  135:      {
  136:         $eqdate=0;
  137:      }
  138:      $info||=$info{$_};
  139:      if (!$info{$_} || ($info{$_} ne $info))
  140:      {
  141:         $eqinfo=0;
  142:      }
  143: 
  144:   }
  145:   print OUT "Вид:\n$vid\n\n" if $eqvid;
  146:   print OUT "Автор:\n$author\n\n" if $eqauthor;
  147: 
  148:   foreach my $tour(@tours)
  149:   {
  150:      print OUT "Тур:\n$tourtitle{$tour}\n\n";
  151:      print OUT "Вид:\n$vid{$tour}\n\n" if  !$eqvid;
  152:      print OUT "Копирайт:\n$copyright{$tour}\n\n" if $copyright{$tour} && ($copyright{$tour} ne $Copyright);
  153:      print OUT "Редактор:\n$editor{$tour}\n\n" if $editor{$tour} && ($editor{$tour} ne $Editors);
  154:      print OUT "Дата:\n".PrintDate($date{$tour})."\n\n" if $date{$tour} && (!$eqdate  || !$PlayedAt || $PlayedAt ne $date{$tour});
  155:      print OUT "Инфо:\n$info{$tour}\n\n" if $info{$tour} && (!$eqinfo  || !$Info || $Info ne $info{$tour});
  156:      $tourauthor=0;
  157:      if (!$eqauthor && $author{$tour})
  158:      { 
  159:        print OUT "Автор:\n$author{$tour}\n\n";
  160:        $tourauthor=1;
  161:      }
  162:      foreach my $q(1..$qnumber{$tour})
  163:      {
  164:         print OUT "Вопрос $q:\n".$Question{$tour}[$q]{'Question'}."\n\n";
  165: 	print OUT  "Ответ:\n".$Question{$tour}[$q]{'Answer'}."\n\n";
  166: 	print OUT  "Автор:\n".$Question{$tour}[$q]{'Authors'}."\n\n" 
  167:                if !$tourauthor && !$eqauthor && $Question{$tour}[$q]{'Authors'};
  168: 	print OUT  "Комментарий:\n".$Question{$tour}[$q]{'Comments'}."\n\n" 
  169:                if $Question{$tour}[$q]{'Comments'};
  170: 	print OUT "Источник:\n".$Question{$tour}[$q]{'Sources'}."\n\n" 
  171:                if $Question{$tour}[$q]{'Sources'};
  172: 	print OUT "Рейтинг:\n".$Question{$tour}[$q]{'Rating'}."\n\n" 
  173:                if $Question{$tour}[$q]{'Rating'};
  174: 
  175:      }
  176:   }
  177: 
  178:   close OUT;
  179: 
  180: 
  181: 
  182: }
  183: 
  184: 

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