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

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

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