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.12 2000/10/22 02:25:02 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: 'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',
37: 'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8',
38: 'Окт', '9', 'Ноя', '19', 'Дек', '11');
39: my ($sth);
40:
41: use vars qw($/);
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 (s/^(.*?)[:\.]\s*// && !$key) {
82: $key = $1;
83: $value=$_;
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:
202: MAIN:
203: {
204: my($key, $value, $addition);
205:
206: my($source);
207:
208: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
209: or die "Can't connect to DB chgk\n";
210:
211: while ($source = shift) {
212: my($PlayedAt) = '';
213: my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
214: my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
215: my (@d) = (localtime((stat($source))[9]))[5,4,3];
216: $d[1]++;
217: $d[0]+=1900;
218: my ($CreatedAt) = $dbh->quote( join('-', @d));
219:
220: open INFD, $source
221: or die "Can't open input file: $!\n";
222:
223: $source =~ s/^.*\/([^\/]*)$/$1/;
224: $source = $dbh->quote($source);
225: print STDERR "Файл: $source, дата: $CreatedAt ";
226: while (($key, $value) = getField(\*INFD, $dbh)) {
227: last if (!$key);
228:
229: if ($key =~ /Мета/) {
230: next; # This is obsolete
231: }
232: if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
233: ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
234: if (!$TournamentId) {
235: last;
236: }
237: $sth = $dbh->prepare("UPDATE Tournaments SET
238: Title=$value, Type='Ч',
239: ParentId=$ParentId,
240: FileName=$source,
241: CreatedAt=$CreatedAt
242: WHERE
243: Id=$TournamentId");
244: $sth->execute;
245: next;
246: }
247: if ($key =~ /Тур/) {
248: if ($TourId) {
249: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
250: WHERE Id=$TourId");
251: }
252: $qnum = 0;
253: $sth = $dbh->prepare("INSERT INTO Tournaments
254: (Title, Type, ParentId, CreatedAt)
255: VALUES ($value, 'Т', $TournamentId,
256: $CreatedAt)");
257: $sth->execute;
258: $TourId = $sth->{mysql_insertid};
259: next;
260: }
261: if ($key =~ /Вид/ || $key =~ /Тип/) {
262: $qtype = $value;
263: $qtype =~ s/\'//g;
264: next;
265: }
266: if ($key =~ /Вопрос/) {
267: if (!$TourId) {
268: $qnum = 0;
269: $sth = $dbh->prepare("INSERT INTO Tournaments
270: (Title, Type, ParentId, CreatedAt)
271: VALUES ('1', 'Т', $TournamentId,
272: $CreatedAt)");
273: $sth->execute;
274: $TourId = $sth->{mysql_insertid};
275: }
276: my $query = "INSERT INTO Questions
277: (ParentId, Number, Type)
278: VALUES ($TourId, $qnum+1, \'$qtype\')";
279: $sth = $dbh->prepare($query);
280: $sth->execute or print $query;;
281: $QuestionId = $sth->{mysql_insertid};
282: &UpdateQuestion($dbh, $QuestionId, "Question", $value);
283: $qnum++;
284: $all_qnum++;
285: next;
286: }
287:
288: if ($key =~ /Ответ/) {
289: &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
290: next;
291: }
292:
293: if ($key =~ /Рейтинг/) {
294: &UpdateQuestion($dbh, $QuestionId, "Rating", $value);
295: next;
296: }
297:
298:
299: if ($key =~ /Автор/) {
300: &UpdateQuestion($dbh, $QuestionId, "Authors", $value);
301: next;
302: }
303:
304:
305: if ($key =~ /Источник/) {
306: &UpdateQuestion($dbh, $QuestionId, "Sources", $value);
307: next;
308: }
309:
310:
311: if ($key =~ /Комментари/) {
312: &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
313: next;
314: }
315:
316:
317: if ($key =~ /URL/ || $key =~ /Ссылка/) {
318: &UpdateTournament($dbh, $TournamentId, "URL", $value);
319: next;
320: }
321:
322:
323: if ($key =~ /Копирайт/) {
324: &UpdateTournament($dbh, $TournamentId, "Copyright", $value);
325: next;
326: }
327:
328:
329: if ($key =~ /Инфо/) {
330: &UpdateTournament($dbh, $TournamentId, "Info", $value);
331: next;
332: }
333:
334:
335: if ($key =~ /Редактор/) {
336: &UpdateTournament($dbh, $TournamentId, "Editors", $value);
337: next;
338: }
339:
340:
341: if ($key =~ /Обработан/) {
342: &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);
343: next;
344: }
345:
346: if ($key =~ /Дата/) {
347: if ($TourId) {
348: &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
349: } else {
350: &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
351: }
352: next;
353: }
354:
355: #
356: # If we are here, something got wrong!
357: #
358: print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
359:
360: }
361: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
362: WHERE Id=$TourId");
363: $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
364: WHERE Id=$TournamentId");
365: &UpdateParents($dbh, $ParentId, $all_qnum);
366: print STDERR "Всего вопросов: $all_qnum \n";
367: }
368: $dbh->disconnect;
369: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>