Diff for /db/prgsrc/updatedb.pl between versions 1.17 and 1.34

version 1.17, 2000/11/11 00:30:06 version 1.34, 2003/02/07 23:20:15
Line 6  updatedb.pl - a script for creation of n Line 6  updatedb.pl - a script for creation of n
   
 =head1 SYNOPSIS  =head1 SYNOPSIS
   
 updatedb.pl I<file1> I<file2>....  updatedb.pl B<[-i]> I<file1> I<file2>....
   
   
 =head1 DESCRIPTION  =head1 DESCRIPTION
   
 Updates information in the B<chgk> databse. Uses file  Updates information in the B<chgk> databse. Uses file
   
   =head1 OPTIONS
   
   =item B<-i> 
   
   Ask about ParentId. 
   
   
 =head1 BUGS  =head1 BUGS
   
Line 26  Dmitry Rubinstein Line 32  Dmitry Rubinstein
   
 =cut  =cut
   
   use vars qw($opt_i);
   
   use Getopt::Std;
   my $unsortedname="../dump/unsorted";
   getopts('i');
   #open STDERR, ">errors";
   my $Interactive=$opt_i || 0;
   my $DUMPDIR = $ENV{DUMPDIR} || "../dump";
   
 my (%RevMonths) =   my (%RevMonths) = 
     ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',      ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
      'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',       'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
