-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_training_databases.php
executable file
·351 lines (294 loc) · 13.9 KB
/
create_training_databases.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/php
<?php
/*******************************************************************************
*
* LOVD scripts: Training database creator
*
* Created : 2016-05-27
* Modified : 2016-05-27
* Version : 0.1
* For LOVD : 3.0-15
*
* Purpose : Create or reset LOVD3 training databases, based on a master
* database installation. The master database must have "master"
* in its table prefix (config.ini.php), in its URL (TABLE_CONFIG)
* and in its LOVD signature (TABLE_STATUS).
*
* Changelog : 0.1 2016-05-27
* Initial release.
*
* Copyright : 2004-2016 Leiden University Medical Center; http://www.LUMC.nl/
* Programmer : Ing. Ivo F.A.C. Fokkema <[email protected]>
*
*
* This file is part of LOVD.
*
* LOVD is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LOVD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LOVD. If not, see <http://www.gnu.org/licenses/>.
*
*************/
if (isset($_SERVER['HTTP_HOST'])) {
die('Please run this script through the command line.' . "\n");
}
$_CONFIG = array(
'version' => '0.1',
'config_file' => 'config.ini.php', // The name of the LOVD config file that we'll search for.
'master_dump_file' => 'SQL_dump_master.sql',
'full_dump_file' => 'SQL_dump_ALL.sql',
'default_relative_training_path' => '../', // The path where we build the trainings databases, relative to the master.
'maximum_training_instances' => 25,
'user' => array(
// Variables we will be asking the user.
'lovd_master_path' => '',
'lovd_training_databases_path' => '',
'training_instances' => 25,
'master_dump_file_overwrite' => 'y',
'mysql_username' => '', // Will only be used if the LOVD's user does not have the proper rights.
'mysql_password' => '', // Will only be used if the LOVD's user does not have the proper rights.
),
);
function lovd_verifySettings ($sKeyName, $sMessage, $sVerifyType, $options)
{
// Based on a function provided by Ileos.nl in the interest of Open Source.
// Check if settings match certain input.
global $_CONFIG;
switch($sVerifyType) {
case 'array':
$aOptions = $options;
if (!is_array($aOptions)) {
return false;
}
break;
case 'int':
// Integer, options define a range in the format '1,3' (1 to 3) or '1,' (1 or higher).
$aRange = explode(',', $options);
if (!is_array($aRange) ||
($aRange[0] === '' && $aRange[1] === '') ||
($aRange[0] !== '' && !ctype_digit($aRange[0])) ||
($aRange[1] !== '' && !ctype_digit($aRange[1]))) {
return false;
}
break;
}
while (true) {
print(' ' . $sMessage .
($sVerifyType != 'int' || ($aRange === array('', ''))? '' : ' (' . (int) $aRange[0] . '-' . $aRange[1] . ')') .
(empty($_CONFIG['user'][$sKeyName])? '' : ' [' . $_CONFIG['user'][$sKeyName] . ']') . ' : ');
$sInput = trim(fgets(STDIN));
if (!strlen($sInput) && !empty($_CONFIG['user'][$sKeyName])) {
$sInput = $_CONFIG['user'][$sKeyName];
}
switch ($sVerifyType) {
case 'array':
$sInput = strtolower($sInput);
if (in_array($sInput, $aOptions)) {
$_CONFIG['user'][$sKeyName] = $sInput;
return true;
}
break;
case 'int':
$sInput = (int) $sInput;
// Check if input is lower than minimum required value (if configured).
if ($aRange[0] !== '' && $sInput < $aRange[0]) {
break;
}
// Check if input is higher than maximum required value (if configured).
if ($aRange[1] !== '' && $sInput > $aRange[1]) {
break;
}
$_CONFIG['user'][$sKeyName] = $sInput;
return true;
case 'string':
$sInput = $sInput;
$_CONFIG['user'][$sKeyName] = $sInput;
return true;
case 'file':
case 'lovd_path':
case 'path':
// Always accept the default (if non-empty) or the given options.
if (($sInput && ($sInput == $_CONFIG['user'][$sKeyName] ||
$sInput === $options)) ||
(is_array($options) && in_array($sInput, $options))) {
$_CONFIG['user'][$sKeyName] = $sInput; // In case an option was chosen that was not the default.
return true;
}
if (in_array($sVerifyType, array('lovd_path', 'path')) && !is_dir($sInput)) {
print(' Given path is not a directory.' . "\n");
break;
} elseif (!is_readable($sInput)) {
print(' Cannot read given path.' . "\n");
break;
}
if ($sVerifyType == 'lovd_path') {
if (!file_exists($sInput . '/' . $_CONFIG['config_file'])) {
if (file_exists($sInput . '/src/' . $_CONFIG['config_file'])) {
$sInput .= '/src';
} else {
print(' Cannot locate ' . $_CONFIG['config_file'] . ' in given path.' . "\n" .
' Please check that the given path is a correct path to an LOVD installation.' . "\n");
break;
}
}
if (!is_readable($sInput . '/' . $_CONFIG['config_file'])) {
print(' Cannot read configuration file in given LOVD directory.' . "\n");
break;
}
// We'll set everything up later, because we don't want to
// keep the $_DB open for as long as the user is answering questions.
}
$_CONFIG['user'][$sKeyName] = $sInput;
return true;
default:
return false;
}
}
return false; // We'd actually never get here.
}
// We might be running for quite some time.
set_time_limit(0);
print('LOVD3 training database creator v' . $_CONFIG['version'] . '.' . "\n");
// Verify settings with user.
if (!lovd_verifySettings('lovd_master_path', 'Path of LOVD master installation to base installations on', 'lovd_path', '')) {
die(' Failed to get LOVD path.' . "\n");
}
// Master directory should not be numeric, to prevent possible overlap with the children databases.
if (ctype_digit(basename($_CONFIG['user']['lovd_master_path']))) {
die(' The LOVD master path can not be numeric,
to prevent overlap with copied instances.' . "\n");
}
// Predict where we'll put the trainings databases.
$_CONFIG['user']['lovd_training_databases_path'] = realpath($_CONFIG['user']['lovd_master_path'] . '/' . $_CONFIG['default_relative_training_path']);
lovd_verifySettings('lovd_training_databases_path', 'Path where trainings databases will be created, or reset', 'path', '');
lovd_verifySettings('training_instances', 'Number of training databases to create', 'int', '1,' . $_CONFIG['maximum_training_instances']);
lovd_verifySettings('master_dump_file_overwrite', 'I\'m going to dump data into ' . $_CONFIG['master_dump_file'] . '. Overwrite file if exists? (Yes/No)', 'array', array('yes', 'no', 'y', 'n'));
$_CONFIG['user']['master_dump_file_overwrite'] = (in_array($_CONFIG['user']['master_dump_file_overwrite'], array('y', 'yes'))); // Turn into a boolean.
// Check if MySQL database dump already exists.
if (file_exists($_CONFIG['master_dump_file']) && !$_CONFIG['user']['master_dump_file_overwrite']) {
die(' Dump file ' . $_CONFIG['master_dump_file'] . ' exists, and was requested not to overwrite.' . "\n");
}
// To create the dump, connect to the LOVD for its settings (easier than to parse the config.ini.php here, and find tables, etc.
// Find LOVD installation, run it's inc-init.php to get DB connection.
define('ROOT_PATH', $_CONFIG['user']['lovd_master_path'] . '/');
define('FORMAT_ALLOW_TEXTPLAIN', true);
$_GET['format'] = 'text/plain';
// To prevent notices when running inc-init.php.
$_SERVER = array_merge($_SERVER, array(
'HTTP_HOST' => 'localhost',
'REQUEST_URI' => '/' . basename(__FILE__),
'QUERY_STRING' => '',
'REQUEST_METHOD' => 'GET',
));
// If I put a require here, I can't nicely handle errors, because PHP will die if something is wrong.
// However, I need to get rid of the "headers already sent" warnings from inc-init.php.
// So, sadly if there is a problem connecting to LOVD, the script will die here without any output whatsoever.
ini_set('display_errors', '0');
ini_set('log_errors', '0'); // CLI logs errors to the screen, apparently.
require ROOT_PATH . 'inc-init.php';
ini_set('display_errors', '1'); // We do want to see errors from here on.
// Check if we have the FLUSH TABLES command, which is needed for the dump.
$bUseLOVDUser = (bool) $_DB->query('FLUSH TABLES', array(), false);
// If the LOVD's MySQL user does not have the proper privileges, ask the information from the user.
if (!$bUseLOVDUser) {
print(' This LOVD\'s MySQL username does not have the proper privileges to create a database dump.' . "\n");
while (!$_CONFIG['user']['mysql_username']) {
lovd_verifySettings('mysql_username', ' MySQL username to use for the database dump', 'string', '');
}
}
// Prepare dump file.
$f = fopen($_CONFIG['master_dump_file'], 'w');
fputs($f, 'SET FOREIGN_KEY_CHECKS=0; -- Necessary because we\'re dropping the tables and recreating them.' . "\n\n");
fclose($f);
// Prepare command to dump the tables.
// Note that the ORDER OF ARGUMENTS is extremely important. If --skip-opt is done last, part of the arguments are ignored.
$sCommand = 'mysqldump --skip-opt --add-drop-table --add-locks --create-options --disable-keys --flush-logs --order-by-primary --quick --set-charset --single-transaction' .
' --user ' . escapeshellarg(($_CONFIG['user']['mysql_username']? $_CONFIG['user']['mysql_username'] : $_INI['database']['username']));
if ($bUseLOVDUser) {
$sCommand .= ' --password=' . escapeshellarg($_INI['database']['password']);
} else {
// Let MySQL dump ask for a password.
$sCommand .= ' -p';
}
$sCommand .= ' ' . escapeshellarg($_INI['database']['database']);
// Gather list of this LOVD's tables.
require ROOT_PATH . 'install/inc-sql-tables.php';
$aTables = array_map('constant', array_keys($aTableSQL));
sort($aTables); // Make it like a normal mysqldump file, which sorts the tables by name.
$sCommand .= ' ' . implode(' ', $aTables) . ' >> ' . escapeshellarg($_CONFIG['master_dump_file']);
// Dump the data!
print(' Creating an SQL dump of the master database...' . "\n" .
' '); // Putting a newline here, because mysqldump might ask for a password.
exec($sCommand, $aOutput, $nReturn);
if ($nReturn) {
die(' Failed.
Could not create database dump.' . "\n");
}
// Finish dump file.
$f = fopen($_CONFIG['master_dump_file'], 'a');
fputs($f, "\n" . 'SET FOREIGN_KEY_CHECKS=1;' . "\n");
fclose($f);
print(' OK!' . "\n");
// Write massive SQL file for all instances.
print(' Creating SQL dump for all instances... ');
$f = fopen($_CONFIG['full_dump_file'], 'w');
fputs($f, 'USE ' . $_INI['database']['database'] . "\n\n");
$sTemplate = file_get_contents($_CONFIG['master_dump_file']);
for ($i = 1; $i <= $_CONFIG['user']['training_instances']; $i++) {
$i = str_pad($i, 2, '0', STR_PAD_LEFT);
fputs($f, str_replace('master', $i, $sTemplate) . "\n\n\n");
}
fclose($f);
print('OK!' . "\n");
// Loop through instances, removing the original directory, copying
// master to this directory, and fix the config.ini.php file.
print(' Resetting directories... ');
for ($i = 1; $i <= $_CONFIG['user']['training_instances']; $i++) {
$i = str_pad($i, 2, '0', STR_PAD_LEFT);
print(chr(8) . chr(8) . $i); // Backspace (x2), new dir name.
$sDirName = $_CONFIG['user']['lovd_training_databases_path'] . '/' . $i;
// Remove directory, if it exists.
if (file_exists($sDirName)) {
exec('rm -rf ' . $sDirName, $aOutput, $nReturn);
if ($nReturn) {
die("\n" . ' Failed.
Could not remove directory ' . $sDirName . '.' . "\n");
}
}
// Copy it from master.
exec('cp -pr ' . $_CONFIG['user']['lovd_master_path'] . ' ' . $sDirName, $aOutput, $nReturn);
if ($nReturn) {
die("\n" . ' Failed.
Could not copy master to ' . $sDirName . '.' . "\n");
}
// Modify the config.ini.php file. It's easier to take the one from master.
exec('sed \'s/master/' . $i . '/\' ' . $_CONFIG['user']['lovd_master_path'] . '/' .
$_CONFIG['config_file'] . ' > ' . $sDirName . '/' . $_CONFIG['config_file'],
$aOutput, $nReturn);
if ($nReturn) {
die("\n" . ' Failed.
Could not create ' . $_CONFIG['config_file'] . ' for ' . $sDirName . '.' . "\n");
}
}
print(chr(8) . chr(8) . 'OK!' . "\n");
// Finally, load in the MySQL dump file.
// Write massive SQL file for all instances.
print(' Loading SQL dump for all instances... ');
exec('mysql --user ' . escapeshellarg($_INI['database']['username']) .
' --password=' . escapeshellarg($_INI['database']['password']) .
' < ' . $_CONFIG['full_dump_file'], $aOutput, $nReturn);
if ($nReturn) {
die('Failed.
Could not load SQL dump.' . "\n");
}
print('OK!' . "\n" .
'All Done!' . "\n\n");
?>