1: #!/usr/local/bin/perl
2:
3: =head1 NAME
4:
5: updatedb.pl - a script for creation of new database.
6:
7: =head1 SYNOPSIS
8:
9: updatedb.pl I<file1> I<file2>....
10:
11:
12: =head1 DESCRIPTION
13:
14: Updates information in the B<chgk> databse. Uses file
15:
16:
17: =head1 BUGS
18:
19: The database, user and password are hardcoded.
20:
21: =head1 AUTHOR
22:
23: Dmitry Rubinstein
24:
25: =head1 $Id: updatedb.pl,v 1.20 2001/07/21 23:59:52 boris Exp $
26:
27: =cut
28:
29: my (%RevMonths) =
30: ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
31: 'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
32: 'Dec', '12',
33: 'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',
34: 'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',
35: 'DEC', '12',
36: 'Янв', '1', 'Фев', '2', 'Мар', '3', 'Апр', '4', 'Май', '5',
37: 'Июн', '6', 'Июл', '7', 'Авг', '8', 'Сен', '9',
38: 'Окт', '10', 'Ноя', '11', 'Дек', '12');
39: my ($sth);
40:
41:
42:
43:
44:
45: use DBI;
46: use strict;
47:
48: sub UpdateParents {
49: my ($dbh, $ParentId, $all_qnum) = @_;
50: if ($ParentId) {
51: my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId
52: FROM Tournaments WHERE Id = $ParentId");
53: $sth1->execute;
54: my ($q, $p) = ($sth1->fetchrow)[0, 1];
55: $dbh->do("UPDATE Tournaments SET
56: QuestionsNum=$q + $all_qnum
57: WHERE Id = $ParentId");
58: &UpdateParents($dbh, $p, $all_qnum);
59: }
60: }
61:
62: sub getField {
63: my($desc, $dbh) = @_;
64: my($key);
65: my($value) = ('');
66: while (<$desc>) {
67: s/[
]//g;
68: if ($key && /^\s*$/) {
69: chomp $value;
70: chomp $key;
71: if ($key eq 'Дата') {
72: $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
73: my($month) = $RevMonths{$2} || '01';
74: $value =~ s/-(.*)-/-$month-/;
75: $value =~ s/-00*$/-01/;
76: }
77: $value = $dbh->quote($value);
78: return ($key, $value);
79: }
80: next if (/^\s*$/);
81:
82: if (!$key && /^(.*?)[:\.]\s*(.*)$/s) {
83: $key = $1;
84: $value=$2;
85: next;
86: }
87: if ($key) {
88: $value .= $_;
89: next;
90: }
91: }
92: if ($key && $value) {
93: $value = $dbh->quote($value);
94: return ($key, $value);
95: }
96: return (0, 0);
97: }
98:
99: sub SelectGroup {
100: my ($dbh, $source, $TourName) = @_;
101: my ($sth, $ParentId, $i, @arr);
102:
103: $sth = $dbh->prepare("SELECT Id, Title FROM
104: Tournaments WHERE Type = 'Г'");
105: $sth->execute;
106: print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
107: while (@arr=$sth->fetchrow) {
108: print "[$arr[0]] $arr[1]\n";
109: }
110: $ParentId = <STDIN>;
111: chomp $ParentId;
112: if (!$ParentId) {
113: print "Пропускаем файл $source\n";
114: print STDERR "Файл $source отвергнут оператором\n";
115: return (0,0);
116: } else {
117: print "Вы выбрали турнир: $ParentId\n";
118: $sth = $dbh->prepare("INSERT INTO Tournaments
119: (Title, Type, ParentId, FileName)
120: VALUES ($TourName, 'Ч', $ParentId,
121: $source)");
122: $sth->execute;
123: my $TournamentId = $sth->{mysql_insertid};
124: return ($TournamentId,$ParentId);
125: }
126:
127:
128: }
129:
130: sub UpdateTournament {
131: my ($dbh, $TournamentId, $field, $value) = @_;
132: $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
133: or die $dbh->errstr;
134: }
135:
136: sub UpdateQuestion {
137: my ($dbh, $QuestionId, $field, $value) = @_;
138: $dbh->do("UPDATE Questions SET $field=$value
139: WHERE QuestionId=$QuestionId")
140: or die $dbh->errstr;
141: }
142:
143: sub CheckFile {
144: my ($dbh, $source, $title) = @_;
145: my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
146: WHERE FileName=$source AND Type='Ч'");
147: $sth->execute or die $dbh->errstr;
148: my @arr = $sth->fetchrow;
149: if (! scalar @arr) {
150: return SelectGroup($dbh,$source,$title);
151: }
152: my($Id,$ParentId,$QuestionsNum)=@arr;
153: if($QuestionsNum) {
154: print "Файл $source с данными $title уже существует. ",
155: "Заменить?[y/N]\n";
156: my $answer = <STDIN>;
157: if ($answer !~ /^[yY]/) {
158: return (0,0);
159: } else {
160: DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0);
161: }
162: }
163: return($Id,$ParentId);
164: }
165:
166:
167: sub DeleteTournament {
168: my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
169: if ($QuestionsNum) {
170: UpdateParents($dbh,$ParentId,-$QuestionsNum);
171: }
172: my (@Tours) = &GetTours($dbh, $Id);
173: foreach my $Tour (@Tours) {
174: DeleteTournament($dbh,$Tour,$Id,0,1);
175: }
176: my $sth = $dbh->prepare("DELETE FROM Questions
177: WHERE ParentId=$Id");
178: $sth->execute or die $dbh->errstr;
179: if($DeleteMyself) {
180: $sth = $dbh->prepare("DELETE FROM Tournaments
181: WHERE Id=$Id");
182: $sth->execute or die $dbh->errstr;
183: }
184: }
185:
186: sub GetTours {
187: my ($dbh, $ParentId) = @_;
188: my (@arr, @Tours);
189:
190: my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
191: WHERE ParentId=$ParentId ORDER BY Id");
192:
193: $sth->execute;
194:
195: while (@arr = $sth->fetchrow) {
196: push @Tours, $arr[0];
197: }
198:
199: return @Tours;
200: }
201:
202: sub CreateTour {
203: my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;
204: my $sth = $dbh->prepare("INSERT INTO Tournaments
205: (Title, Type, ParentId, Number)
206: VALUES ($title, 'Т', $ParentId, $TourNum)");
207: $sth->execute;
208: my $TourId = $sth->{mysql_insertid};
209: while (my ($key,$value)=each %$rh_defaults) {
210: &UpdateTournament($dbh, $TourId, $key, $value);
211: }
212: return $TourId;
213: }
214:
215:
216: MAIN:
217: {
218: my($key, $value, $addition);
219: #
220: # Inherited fields for a Tour or Tournament
221: #
222: my %TourFields = ('Копирайт' => 'Copyright',
223: 'Инфо' => 'Info', 'URL' => 'URL',
224: 'Ссылка' => 'URL', 'Редактор' => 'Editors',
225: 'Обработан'=> 'EnteredBy',
226: 'Дата'=>'PlayedAt');
227: #
228: # Inherited fields for a Question
229: #
230: my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type',
231: 'Автор' => 'Authors', 'Рейтинг'=>'Rating',
232: 'Источник' => 'Sources');
233:
234:
235: my($source);
236:
237: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
238: or die "Can't connect to DB chgk\n";
239:
240: while ($source = shift) {
241: my $TourNum=0;
242: my($PlayedAt) = '';
243: my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
244: my($tournum, $qnum, $all_qnum) = (0, 0, 0);
245: my (@d) = (localtime((stat($source))[9]))[5,4,3];
246: $d[1]++;
247: $d[0]+=1900;
248: my ($CreatedAt) = $dbh->quote( join('-', @d));
249:
250: open INFD, $source
251: or die "Can't open input file: $!\n";
252:
253: $source =~ s/^.*\/([^\/]*)$/$1/;
254: $source = $dbh->quote($source);
255: print STDERR "Файл: $source, дата: $CreatedAt ";
256: my %TourDefaults=('CreatedAt'=>$CreatedAt);
257: my %QuestionDefaults=();
258: my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч'));
259: while (($key, $value) = getField(\*INFD, $dbh)) {
260: last if (!$key);
261:
262: if ($key =~ /Мета/) {
263: next; # This is obsolete
264: }
265: if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
266: ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
267: if (!$TournamentId) {
268: last;
269: }
270: $sth = $dbh->prepare("UPDATE Tournaments SET
271: Title=$value, Type='Ч',
272: ParentId=$ParentId,
273: FileName=$source,
274: CreatedAt=$CreatedAt
275: WHERE
276: Id=$TournamentId");
277: $sth->execute;
278: next;
279: }
280: if ($key =~ /Тур/) {
281: if ($TourId) {
282: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
283: WHERE Id=$TourId");
284: }
285: $qnum = 0;
286: $TourNum++;
287: $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
288: \%TourDefaults);
289: %QuestionDefaults=%QuestionGlobalDefaults;
290: $QuestionId=0;
291: next;
292: }
293: if ($key =~ /Вопрос/) {
294: if (!$TourId) {
295: $qnum = 0;
296: $TourNum++;
297: $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
298: \%TourDefaults);
299: %QuestionDefaults=%QuestionGlobalDefaults;
300: }
301: my $query = "INSERT INTO Questions
302: (ParentId, Number)
303: VALUES ($TourId, $qnum+1)";
304: $sth = $dbh->prepare($query);
305: $sth->execute or print $query;;
306: $QuestionId = $sth->{mysql_insertid};
307: &UpdateQuestion($dbh, $QuestionId, "Question", $value);
308: while (my ($key,$value)=each %QuestionDefaults) {
309: &UpdateQuestion($dbh, $QuestionId, $key, $value);
310: }
311: $qnum++;
312: $all_qnum++;
313: next;
314: }
315:
316: if ($key =~ /Ответ/) {
317: &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
318: next;
319: }
320:
321: if ($key =~ /Комментари/) {
322: &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
323: next;
324: }
325:
326: my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
327:
328: if (scalar @Fields) {
329: my $word = shift @Fields;
330: my $field = $QuestionFields{$word};
331: if ($QuestionId) {
332: &UpdateQuestion($dbh, $QuestionId, $field, $value);
333: } elsif ($TourId) {
334: $QuestionDefaults{$field}=$value;
335: } else {
336: $QuestionGlobalDefaults{$field}=$value;
337: }
338: next;
339: }
340:
341: @Fields = grep { $key =~ /$_/ } keys %TourFields;
342:
343: if (scalar @Fields) {
344: my $word = shift @Fields;
345: my $field = $TourFields{$word};
346: if ($QuestionId) {
347: print STDERR "ОШИБКА: $key $value недопустимы после",
348: " начала вопросов\n";
349: } elsif ($TourId) {
350: &UpdateTournament($dbh, $TourId, $field, $value);
351: } else {
352: &UpdateTournament($dbh, $TournamentId, $field, $value);
353: $TourDefaults{$field}=$value;
354: }
355: next;
356: }
357:
358:
359: #
360: # If we are here, something got wrong!
361: #
362: print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
363:
364: }
365: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
366: WHERE Id=$TourId");
367: $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
368: WHERE Id=$TournamentId");
369: &UpdateParents($dbh, $ParentId, $all_qnum);
370: print STDERR "Всего вопросов: $all_qnum \n";
371: }
372: $dbh->disconnect;
373: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>