Annotation of db/prgsrc/updateindex.pl, revision 1.7

1.1       boris       1: #!/usr/local/bin/perl -w
                      2: 
                      3: =head1 NAME
                      4: 
                      5: updateindex.pl - a script for creation of new database. 
                      6: 
                      7: =head1 SYNOPSIS
                      8: 
1.7     ! boris       9: updateind.pl [B<-i> I<indexfile>] [B<-y>|B<-n>] [B<-r>]
1.1       boris      10: 
                     11: 
                     12: =head1 DESCRIPTION
                     13: 
1.6       boris      14: Upadets metainformation in the B<chgk> databse. 
1.1       boris      15: 
                     16: An example of the index file follows:
                     17: 
1.3       boris      18:                 Авторские вопросы
                     19:                         Виктор Байрак
                     20:  bayrak.txt                    Вопросы В.Байрака
                     21:                         Борис Бурда
                     22:  burda.txt                     Вопросы Бориса Бурды
                     23:  burda1.txt                    Тренировки Бориса Бурды 1
                     24:  burda10.txt                   Тренировки Бориса Бурды 10
                     25:  burda11.txt                   Тренировки Бориса Бурды 11
                     26:  burda12.txt                   Тренировки Бориса Бурды 12
                     27: 
1.1       boris      28: 
1.6       boris      29: =head1 OPTIONS
                     30: 
                     31: =over 4
                     32: 
                     33: =item B<-i> I<indexfile>
                     34: 
                     35: The index file to read (Standard input by default)
                     36: 
                     37: =item B<-y>
                     38: 
                     39: Answer 'yes' to all questions
                     40: 
                     41: =item B<-n>
                     42: 
                     43: Answer 'no' to all questions
                     44: 
1.7     ! boris      45: =item B<-r>
        !            46: 
        !            47: Remove all entries with zero QuestionNum
        !            48: 
1.1       boris      49: =head1 BUGS
                     50: 
                     51: The database, user and password are hardcoded. 
                     52: 
                     53: =head1 SEE ALSO
                     54: 
                     55: createindex.pl(1)
                     56: 
                     57: =head1 AUTHOR
                     58: 
                     59: Boris Veytsman
                     60: 
1.7     ! boris      61: =head1 $Id: updateindex.pl,v 1.6 2000/10/22 01:25:25 boris Exp $
1.1       boris      62: 
                     63: =cut
                     64: 
                     65:     use strict;
