Diff for /db/prgsrc/updateindex.pl between versions 1.5 and 1.6

version 1.5, 2000/10/19 03:05:18 version 1.6, 2000/10/22 01:25:25
Line 6  updateindex.pl - a script for creation o Line 6  updateindex.pl - a script for creation o
   
 =head1 SYNOPSIS  =head1 SYNOPSIS
   
 updateind.pl [B<-i> I<indexfile>]  updateind.pl [B<-i> I<indexfile>] [B<-y>|B<-n>] 
   
   
 =head1 DESCRIPTION  =head1 DESCRIPTION
   
 Upadets metainformation in the B<chgk> databse. Uses file  Upadets metainformation in the B<chgk> databse. 
     L<./index> unless [B<-i>] option is used  
   
 An example of the index file follows:  An example of the index file follows:
   
Line 27  An example of the index file follows: Line 26  An example of the index file follows:
  burda12.txt                    Тренировки Бориса Бурды 12   burda12.txt                    Тренировки Бориса Бурды 12
   
   
   =head1 OPTIONS
   
   =over 4
   
   =item B<-i> I<indexfile>
   
   The index file to read (Standard input by default)
   
   =item B<-y>
   
   Answer 'yes' to all questions
   
   =item B<-n>
   
   Answer 'no' to all questions
   
 =head1 BUGS  =head1 BUGS
   
 The database, user and password are hardcoded.   The database, user and password are hardcoded. 
Line 44  Boris Veytsman Line 59  Boris Veytsman
 =cut  =cut
   
     use strict;      use strict;
 use vars qw($opt_i $opt_h);  use vars qw($opt_i $opt_h $opt_y $opt_n);
   
 use Getopt::Std;  use Getopt::Std;
 use DBI;  use DBI;
   
 MAIN:   MAIN: 
 {  {
     my $USAGE="Usage: updateindex.pl -i indexfile\n";      my $USAGE="Usage: updateindex.pl [-i indexfile] [-y|-n]\n";
     getopts('hi:') or die $USAGE;      getopts('hi:yn') or die $USAGE;
     if ($opt_h) {      if ($opt_h) {
         print $USAGE;          print $USAGE;
         exit 0;          exit 0;
     }      }
     my($source) = $opt_i || 'index';      my $decision='askuser';
       if ($opt_y) {
           $decision = 'yes';
       } 
       if ($opt_n ) {
           $decision = 'no';
       }
       my($source) = $opt_i;
     my($depth, @depthId);      my($depth, @depthId);
     my $filename;      my $filename;
     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")       my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
         or die "Can't connect to DB chgk\n";          or die "Can't connect to DB chgk\n";
   
     open INFD, $source or die "Can't open input file: $!\n";      if ($source) {
     while (<INFD>) {          open INFO, $source or die "Can't open input file: $!\n";
       } else {
           *INFO=*STDIN;
       }
       while (<INFO>) {
         chomp;          chomp;
         s/ //;          s/ //;
         next if (/^\s*$/);          next if (/^\s*$/);
Line 84  MAIN: Line 110  MAIN:
         s/\s$//;          s/\s$//;
         my $title = $_;          my $title = $_;
         my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;          my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;
         my $Id = CheckId($dbh,$title,$ParentId,$filename);          my $Id = CheckId($dbh,$title,$ParentId,$decision,$filename);
         if (!$Id  || $filename) {          if (!$Id  || $filename) {
             next;              next;
         }          }
         $depthId[$depth] = $Id;          $depthId[$depth] = $Id;
   
     }      }
       print STDERR "Всего вопросов: ",
       UpdateGroup($dbh,0),"\n";
     $dbh->disconnect;      $dbh->disconnect;
 }  }
   
   
 sub CheckId {  sub CheckId {
     my ($dbh,$title,$ParentId,$filename) = @_;      my ($dbh,$title,$ParentId,$answer,$filename) = @_;
     my $type;      my $type;
     my $key;      my $key;
     my $value;      my $value;
Line 116  sub CheckId { Line 144  sub CheckId {
     $sth->execute or die $dbh->errstr;      $sth->execute or die $dbh->errstr;
     my @arr = $sth->fetchrow;      my @arr = $sth->fetchrow;
     if (scalar @arr) {      if (scalar @arr) {
         print "$value is already in the DB!\n";          if ($answer eq 'askuser') {
         print "Заменить новым значением? [y/N]\n";              print "$value is already in the DB!\n";
         my $answer = <STDIN>;              print "Заменить новым значением? [y/N]\n";
               $answer = <STDIN>;
           }
         if ($answer !~ /^[yY]/) {          if ($answer !~ /^[yY]/) {
               print STDERR  "Не заменяем $value\n";
             return 0;              return 0;
         } else {          } else {
               print STDERR  "Заменяем $value\n";
             $Id = $arr[0];              $Id = $arr[0];
         }           } 
     }      }
Line 150  sub CheckId { Line 182  sub CheckId {
     }         }   
     return $Id;      return $Id;
 }  }
   
   sub UpdateGroup {
       my ($dbh,$Id) = @_;
       my $sth = $dbh->prepare("SELECT COUNT(*) FROM Questions
                                WHERE ParentId=$Id");
       $sth->execute;
       my @arr=$sth->fetchrow;
       my $result=$arr[0];
       my @Tours = GetTours($dbh,$Id);
       foreach my $TourId (@Tours) {
           $result += UpdateGroup($dbh,$TourId);
       }
       $sth=$dbh->prepare("UPDATE Tournaments SET
                      QuestionsNum=$result 
                      WHERE Id=$Id");
       $sth->execute;
       return $result;
   }
   
   sub GetTours {
           my ($dbh, $ParentId) = @_;
           my (@arr, @Tours);
   
           my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
                   WHERE ParentId=$ParentId ORDER BY Id");
   
           $sth->execute;
   
           while (@arr = $sth->fetchrow) {
                   push @Tours, $arr[0];
           }
   
           return @Tours;
   }
   

Removed from v.1.5  
changed lines
  Added in v.1.6


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