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