Annotation of db/prgsrc/writefile.pl, revision 1.2

1.2     ! roma7       1: #!perl 
1.1       roma7       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$//;
1.2     ! roma7       8: $TMPDIR="new";
1.1       roma7       9:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
                     10:        or die "Can't connect to DB chgk\n";
                     11: 
1.2     ! roma7      12:    if (WriteFile($dbh,$fname)==-1) {print "File?";}
        !            13: 
        !            14:    $dbh->disconnect;
1.1       roma7      15: 
                     16: 
1.2     ! roma7      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: 
1.1       roma7      24: sub WriteFile {
1.2     ! roma7      25:   my ($dbh,$fname) = @_;
        !            26:   $fname=~s/\.txt$//;
        !            27:   $fname=~s/.*\/(\w+)/$1/;
1.1       roma7      28:   my $query= "SELECT Id, Title, Copyright, Info, URL, 
                     29:                       Editors, EnteredBy, PlayedAt, CreatedAt 
                     30:                      from Tournaments where FileName=".$dbh->quote("$fname.txt");
1.2     ! roma7      31:   my $sth=$dbh->prepare($query);
        !            32:   my (%Question,%editor,%qnumber,%copyright,%author,%vid,%tourtitle);
1.1       roma7      33:   $sth->execute;
1.2     ! roma7      34:   my ($Id, $Title, $Copyright, $Info, $URL, 
1.1       roma7      35:    $Editors, $EnteredBy, $PlayedAt, $CreatedAt)=
                     36:       $sth->fetchrow;
                     37:   return -1 unless $Id;
1.2     ! roma7      38:   open (OUT, ">$TMPDIR/$fname.txt");
1.1       roma7      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: 
1.2     ! roma7      49:   print OUT "Дата:\n".PrintDate($PlayedAt)."\n\n" if $PlayedAt;
1.1       roma7      50: 
1.2     ! roma7      51: 
        !            52:   $query= "SELECT Id, Title, Copyright, Editors, PlayedAt from Tournaments where ParentId=$Id order by Id";
1.1       roma7      53:   $sth=$dbh->prepare($query);
                     54:   $sth->execute;
1.2     ! roma7      55:   my ($tourid,$tourtitle,$cright,$editor,@tours,$vid,$author,$tourauthor);
        !            56: 
        !            57: 
        !            58:   while (($tourid,$tourtitle,$cright,$editor,$date)=$sth->fetchrow,$tourid)
1.1       roma7      59:   {
                     60: #    $text{$tourid}="Тур:\n$tourtitle\n\n";
                     61:     $query= "SELECT * from Questions where ParentId=$tourid order by QuestionId";
1.2     ! roma7      62:     my $sth1=$dbh->prepare($query);
1.1       roma7      63:     $sth1->execute;
                     64:     push(@tours,$tourid);
                     65:     $tourtitle{$tourid}=$tourtitle;
                     66:     $copyright{$tourid}=$cright;
                     67:     $editor{$tourid}=$editor;
1.2     ! roma7      68:     $date{$tourid}=$date;
1.1       roma7      69:     $vid='';
1.2     ! roma7      70:     my $author='';
        !            71:     my $eqauthor=1;
        !            72:     my $qnumber=0;
        !            73:     my @arr;
        !            74:     while ( @arr=$sth1->fetchrow, $arr[0])
1.1       roma7      75:     {
1.2     ! roma7      76:        my $i = 0;
1.1       roma7      77:        $qnumber++;
1.2     ! roma7      78: 
        !            79:        foreach my $name (@{$sth1->{NAME}}) {
        !            80:                $arr[$i]=~s/^(.*?)\s*$/$1/ if $arr[$i];
1.1       roma7      81:                $Question{$tourid}[$qnumber]{$name} = $arr[$i++];
                     82:        }
                     83:        if ($vid)
                     84:         {
1.2     ! roma7      85:           if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print STDERR "Warning: Different types for Tournament $tourid\n"}
1.1       roma7      86:         } else 
                     87:         {
                     88:             $vid=$Question{$tourid}[$qnumber]{'Type'};
                     89:         } 
                     90: 
1.2     ! roma7      91: 
1.1       roma7      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='';
1.2     ! roma7     111:   my $eqvid=1;
        !           112:   my $eqauthor=1;
        !           113:   my $eqdate=1;
        !           114:   $date='';
        !           115: 
        !           116: 
1.1       roma7     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:      }
1.2     ! roma7     129:      $date||=$date{$_};
        !           130:      if (!$date{$_} || ($date{$_} ne $date))
        !           131:      {
        !           132:         $eqdate=0;
        !           133:      }
1.1       roma7     134:   }
                    135:   print OUT "Вид:\n$vid\n\n" if $eqvid;
                    136:   print OUT "Автор:\n$author\n\n" if $eqauthor;
                    137: 
1.2     ! roma7     138:   foreach my $tour(@tours)
1.1       roma7     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);
1.2     ! roma7     144:      print OUT "Дата:\n".PrintDate($date{$tour})."\n\n" if !$eqdate && $date{$tour} || !$PlayedAt;
1.1       roma7     145:      $tourauthor=0;
                    146:      if (!$eqauthor && $author{$tour})
                    147:      { 
                    148:        print OUT "Автор:\n$author{$tour}\n\n";
                    149:        $tourauthor=1;
                    150:      }
1.2     ! roma7     151:      foreach my $q(1..$qnumber{$tour})
1.1       roma7     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: 
1.2     ! roma7     171: }
        !           172: 
        !           173: 

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