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