Diff for /db/prgsrc/updatedb.pl between versions 1.43 and 1.44

version 1.43, 2008/11/14 11:23:03 version 1.44, 2009/05/02 21:23:07
Line 59  my ($sth); Line 59  my ($sth);
   
 use DBI;  use DBI;
 use strict;  use strict;
   use Data::Dumper;
 my $isunsorted=0;  my $isunsorted=0;
 sub UpdateParents {  sub UpdateParents {
     my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_;      my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_;
Line 76  FROM Tournaments WHERE Id = $ParentId"); Line 77  FROM Tournaments WHERE Id = $ParentId");
     }      }
 }  }
   
   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/;
     }
     print Dumper($from, $to);
     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/[ ]//g;          s/[
   ]//g;
         if ($key && /^\s*$/) {          if ($key && /^\s*$/) {
             chomp $value;              chomp $value;
             $value =~ s/\s+$//;              $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} || '01';                $value = {'PlayedAt'=>$from, 'PlayedAt2'=>$to};
                 $value =~ s/-(.*)-/-$month-/;  
                 $value =~ s/-00*$/-01/;  
             }              }
             if ($key eq 'Автор') {$value=~s/\.$//;}              if ($key eq 'Автор') {$value=~s/\.$//;}
             $value = $dbh->quote($value);  
             return ($key, $value);              return ($key, $value);
         }          }
         next if (/^\s*$/);          next if (/^\s*$/);
Line 111  sub getField { Line 127  sub getField {
     }      }
     if ($key && $value) {      if ($key && $value) {
         $value=~s/\s+$//;          $value=~s/\s+$//;
         $value = $dbh->quote($value);                return ($key, $value);
         return ($key, $value);  
     }      }
     return (0, 0);      return (0, 0);
 }  }
