Diff for /db/prgsrc/updatedb.pl between versions 1.6 and 1.21

version 1.6, 2000/10/18 02:01:19 version 1.21, 2002/01/07 01:32:26
Line 33  my (%RevMonths) = Line 33  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',
      'DEC', '12',        'DEC', '12', 
      'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',       'Янв', '1', 'Фев', '2', 'Мар', '3', 'Апр', '4', 'Май', '5',
      'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8',        'Июн', '6', 'Июл', '7', 'Авг', '8', 'Сен', '9', 
      'Окт', '9', 'Ноя', '19', 'Дек', '11');       'Окт', '10', 'Ноя', '11', 'Дек', '12');
 my ($sth);  my ($sth);
   
 use vars qw($/);  
   
   
   
Line 64  sub getField { Line 64  sub getField {
     my($key);      my($key);
     my($value) = ('');      my($value) = ('');
     while (<$desc>) {      while (<$desc>) {
         s/ //;          s/[ ]//g;
         if ($key && /^\s*$/) {          if ($key && /^\s*$/) {
             chomp $value;              chomp $value;
             chomp $key;              chomp $key;
             if ($key eq 'Дата') {              if ($key eq 'Дата') {
                 $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;                  $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
                 my($month) = $RevMonths{$2};                  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);
         }          }
         next if (/^\s*$/);          next if (/^\s*$/);
                   
         if (/^(.*)[:\.]\s*$/ && !$key) {          if (!$key && /^(.*?)[:\.]\s*(.*)$/s) {
             $key = $1;              $key = $1;
               $value=$2;
             next;              next;
         }          }
         if ($key) {          if ($key) {
Line 95  sub getField { Line 97  sub getField {
 }  }
   
 sub SelectGroup {  sub SelectGroup {
     my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_;      my ($dbh, $source, $TourName) = @_;
       my ($sth, $ParentId, $i, @arr);
           
     $sth = $dbh->prepare("SELECT Id, Title FROM      $sth = $dbh->prepare("SELECT Id, Title FROM
                 Tournaments WHERE Type = 'Г'");                  Tournaments WHERE Type = 'Г'");
     $sth->execute;      $sth->execute;
     print "Выберите группу для турнира:\n$TourName\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>;
     print "Вы выбрали турнир: $ParentId\n";      chomp $ParentId;
     return $ParentId;      if (!$ParentId) {
           print "Пропускаем файл $source\n";
           print STDERR "Файл $source отвергнут оператором\n";
           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);
       }
                   
       
 }  }
   
 sub UpdateTournament {  sub UpdateTournament {
Line 122  sub UpdateQuestion { Line 140  sub UpdateQuestion {
         or die $dbh->errstr;          or die $dbh->errstr;
 }  }
   
   sub CheckFile {
       my ($dbh, $source, $title) = @_;
       my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
                                WHERE FileName=$source AND Type='Ч'");
       $sth->execute or die $dbh->errstr;
       my @arr = $sth->fetchrow;
       if (! scalar @arr) {
           return SelectGroup($dbh,$source,$title);
       }
       my($Id,$ParentId,$QuestionsNum)=@arr;
       if($QuestionsNum) { 
           print "Файл $source с данными $title уже существует. ",
           "Заменить?[y/N]\n";
           my $answer = <STDIN>;
           if ($answer !~ /^[yY]/) {
               return (0,0);
           } else {
               DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0);
           }
       } 
       return($Id,$ParentId);      
   }
   
   
   sub DeleteTournament {
       my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
       if ($QuestionsNum) {
           UpdateParents($dbh,$ParentId,-$QuestionsNum);
       }
       my (@Tours) = &GetTours($dbh, $Id);
       foreach my $Tour (@Tours) {
           DeleteTournament($dbh,$Tour,$Id,0,1);
       }
       my $sth = $dbh->prepare("DELETE FROM Questions
                                WHERE ParentId=$Id");
       $sth->execute or die $dbh->errstr;
       if($DeleteMyself) {
           $sth = $dbh->prepare("DELETE FROM Tournaments
                                WHERE Id=$Id");
           $sth->execute or die $dbh->errstr;
       }
   }
   
   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;
   }
   
   sub CreateTour {
       my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;
       my $sth = $dbh->prepare("INSERT INTO Tournaments
                                (Title, Type, ParentId, Number) 
                                VALUES ($title, 'Т', $ParentId, $TourNum)");
       $sth->execute;
       my $TourId = $sth->{mysql_insertid};
       while (my ($key,$value)=each %$rh_defaults) {
           &UpdateTournament($dbh, $TourId, $key, $value);
       }
       return $TourId;
   }
                   
   
 MAIN:   MAIN: 
 {  {
     my($key, $value, $addition);      my($key, $value, $addition);
           #
       # Inherited fields for a Tour or Tournament
       #
       my %TourFields = ('Копирайт' => 'Copyright',
                         'Инфо' => 'Info', 'URL' => 'URL',
                         'Ссылка' => 'URL', 'Редактор' => 'Editors',
                         'Обработан'=> 'EnteredBy',
                         'Дата'=>'PlayedAt');
       #
       # Inherited fields for a Question
       #
       my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type', 
                             'Автор' => 'Authors', 'Рейтинг'=>'Rating', 
                             'Источник' => '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";
           
     while ($source = shift) {      while ($source = shift) {
           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);
         my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');          my($tournum, $qnum, $all_qnum) = (0, 0, 0);
         my (@d) = (localtime((stat($source))[9]))[5,4,3];          my (@d) = (localtime((stat($source))[9]))[5,4,3];
         $d[1]++;          $d[1]++;
         $d[0]+=1900;          $d[0]+=1900;
Line 146  MAIN: Line 254  MAIN:
         $source =~ s/^.*\/([^\/]*)$/$1/;          $source =~ s/^.*\/([^\/]*)$/$1/;
         $source = $dbh->quote($source);          $source = $dbh->quote($source);
         print STDERR "Файл: $source, дата: $CreatedAt ";          print STDERR "Файл: $source, дата: $CreatedAt ";
                   my %TourDefaults=('CreatedAt'=>$CreatedAt);
           my %QuestionDefaults=();
           my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч'));
         while (($key, $value) = getField(\*INFD, $dbh)) {          while (($key, $value) = getField(\*INFD, $dbh)) {
             last if (!$key);              last if (!$key);
                           
             if ($key =~ /Мета/) {              if ($key =~ /Мета/) {
                 $value =~ s/[^\d]*//g;                  next;   # This is obsolete
                 $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE              }
                                         MetaId=$value");              if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {               
                 $sth->execute                   ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
                     or die "Invalid Meta field: $value";                  if (!$TournamentId)  {
                 $ParentId = ($sth->fetchrow)[0];                      last;
                 next;                  }       
             }                  $sth = $dbh->prepare("UPDATE Tournaments SET
             if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {                                       Title=$value, Type='Ч', 
                 $ParentId = &SelectGroup($dbh, $value)                                       ParentId=$ParentId, 
                     unless ($ParentId);                                       FileName=$source, 
                 $sth = $dbh->prepare("INSERT INTO Tournaments                                       CreatedAt=$CreatedAt
                                      (Title, Type, ParentId, FileName,                                        WHERE
                                       CreatedAt)                                        Id=$TournamentId");
                                       VALUES ($value, 'Ч', $ParentId,   
                                        $source, $CreatedAt)");  
                 $sth->execute;                  $sth->execute;
                 $TournamentId = $sth->{mysql_insertid};  
                 next;                  next;
             }              }
             if ($key =~ /Тур/) {              if ($key =~ /Тур/) {
Line 177  MAIN: Line 284  MAIN:
                               WHERE Id=$TourId");                                WHERE Id=$TourId");
                 }                  }
                 $qnum = 0;                  $qnum = 0;
                 $qtype = 'Ч';                  $TourNum++;
                 $sth = $dbh->prepare("INSERT INTO Tournaments                  $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
                                       (Title, Type, ParentId, CreatedAt)                                      \%TourDefaults);
                                       VALUES ($value, 'Т', $TournamentId,                   %QuestionDefaults=%QuestionGlobalDefaults;
                                       $CreatedAt)");                  $QuestionId=0;
                 $sth->execute;                  next;   
                 $TourId = $sth->{mysql_insertid};  
                 next;  
             }  
             if ($key =~ /Вид/) {  
                 $qtype = $value;  
                 $qtype =~ s/\'//g;  
                 next;  
             }              }
             if ($key =~ /Вопрос/) {              if ($key =~ /Вопрос/) {
                   if (!$TourId) {
                       $qnum = 0;
                       $TourNum++;
                       $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
                                          \%TourDefaults);
                       %QuestionDefaults=%QuestionGlobalDefaults;
                   }
                 my $query = "INSERT INTO Questions                   my $query = "INSERT INTO Questions 
                              (ParentId, Number, Type)                                (ParentId, Number) 
                              VALUES ($TourId, $qnum+1, \'$qtype\')";                               VALUES ($TourId, $qnum+1)";
                 $sth = $dbh->prepare($query);                  $sth = $dbh->prepare($query);
                 $sth->execute or print $query;;                  $sth->execute or print $query;;
                 $QuestionId = $sth->{mysql_insertid};                  $QuestionId = $sth->{mysql_insertid};
                 &UpdateQuestion($dbh, $QuestionId, "Question", $value);                  &UpdateQuestion($dbh, $QuestionId, "Question", $value);
                   while (my ($key,$value)=each %QuestionDefaults) {
                       &UpdateQuestion($dbh, $QuestionId, $key, $value);
                   }               
                 $qnum++;                  $qnum++;
                 $all_qnum++;                  $all_qnum++;
                 next;                  next;
Line 208  MAIN: Line 318  MAIN:
                 &UpdateQuestion($dbh, $QuestionId, "Answer", $value);                  &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
                 next;                  next;
             }              }
               
   
             if ($key =~ /Автор/) {  
                 &UpdateQuestion($dbh, $QuestionId, "Authors", $value);  
                 next;  
             }  
               
   
             if ($key =~ /Источник/) {  
                 &UpdateQuestion($dbh, $QuestionId, "Sources", $value);  
                 next;  
             }  
               
   
             if ($key =~ /Комментари/) {              if ($key =~ /Комментари/) {
                 &UpdateQuestion($dbh, $QuestionId, "Comments", $value);                  &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
                 next;                  next;
             }              }
                           
               my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
   
             if ($key =~ /URL/ || key =~ /Ссылка/) {              if (scalar @Fields) {
                 &UpdateTournament($dbh, $TournamentId, "URL", $value);                  my $word = shift @Fields;
                 next;                  my $field = $QuestionFields{$word};
             }                  if ($QuestionId) {
                                   &UpdateQuestion($dbh, $QuestionId, $field, $value);
                   } elsif ($TourId) {
             if ($key =~ /Копирайт/) {                      $QuestionDefaults{$field}=$value;
                 &UpdateTournament($dbh, $TournamentId, "Copyright", $value);                  } else {
                 next;                      $QuestionGlobalDefaults{$field}=$value;
             }                  }
               
   
             if ($key =~ /Инфо/) {  
                 &UpdateTournament($dbh, $TournamentId, "Info", $value);  
                 next;                  next;
             }              }
               
   
             if ($key =~ /Редактор/) {              @Fields = grep { $key =~ /$_/ } keys %TourFields;
                 &UpdateTournament($dbh, $TournamentId, "Editors", $value);  
                 next;  
             }  
               
   
             if ($key =~ /Обработан/) {              if (scalar @Fields) {
                 &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value);                  my $word = shift @Fields;
                 next;                  my $field = $TourFields{$word};
                               }                  if ($QuestionId) {
                                   print STDERR "ОШИБКА: $key $value недопустимы после",
             if ($key =~ /Дата/) {                      " начала вопросов\n";
                 if ($TourId) {                  } elsif ($TourId) {
                     &UpdateTournament($dbh, $TourId, "PlayedAt", $value);                      &UpdateTournament($dbh, $TourId, $field, $value);
                 } else {                  } else {
                     &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);                      &UpdateTournament($dbh, $TournamentId, $field, $value);
                       $TourDefaults{$field}=$value;
                 }                  }
                 next;                  next;
             }              }
   
                           
             #              #
             # If we are here, something got wrong!              # If we are here, something got wrong!

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


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