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

version 1.1, 2000/10/18 18:48:58 version 1.5, 2000/10/19 03:05:18
Line 16  Upadets metainformation in the B<chgk> d Line 16  Upadets metainformation in the B<chgk> d
   
 An example of the index file follows:  An example of the index file follows:
   
                    Авторские вопросы
                            Виктор Байрак
    bayrak.txt                     Вопросы В.Байрака
                            Борис Бурда
    burda.txt                      Вопросы Бориса Бурды
    burda1.txt                     Тренировки Бориса Бурды 1
    burda10.txt                    Тренировки Бориса Бурды 10
    burda11.txt                    Тренировки Бориса Бурды 11
    burda12.txt                    Тренировки Бориса Бурды 12
   
   
 =head1 BUGS  =head1 BUGS
   
Line 34  Boris Veytsman Line 44  Boris Veytsman
 =cut  =cut
   
     use strict;      use strict;
 use vars qw($opt_m);  use vars qw($opt_i $opt_h);
   
 use Getopt::Std;  use Getopt::Std;
 use DBI;  use DBI;
   
 MAIN:   MAIN: 
 {  {
     getopts('m:') or die "Wrong options";      my $USAGE="Usage: updateindex.pl -i indexfile\n";
         my($source) = $opt_m || 'meta';      getopts('hi:') or die $USAGE;
         my($depth, @depthId);      if ($opt_h) {
         my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")           print $USAGE;
                 or die "Can't connect to DB chgk\n";          exit 0;
       }
         open INFD, $source or die "Can't open input file: $!\n";      my($source) = $opt_i || 'index';
         while (<INFD>) {      my($depth, @depthId);
             my $MetaId;      my $filename;
                 s/^\[(\d*)\]\t//;      my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "") 
                 $MetaId = $1;          or die "Can't connect to DB chgk\n";
                 s/^(\t*)//;  
                 chomp;      open INFD, $source or die "Can't open input file: $!\n";
                 $depth = length($1);      while (<INFD>) {
                 my $title = $dbh->quote($_);          chomp;
                 my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;          s/ //;
                 my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments WHERE          next if (/^\s*$/);
                         MetaId=$MetaId");          if (s/^(\S+) *//) { # File found
                 $sth->execute;              $filename = $1;
                 if ($sth->fetchrow) {              $depth = -1;
                         print "$title is already in the DB!\n";          } else {  # Group found
                         next;              $filename = '';
                 }              $depth = -2;
                 $sth = $dbh->prepare("INSERT INTO Tournaments          }
                         (Title, ParentId, MetaId, Type)           s/^(\t*)//;
                         VALUES ($title, $ParentId, $MetaId, 'Г')");          $depth += length($1);
                 $sth->execute;          if ($depth < 0) {
                 my $Id = $sth->{'mysql_insertid'};              die "Wrong line $_\n";
                 $depthId[$depth] = $Id;  
         }          }
         $dbh->disconnect;          s/^\s*//;
           s/\s$//;
           my $title = $_;
           my $ParentId = ($depth) ? $depthId[$depth - 1] : 0;
           my $Id = CheckId($dbh,$title,$ParentId,$filename);
           if (!$Id  || $filename) {
               next;
           }
           $depthId[$depth] = $Id;
   
       }
       $dbh->disconnect;
   }
   
   
   sub CheckId {
       my ($dbh,$title,$ParentId,$filename) = @_;
       my $type;
       my $key;
       my $value;
       my $Id = 0;
       if ($filename) {
           $type=$dbh->quote('Ч');
           $key = "FileName";
           $value = $dbh->quote($filename);
       } else {
           $type=$dbh->quote('Г');
           $key = "Title";
           $value = $dbh->quote($title);
       }
       $title=$dbh->quote($title);    
       my $sth = $dbh->prepare("SELECT Id FROM Tournaments 
                                WHERE $key=$value");
       $sth->execute or die $dbh->errstr;
       my @arr = $sth->fetchrow;
       if (scalar @arr) {
           print "$value is already in the DB!\n";
           print "Заменить новым значением? [y/N]\n";
           my $answer = <STDIN>;
           if ($answer !~ /^[yY]/) {
               return 0;
           } else {
               $Id = $arr[0];
           } 
       }
       if ($Id) {
           $sth = $dbh->prepare("UPDATE Tournaments
                                SET Title=$title, ParentId=$ParentId, 
                                Type=$type
                                WHERE Id=$Id");
   
       } else {
           $sth = $dbh->prepare("INSERT INTO Tournaments
                                (Title, ParentId, Type) 
                                VALUES
                                ($title, $ParentId,$type)");
       }
       $sth->execute or die $dbh->errstr;
       if (!$Id) {
           $Id = $sth->{'mysql_insertid'};
       }
       if ($filename) {
           $filename=$dbh->quote($filename);
           $sth = $dbh->prepare("UPDATE Tournaments
                                SET FileName=$filename
                                WHERE Id=$Id");
           $sth->execute or die $dbh->errstr;
       }   
       return $Id;
 }  }

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


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