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

1.4     ! 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);
1.4     ! roma7      32:   my (%Question,%editor,%qnumber,%copyright,%author,%vid,%tourtitle,%info);
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.3       roma7      51:   print OUT "Обработан:\n$EnteredBy\n\n" if $EnteredBy;
                     52: 
1.2       roma7      53: 
1.4     ! roma7      54:   $query= "SELECT Id, Title, Copyright, Editors, PlayedAt, Info from Tournaments where ParentId=$Id order by Id";
1.1       roma7      55:   $sth=$dbh->prepare($query);
                     56:   $sth->execute;
1.2       roma7      57:   my ($tourid,$tourtitle,$cright,$editor,@tours,$vid,$author,$tourauthor);
                     58: 
                     59: 
1.4     ! roma7      60:   while (($tourid,$tourtitle,$cright,$editor,$date,$info)=$sth->fetchrow,$tourid)
1.1       roma7      61:   {
                     62: #    $text{$tourid}="Тур:\n$tourtitle\n\n";
                     63:     $query= "SELECT * from Questions where ParentId=$tourid order by QuestionId";
1.2       roma7      64:     my $sth1=$dbh->prepare($query);
1.1       roma7      65:     $sth1->execute;
                     66:     push(@tours,$tourid);
                     67:     $tourtitle{$tourid}=$tourtitle;
                     68:     $copyright{$tourid}=$cright;
1.4     ! roma7      69:     $info{$tourid}=$info;
1.1       roma7      70:     $editor{$tourid}=$editor;
1.3       roma7      71:     $date{$tourid}=$date if $date;
1.1       roma7      72:     $vid='';
1.2       roma7      73:     my $author='';
                     74:     my $eqauthor=1;
                     75:     my $qnumber=0;
                     76:     my @arr;
                     77:     while ( @arr=$sth1->fetchrow, $arr[0])
1.1       roma7      78:     {
1.2       roma7      79:        my $i = 0;
1.1       roma7      80:        $qnumber++;
1.2       roma7      81: 
                     82:        foreach my $name (@{$sth1->{NAME}}) {
                     83:                $arr[$i]=~s/^(.*?)\s*$/$1/ if $arr[$i];
1.1       roma7      84:                $Question{$tourid}[$qnumber]{$name} = $arr[$i++];
                     85:        }
                     86:        if ($vid)
                     87:         {
1.2       roma7      88:           if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print STDERR "Warning: Different types for Tournament $tourid\n"}
1.1       roma7      89:         } else 
                     90:         {
                     91:             $vid=$Question{$tourid}[$qnumber]{'Type'};
                     92:         } 
                     93: 
1.2       roma7      94: 
1.1       roma7      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='';
1.2       roma7     114:   my $eqvid=1;
                    115:   my $eqauthor=1;
                    116:   my $eqdate=1;
1.4     ! roma7     117:   my $eqinfo=1;
1.2       roma7     118:   $date='';
                    119: 
                    120: 
1.1       roma7     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:      }
1.2       roma7     133:      $date||=$date{$_};
                    134:      if (!$date{$_} || ($date{$_} ne $date))
                    135:      {
                    136:         $eqdate=0;
                    137:      }
1.4     ! roma7     138:      $info||=$info{$_};
        !           139:      if (!$info{$_} || ($info{$_} ne $info))
        !           140:      {
        !           141:         $eqinfo=0;
        !           142:      }
        !           143: 
1.1       roma7     144:   }
                    145:   print OUT "Вид:\n$vid\n\n" if $eqvid;
                    146:   print OUT "Автор:\n$author\n\n" if $eqauthor;
                    147: 
1.2       roma7     148:   foreach my $tour(@tours)
1.1       roma7     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);
1.4     ! roma7     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});
1.1       roma7     156:      $tourauthor=0;
                    157:      if (!$eqauthor && $author{$tour})
                    158:      { 
                    159:        print OUT "Автор:\n$author{$tour}\n\n";
                    160:        $tourauthor=1;
                    161:      }
1.2       roma7     162:      foreach my $q(1..$qnumber{$tour})
1.1       roma7     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: 
1.2       roma7     182: }
                    183: 
                    184: 

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