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

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

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