File:  [Local Repository] / db / prgsrc / updatedb.pl
Revision 1.46: download - view: text, annotated - select for diffs - revision graph
Sun May 24 10:10:17 2009 UTC (14 years, 11 months ago) by roma7
Branches: MAIN
CVS tags: HEAD
CreatedAt2 is added again. KandId is added to the Tournament but is not filled yet

    1: #!/usr/local/bin/perl 
    2: 
    3: =head1 NAME
    4: 
    5: updatedb.pl - a script for creation of new database. 
    6: 
    7: =head1 SYNOPSIS
    8: 
    9: updatedb.pl B<[-i]> I<file1> I<file2>....
   10: 
   11: 
   12: =head1 DESCRIPTION
   13: 
   14: Updates information in the B<chgk> databse. Uses file
   15: 
   16: =head1 OPTIONS
   17: 
   18: =item B<-i> 
   19: 
   20: Ask about ParentId. 
   21: 
   22: 
   23: =head1 BUGS
   24: 
   25: The database, user and password are hardcoded. 
   26: 
   27: =head1 AUTHOR
   28: 
   29: Dmitry Rubinstein
   30: 
   31: =head1 $Id: updatedb.pl,v 1.46 2009/05/24 10:10:17 roma7 Exp $
   32: 
   33: =cut
   34: 
   35: use vars qw($opt_i);
   36: 
   37: use Getopt::Std;
   38: my $unsortedname="../dump/unsorted";
   39: getopts('i');
   40: #open STDERR, ">errors";
   41: my $Interactive=$opt_i || 0;
   42: my $DUMPDIR = $ENV{DUMPDIR} || "../dump";
   43: 
   44: my (%RevMonths) = 
   45:     ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
   46:      'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
   47:      'Dec', '12', 
   48:      'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',
   49:      'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',
   50:      'DEC', '12', 
   51:      'Янв', '1', 'Фев', '2', 'Мар', '3', 'Апр', '4', 'Май', '5',
   52:      'Июн', '6', 'Июл', '7', 'Авг', '8', 'Сен', '9', 
   53:      'Окт', '10', 'Ноя', '11', 'Дек', '12');
   54: my ($sth);
   55: 
   56: 
   57: 
   58: 
   59: 
   60: use DBI;
   61: use strict;
   62: use Data::Dumper;
   63: my $isunsorted=0;
   64: sub UpdateParents {
   65:     my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_;
   66:     if ($ParentId) {
   67: 	my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId, CreatedAt
   68: FROM Tournaments WHERE Id = $ParentId");
   69: 	$sth1->execute;
   70: 	my ($q, $p,$c) = ($sth1->fetchrow)[0, 1, 2];
   71: 	$c=$CreatedAt if $CreatedAt && ($CreatedAt gt $c);
   72: 	my $qc=$dbh->quote($c);
   73: 	$dbh->do("UPDATE Tournaments SET 
   74:                   QuestionsNum=$q + $all_qnum, CreatedAt=$qc
   75:                   WHERE Id = $ParentId");
   76: 	&UpdateParents($dbh, $p, $all_qnum,$c);
   77:     }
   78: }
   79: 
   80: sub parseDate {
   81:   my $value = shift;
   82:   my ($from, $to) = split /\s+\-+\s+/, $value;
   83:   $from =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
   84:   my($month) = $RevMonths{$2} || '01';
   85:   $from =~ s/-(.*)-/-$month-/;
   86:   $from =~ s/-00*$/-01/;
   87:   if ($to) {
   88:     $to =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
   89:     $month = $RevMonths{$2} || '01';    
   90:     $to =~ s/-(.*)-/-$month-/;
   91:     $to =~ s/-00*$/-01/;
   92:   }
   93:   return ($from, $to);
   94:   
   95: }
   96: 
   97: sub getField {
   98:     my($desc, $dbh) = @_;
   99:     my($key);
  100:     my($value) = ('');
  101:     while (<$desc>) {
  102: 	s/[
  103: ]//g;
  104: 	if ($key && /^\s*$/) {
  105: 	    chomp $value;
  106:             $value =~ s/\s+$//;
  107: 	    chomp $key;
  108: 	    if ($key eq 'Дата') {
  109: 	      my ($from, $to) = parseDate($value);
  110: 	      $value = {'PlayedAt'=>$from, 'PlayedAt2'=>$to};
  111: 	    }
  112: 	    if ($key eq 'Автор') {$value=~s/\.$//;}
  113: 	    return ($key, $value);
  114: 	}
  115: 	next if (/^\s*$/);
  116: 	
  117: 	if (!$key && /^(.*?)[:\.]\s*(.*)$/s) {
  118: 	    $key = $1;
  119: 	    $value=$2;
  120: 	    next;
  121: 	}
  122: 	if ($key) {
  123: 	    $value .= $_."\n";
  124: 	    next;
  125: 	}
  126:     }
  127:     if ($key && $value) {
  128:         $value=~s/\s+$//sm;
  129: 	      return ($key, $value);
  130:     }
  131:     return (0, 0);
  132: }
  133: 
  134: sub SelectGroup {
  135:     my ($dbh, $source, $TourName) = @_;
  136:     my ($sth, $ParentId, $i, @arr);
  137: 
  138:     if ($Interactive) {    
  139:     	$sth = $dbh->prepare("SELECT Id, Title FROM
  140: 		Tournaments WHERE Type = 'Г'");
  141:     	$sth->execute;
  142:     	print "Выберите группу для турнира:\n$TourName, файл $source\n\n";
  143:     	while (@arr=$sth->fetchrow) {
  144: 		print "[$arr[0]] $arr[1]\n";
  145:     	}
  146:     	$ParentId = <STDIN>;
  147:     	chomp $ParentId;
  148:     	if (!$ParentId) {
  149: 		print "Пропускаем файл $source\n";
  150: 		print STDERR "Файл $source отвергнут оператором\n";
  151: 		return (0,0);
  152:     	} else {
  153: 		print "Вы выбрали турнир: $ParentId\n";
  154: 		$sth = $dbh->prepare("INSERT INTO Tournaments
  155: 			      (Title, Type, ParentId, FileName) 
  156: 			       VALUES ($TourName, 'Ч', $ParentId, 
  157:                                        $source)");
  158: 		$sth->execute;
  159: 		my $TournamentId = $sth->{mysql_insertid};
  160: 		return ($TournamentId,$ParentId);
  161: 	}
  162:     } else {
  163: # Теперь, если файла нет в дереве турниров, никаких вопросов не 
  164: # задаётся, а вместо этого он добавляется в группу 9999
  165: 		$ParentId = 9999;
  166: 		my $tempsource=$source;
  167: 		my $temptname=$TourName;
  168:                 $tempsource=~s/^\'(.*)\'$/$1/;
  169:                 $temptname=~s/^\'(.*)\'$/$1/;
  170: 	   	print UNSORTED "$tempsource".((12 -length($source))x' ')."\t$temptname\n";
  171: 		$isunsorted=1;
  172: 		$sth = $dbh->prepare("INSERT INTO Tournaments
  173: 			      (Title, Type, ParentId, FileName,CreatedAt) 
  174: 			       VALUES (".$dbh->quote($TourName).", 'Ч', $ParentId, 
  175:                                        $source,NOW())");
  176:                 $sth->execute;
  177: 		my $TournamentId = $sth->{mysql_insertid};
  178: 		return ($TournamentId,$ParentId);
  179: 	}
  180: 		
  181:     
  182: }
  183: 
  184: sub UpdateTournament {
  185:     my ($dbh, $TournamentId, $field, $value) = @_;
  186:     if (ref $value eq 'HASH') {
  187: 	    # It means that $value contains information about several fields
  188: 	    foreach my $k(keys %$value) {
  189: 	      if ($value->{$k}) {
  190:           &UpdateTournament($dbh, $TournamentId, $k, $value->{$k});
  191:         }
  192:       }
  193:     } else {    
  194:       my $v = $dbh->quote($value);
  195:       $dbh->do("UPDATE Tournaments SET $field=$v WHERE Id=$TournamentId")
  196: 	    or die $dbh->errstr;
  197:     }
  198: }
  199: 
  200: sub UpdateQuestion {
  201:     my ($dbh, $QuestionId, $field, $value) = @_;
  202:     
  203:     if (($field eq 'Type') && ($value eq "Д")) {
  204:          $value = "ЧД";
  205:     }
  206:     my $v = $dbh->quote($value);
  207:     $dbh->do("UPDATE Questions SET $field=$v
  208: 		WHERE QuestionId=$QuestionId")
  209: 	or die $dbh->errstr;
  210: }
  211: 
  212: sub CheckFile {
  213:     my ($dbh, $source, $title) = @_;
  214:     my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
  215:                              WHERE FileName=? AND Type='Ч'");
  216:     $sth->execute($source) or die $dbh->errstr;
  217:     my @arr = $sth->fetchrow;
  218:     if (! scalar @arr) {
  219: 	return SelectGroup($dbh,$source,$title);
  220:     }
  221:     my($Id,$ParentId,$QuestionsNum)=@arr;
  222:     if($QuestionsNum) {	
  223: 	print "Файл $source с данными $title уже существует. ",
  224: 	"Заменить?[y/N]\n";
  225: 	my $answer = <STDIN>;
  226: 	if ($answer !~ /^[yY]/) {
  227: 	    return (0,0);
  228: 	} else {
  229: 	    DeleteTournament($dbh,$Id,$ParentId,$QuestionsNum,0);
  230: 	}
  231:     } 
  232:     return($Id,$ParentId);	
  233: }
  234: 
  235: 
  236: sub DeleteTournament {
  237:     my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
  238:     if ($QuestionsNum) {
  239: 	UpdateParents($dbh,$ParentId,-$QuestionsNum);
  240:     }
  241:     my (@Tours) = &GetTours($dbh, $Id);
  242:     foreach my $Tour (@Tours) {
  243: 	DeleteTournament($dbh,$Tour,$Id,0,1);
  244:     }
  245:     my $sth = $dbh->prepare("DELETE FROM Questions
  246:                              WHERE ParentId=$Id");
  247:     $sth->execute or die $dbh->errstr;
  248:     if($DeleteMyself) {
  249: 	$sth = $dbh->prepare("DELETE FROM Tournaments
  250:                              WHERE Id=$Id");
  251: 	$sth->execute or die $dbh->errstr;
  252:     }
  253: }
  254: 
  255: sub GetTours {
  256: 	my ($dbh, $ParentId) = @_;
  257: 	my (@arr, @Tours);
  258: 
  259: 	my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
  260: 		WHERE ParentId=$ParentId ORDER BY Id");
  261: 
  262: 	$sth->execute;
  263: 
  264: 	while (@arr = $sth->fetchrow) {
  265: 		push @Tours, $arr[0];
  266: 	}
  267: 
  268: 	return @Tours;
  269: }
  270: 
  271: sub CreateTour {
  272:     my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;   
  273:     my $sth = $dbh->prepare("INSERT INTO Tournaments
  274: 			     (Title, Type, ParentId, Number,CreatedAt) 
  275: 			     VALUES (?, 'Т', $ParentId, $TourNum,NOW())");
  276:     $sth->execute($title);
  277:     my $TourId = $sth->{mysql_insertid};
  278:     while (my ($key,$value)=each %$rh_defaults) {
  279: 	    &UpdateTournament($dbh, $TourId, $key, $value);
  280:     }
  281:     return $TourId;
  282: }
  283: 		
  284: 
  285: MAIN: 
  286: {
  287:     my($key, $value, $addition);
  288:     #
  289:     # Inherited fields for a Tour or Tournament
  290:     #
  291:     my %TourFields = ('Копирайт' => 'Copyright',
  292: 		      'Инфо' => 'Info', 'URL' => 'URL',
  293: 		      'Ссылка' => 'URL', 'Редактор' => 'Editors',
  294: 		      'Обработан'=> 'EnteredBy',
  295: 		      'Дата'=>'PlayedAt', 'PlayedAt2'=>'PlayedAt2');
  296:     #
  297:     # Inherited fields for a Question
  298:     #
  299:     my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type', 
  300: 			  'Автор' => 'Authors', 'Рейтинг'=>'Rating', 
  301: 			  'Источник' => 'Sources',
  302: 			  'Тема' => 'Topic');
  303: 			  
  304: 		      
  305:     my($source);
  306:     
  307:     my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
  308: 	or die "Can't connect to DB chgk\n";
  309: if ($dbh->get_info( 18 )=~/^(5|(4.1))/)  {$dbh->do("SET NAMES 'koi8r'");}
  310:     my @sources;	
  311:     open UNSORTED, ">$unsortedname";
  312:     while ($source = shift) {
  313:        push @sources,glob($source);
  314:     }
  315:     foreach $source(@sources) {
  316: 	my $TourNum=0;
  317: 	my($PlayedAt) = '';
  318: 	my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
  319: 	my($tournum, $qnum, $all_qnum) = (0, 0, 0);
  320: 	my (@d) = (localtime((stat($source))[9]))[5,4,3];
  321: 	$d[1]++;
  322: 	$d[1]=sprintf("%02d",$d[1]);
  323: 	$d[2]=sprintf("%02d",$d[2]);
  324: 	$d[0]+=1900;
  325: 	my $UnquotedCreated=join('-', @d);
  326: 	my ($CreatedAt) = $UnquotedCreated;
  327: 
  328: 	open INFD, $source 
  329: 	    or die "Can't open input file: $!\n";
  330: 	
  331: 	$source =~ s/^.*\/([^\/]*)$/$1/;
  332: 	my $unquotedsource=$source;
  333: 	$unquotedsource=~s/\.txt\s*$//;
  334: 	print STDERR "Файл: $source, дата: $CreatedAt ";
  335: 	my %TourDefaults=('CreatedAt'=>$CreatedAt);
  336: 	my %QuestionDefaults=();
  337: 	my %QuestionGlobalDefaults=('Type'=>'Ч');
  338: 	while (($key, $value) = getField(\*INFD, $dbh)) {
  339: 	  
  340:     last if (!$key);
  341: 	    
  342:     if ($key =~ /Мета/) {
  343: 		  next;   # This is obsolete
  344:     }
  345:     if ($key =~ /Чемпионат/ || $key =~ /Пакет/) {		
  346: 		  ($TournamentId, $ParentId) = CheckFile($dbh,$source,$value);
  347: 		  if (!$TournamentId)  {
  348: 		    last;
  349: 		  }	
  350: 
  351: 		  $sth = $dbh->prepare("UPDATE Tournaments SET
  352: 			             Title=?, Type='Ч', 
  353:                                      ParentId=?, 
  354:                                      FileName=?, 
  355:                                      CreatedAt=?
  356:                                      WHERE
  357:                                      Id=?");
  358: 		  $sth->execute($value,$ParentId,$source,$CreatedAt,$TournamentId);
  359: 		  next;
  360:     }
  361:     if ($key =~ /Тур/) {      
  362: 		  if ($TourId) {
  363: 	    		$dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
  364: 			      WHERE Id=$TourId");
  365: 		  }
  366: 		  $qnum = 0;
  367: 		  $TourNum++;
  368: 		  $TourDefaults{'FileName'}= "$unquotedsource.$TourNum";
  369: 						
  370: 
  371: 		  $TourId=CreateTour($dbh,$value,$TournamentId,$TourNum,
  372: 				   \%TourDefaults);
  373: 		  %QuestionDefaults=%QuestionGlobalDefaults;
  374: 		  $QuestionId=0;
  375: 		  next;	
  376:     }
  377:     if ($key =~ /Вопрос/) {
  378: 		  if (!$TourId) {
  379: 		    $qnum = 0;
  380: 		    $TourNum++;	    
  381: 		    $TourId=CreateTour($dbh,'1',$TournamentId,$TourNum,
  382: 				       \%TourDefaults);
  383:         %QuestionDefaults=%QuestionGlobalDefaults;
  384:   		}
  385:   		my $query = "INSERT INTO Questions 
  386: 			     (ParentId, Number) 
  387: 			     VALUES ($TourId, $qnum+1)";
  388:   		$sth = $dbh->prepare($query);
  389:   		$sth->execute or print $query;;
  390:   		$QuestionId = $sth->{mysql_insertid};
  391:   		&UpdateQuestion($dbh, $QuestionId, "Question", $value);
  392:   		while (my ($key,$value)=each %QuestionDefaults) {
  393: 		    &UpdateQuestion($dbh, $QuestionId, $key, $value);
  394:   		}		
  395:   		$qnum++;
  396:   		$all_qnum++;
  397:   		next;
  398:     }
  399:     if ($key =~ /Ответ/) {
  400:       &UpdateQuestion($dbh, $QuestionId, "Answer", $value);
  401: 		  next;
  402:     }
  403: 
  404:     if ($key =~ /Зач[её]т/) {
  405: 		  &UpdateQuestion($dbh, $QuestionId, "PassCriteria", $value);
  406: 		  next;
  407:     }
  408: 
  409:     if ($key =~ /Комментари/) {
  410:   		&UpdateQuestion($dbh, $QuestionId, "Comments", $value);
  411:   		next;
  412:     }
  413:    
  414:     my @Fields = grep { $key =~ /$_/ } keys %QuestionFields;
  415: 
  416:     if (scalar @Fields) {
  417: 		  my $word = shift @Fields;
  418: 		  my $field = $QuestionFields{$word};
  419: 		  if ($QuestionId) {
  420:           &UpdateQuestion($dbh, $QuestionId, $field, $value);
  421: 		  } elsif ($TourId) {
  422: 		    $QuestionDefaults{$field}=$value;
  423: 		  } else {
  424: 		    $QuestionGlobalDefaults{$field}=$value;
  425: 		  }
  426: 		  next;
  427:     }
  428: 
  429:     @Fields = grep { $key =~ /$_/ } keys %TourFields;
  430: 
  431:     if (scalar @Fields) {
  432:   		my $word = shift @Fields;
  433:   		my $field = $TourFields{$word};
  434:   		my $updateId;
  435:   		if ($QuestionId) {
  436: 		    print STDERR "ОШИБКА: $key $value недопустимы после",
  437: 		    " начала вопросов\n";
  438:   		} else {
  439:   		  if ($TourId) {
  440:   		    $updateId = $TourId;
  441:         } else {
  442:           $updateId = $TournamentId;
  443:           $TourDefaults{$field}=$value;
  444:         }
  445:         &UpdateTournament($dbh, $updateId, $field, $value);
  446:   		}
  447:   		next;
  448:     }
  449: 
  450: 	    
  451: 	    #
  452: 	    # If we are here, something got wrong!
  453: 	    #
  454: 	    print STDERR "\nЯ НЕ ПОНИМАЮ: $key, $value!\n";
  455: 	    
  456: 	}
  457: 	$dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
  458: 			WHERE Id=$TourId");
  459: 	$dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
  460: 			WHERE Id=$TournamentId");
  461: 	&UpdateParents($dbh, $ParentId, $all_qnum,$UnquotedCreated);		
  462: 	print STDERR "Всего вопросов: $all_qnum \n";
  463:     }
  464:     close UNSORTED;
  465:     unlink $unsortedname unless $isunsorted;
  466:     $dbh->disconnect;
  467: }

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