Annotation of db/prgsrc/updatedb.pl, revision 1.15

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>