Line 44  my ($sth); Line 59  my ($sth);
   
 use DBI;  use DBI;
 use strict;  use strict;
   my $isunsorted=0;
 sub UpdateParents {  sub UpdateParents {
     my ($dbh, $ParentId, $all_qnum) = @_;      my ($dbh, $ParentId, $all_qnum) = @_;
     if ($ParentId) {      if ($ParentId) {
Line 67  sub getField { Line 82  sub getField {
         s/[ ]//g;          s/[ ]//g;
         if ($key && /^\s*$/) {          if ($key && /^\s*$/) {
             chomp $value;              chomp $value;
               $value =~ s/\s+$//;
             chomp $key;              chomp $key;
             if ($key eq 'Дата') {              if ($key eq 'Дата') {
                 $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;                  $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
                 my($month) = $RevMonths{$2} || '000';                  my($month) = $RevMonths{$2} || '01';
                 $value =~ s/$2/$month/;                  $value =~ s/-(.*)-/-$month-/;
                   $value =~ s/-00*$/-01/;
             }              }
             $value = $dbh->quote($value);              $value = $dbh->quote($value);
             return ($key, $value);              return ($key, $value);
Line 89  sub getField { Line 106  sub getField {
         }          }
     }      }
     if ($key && $value) {      if ($key && $value) {
           $value=~s/\s+$//;
         $value = $dbh->quote($value);          $value = $dbh->quote($value);
         return ($key, $value);          return ($key, $value);
     }      }
Line 98  sub getField { Line 116  sub getField {
 sub SelectGroup {  sub SelectGroup {
     my ($dbh, $source, $TourName) = @_;      my ($dbh, $source, $TourName) = @_;
     my ($sth, $ParentId, $i, @arr);      my ($sth, $ParentId, $i, @arr);
       
     $sth = $dbh->prepare("SELECT Id, Title FROM      if ($Interactive) {    
           $sth = $dbh->prepare("SELECT Id, Title FROM
                 Tournaments WHERE Type = 'Г'");                  Tournaments WHERE Type = 'Г'");
     $sth->execute;          $sth->execute;
     print "Выберите группу для турнира:\n$TourName, файл $source\n\n";          print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
     while (@arr=$sth->fetchrow) {          while (@arr=$sth->fetchrow) {
         print "[$arr[0]] $arr[1]\n";                  print "[$arr[0]] $arr[1]\n";
     }          }
     $ParentId = <STDIN>;          $ParentId = <STDIN>;
     chomp $ParentId;          chomp $ParentId;
     if (!$ParentId) {          if (!$ParentId) {
         print "Пропускаем файл $source\n";                  print "Пропускаем файл $source\n";
         print STDERR "Файл $source отвергнут оператором\n";                  print STDERR "Файл $source отвергнут оператором\n";
         return (0,0);                  return (0,0);
           } else {
                   print "Вы выбрали турнир: $ParentId\n";
                   $sth = $dbh->prepare("INSERT INTO Tournaments
                                 (Title, Type, ParentId, FileName) 
                                  VALUES ($TourName, 'Ч', $ParentId, 
                                          $source)");
                   $sth->execute;
                   my $TournamentId = $sth->{mysql_insertid};
                   return ($TournamentId,$ParentId);
           }
     } else {      } else {
         print "Вы выбрали турнир: $ParentId\n";  # Теперь, если файла нет в дереве турниров, никаких вопросов не 
         $sth = $dbh->prepare("INSERT INTO Tournaments  # задаётся, а вместо этого он добавляется в группу 9999
                   $ParentId = 9999;
                   my $tempsource=$source;
                   my $temptname=$TourName;
                   $tempsource=~s/^\'(.*)\'$/$1/;
                   $temptname=~s/^\'(.*)\'$/$1/;
                   print UNSORTED "$tempsource".((12 -length($source))x' ')."\t$temptname\n";
                   $isunsorted=1;
                   $sth = $dbh->prepare("INSERT INTO Tournaments
                               (Title, Type, ParentId, FileName)                                 (Title, Type, ParentId, FileName) 
                                VALUES ($TourName, 'Ч', $ParentId,                                  VALUES ($TourName, 'Ч', $ParentId, 
                                        $source)");                                         $source)");
         $sth->execute;                  $sth->execute;
         my $TournamentId = $sth->{mysql_insertid};                  my $TournamentId = $sth->{mysql_insertid};
         return ($TournamentId,$ParentId);                  return ($TournamentId,$ParentId);
     }          }
                                   
           
 }  }
Line 201  sub GetTours { Line 238  sub GetTours {
 sub CreateTour {  sub CreateTour {
     my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;      my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;
     my $sth = $dbh->prepare("INSERT INTO Tournaments      my $sth = $dbh->prepare("INSERT INTO Tournaments
                              (Title, Type, ParentId, TourNum)                                (Title, Type, ParentId, Number) 
                              VALUES ($title, 'Т', $ParentId, $TourNum)");                               VALUES ($title, 'Т', $ParentId, $TourNum)");
     $sth->execute;      $sth->execute;
     my $TourId = $sth->{mysql_insertid};      my $TourId = $sth->{mysql_insertid};
Line 228  MAIN: Line 265  MAIN:
     #      #
     my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type',       my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type', 
                           'Автор' => 'Authors', 'Рейтинг'=>'Rating',                             'Автор' => 'Authors', 'Рейтинг'=>'Rating', 
                           'Источник' => 'Sources');                            'Источник' => 'Sources',
                             'Тема' => 'Topic');
                                                       
                                               
     my($source);      my($source);
           
     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";
           my @sources;        
       open UNSORTED, ">$unsortedname";
     while ($source = shift) {      while ($source = shift) {
          push @sources,glob($source);
       }
       foreach $source(@sources) {
         my $TourNum=0;          my $TourNum=0;
         my($PlayedAt) = '';          my($PlayedAt) = '';
         my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);          my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
Line 292  MAIN: Line 334  MAIN:
             if ($key =~ /Вопрос/) {              if ($key =~ /Вопрос/) {
                 if (!$TourId) {                  if (!$TourId) {
                     $qnum = 0;                      $qnum = 0;
                     $TourId=CreateTour($dbh,'1',$TournamentId,                      $TourNum++;
                       $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
                                        \%TourDefaults);                                         \%TourDefaults);
                     %QuestionDefaults=%QuestionGlobalDefaults;                      %QuestionDefaults=%QuestionGlobalDefaults;
                 }                  }
Line 367  MAIN: Line 410  MAIN:
         &UpdateParents($dbh, $ParentId, $all_qnum);                       &UpdateParents($dbh, $ParentId, $all_qnum);             
         print STDERR "Всего вопросов: $all_qnum \n";          print STDERR "Всего вопросов: $all_qnum \n";
     }      }
       close UNSORTED;
       unlink $unsortedname unless $isunsorted;
     $dbh->disconnect;      $dbh->disconnect;
 }  }

Removed from v.1.17  
changed lines
  Added in v.1.34


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