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

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

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