-
Notifications
You must be signed in to change notification settings - Fork 13
/
sql-importer.php
103 lines (86 loc) · 2.73 KB
/
sql-importer.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
<?php
$f3 = require_once('fatfree/base.php');
if ((float)PCRE_VERSION < 7.9) {
trigger_error('PCRE version is out of date');
}
// Read configs
$f3->config('configs/config.ini');
function importSqlFile($pdo, $sqlFile, $tablePrefix = null)
{
try {
// Enable LOAD LOCAL INFILE
$pdo->setAttribute(\PDO::MYSQL_ATTR_LOCAL_INFILE, true);
$errorDetect = false;
// Temporary variable, used to store current query
$tmpLine = '';
// Read in entire file
$lines = file($sqlFile);
// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || trim($line) == '') {
continue;
}
// Read & replace prefix
$line = str_replace(['<<__prefix__>>'], [$tablePrefix], $line);
// Add this line to the current segment
$tmpLine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
try {
// Perform the Query
$pdo->exec($tmpLine);
} catch (\PDOException $e) {
echo "<br><pre>Error performing Query: '<strong>" . $tmpLine . "</strong>': " . $e->getMessage() . "</pre>\n";
$errorDetect = true;
}
// Reset temp variable to empty
$tmpLine = '';
}
}
// Check if error is detected
if ($errorDetect) {
return false;
}
} catch (\Exception $e) {
echo "<br><pre>Exception => " . $e->getMessage() . "</pre>\n";
return false;
}
return true;
}
// Prepare to import
// PDO DSN
$dsn = 'mysql:host='.$f3->db_host.
';port='.$f3->db_port.
';dbname='.$f3->db_name.
';charset=utf8';
// PDO options
$options = [
PDO::ATTR_EMULATE_PREPARES => false, // turn off emulation mode for "real" prepared statements
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //turn on errors in the form of exceptions
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //make the default fetch be an associative array
];
// Start import
try {
$pdo = new PDO($dsn, $f3->db_user, $f3->db_pass, $options);
if (!empty($_GET['update'])) {
$update_file = 'update-'.$_GET['update'];
if (importSqlFile($pdo, './sqls/'.$update_file.'.sql', $f3->db_prefix)) {
echo 'Update Done ;)';
}
} else if (!empty($_GET['extension']) && !empty($_GET['version'])) {
$extension = $_GET['extension'];
$version = $_GET['version'];
if (importSqlFile($pdo, './extensions/'.$extension.'/sqls/'.$version.'.sql', $f3->db_prefix)) {
echo 'Imported Extension: '.$extension.' Version: '.$version.' ;)';
}
} else {
if (importSqlFile($pdo, './sqls/mysql_main.sql', $f3->db_prefix)) {
echo 'Done ;)';
}
}
} catch (Exception $e) {
error_log($e->getMessage());
exit('Cannot connect to database server, check your config in configs/config.ini');
}
?>