This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathdump_schema.php
81 lines (67 loc) · 2.69 KB
/
dump_schema.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
<?php
// modify these constants to your settings
$host = 'localhost';
$mysqli_user = 'root';
$mysqli_password = '';
$db = 'achecker';
$port = 3306;
define('TABLE_PREFIX', 'AC_');
// the script starts here
$lang_db = mysqli_connect($host, $mysqli_user, $mysqli_password, $db, $port) or die(mysqli_error($lang_db));
mysqli_select_db($lang_db, $db) or die(mysqli_error($lang_db));
echo "Database connected!<br /><br />";
$dump_tables = array('checks', 'check_examples', 'check_prerequisites', 'color_mapping',
'guidelines', 'guideline_groups', 'guideline_subgroups', 'languages',
'lang_codes', 'privileges', 'subgroup_checks', 'techniques',
'test_pass', 'themes', 'users', 'user_groups', 'user_group_privilege');
foreach ($dump_tables as $table_name) {
$sql = "SELECT * FROM ".TABLE_PREFIX.$table_name;
$result = mysqli_query($lang_db, $sql) or die(mysqli_error($lang_db));
// if the table contains data
if (mysqli_num_rows($result) > 0) {
$field_types = array();
$output .= "# Dumping data for table `".$table_name."`\n\n".
"INSERT INTO `".$table_name."` (";
for ($i = 0; $i < mysqli_num_fields($result); ++$i) {
$field_name = mysqli_fetch_field_direct($result, $i);
$field_type = mysqli_fetch_field_direct($result, $i);
$output .= "`".$field_name."`, ";
$field_types[$field_name] = $field_type;
}
$output = substr($output, 0, -2); // remove the last ", "
$output .= ") VALUES\n";
while($row = mysqli_fetch_assoc($result)) {
$output .= "(";
foreach ($field_types as $field_name => $field_type) {
if ($field_type == "int") {
$output .= $row[$field_name].", ";
} else if (is_null($row[$field_name])) {
$output .= "NULL, ";
} else {
$output .= "'". mysqli_real_escape_string($lang_db, $row[$field_name])."', ";
}
}
$output = substr($output, 0, -2); // remove the last ", "
$output .= "),\n";
}
$output = substr($output, 0, -2); // remove the last ",\n"
$output .= ";\n\n";
}
}
echo "Done with dumping insert SQL!<br /><br />";
$schema_file = 'install/db/achecker_schema.sql';
$location_to_replace = "############ DO NOT remove this line. Below are the required data insert ############";
// read the original content
$filesize = filesize($schema_file);
$fh = fopen($schema_file, 'r');
$file_content = fread($fh, $filesize);
fclose($fh);
$content_need = substr($file_content, 0, strpos($file_content, $location_to_replace) + strlen($location_to_replace));
$new_content = $content_need . $output;
// write the new content
$fh = fopen($schema_file, 'w');
fwrite($fh, $new_content);
fclose($fh);
echo "Done with writing into schema file!<br /><br />";
echo "Done!!!";
?>