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