Diff for /db/prgsrc/updatedb.pl between versions 1.5 and 1.48

version 1.5, 2000/10/18 01:52:16 version 1.48, 2010/04/24 18:13:03
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, $opt_n);
   
   use Getopt::Std;
   my $unsortedname="../dump/unsorted";
   getopts('in');
   #open STDERR, ">errors";
   my $Interactive=$opt_i || 0;
   my $newOnly = $opt_n ||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 33  my (%RevMonths) = Line 49  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($/);  
   
   
   
 use DBI;  use DBI;
 use strict;  use strict;
   use Data::Dumper;
   my $isunsorted=0;
 sub UpdateParents {  sub UpdateParents {
     my ($dbh, $ParentId, $all_qnum) = @_;      my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_;
     if ($ParentId) {      if ($ParentId) {
         my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId           my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId, CreatedAt
 FROM Tournaments WHERE Id = $ParentId");  FROM Tournaments WHERE Id = $ParentId");
         $sth1->execute;          $sth1->execute;
         my ($q, $p) = ($sth1->fetchrow)[0, 1];          my ($q, $p,$c) = ($sth1->fetchrow)[0, 1, 2];
           $c=$CreatedAt if $CreatedAt && ($CreatedAt gt $c);
           my $qc=$dbh->quote($c);
         $dbh->do("UPDATE Tournaments SET           $dbh->do("UPDATE Tournaments SET 
                   QuestionsNum=$q + $all_qnum                     QuestionsNum=$q + $all_qnum, CreatedAt=$qc
                   WHERE Id = $ParentId");                    WHERE Id = $ParentId");
         &UpdateParents($dbh, $p, $all_qnum);          &UpdateParents($dbh, $p, $all_qnum,$c);
     }      }
 }  }
   
   sub parseDate {
     my $value = shift;
     my ($from, $to) = split /\s+\-+\s+/, $value;
     $from =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
     my($month) = $RevMonths{$2} || '01';
     $from =~ s/-(.*)-/-$month-/;
     $from =~ s/-00*$/-01/;
     if ($to) {
       $to =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
       $month = $RevMonths{$2} || '01';    
       $to =~ s/-(.*)-/-$month-/;
       $to =~ s/-00*$/-01/;
     }
     return ($from, $to);
     
   }
   
 sub getField {  sub getField {
     my($desc, $dbh) = @_;      my($desc, $dbh) = @_;
     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;
               $value =~ s/\s+$//;
             chomp $key;              chomp $key;
             if ($key eq 'Дата') {              if ($key eq 'Дата') {
                 $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;                my ($from, $to) = parseDate($value);
                 my($month) = $RevMonths{$2};                $value = {'PlayedAt'=>$from, 'PlayedAt2'=>$to};
                 $value =~ s/$2/$month/;  
             }              }
             $value = $dbh->quote($value);              if ($key eq 'Автор') {$value=~s/\.$//;}
             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) {
             $value .= $_;              $value .= $_."\n";
             next;              next;
         }          }
     }      }
     if ($key && $value) {      if ($key && $value) {
         $value = $dbh->quote($value);          $value=~s/\s+$//sm;
         return ($key, $value);                return ($key, $value);
     }      }
     return (0, 0);      return (0, 0);
 }  }
   
 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  
       if ($Interactive) {    
           $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);
           }
       } else {
   # Теперь, если файла нет в дереве турниров, никаких вопросов не 
   # задаётся, а вместо этого он добавляется в группу 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,CreatedAt) 
                                  VALUES (".$dbh->quote($TourName).", 'Ч', $ParentId, 
                                          ?,NOW())");
                   $sth->execute($source);
                   my $TournamentId = $sth->{mysql_insertid};
                   return ($TournamentId,$ParentId);
           }
                   
       
 }  }
   
 sub UpdateTournament {  sub UpdateTournament {
     my ($dbh, $TournamentId, $field, $value) = @_;      my ($dbh, $TournamentId, $field, $value) = @_;
     $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")      if (ref $value eq 'HASH') {
         or die $dbh->errstr;              # It means that $value contains information about several fields
               foreach my $k(keys %$value) {
                 if ($value->{$k}) {
             &UpdateTournament($dbh, $TournamentId, $k, $value->{$k});
           }
         }
       } else {    
         my $v = $dbh->quote($value);
         $dbh->do("UPDATE Tournaments SET $field=$v WHERE Id=$TournamentId")
               or die $dbh->errstr;
       }
 }  }
   
 sub UpdateQuestion {  sub UpdateQuestion {
     my ($dbh, $QuestionId, $field, $value) = @_;      my ($dbh, $QuestionId, $field, $value) = @_;
     $dbh->do("UPDATE Questions SET $field=$value       
       if (($field eq 'Type') && ($value eq "Д")) {
            $value = "ЧД";
       }
       my $v = $dbh->quote($value);
       $dbh->do("UPDATE Questions SET $field=$v
                 WHERE QuestionId=$QuestionId")                  WHERE QuestionId=$QuestionId")
         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=? AND Type='Ч'");
       $sth->execute($source) or die $dbh->errstr;
       my @arr = $sth->fetchrow;
       if (! scalar @arr) {
           return SelectGroup($dbh,$source,$title);
       }
       my($Id,$ParentId,$QuestionsNum)=@arr;
       if($QuestionsNum) { 
           if ($newOnly) {
             return (0,0);
           }
           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,CreatedAt) 
                                VALUES (?, 'Т', $ParentId, $TourNum,NOW())");
       $sth->execute($title);
       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', 'PlayedAt2'=>'PlayedAt2');
       #
       # 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";
       if ($dbh->get_info( 18 )=~/^(5|(4.1))/)  {$dbh->do("SET NAMES 'koi8r'");}
       my @sources;        
       open UNSORTED, ">$unsortedname";
     while ($source = shift) {      while ($source = shift) {
          push @sources,glob($source);
       }
       foreach $source(@sources) {
           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[1]=sprintf("%02d",$d[1]);
           $d[2]=sprintf("%02d",$d[2]);
         $d[0]+=1900;          $d[0]+=1900;
         my ($CreatedAt) = $dbh->quote( join('-', @d));          my $UnquotedCreated=join('-', @d);
         print STDERR "File created on: $CreatedAt\n";          my ($CreatedAt) = $UnquotedCreated;
           
         open INFD, $source           open INFD, $source 
             or die "Can't open input file: $!\n";              or die "Can't open input file: $!\n";
                   
         $source =~ s/^.*\/([^\/]*)$/$1/;          $source =~ s/^.*\/([^\/]*)$/$1/;
         $source = $dbh->quote($source);          my $unquotedsource=$source;
         print STDERR "Processing file: $source \n";          $unquotedsource=~s/\.txt\s*$//;
                   print STDERR "Файл: $source, дата: $CreatedAt ";
           my %TourDefaults=('CreatedAt'=>$CreatedAt);
           my %QuestionDefaults=();
           my %QuestionGlobalDefaults=('Type'=>'Ч');
         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;                    }     
             }  
             if ($key =~ /Чемпионат/) {                    $sth = $dbh->prepare("UPDATE Tournaments SET
                 $ParentId = &SelectGroup($dbh, $value)                                       Title=?, Type='Ч', 
                     unless ($ParentId);                                       ParentId=?, 
                 $sth = $dbh->prepare("INSERT INTO Tournaments                                       FileName=?, 
                                      (Title, Type, ParentId, FileName,                                        CreatedAt=?
                                       CreatedAt)                                        WHERE
                                       VALUES ($value, 'Ч', $ParentId,                                        Id=?");
                                        $source, $CreatedAt)");                    $sth->execute($value,$ParentId,$source,$CreatedAt,$TournamentId);
                 $sth->execute;                    next;
                 $TournamentId = $sth->{mysql_insertid};      }
                 next;      if ($key =~ /Тур/) {      
             }                    if ($TourId) {
             if ($key =~ /Тур/) {                          $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                 if ($TourId) {  
                     $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum  
                               WHERE Id=$TourId");                                WHERE Id=$TourId");
                 }                    }
                 $qnum = 0;                    $qnum = 0;
                 $qtype = 'Ч';                    $TourNum++;
                 $sth = $dbh->prepare("INSERT INTO Tournaments                    $TourDefaults{'FileName'}= "$unquotedsource.$TourNum";
                                       (Title, Type, ParentId, CreatedAt)                                                   
                                       VALUES ($value, 'Т', $TournamentId,   
                                       $CreatedAt)");                    $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
                 $sth->execute;                                     \%TourDefaults);
                 $TourId = $sth->{mysql_insertid};                    %QuestionDefaults=%QuestionGlobalDefaults;
                 next;                    $QuestionId=0;
             }                    next; 
             if ($key =~ /Вид/) {      }
                 $qtype = $value;      if ($key =~ /Вопрос/) {
                 $qtype =~ s/\'//g;                    if (!$TourId) {
                 next;                      $qnum = 0;
             }                      $TourNum++;     
             if ($key =~ /Вопрос/) {                      $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
                 my $query = "INSERT INTO Questions                                          \%TourDefaults);
                              (ParentId, Number, Type)           %QuestionDefaults=%QuestionGlobalDefaults;
                              VALUES ($TourId, $qnum+1, \'$qtype\')";                  }
                 $sth = $dbh->prepare($query);                  my $query = "INSERT INTO Questions 
                 $sth->execute or print $query;;                               (ParentId, Number) 
                 $QuestionId = $sth->{mysql_insertid};                               VALUES ($TourId, $qnum+1)";
                 &UpdateQuestion($dbh, $QuestionId, "Question", $value);                  $sth = $dbh->prepare($query);
                 $qnum++;                  $sth->execute or print $query;;
                 $all_qnum++;                  $QuestionId = $sth->{mysql_insertid};
                 next;                  &UpdateQuestion($dbh, $QuestionId, "Question", $value);
             }                  while (my ($key,$value)=each %QuestionDefaults) {
             &UpdateQuestion($dbh, $QuestionId, "Answer", $value)                       &UpdateQuestion($dbh, $QuestionId, $key, $value);
                 if ($key =~ /Ответ/);                  }               
                               $qnum++;
             &UpdateQuestion($dbh, $QuestionId, "Authors", $value)                   $all_qnum++;
                 if ($key =~ /Автор/);                  next;
                   }
             &UpdateQuestion($dbh, $QuestionId, "Sources", $value)       if ($key =~ /Ответ/) {
                 if ($key =~ /Источник/);        &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
                                 next;
             &UpdateQuestion($dbh, $QuestionId, "Comments", $value)       }
                 if ($key =~ /Комментарий/);  
                   if ($key =~ /Зач[её]т/) {
             &UpdateTournament($dbh, $TournamentId, "URL", $value)                    &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value);
                 if ($key =~ /URL/);                    next;
                   }
             &UpdateTournament($dbh, $TournamentId, "Copyright", $value)  
                 if ($key =~ /Копирайт/);      if ($key =~ /Комментари/) {
                               &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
             &UpdateTournament($dbh, $TournamentId, "Info", $value)                  next;
                 if ($key =~ /Инфо/);      }
                  
             &UpdateTournament($dbh, $TournamentId, "Editors", $value)      my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
                 if ($key =~ /Редактор/);  
       if (scalar @Fields) {
                     my $word = shift @Fields;
                     my $field = $QuestionFields{$word};
                     if ($QuestionId) {
             &UpdateQuestion($dbh, $QuestionId, $field, $value);
                     } elsif ($TourId) {
                       $QuestionDefaults{$field}=$value;
                     } else {
                       $QuestionGlobalDefaults{$field}=$value;
                     }
                     next;
       }
   
       @Fields = grep { $key =~ /$_/ } keys %TourFields;
   
       if (scalar @Fields) {
                   my $word = shift @Fields;
                   my $field = $TourFields{$word};
                   my $updateId;
                   if ($QuestionId) {
                       print STDERR "ОШИБКА: $key $value недопустимы после",
                       " начала вопросов\n";
                   } else {
                     if ($TourId) {
                       $updateId = $TourId;
           } else {
             $updateId = $TournamentId;
             $TourDefaults{$field}=$value;
           }
           &UpdateTournament($dbh, $updateId, $field, $value);
                   }
                   next;
       }
   
                           
             &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value)              #
                 if ($key =~ /Обработан/);              # If we are here, something got wrong!
               #
               print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
                           
             if ($key =~ /Дата/) {  
                 if ($TourId) {  
                     &UpdateTournament($dbh, $TourId, "PlayedAt", $value);  
                 } else {  
                     &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);  
                 }  
             }  
         }          }
         $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum          $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                         WHERE Id=$TourId");                          WHERE Id=$TourId");
         $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum          $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
                         WHERE Id=$TournamentId");                          WHERE Id=$TournamentId");
         &UpdateParents($dbh, $ParentId, $all_qnum);                       &UpdateParents($dbh, $ParentId, $all_qnum,$UnquotedCreated);            
         print STDERR "Total number of questions: $all_qnum \n";          print STDERR "Всего вопросов: $all_qnum \n";
     }      }
       close UNSORTED;
       unlink $unsortedname unless $isunsorted;
     $dbh->disconnect;      $dbh->disconnect;
 }  }

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


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