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