File:  [Local Repository] / db / prgsrc / updatedb.pl
Revision 1.37: download - view: text, annotated - select for diffs - revision graph
Wed Jan 7 14:31:31 2004 UTC (20 years, 4 months ago) by boris
Branches: MAIN
CVS tags: HEAD
Added field PassCriteria

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

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