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