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