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.19 2000/11/11 04:05: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} || '000';
74: $value =~ s/$2/$month/;
75: }
76: $value = $dbh->quote($value);
77: return ($key, $value);
78: }
79: next if (/^\s*$/);
80:
81: if (!$key && /^(.*?)[:\.]\s*(.*)$/s) {
82: $key = $1;
83: $value=$2;
84: next;
85: }
86: if ($key) {
87: $value .= $_;
88: next;
89: }
90: }
91: if ($key && $value) {
92: $value = $dbh->quote($value);
93: return ($key, $value);
94: }
95: return (0, 0);
96: }
97:
98: sub SelectGroup {
99: my ($dbh, $source, $TourName) = @_;
100: my ($sth, $ParentId, $i, @arr);
101:
102: $sth = $dbh->prepare("SELECT Id, Title FROM
103: Tournaments WHERE Type = 'Г'");
104: $sth->execute;
105: print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
106: while (@arr=$sth->fetchrow) {
107: print "[$arr[0]] $arr[1]\n";
108: }
109: $ParentId = <STDIN>;
110: chomp $ParentId;
111: if (!$ParentId) {
112: print "Пропускаем файл $source\n";
113: print STDERR "Файл $source отвергнут оператором\n";
114: return (0,0);
115: } else {
116: print "Вы выбрали турнир: $ParentId\n";
117: $sth = $dbh->prepare("INSERT INTO Tournaments
118: (Title, Type, ParentId, FileName)
119: VALUES ($TourName, 'Ч', $ParentId,
120: $source)");
121: $sth->execute;
122: my $TournamentId = $sth->{mysql_insertid};
123: return ($TournamentId,$ParentId);
124: }
125:
126:
127: }
128:
129: sub UpdateTournament {
130: my ($dbh, $TournamentId, $field, $value) = @_;
131: $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
132: or die $dbh->errstr;
133: }
134:
135: sub UpdateQuestion {
136: my ($dbh, $QuestionId, $field, $value) = @_;
137: $dbh->do("UPDATE Questions SET $field=$value
138: WHERE QuestionId=$QuestionId")
139: or die $dbh->errstr;
140: }
141:
142: sub CheckFile {
143: my ($dbh, $source, $title) = @_;
144: my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
145: WHERE FileName=$source AND Type='Ч'");
146: $sth->execute or die $dbh->errstr;
147: my @arr = $sth->fetchrow;
148: if (! scalar @arr) {
149: return SelectGroup($dbh,$source,$title);
150: }
151: my($Id,$ParentId,$QuestionsNum)=@arr;
152: if($QuestionsNum) {
153: print "Файл $source с данными $title уже существует. ",
154: "Заменить?[y/N]\n";
155: my $answer = <STDIN>;
156: if ($answer !~ /^[yY]/) {
157: return (0,0);
158: } else {
159: DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0);
160: }
161: }
162: return($Id,$ParentId);
163: }
164:
165:
166: sub DeleteTournament {
167: my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
168: if ($QuestionsNum) {
169: UpdateParents($dbh,$ParentId,-$QuestionsNum);
170: }
171: my (@Tours) = &GetTours($dbh, $Id);
172: foreach my $Tour (@Tours) {
173: DeleteTournament($dbh,$Tour,$Id,0,1);
174: }
175: my $sth = $dbh->prepare("DELETE FROM Questions
176: WHERE ParentId=$Id");
177: $sth->execute or die $dbh->errstr;
178: if($DeleteMyself) {
179: $sth = $dbh->prepare("DELETE FROM Tournaments
180: WHERE Id=$Id");
181: $sth->execute or die $dbh->errstr;
182: }
183: }
184:
185: sub GetTours {
186: my ($dbh, $ParentId) = @_;
187: my (@arr, @Tours);
188:
189: my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
190: WHERE ParentId=$ParentId ORDER BY Id");
191:
192: $sth->execute;
193:
194: while (@arr = $sth->fetchrow) {
195: push @Tours, $arr[0];
196: }
197:
198: return @Tours;
199: }
200:
201: sub CreateTour {
202: my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;
203: my $sth = $dbh->prepare("INSERT INTO Tournaments
204: (Title, Type, ParentId, Number)
205: VALUES ($title, 'Т', $ParentId, $TourNum)");
206: $sth->execute;
207: my $TourId = $sth->{mysql_insertid};
208: while (my ($key,$value)=each %$rh_defaults) {
209: &UpdateTournament($dbh, $TourId, $key, $value);
210: }
211: return $TourId;
212: }
213:
214:
215: MAIN:
216: {
217: my($key, $value, $addition);
218: #
219: # Inherited fields for a Tour or Tournament
220: #
221: my %TourFields = ('Копирайт' => 'Copyright',
222: 'Инфо' => 'Info', 'URL' => 'URL',
223: 'Ссылка' => 'URL', 'Редактор' => 'Editors',
224: 'Обработан'=> 'EnteredBy',
225: 'Дата'=>'PlayedAt');
226: #
227: # Inherited fields for a Question
228: #
229: my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type',
230: 'Автор' => 'Authors', 'Рейтинг'=>'Rating',
231: 'Источник' => 'Sources');
232:
233:
234: my($source);
235:
236: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
237: or die "Can't connect to DB chgk\n";
238:
239: while ($source = shift) {
240: my $TourNum=0;
241: my($PlayedAt) = '';
242: my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
243: my($tournum, $qnum, $all_qnum) = (0, 0, 0);
244: my (@d) = (localtime((stat($source))[9]))[5,4,3];
245: $d[1]++;
246: $d[0]+=1900;
247: my ($CreatedAt) = $dbh->quote( join('-', @d));
248:
249: open INFD, $source
250: or die "Can't open input file: $!\n";
251:
252: $source =~ s/^.*\/([^\/]*)$/$1/;
253: $source = $dbh->quote($source);
254: print STDERR "Файл: $source, дата: $CreatedAt ";
255: my %TourDefaults=('CreatedAt'=>$CreatedAt);
256: my %QuestionDefaults=();
257: my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч'));
258: while (($key, $value) = getField(\*INFD, $dbh)) {
259: last if (!$key);
260:
261: if ($key =~ /Мета/) {
262: next; # This is obsolete
263: }
264: if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
265: ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
266: if (!$TournamentId) {
267: last;
268: }
269: $sth = $dbh->prepare("UPDATE Tournaments SET
270: Title=$value, Type='Ч',
271: ParentId=$ParentId,
272: FileName=$source,
273: CreatedAt=$CreatedAt
274: WHERE
275: Id=$TournamentId");
276: $sth->execute;
277: next;
278: }
279: if ($key =~ /Тур/) {
280: if ($TourId) {
281: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
282: WHERE Id=$TourId");
283: }
284: $qnum = 0;
285: $TourNum++;
286: $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
287: \%TourDefaults);
288: %QuestionDefaults=%QuestionGlobalDefaults;
289: $QuestionId=0;
290: next;
291: }
292: if ($key =~ /Вопрос/) {
293: if (!$TourId) {
294: $qnum = 0;
295: $TourNum++;
296: $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
297: \%TourDefaults);
298: %QuestionDefaults=%QuestionGlobalDefaults;
299: }
300: my $query = "INSERT INTO Questions
301: (ParentId, Number)
302: VALUES ($TourId, $qnum+1)";
303: $sth = $dbh->prepare($query);
304: $sth->execute or print $query;;
305: $QuestionId = $sth->{mysql_insertid};
306: &UpdateQuestion($dbh, $QuestionId, "Question", $value);
307: while (my ($key,$value)=each %QuestionDefaults) {
308: &UpdateQuestion($dbh, $QuestionId, $key, $value);
309: }
310: $qnum++;
311: $all_qnum++;
312: next;
313: }
314:
315: if ($key =~ /Ответ/) {
316: &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
317: next;
318: }
319:
320: if ($key =~ /Комментари/) {
321: &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
322: next;
323: }
324:
325: my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
326:
327: if (scalar @Fields) {
328: my $word = shift @Fields;
329: my $field = $QuestionFields{$word};
330: if ($QuestionId) {
331: &UpdateQuestion($dbh, $QuestionId, $field, $value);
332: } elsif ($TourId) {
333: $QuestionDefaults{$field}=$value;
334: } else {
335: $QuestionGlobalDefaults{$field}=$value;
336: }
337: next;
338: }
339:
340: @Fields = grep { $key =~ /$_/ } keys %TourFields;
341:
342: if (scalar @Fields) {
343: my $word = shift @Fields;
344: my $field = $TourFields{$word};
345: if ($QuestionId) {
346: print STDERR "ОШИБКА: $key $value недопустимы после",
347: " начала вопросов\n";
348: } elsif ($TourId) {
349: &UpdateTournament($dbh, $TourId, $field, $value);
350: } else {
351: &UpdateTournament($dbh, $TournamentId, $field, $value);
352: $TourDefaults{$field}=$value;
353: }
354: next;
355: }
356:
357:
358: #
359: # If we are here, something got wrong!
360: #
361: print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
362:
363: }
364: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
365: WHERE Id=$TourId");
366: $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
367: WHERE Id=$TournamentId");
368: &UpdateParents($dbh, $ParentId, $all_qnum);
369: print STDERR "Всего вопросов: $all_qnum \n";
370: }
371: $dbh->disconnect;
372: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>