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: print OUT "Обработан:\n$EnteredBy\n\n" if $EnteredBy;
52:
53:
54: $query= "SELECT Id, Title, Copyright, Editors, PlayedAt 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)=$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: $editor{$tourid}=$editor;
70: $date{$tourid}=$date if $date;
71: $vid='';
72: my $author='';
73: my $eqauthor=1;
74: my $qnumber=0;
75: my @arr;
76: while ( @arr=$sth1->fetchrow, $arr[0])
77: {
78: my $i = 0;
79: $qnumber++;
80:
81: foreach my $name (@{$sth1->{NAME}}) {
82: $arr[$i]=~s/^(.*?)\s*$/$1/ if $arr[$i];
83: $Question{$tourid}[$qnumber]{$name} = $arr[$i++];
84: }
85: if ($vid)
86: {
87: if ($vid ne $Question{$tourid}[$qnumber]{'Type'}) {print STDERR "Warning: Different types for Tournament $tourid\n"}
88: } else
89: {
90: $vid=$Question{$tourid}[$qnumber]{'Type'};
91: }
92:
93:
94: if ($author)
95: {
96: if ($author ne $Question{$tourid}[$qnumber]{'Authors'})
97: {
98: $eqauthor=0;
99: }
100: } else
101: {
102: $author=$Question{$tourid}[$qnumber]{'Authors'};
103: $eqauthor=0 unless $author;
104: }
105: }
106: $vid{$tourid}=$vid;
107: $qnumber{$tourid}=$qnumber;
108: $author{$tourid}=$eqauthor ? $author : '';
109: }
110:
111:
112: $vid='';
113: my $eqvid=1;
114: my $eqauthor=1;
115: my $eqdate=1;
116: $date='';
117:
118:
119: foreach (@tours)
120: {
121: $vid||=$vid{$_};
122: if ($vid{$_} ne $vid)
123: {
124: $eqvid=0;
125: }
126: $author||=$author{$_};
127: if (!$author{$_} || ($author{$_} ne $author))
128: {
129: $eqauthor=0;
130: }
131: $date||=$date{$_};
132: if (!$date{$_} || ($date{$_} ne $date))
133: {
134: $eqdate=0;
135: }
136: }
137: print OUT "Вид:\n$vid\n\n" if $eqvid;
138: print OUT "Автор:\n$author\n\n" if $eqauthor;
139:
140: foreach my $tour(@tours)
141: {
142: print OUT "Тур:\n$tourtitle{$tour}\n\n";
143: print OUT "Вид:\n$vid{$tour}\n\n" if !$eqvid;
144: print OUT "Копирайт:\n$copyright{$tour}\n\n" if $copyright{$tour} && ($copyright{$tour} ne $Copyright);
145: print OUT "Редактор:\n$editor{$tour}\n\n" if $editor{$tour} && ($editor{$tour} ne $Editors);
146: print OUT "Дата:\n".PrintDate($date{$tour})."\n\n" if $date{$tour} && (!$eqdate || !$PlayedAt);
147: $tourauthor=0;
148: if (!$eqauthor && $author{$tour})
149: {
150: print OUT "Автор:\n$author{$tour}\n\n";
151: $tourauthor=1;
152: }
153: foreach my $q(1..$qnumber{$tour})
154: {
155: print OUT "Вопрос $q:\n".$Question{$tour}[$q]{'Question'}."\n\n";
156: print OUT "Ответ:\n".$Question{$tour}[$q]{'Answer'}."\n\n";
157: print OUT "Автор:\n".$Question{$tour}[$q]{'Authors'}."\n\n"
158: if !$tourauthor && !$eqauthor && $Question{$tour}[$q]{'Authors'};
159: print OUT "Комментарий:\n".$Question{$tour}[$q]{'Comments'}."\n\n"
160: if $Question{$tour}[$q]{'Comments'};
161: print OUT "Источник:\n".$Question{$tour}[$q]{'Sources'}."\n\n"
162: if $Question{$tour}[$q]{'Sources'};
163: print OUT "Рейтинг:\n".$Question{$tour}[$q]{'Rating'}."\n\n"
164: if $Question{$tour}[$q]{'Rating'};
165:
166: }
167: }
168:
169: close OUT;
170:
171:
172:
173: }
174:
175:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>