Line 157  sub SelectGroup { Line 172  sub SelectGroup {
                 $isunsorted=1;                  $isunsorted=1;
                 $sth = $dbh->prepare("INSERT INTO Tournaments                  $sth = $dbh->prepare("INSERT INTO Tournaments
                               (Title, Type, ParentId, FileName,CreatedAt)                                 (Title, Type, ParentId, FileName,CreatedAt) 
                                VALUES ($TourName, 'Ч', $ParentId,                                  VALUES (".$dbh->quote($TourName).", 'Ч', $ParentId, 
                                        $source,NOW())");                                         $source,NOW())");
                 $sth->execute;                  $sth->execute;
                 my $TournamentId = $sth->{mysql_insertid};                  my $TournamentId = $sth->{mysql_insertid};
Line 169  sub SelectGroup { Line 184  sub SelectGroup {
   
 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) = @_;
           
     if (($field eq 'Type') && ($value eq "'Д'")) {      if (($field eq 'Type') && ($value eq "Д")) {
          $value = "'ЧД'";           $value = "ЧД";
     }      }
     $dbh->do("UPDATE Questions SET $field=$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;
 }  }
Line 187  sub UpdateQuestion { Line 213  sub UpdateQuestion {
 sub CheckFile {  sub CheckFile {
     my ($dbh, $source, $title) = @_;      my ($dbh, $source, $title) = @_;
     my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments      my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
                              WHERE FileName=$source AND Type='Ч'");                               WHERE FileName=? AND Type='Ч'");
     $sth->execute or die $dbh->errstr;      $sth->execute($source) or die $dbh->errstr;
     my @arr = $sth->fetchrow;      my @arr = $sth->fetchrow;
     if (! scalar @arr) {      if (! scalar @arr) {
         return SelectGroup($dbh,$source,$title);          return SelectGroup($dbh,$source,$title);
Line 244  sub GetTours { Line 270  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, Number,CreatedAt)                                (Title, Type, ParentId, Number,CreatedAt) 
                              VALUES ($title, 'Т', $ParentId, $TourNum,NOW())");                               VALUES (?, 'Т', $ParentId, $TourNum,NOW())");
     $sth->execute;      $sth->execute($title);
     my $TourId = $sth->{mysql_insertid};      my $TourId = $sth->{mysql_insertid};
     while (my ($key,$value)=each %$rh_defaults) {      while (my ($key,$value)=each %$rh_defaults) {
         &UpdateTournament($dbh, $TourId, $key, $value);              &UpdateTournament($dbh, $TourId, $key, $value);
     }      }
     return $TourId;      return $TourId;
 }  }
Line 267  MAIN: Line 293  MAIN:
                       'Инфо' => 'Info', 'URL' => 'URL',                        'Инфо' => 'Info', 'URL' => 'URL',
                       'Ссылка' => 'URL', 'Редактор' => 'Editors',                        'Ссылка' => 'URL', 'Редактор' => 'Editors',
                       'Обработан'=> 'EnteredBy',                        'Обработан'=> 'EnteredBy',
                       'Дата'=>'PlayedAt');                        'Дата'=>'PlayedAt', 'PlayedAt2'=>'PlayedAt2');
     #      #
     # Inherited fields for a Question      # Inherited fields for a Question
     #      #
Line 298  if ($dbh->get_info( 18 )=~/^(5|(4.1))/) Line 324  if ($dbh->get_info( 18 )=~/^(5|(4.1))/)
         $d[2]=sprintf("%02d",$d[2]);          $d[2]=sprintf("%02d",$d[2]);
         $d[0]+=1900;          $d[0]+=1900;
         my $UnquotedCreated=join('-', @d);          my $UnquotedCreated=join('-', @d);
         my ($CreatedAt) = $dbh->quote($UnquotedCreated);          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";
Line 306  if ($dbh->get_info( 18 )=~/^(5|(4.1))/) Line 332  if ($dbh->get_info( 18 )=~/^(5|(4.1))/)
         $source =~ s/^.*\/([^\/]*)$/$1/;          $source =~ s/^.*\/([^\/]*)$/$1/;
         my $unquotedsource=$source;          my $unquotedsource=$source;
         $unquotedsource=~s/\.txt\s*$//;          $unquotedsource=~s/\.txt\s*$//;
         $source = $dbh->quote($source);  
         print STDERR "Файл: $source, дата: $CreatedAt ";          print STDERR "Файл: $source, дата: $CreatedAt ";
         my %TourDefaults=('CreatedAt'=>$CreatedAt);          my %TourDefaults=('CreatedAt'=>$CreatedAt);
         my %QuestionDefaults=();          my %QuestionDefaults=();
         my %QuestionGlobalDefaults=('Type'=>$dbh->quote('Ч'));          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 =~ /Мета/) {
                 next;   # This is obsolete                    next;   # This is obsolete
             }      }
             if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {                     if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {               
                 ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);                    ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
                 if (!$TournamentId)  {                    if (!$TournamentId)  {
                     last;                      last;
                 }                           }     
                 $sth = $dbh->prepare("UPDATE Tournaments SET  
                                      Title=$value, Type='Ч',   
                                      ParentId=$ParentId,   
                                      FileName=$source,   
                                      CreatedAt=$CreatedAt  
                                      WHERE  
                                      Id=$TournamentId");  
                 $sth->execute;  
                 next;  
             }  
             if ($key =~ /Тур/) {  
                 if ($TourId) {  
   
                     $sth = $dbh->prepare("UPDATE Tournaments SET
                                        Title=?, Type='Ч', 
                                        ParentId=?, 
                                        FileName=?, 
                                        CreatedAt=?
                                        WHERE
                                        Id=?");
                     $sth->execute($value,$ParentId,$source,$CreatedAt,$TournamentId);
                     next;
       }
       if ($key =~ /Тур/) {      
                     if ($TourId) {
                         $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum                          $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
                               WHERE Id=$TourId");                                WHERE Id=$TourId");
                 }                    }
                 $qnum = 0;                    $qnum = 0;
                 $TourNum++;                    $TourNum++;
                 $TourDefaults{'FileName'}=$dbh->quote(                    $TourDefaults{'FileName'}= "$unquotedsource.$TourNum";
                                                 "$unquotedsource.$TourNum"                                                  
                                                 );  
                 $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,                    $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
                                    \%TourDefaults);                                     \%TourDefaults);
                 %QuestionDefaults=%QuestionGlobalDefaults;                    %QuestionDefaults=%QuestionGlobalDefaults;
                 $QuestionId=0;                    $QuestionId=0;
                 next;                       next; 
             }      }
             if ($key =~ /Вопрос/) {      if ($key =~ /Вопрос/) {
                 if (!$TourId) {                    if (!$TourId) {
                     $qnum = 0;                      $qnum = 0;
                     $TourNum++;                      $TourNum++;     
                     $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,                      $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
                                        \%TourDefaults);                                         \%TourDefaults);
                     %QuestionDefaults=%QuestionGlobalDefaults;          %QuestionDefaults=%QuestionGlobalDefaults;
                 }                  }
                 my $query = "INSERT INTO Questions                   my $query = "INSERT INTO Questions 
                              (ParentId, Number)                                (ParentId, Number) 
                              VALUES ($TourId, $qnum+1)";                               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) {                  while (my ($key,$value)=each %QuestionDefaults) {
                     &UpdateQuestion($dbh, $QuestionId, $key, $value);                      &UpdateQuestion($dbh, $QuestionId, $key, $value);
                 }                                 }               
                 $qnum++;                  $qnum++;
                 $all_qnum++;                  $all_qnum++;
                 next;                  next;
             }      }
       if ($key =~ /Ответ/) {
             if ($key =~ /Ответ/) {        &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
                 &UpdateQuestion($dbh, $QuestionId, "Answer", $value);                    next;
                 next;      }
             }  
   
   
             if ($key =~ /Зач[её]т/) {  
                 &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value);  
                 next;  
             }  
   
   
   
             if ($key =~ /Комментари/) {  
                 &UpdateQuestion($dbh, $QuestionId, "Comments", $value);  
                 next;  
             }  
   
       if ($key =~ /Зач[её]т/) {
                     &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value);
                     next;
       }
   
                   if ($key =~ /Комментари/) {
             my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;                  &UpdateQuestion($dbh, $QuestionId, "Comments", $value);
                   next;
       }
      
       my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
   
             if (scalar @Fields) {      if (scalar @Fields) {
                 my $word = shift @Fields;                    my $word = shift @Fields;
                 my $field = $QuestionFields{$word};                    my $field = $QuestionFields{$word};
                 if ($QuestionId) {                    if ($QuestionId) {
                     &UpdateQuestion($dbh, $QuestionId, $field, $value);            &UpdateQuestion($dbh, $QuestionId, $field, $value);
                 } elsif ($TourId) {                    } elsif ($TourId) {
                     $QuestionDefaults{$field}=$value;                      $QuestionDefaults{$field}=$value;
                 } else {                    } else {
                     $QuestionGlobalDefaults{$field}=$value;                      $QuestionGlobalDefaults{$field}=$value;
                 }                    }
                 next;                    next;
             }      }
   
             @Fields = grep { $key =~ /$_/ } keys %TourFields;      @Fields = grep { $key =~ /$_/ } keys %TourFields;
   
             if (scalar @Fields) {      if (scalar @Fields) {
                 my $word = shift @Fields;                  my $word = shift @Fields;
                 my $field = $TourFields{$word};                  my $field = $TourFields{$word};
                 if ($QuestionId) {                  my $updateId;
                   if ($QuestionId) {
                     print STDERR "ОШИБКА: $key $value недопустимы после",                      print STDERR "ОШИБКА: $key $value недопустимы после",
                     " начала вопросов\n";                      " начала вопросов\n";
                 } elsif ($TourId) {                  } else {
                     &UpdateTournament($dbh, $TourId, $field, $value);                    if ($TourId) {
                 } else {                      $updateId = $TourId;
                     &UpdateTournament($dbh, $TournamentId, $field, $value);          } else {
                     $TourDefaults{$field}=$value;            $updateId = $TournamentId;
                 }            $TourDefaults{$field}=$value;
                 next;          }
             }          &UpdateTournament($dbh, $updateId, $field, $value);
                   }
                   next;
       }
   
                           
             #              #

Removed from v.1.43  
changed lines
  Added in v.1.44


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