Annotation of db/prgsrc/updatedb.pl, revision 1.48
1.1 boris 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:
1.25 roma7 9: updatedb.pl B<[-i]> I<file1> I<file2>....
1.1 boris 10:
11:
12: =head1 DESCRIPTION
13:
14: Updates information in the B<chgk> databse. Uses file
15:
1.25 roma7 16: =head1 OPTIONS
17:
18: =item B<-i>
19:
20: Ask about ParentId.
21:
1.1 boris 22:
23: =head1 BUGS
24:
25: The database, user and password are hardcoded.
26:
27: =head1 AUTHOR
28:
29: Dmitry Rubinstein
30:
1.48 ! roma7 31: =head1 $Id: updatedb.pl,v 1.47 2010-04-24 18:04:14 roma7 Exp $
1.1 boris 32:
33: =cut
34:
1.47 roma7 35: use vars qw($opt_i, $opt_n);
1.25 roma7 36:
37: use Getopt::Std;
1.34 roma7 38: my $unsortedname="../dump/unsorted";
1.47 roma7 39: getopts('in');
1.29 roma7 40: #open STDERR, ">errors";
1.25 roma7 41: my $Interactive=$opt_i || 0;
1.47 roma7 42: my $newOnly = $opt_n ||0;
1.31 boris 43: my $DUMPDIR = $ENV{DUMPDIR} || "../dump";
1.25 roma7 44:
1.1 boris 45: my (%RevMonths) =
1.5 boris 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',
1.13 boris 52: 'Янв', '1', 'Фев', '2', 'Мар', '3', 'Апр', '4', 'Май', '5',
53: 'Июн', '6', 'Июл', '7', 'Авг', '8', 'Сен', '9',
54: 'Окт', '10', 'Ноя', '11', 'Дек', '12');
1.1 boris 55: my ($sth);
56:
1.13 boris 57:
1.3 boris 58:
1.4 boris 59:
1.3 boris 60:
1.1 boris 61: use DBI;
62: use strict;
1.46 roma7 63: use Data::Dumper;
1.28 roma7 64: my $isunsorted=0;
1.1 boris 65: sub UpdateParents {
1.36 roma7 66: my ($dbh, $ParentId, $all_qnum,$CreatedAt) = @_;
1.5 boris 67: if ($ParentId) {
1.46 roma7 68: my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId, CreatedAt
1.5 boris 69: FROM Tournaments WHERE Id = $ParentId");
1.46 roma7 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
1.36 roma7 75: QuestionsNum=$q + $all_qnum, CreatedAt=$qc
1.5 boris 76: WHERE Id = $ParentId");
1.46 roma7 77: &UpdateParents($dbh, $p, $all_qnum,$c);
1.5 boris 78: }
1.1 boris 79: }
80:
1.46 roma7 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: }
1.36 roma7 97:
1.1 boris 98: sub getField {
1.5 boris 99: my($desc, $dbh) = @_;
100: my($key);
101: my($value) = ('');
102: while (<$desc>) {
1.46 roma7 103: s/[
104: ]//g;
105: if ($key && /^\s*$/) {
106: chomp $value;
1.24 roma7 107: $value =~ s/\s+$//;
1.46 roma7 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: }
1.5 boris 127: }
128: if ($key && $value) {
1.46 roma7 129: $value=~s/\s+$//sm;
130: return ($key, $value);
1.5 boris 131: }
132: return (0, 0);
1.1 boris 133: }
134:
135: sub SelectGroup {
1.7 boris 136: my ($dbh, $source, $TourName) = @_;
137: my ($sth, $ParentId, $i, @arr);
1.25 roma7 138:
139: if ($Interactive) {
1.46 roma7 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,
1.25 roma7 158: $source)");
1.46 roma7 159: $sth->execute;
160: my $TournamentId = $sth->{mysql_insertid};
161: return ($TournamentId,$ParentId);
162: }
1.7 boris 163: } else {
1.25 roma7 164: # Теперь, если файла нет в дереве турниров, никаких вопросов не
165: # задаётся, а вместо этого он добавляется в группу 9999
1.46 roma7 166: $ParentId = 9999;
167: my $tempsource=$source;
168: my $temptname=$TourName;
1.33 roma7 169: $tempsource=~s/^\'(.*)\'$/$1/;
170: $temptname=~s/^\'(.*)\'$/$1/;
1.46 roma7 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,
1.47 roma7 176: ?,NOW())");
177: $sth->execute($source);
1.46 roma7 178: my $TournamentId = $sth->{mysql_insertid};
179: return ($TournamentId,$ParentId);
180: }
181:
1.7 boris 182:
1.1 boris 183: }
184:
185: sub UpdateTournament {
1.5 boris 186: my ($dbh, $TournamentId, $field, $value) = @_;
1.46 roma7 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: }
1.1 boris 199: }
200:
201: sub UpdateQuestion {
1.5 boris 202: my ($dbh, $QuestionId, $field, $value) = @_;
1.40 boris 203:
1.46 roma7 204: if (($field eq 'Type') && ($value eq "Д")) {
205: $value = "ЧД";
1.40 boris 206: }
1.46 roma7 207: my $v = $dbh->quote($value);
208: $dbh->do("UPDATE Questions SET $field=$v
209: WHERE QuestionId=$QuestionId")
210: or die $dbh->errstr;
1.1 boris 211: }
212:
1.7 boris 213: sub CheckFile {
1.10 boris 214: my ($dbh, $source, $title) = @_;
1.7 boris 215: my $sth = $dbh->prepare("SELECT Id,ParentId,QuestionsNum FROM Tournaments
1.46 roma7 216: WHERE FileName=? AND Type='Ч'");
217: $sth->execute($source) or die $dbh->errstr;
1.7 boris 218: my @arr = $sth->fetchrow;
219: if (! scalar @arr) {
1.46 roma7 220: return SelectGroup($dbh,$source,$title);
1.7 boris 221: }
222: my($Id,$ParentId,$QuestionsNum)=@arr;
1.46 roma7 223: if($QuestionsNum) {
1.47 roma7 224: if ($newOnly) {
225: return (0,0);
226: }
1.46 roma7 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: }
1.7 boris 235: }
1.46 roma7 236: return($Id,$ParentId);
1.7 boris 237: }
238:
239:
240: sub DeleteTournament {
1.10 boris 241: my ($dbh,$Id,$ParentId,$QuestionsNum,$DeleteMyself) = @_;
242: if ($QuestionsNum) {
1.46 roma7 243: UpdateParents($dbh,$ParentId,-$QuestionsNum);
1.10 boris 244: }
1.7 boris 245: my (@Tours) = &GetTours($dbh, $Id);
246: foreach my $Tour (@Tours) {
1.46 roma7 247: DeleteTournament($dbh,$Tour,$Id,0,1);
1.7 boris 248: }
249: my $sth = $dbh->prepare("DELETE FROM Questions
250: WHERE ParentId=$Id");
251: $sth->execute or die $dbh->errstr;
252: if($DeleteMyself) {
1.46 roma7 253: $sth = $dbh->prepare("DELETE FROM Tournaments
1.7 boris 254: WHERE Id=$Id");
1.46 roma7 255: $sth->execute or die $dbh->errstr;
1.7 boris 256: }
257: }
1.8 boris 258:
259: sub GetTours {
1.46 roma7 260: my ($dbh, $ParentId) = @_;
261: my (@arr, @Tours);
1.8 boris 262:
1.46 roma7 263: my ($sth) = $dbh->prepare("SELECT Id FROM Tournaments
264: WHERE ParentId=$ParentId ORDER BY Id");
1.8 boris 265:
1.46 roma7 266: $sth->execute;
1.8 boris 267:
1.46 roma7 268: while (@arr = $sth->fetchrow) {
269: push @Tours, $arr[0];
270: }
1.8 boris 271:
1.46 roma7 272: return @Tours;
1.8 boris 273: }
274:
1.13 boris 275: sub CreateTour {
1.46 roma7 276: my ($dbh,$title,$ParentId,$TourNum,$rh_defaults)=@_;
1.13 boris 277: my $sth = $dbh->prepare("INSERT INTO Tournaments
1.46 roma7 278: (Title, Type, ParentId, Number,CreatedAt)
279: VALUES (?, 'Т', $ParentId, $TourNum,NOW())");
280: $sth->execute($title);
1.13 boris 281: my $TourId = $sth->{mysql_insertid};
282: while (my ($key,$value)=each %$rh_defaults) {
1.46 roma7 283: &UpdateTournament($dbh, $TourId, $key, $value);
1.13 boris 284: }
285: return $TourId;
286: }
1.46 roma7 287:
1.8 boris 288:
1.1 boris 289: MAIN:
290: {
1.5 boris 291: my($key, $value, $addition);
1.13 boris 292: #
293: # Inherited fields for a Tour or Tournament
294: #
295: my %TourFields = ('Копирайт' => 'Copyright',
1.46 roma7 296: 'Инфо' => 'Info', 'URL' => 'URL',
297: 'Ссылка' => 'URL', 'Редактор' => 'Editors',
298: 'Обработан'=> 'EnteredBy',
299: 'Дата'=>'PlayedAt', 'PlayedAt2'=>'PlayedAt2');
1.13 boris 300: #
301: # Inherited fields for a Question
302: #
303: my %QuestionFields = ('Тип'=> 'Type', 'Вид'=> 'Type',
1.46 roma7 304: 'Автор' => 'Authors', 'Рейтинг'=>'Rating',
305: 'Источник' => 'Sources',
306: 'Тема' => 'Topic');
307:
308:
1.5 boris 309: my($source);
310:
1.48 ! roma7 311: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
1.46 roma7 312: or die "Can't connect to DB chgk\n";
1.42 roma7 313: if ($dbh->get_info( 18 )=~/^(5|(4.1))/) {$dbh->do("SET NAMES 'koi8r'");}
1.46 roma7 314: my @sources;
1.34 roma7 315: open UNSORTED, ">$unsortedname";
1.5 boris 316: while ($source = shift) {
1.25 roma7 317: push @sources,glob($source);
318: }
319: foreach $source(@sources) {
1.46 roma7 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=?
1.7 boris 360: WHERE
1.46 roma7 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);
1.44 roma7 387: %QuestionDefaults=%QuestionGlobalDefaults;
1.46 roma7 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";
1.5 boris 467: }
1.27 roma7 468: close UNSORTED;
1.34 roma7 469: unlink $unsortedname unless $isunsorted;
1.5 boris 470: $dbh->disconnect;
1.1 boris 471: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>