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

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

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