1.7     ! boris      66: use vars qw($opt_i $opt_h $opt_y $opt_n $opt_r);
1.1       boris      67: 
                     68: use Getopt::Std;
                     69: use DBI;
                     70: 
                     71: MAIN: 
                     72: {
1.7     ! boris      73:     my $USAGE="Usage: updateindex.pl [-i indexfile] [-y|-n][-r]\n";
        !            74:     my $REMOVE=0;
        !            75:     getopts('hi:ynr') or die $USAGE;
1.2       boris      76:     if ($opt_h) {
                     77:        print $USAGE;
                     78:        exit 0;
                     79:     }
1.6       boris      80:     my $decision='askuser';
                     81:     if ($opt_y) {
                     82:        $decision = 'yes';
                     83:     } 
                     84:     if ($opt_n ) {
                     85:        $decision = 'no';
                     86:     }
1.7     ! boris      87:     if ($opt_r) {
        !            88:        $REMOVE=1;
        !            89:     }
1.6       boris      90:     my($source) = $opt_i;
1.2       boris      91:     my($depth, @depthId);
                     92:     my $filename;
                     93:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
                     94:        or die "Can't connect to DB chgk\n";
                     95: 
1.6       boris      96:     if ($source) {
                     97:        open INFO, $source or die "Can't open input file: $!\n";
                     98:     } else {
                     99:        *INFO=*STDIN;
                    100:     }
                    101:     while (<INFO>) {
1.2       boris     102:        chomp;
                    103:        s/
//;
                    104:        next if (/^\s*$/);
1.3       boris     105:        if (s/^(\S+) *//) { # File found
1.2       boris     106:            $filename = $1;
                    107:            $depth = -1;
                    108:        } else {  # Group found
1.3       boris     109:            $filename = '';
1.2       boris     110:            $depth = -2;
                    111:        }
                    112:        s/^(\t*)//;
                    113:        $depth += length($1);
                    114:        if ($depth < 0) {
                    115:            die "Wrong line $_\n";
1.1       boris     116:        }
1.2       boris     117:        s/^\s*//;
                    118:        s/\s$//;
1.4       boris     119:        my $title = $_;
1.2       boris     120:        my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;
1.6       boris     121:        my $Id = CheckId($dbh,$title,$ParentId,$decision,$filename);
1.4       boris     122:        if (!$Id  || $filename) {
                    123:            next;
1.2       boris     124:        }
1.4       boris     125:        $depthId[$depth] = $Id;
1.2       boris     126: 
                    127:     }
1.6       boris     128:     print STDERR "Всего вопросов: ",
                    129:     UpdateGroup($dbh,0),"\n";
1.7     ! boris     130:     if ($REMOVE) {
        !           131:        print STDERR "Removing empty tours.";
        !           132:        $dbh->do("DELETE FROM Tournaments WHERE QuestionsNum=0");
        !           133:     }
1.2       boris     134:     $dbh->disconnect;
1.4       boris     135: }
                    136: 
                    137: 
                    138: sub CheckId {
1.6       boris     139:     my ($dbh,$title,$ParentId,$answer,$filename) = @_;
1.4       boris     140:     my $type;
                    141:     my $key;
                    142:     my $value;
                    143:     my $Id = 0;
                    144:     if ($filename) {
                    145:        $type=$dbh->quote('Ч');
                    146:        $key = "FileName";
                    147:        $value = $dbh->quote($filename);
                    148:     } else {
                    149:        $type=$dbh->quote('Г');
                    150:        $key = "Title";
                    151:        $value = $dbh->quote($title);
                    152:     }
                    153:     $title=$dbh->quote($title);    
                    154:     my $sth = $dbh->prepare("SELECT Id FROM Tournaments 
                    155:                              WHERE $key=$value");
                    156:     $sth->execute or die $dbh->errstr;
                    157:     my @arr = $sth->fetchrow;
                    158:     if (scalar @arr) {
1.6       boris     159:        if ($answer eq 'askuser') {
                    160:            print "$value is already in the DB!\n";
                    161:            print "Заменить новым значением? [y/N]\n";
                    162:            $answer = <STDIN>;
                    163:        }
1.4       boris     164:        if ($answer !~ /^[yY]/) {
1.6       boris     165:            print STDERR  "Не заменяем $value\n";
1.4       boris     166:            return 0;
                    167:        } else {
1.6       boris     168:            print STDERR  "Заменяем $value\n";
1.4       boris     169:            $Id = $arr[0];
                    170:        } 
                    171:     }
                    172:     if ($Id) {
                    173:        $sth = $dbh->prepare("UPDATE Tournaments
                    174:                              SET Title=$title, ParentId=$ParentId, 
                    175:                              Type=$type
                    176:                              WHERE Id=$Id");
                    177: 
                    178:     } else {
                    179:        $sth = $dbh->prepare("INSERT INTO Tournaments
                    180:                              (Title, ParentId, Type) 
                    181:                              VALUES
                    182:                              ($title, $ParentId,$type)");
                    183:     }
                    184:     $sth->execute or die $dbh->errstr;
                    185:     if (!$Id) {
                    186:        $Id = $sth->{'mysql_insertid'};
                    187:     }
                    188:     if ($filename) {
                    189:        $filename=$dbh->quote($filename);
                    190:        $sth = $dbh->prepare("UPDATE Tournaments
                    191:                              SET FileName=$filename
                    192:                              WHERE Id=$Id");
1.5       boris     193:        $sth->execute or die $dbh->errstr;
1.4       boris     194:     }  
                    195:     return $Id;
1.1       boris     196: }
1.6       boris     197: 
                    198: sub UpdateGroup {
                    199:     my ($dbh,$Id) = @_;
                    200:     my $sth = $dbh->prepare("SELECT COUNT(*) FROM Questions
                    201:                             WHERE ParentId=$Id");
                    202:     $sth->execute;
                    203:     my @arr=$sth->fetchrow;
                    204:     my $result=$arr[0];
                    205:     my @Tours = GetTours($dbh,$Id);
                    206:     foreach my $TourId (@Tours) {
                    207:        $result += UpdateGroup($dbh,$TourId);
                    208:     }
                    209:     $sth=$dbh->prepare("UPDATE Tournaments SET
                    210:                    QuestionsNum=$result 
                    211:                    WHERE Id=$Id");
                    212:     $sth->execute;
                    213:     return $result;
                    214: }
                    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: 

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