Annotation of db/prgsrc/updatedb.pl, revision 1.17
1.1 boris 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:
1.17 ! boris 25: =head1 $Id: updatedb.pl,v 1.16 2000/10/22 23:47:06 boris Exp $
1.1 boris 26:
27: =cut
28:
29: my (%RevMonths) =
1.5 boris 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',
1.13 boris 36: 'Янв', '1', 'Фев', '2', 'Мар', '3', 'Апр', '4', 'Май', '5',
37: 'Июн', '6', 'Июл', '7', 'Авг', '8', 'Сен', '9',
38: 'Окт', '10', 'Ноя', '11', 'Дек', '12');
1.1 boris 39: my ($sth);
40:
1.13 boris 41:
1.3 boris 42:
1.4 boris 43:
1.3 boris 44:
1.1 boris 45: use DBI;
46: use strict;
47:
48: sub UpdateParents {
1.5 boris 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: }
1.1 boris 60: }
61:
62: sub getField {
1.5 boris 63: my($desc, $dbh) = @_;
64: my($key);
65: my($value) = ('');
66: while (<$desc>) {
1.11 boris 67: s/[
]//g;
1.5 boris 68: if ($key && /^\s*$/) {
69: chomp $value;
70: chomp $key;
71: if ($key eq 'Дата') {
72: $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
1.9 boris 73: my($month) = $RevMonths{$2} || '000';
1.5 boris 74: $value =~ s/$2/$month/;
75: }
76: $value = $dbh->quote($value);
77: return ($key, $value);
78: }
79: next if (/^\s*$/);
80:
1.16 boris 81: if (!$key && /^(.*?)[:\.]\s*(.*)$/s) {
1.5 boris 82: $key = $1;
1.16 boris 83: $value=$2;
1.5 boris 84: next;
1.1 boris 85: }
1.5 boris 86: if ($key) {
87: $value .= $_;
88: next;
1.1 boris 89: }
1.5 boris 90: }
91: if ($key && $value) {
92: $value = $dbh->quote($value);
93: return ($key, $value);
94: }
95: return (0, 0);
1.1 boris 96: }
97:
98: sub SelectGroup {
1.7 boris 99: my ($dbh, $source, $TourName) = @_;
100: my ($sth, $ParentId, $i, @arr);
1.5 boris 101:
102: $sth = $dbh->prepare("SELECT Id, Title FROM
1.1 boris 103: Tournaments WHERE Type = 'Г'");
1.5 boris 104: $sth->execute;
1.7 boris 105: print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
1.5 boris 106: while (@arr=$sth->fetchrow) {
107: print "[$arr[0]] $arr[1]\n";
108: }
109: $ParentId = <STDIN>;
1.8 boris 110: chomp $ParentId;
1.7 boris 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,
1.8 boris 120: $source)");
1.7 boris 121: $sth->execute;
122: my $TournamentId = $sth->{mysql_insertid};
123: return ($TournamentId,$ParentId);
124: }
125:
126:
1.1 boris 127: }
128:
129: sub UpdateTournament {
1.5 boris 130: my ($dbh, $TournamentId, $field, $value) = @_;
131: $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
132: or die $dbh->errstr;
1.1 boris 133: }
134:
135: sub UpdateQuestion {
1.5 boris 136: my ($dbh, $QuestionId, $field, $value) = @_;
137: $dbh->do("UPDATE Questions SET $field=$value
1.1 boris 138: WHERE QuestionId=$QuestionId")
1.5 boris 139: or die $dbh->errstr;
1.1 boris 140: }
141:
1.7 boris 142: sub CheckFile {
1.10 boris 143: my ($dbh, $source, $title) = @_;
1.7 boris 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;
1.10 boris 152: if($QuestionsNum) {
1.7 boris 153: print "Файл $source с данными $title уже существует. ",
154: "Заменить?[y/N]\n";
155: my $answer = <STDIN>;
156: if ($answer !~ /^[yY]/) {
157: return (0,0);
158: } else {
1.10 boris 159: DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0);
1.7 boris 160: }
161: }
162: return($Id,$ParentId);
163: }
164:
165:
166: sub DeleteTournament {
1.10 boris 167: my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
168: if ($QuestionsNum) {
169: UpdateParents($dbh,$ParentId,-$QuestionsNum);
170: }
1.7 boris 171: my (@Tours) = &GetTours($dbh, $Id);
172: foreach my $Tour (@Tours) {
1.12 boris 173: DeleteTournament($dbh,$Tour,$Id,0,1);
1.7 boris 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");
1.9 boris 181: $sth->execute or die $dbh->errstr;
1.7 boris 182: }
183: }
1.8 boris 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:
1.13 boris 201: sub CreateTour {
1.17 ! boris 202: my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;
1.13 boris 203: my $sth = $dbh->prepare("INSERT INTO Tournaments
1.17 ! boris 204: (Title, Type, ParentId, TourNum)
! 205: VALUES ($title, 'Т', $ParentId, $TourNum)");
1.13 boris 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:
1.8 boris 214:
1.1 boris 215: MAIN:
216: {
1.5 boris 217: my($key, $value, $addition);
1.13 boris 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',
1.15 boris 230: 'Автор' => 'Authors', 'Рейтинг'=>'Rating',
1.14 boris 231: 'Источник' => 'Sources');
1.13 boris 232:
233:
1.5 boris 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) {
1.17 ! boris 240: my $TourNum=0;
1.5 boris 241: my($PlayedAt) = '';
242: my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
1.14 boris 243: my($tournum, $qnum, $all_qnum) = (0, 0, 0);
1.5 boris 244: my (@d) = (localtime((stat($source))[9]))[5,4,3];
245: $d[1]++;
246: $d[0]+=1900;
247: my ($CreatedAt) = $dbh->quote( join('-', @d));
1.6 boris 248:
1.5 boris 249: open INFD, $source
250: or die "Can't open input file: $!\n";
251:
252: $source =~ s/^.*\/([^\/]*)$/$1/;
253: $source = $dbh->quote($source);
1.6 boris 254: print STDERR "Файл: $source, дата: $CreatedAt ";
1.13 boris 255: my %TourDefaults=('CreatedAt'=>$CreatedAt);
256: my %QuestionDefaults=();
1.14 boris 257: my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч'));
1.5 boris 258: while (($key, $value) = getField(\*INFD, $dbh)) {
259: last if (!$key);
260:
261: if ($key =~ /Мета/) {
1.7 boris 262: next; # This is obsolete
1.5 boris 263: }
1.10 boris 264: if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {
1.7 boris 265: ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
266: if (!$TournamentId) {
267: last;
1.10 boris 268: }
1.7 boris 269: $sth = $dbh->prepare("UPDATE Tournaments SET
1.10 boris 270: Title=$value, Type='Ч',
1.7 boris 271: ParentId=$ParentId,
272: FileName=$source,
273: CreatedAt=$CreatedAt
274: WHERE
275: Id=$TournamentId");
1.5 boris 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;
1.17 ! boris 285: $TourNum++;
! 286: $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
! 287: \%TourDefaults);
1.13 boris 288: %QuestionDefaults=%QuestionGlobalDefaults;
289: $QuestionId=0;
290: next;
1.5 boris 291: }
292: if ($key =~ /Вопрос/) {
1.11 boris 293: if (!$TourId) {
294: $qnum = 0;
1.13 boris 295: $TourId=CreateTour($dbh,'1',$TournamentId,
296: \%TourDefaults);
297: %QuestionDefaults=%QuestionGlobalDefaults;
1.11 boris 298: }
1.5 boris 299: my $query = "INSERT INTO Questions
1.14 boris 300: (ParentId, Number)
301: VALUES ($TourId, $qnum+1)";
1.5 boris 302: $sth = $dbh->prepare($query);
303: $sth->execute or print $query;;
304: $QuestionId = $sth->{mysql_insertid};
305: &UpdateQuestion($dbh, $QuestionId, "Question", $value);
1.13 boris 306: while (my ($key,$value)=each %QuestionDefaults) {
307: &UpdateQuestion($dbh, $QuestionId, $key, $value);
308: }
1.5 boris 309: $qnum++;
310: $all_qnum++;
311: next;
312: }
1.6 boris 313:
314: if ($key =~ /Ответ/) {
315: &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
316: next;
317: }
1.7 boris 318:
1.6 boris 319: if ($key =~ /Комментари/) {
320: &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
321: next;
322: }
1.5 boris 323:
1.15 boris 324: my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
1.6 boris 325:
1.13 boris 326: if (scalar @Fields) {
327: my $word = shift @Fields;
328: my $field = $QuestionFields{$word};
329: if ($QuestionId) {
330: &UpdateQuestion($dbh, $QuestionId, $field, $value);
331: } elsif ($TourId) {
332: $QuestionDefaults{$field}=$value;
333: } else {
334: $QuestionGlobalDefaults{$field}=$value;
335: }
1.6 boris 336: next;
337: }
338:
1.15 boris 339: @Fields = grep { $key =~ /$_/ } keys %TourFields;
1.6 boris 340:
1.13 boris 341: if (scalar @Fields) {
342: my $word = shift @Fields;
343: my $field = $TourFields{$word};
344: if ($QuestionId) {
345: print STDERR "ОШИБКА: $key $value недопустимы после",
346: " начала вопросов\n";
347: } elsif ($TourId) {
348: &UpdateTournament($dbh, $TourId, $field, $value);
1.5 boris 349: } else {
1.13 boris 350: &UpdateTournament($dbh, $TournamentId, $field, $value);
351: $TourDefaults{$field}=$value;
1.1 boris 352: }
1.6 boris 353: next;
1.5 boris 354: }
1.13 boris 355:
1.6 boris 356:
357: #
358: # If we are here, something got wrong!
359: #
360: print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
361:
1.5 boris 362: }
363: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
1.1 boris 364: WHERE Id=$TourId");
1.5 boris 365: $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
1.1 boris 366: WHERE Id=$TournamentId");
1.5 boris 367: &UpdateParents($dbh, $ParentId, $all_qnum);
1.6 boris 368: print STDERR "Всего вопросов: $all_qnum \n";
1.5 boris 369: }
370: $dbh->disconnect;
1.1 boris 371: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>