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 I<file1> I<file2>....
10:
11:
12: =head1 DESCRIPTION
13:
14: Updates information in the B<chgk> databse. Uses file
15:
16:
17: =head1 BUGS
18:
19: The database, user and password are hardcoded.
20:
21: =head1 AUTHOR
22:
23: Dmitry Rubinstein
24:
25: =head1 $Id: updatedb.pl,v 1.5 2000/10/18 01:52:16 boris Exp $
26:
27: =cut
28:
29: my (%RevMonths) =
30: ('Jan', '1', 'Feb', '2', 'Mar', '3', 'Apr', '4', 'May', '5', 'Jun', '6',
31: 'Jul', '7', 'Aug', '8', 'Sep', '9', 'Oct', '10', 'Nov', '11',
32: 'Dec', '12',
33: 'JAN', '1', 'FEB', '2', 'MAR', '3', 'APR', '4', 'MAY', '5', 'JUN', '6',
34: 'JUL', '7', 'AUG', '8', 'SEP', '9', 'OCT', '10', 'NOV', '11',
35: 'DEC', '12',
36: 'Янв', '0', 'Фев', 1, 'Мар', 2, 'Апр', 3, 'Май', '4',
37: 'Июн', '5', 'Июл', 6, 'Авг', '7', 'Сен', '8',
38: 'Окт', '9', 'Ноя', '19', 'Дек', '11');
39: my ($sth);
40:
41: use vars qw($/);
42:
43:
44:
45: use DBI;
46: use strict;
47:
48: sub UpdateParents {
49: my ($dbh, $ParentId, $all_qnum) = @_;
50: if ($ParentId) {
51: my ($sth1) = $dbh->prepare("SELECT QuestionsNum, ParentId
52: FROM Tournaments WHERE Id = $ParentId");
53: $sth1->execute;
54: my ($q, $p) = ($sth1->fetchrow)[0, 1];
55: $dbh->do("UPDATE Tournaments SET
56: QuestionsNum=$q + $all_qnum
57: WHERE Id = $ParentId");
58: &UpdateParents($dbh, $p, $all_qnum);
59: }
60: }
61:
62: sub getField {
63: my($desc, $dbh) = @_;
64: my($key);
65: my($value) = ('');
66: while (<$desc>) {
67: s/
//;
68: if ($key && /^\s*$/) {
69: chomp $value;
70: chomp $key;
71: if ($key eq 'Дата') {
72: $value =~ s/^(.*)-(.*)-(.*)$/$3-$2-$1/;
73: my($month) = $RevMonths{$2};
74: $value =~ s/$2/$month/;
75: }
76: $value = $dbh->quote($value);
77: return ($key, $value);
78: }
79: next if (/^\s*$/);
80:
81: if (/^(.*):\s*$/ && !$key) {
82: $key = $1;
83: next;
84: }
85: if ($key) {
86: $value .= $_;
87: next;
88: }
89: }
90: if ($key && $value) {
91: $value = $dbh->quote($value);
92: return ($key, $value);
93: }
94: return (0, 0);
95: }
96:
97: sub SelectGroup {
98: my ($dbh, $TourName, $sth, $ParentId, $i, @arr) = @_;
99:
100: $sth = $dbh->prepare("SELECT Id, Title FROM
101: Tournaments WHERE Type = 'Г'");
102: $sth->execute;
103: print "Выберите группу для турнира:\n$TourName\n\n";
104: while (@arr=$sth->fetchrow) {
105: print "[$arr[0]] $arr[1]\n";
106: }
107: $ParentId = <STDIN>;
108: print "Вы выбрали турнир: $ParentId\n";
109: return $ParentId;
110: }
111:
112: sub UpdateTournament {
113: my ($dbh, $TournamentId, $field, $value) = @_;
114: $dbh->do("UPDATE Tournaments SET $field=$value WHERE Id=$TournamentId")
115: or die $dbh->errstr;
116: }
117:
118: sub UpdateQuestion {
119: my ($dbh, $QuestionId, $field, $value) = @_;
120: $dbh->do("UPDATE Questions SET $field=$value
121: WHERE QuestionId=$QuestionId")
122: or die $dbh->errstr;
123: }
124:
125: MAIN:
126: {
127: my($key, $value, $addition);
128:
129: my($source);
130:
131: my($dbh) = DBI->connect("DBI:mysql:chgk", "piataev", "")
132: or die "Can't connect to DB chgk\n";
133:
134: while ($source = shift) {
135: my($PlayedAt) = '';
136: my($QuestionId, $TourId, $TournamentId, $ParentId) = (0, 0, 0, 0);
137: my($tournum, $qnum, $all_qnum, $qtype) = (0, 0, 0, 'Ч');
138: my (@d) = (localtime((stat($source))[9]))[5,4,3];
139: $d[1]++;
140: $d[0]+=1900;
141: my ($CreatedAt) = $dbh->quote( join('-', @d));
142: print STDERR "File created on: $CreatedAt\n";
143:
144: open INFD, $source
145: or die "Can't open input file: $!\n";
146:
147: $source =~ s/^.*\/([^\/]*)$/$1/;
148: $source = $dbh->quote($source);
149: print STDERR "Processing file: $source \n";
150:
151: while (($key, $value) = getField(\*INFD, $dbh)) {
152: last if (!$key);
153:
154: if ($key =~ /Мета/) {
155: $value =~ s/[^\d]*//g;
156: $sth = $dbh->prepare("SELECT Id FROM Tournaments WHERE
157: MetaId=$value");
158: $sth->execute
159: or die "Invalid Meta field: $value";
160: $ParentId = ($sth->fetchrow)[0];
161: next;
162: }
163: if ($key =~ /Чемпионат/) {
164: $ParentId = &SelectGroup($dbh, $value)
165: unless ($ParentId);
166: $sth = $dbh->prepare("INSERT INTO Tournaments
167: (Title, Type, ParentId, FileName,
168: CreatedAt)
169: VALUES ($value, 'Ч', $ParentId,
170: $source, $CreatedAt)");
171: $sth->execute;
172: $TournamentId = $sth->{mysql_insertid};
173: next;
174: }
175: if ($key =~ /Тур/) {
176: if ($TourId) {
177: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
178: WHERE Id=$TourId");
179: }
180: $qnum = 0;
181: $qtype = 'Ч';
182: $sth = $dbh->prepare("INSERT INTO Tournaments
183: (Title, Type, ParentId, CreatedAt)
184: VALUES ($value, 'Т', $TournamentId,
185: $CreatedAt)");
186: $sth->execute;
187: $TourId = $sth->{mysql_insertid};
188: next;
189: }
190: if ($key =~ /Вид/) {
191: $qtype = $value;
192: $qtype =~ s/\'//g;
193: next;
194: }
195: if ($key =~ /Вопрос/) {
196: my $query = "INSERT INTO Questions
197: (ParentId, Number, Type)
198: VALUES ($TourId, $qnum+1, \'$qtype\')";
199: $sth = $dbh->prepare($query);
200: $sth->execute or print $query;;
201: $QuestionId = $sth->{mysql_insertid};
202: &UpdateQuestion($dbh, $QuestionId, "Question", $value);
203: $qnum++;
204: $all_qnum++;
205: next;
206: }
207: &UpdateQuestion($dbh, $QuestionId, "Answer", $value)
208: if ($key =~ /Ответ/);
209:
210: &UpdateQuestion($dbh, $QuestionId, "Authors", $value)
211: if ($key =~ /Автор/);
212:
213: &UpdateQuestion($dbh, $QuestionId, "Sources", $value)
214: if ($key =~ /Источник/);
215:
216: &UpdateQuestion($dbh, $QuestionId, "Comments", $value)
217: if ($key =~ /Комментарий/);
218:
219: &UpdateTournament($dbh, $TournamentId, "URL", $value)
220: if ($key =~ /URL/);
221:
222: &UpdateTournament($dbh, $TournamentId, "Copyright", $value)
223: if ($key =~ /Копирайт/);
224:
225: &UpdateTournament($dbh, $TournamentId, "Info", $value)
226: if ($key =~ /Инфо/);
227:
228: &UpdateTournament($dbh, $TournamentId, "Editors", $value)
229: if ($key =~ /Редактор/);
230:
231: &UpdateTournament($dbh, $TournamentId, "EnteredBy", $value)
232: if ($key =~ /Обработан/);
233:
234: if ($key =~ /Дата/) {
235: if ($TourId) {
236: &UpdateTournament($dbh, $TourId, "PlayedAt", $value);
237: } else {
238: &UpdateTournament($dbh, $TournamentId, "PlayedAt", $value);
239: }
240: }
241: }
242: $dbh->do("UPDATE Tournaments SET QuestionsNum=$qnum
243: WHERE Id=$TourId");
244: $dbh->do("UPDATE Tournaments SET QuestionsNum=$all_qnum
245: WHERE Id=$TournamentId");
246: &UpdateParents($dbh, $ParentId, $all_qnum);
247: print STDERR "Total number of questions: $all_qnum \n";
248: }
249: $dbh->disconnect;
250: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>