File:  [Local Repository] / db / prgsrc / updatedb.pl
Revision 1.48: download - view: text, annotated - select for diffs - revision graph
Sat Apr 24 18:13:03 2010 UTC (14 years ago) by roma7
Branches: MAIN
CVS tags: HEAD
Revert database connection data

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

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