git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@442 7c3ca157-0c34-0410-bff1-cbf682f78f5c
250 lines
9.6 KiB
PHP
250 lines
9.6 KiB
PHP
<?php
|
|
function dump_table($table, $style) {
|
|
global $mysql, $max_packet, $types;
|
|
if ($style) {
|
|
$result = $mysql->query("SHOW CREATE TABLE " . idf_escape($table));
|
|
if ($result) {
|
|
if ($style == "DROP, CREATE") {
|
|
echo "DROP TABLE " . idf_escape($table) . ";\n";
|
|
}
|
|
$create = $mysql->result($result, 1);
|
|
echo ($style == "CREATE, ALTER" ? preg_replace('~^CREATE TABLE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n\n";
|
|
if ($max_packet < 1073741824) { // protocol limit
|
|
$row_size = 21 + strlen(idf_escape($table));
|
|
foreach (fields($table) as $field) {
|
|
$type = $types[$field["type"]];
|
|
$row_size += 5 + ($field["length"] ? $field["length"] : $type) * (preg_match('~char|text|enum~', $field["type"]) ? 3 : 1); // UTF-8 in MySQL uses up to 3 bytes
|
|
}
|
|
if ($row_size > $max_packet) {
|
|
$max_packet = 1024 * ceil($row_size / 1024);
|
|
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
|
|
}
|
|
}
|
|
$result->free();
|
|
}
|
|
if ($mysql->server_info >= 5) {
|
|
$result = $mysql->query("SHOW TRIGGERS LIKE '" . $mysql->escape_string(addcslashes($table, "%_")) . "'");
|
|
if ($result->num_rows) {
|
|
echo "DELIMITER ;;\n\n";
|
|
while ($row = $result->fetch_assoc()) {
|
|
echo "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . idf_escape($row["Table"]) . " FOR EACH ROW $row[Statement];;\n\n";
|
|
}
|
|
echo "DELIMITER ;\n\n";
|
|
}
|
|
$result->free();
|
|
}
|
|
}
|
|
}
|
|
|
|
function dump_data($table, $style) {
|
|
global $mysql, $max_packet;
|
|
if ($style) {
|
|
if ($style == "TRUNCATE, INSERT") {
|
|
echo "TRUNCATE " . idf_escape($table) . ";\n";
|
|
}
|
|
$result = $mysql->query("SELECT * FROM " . idf_escape($table)); //! enum and set as numbers, binary as _binary, microtime
|
|
if ($result) {
|
|
if ($style == "UPDATE") {
|
|
while ($row = $result->fetch_assoc()) {
|
|
$set = array();
|
|
foreach ($row as $key => $val) {
|
|
$row[$key] = (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
|
$set[] = idf_escape($key) . " = " . (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
|
}
|
|
echo "INSERT INTO " . idf_escape($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES (" . implode(", ", $row) . ") ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
|
|
}
|
|
} elseif ($result->num_rows) {
|
|
$insert = "INSERT INTO " . idf_escape($table) . " VALUES ";
|
|
$length = 0;
|
|
while ($row = $result->fetch_row()) {
|
|
foreach ($row as $key => $val) {
|
|
$row[$key] = (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
|
}
|
|
$s = "(" . implode(", ", $row) . ")";
|
|
if (!$length) {
|
|
echo $insert, $s;
|
|
$length = strlen($insert) + strlen($s);
|
|
} else {
|
|
$length += 2 + strlen($s);
|
|
if ($length < $max_packet) {
|
|
echo ", ", $s;
|
|
} else {
|
|
echo ";\n", $insert, $s;
|
|
$length = strlen($insert) + strlen($s);
|
|
}
|
|
}
|
|
}
|
|
echo ";\n";
|
|
}
|
|
$result->free();
|
|
}
|
|
echo "\n";
|
|
}
|
|
}
|
|
|
|
function dump_routines($db) {
|
|
global $mysql;
|
|
if ($mysql->server_info >= 5) {
|
|
$out = "";
|
|
foreach (array("FUNCTION", "PROCEDURE") as $routine) {
|
|
$result = $mysql->query("SHOW $routine STATUS WHERE Db = '" . $mysql->escape_string($db) . "'");
|
|
while ($row = $result->fetch_assoc()) {
|
|
if (!$out) {
|
|
echo "DELIMITER ;;\n\n";
|
|
$out = "DELIMITER ;\n\n";
|
|
}
|
|
echo $mysql->result($mysql->query("SHOW CREATE $routine " . idf_escape($row["Db"]) . "." . idf_escape($row["Name"])), 2) . ";;\n\n";
|
|
}
|
|
$result->free();
|
|
}
|
|
echo $out;
|
|
}
|
|
}
|
|
|
|
function dump($db, $style) {
|
|
global $mysql;
|
|
if (in_array($style, array("DROP, CREATE", "CREATE", "CREATE, ALTER")) && ($result = $mysql->query("SHOW CREATE DATABASE " . idf_escape($db)))) {
|
|
if ($style == "DROP, CREATE") {
|
|
echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n";
|
|
}
|
|
$create = $mysql->result($result, 1);
|
|
echo ($style == "CREATE, ALTER" ? preg_replace('~^CREATE DATABASE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n";
|
|
$result->free();
|
|
}
|
|
if ($style) {
|
|
echo "USE " . idf_escape($db) . ";\n";
|
|
if (!strlen($_GET["db"])) {
|
|
$views = array();
|
|
$result = $mysql->query("SHOW TABLE STATUS");
|
|
while ($row = $result->fetch_assoc()) {
|
|
if (isset($row["Engine"])) {
|
|
dump_table($row["Name"], $_POST["tables"][0]);
|
|
dump_data($row["Name"], $_POST["data"][0]);
|
|
} else {
|
|
$views[] = $row["Name"];
|
|
}
|
|
}
|
|
$result->free();
|
|
foreach ($views as $view) {
|
|
dump_table($view, $_POST["tables"][0]);
|
|
}
|
|
dump_routines($db);
|
|
}
|
|
}
|
|
echo "\n\n";
|
|
}
|
|
|
|
function tar_file($filename, $contents) {
|
|
$return = pack("a100a8a8a8a12a12", $filename, 644, 0, 0, decoct(strlen($contents)), decoct(time()));
|
|
$checksum = 8*32; // space for checksum itself
|
|
for ($i=0; $i < strlen($return); $i++) {
|
|
$checksum += ord($return{$i});
|
|
}
|
|
$return .= sprintf("%06o", $checksum) . "\0 ";
|
|
$return .= str_repeat("\0", 512 - strlen($return));
|
|
$return .= $contents;
|
|
if (strlen($contents) % 512) {
|
|
$return .= str_repeat("\0", 512 - strlen($contents) % 512);
|
|
}
|
|
return $return;
|
|
}
|
|
|
|
if ($_POST) {
|
|
header("Content-Type: text/plain; charset=utf-8");
|
|
$filename = (strlen($_GET["db"]) ? preg_replace('~[^a-z0-9_]~i', '-', (strlen($_GET["dump"]) ? $_GET["dump"] : $_GET["db"])) : "dump");
|
|
$filename .= ($_POST["format"] == "sql" ? ".sql" : (!strlen($_GET["db"]) || count(array_filter($_POST["data"])) >= 1 ? ".tar" : ".csv"));
|
|
header("Content-Disposition: " . ($_POST["output"] == "file" ? "attachment" : "inline") . "; filename=$filename");
|
|
|
|
$max_packet = 16777216;
|
|
echo "SET NAMES utf8;\n";
|
|
echo "SET foreign_key_checks = 0;\n";
|
|
echo "SET time_zone = '" . $mysql->escape_string($mysql->result($mysql->query("SELECT @@time_zone"))) . "';\n";
|
|
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
|
|
echo "\n";
|
|
|
|
foreach ($_POST["databases"] as $db => $style) {
|
|
$db = bracket_escape($db, "back");
|
|
if ($mysql->select_db($db)) {
|
|
dump($db, $style);
|
|
}
|
|
}
|
|
if (strlen($_GET["db"])) {
|
|
foreach ($_POST["tables"] as $key => $style) {
|
|
$table = bracket_escape($key, "back");
|
|
dump_table($table, $style);
|
|
dump_data($table, $_POST["data"][$key]);
|
|
}
|
|
dump_routines($_GET["db"]);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
page_header(lang('Export'), "", (strlen($_GET["export"]) ? array("table" => $_GET["export"]) : array()), $_GET["db"]);
|
|
?>
|
|
|
|
<script type="text/javascript">
|
|
function check(td, name, value) {
|
|
var inputs = td.parentNode.parentNode.parentNode.getElementsByTagName('input');
|
|
for (var i=0; i < inputs.length; i++) {
|
|
if (name.test(inputs[i].name)) {
|
|
inputs[i].checked = (inputs[i].value == value);
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<form action="" method="post">
|
|
<p>
|
|
<?php echo lang('Output'); ?>: <select name="output"><option value="text"><?php echo lang('text'); ?></option><option value="file"><?php echo lang('file'); ?></option></select>
|
|
<?php echo lang('Format'); ?>: <select name="format"><option value="sql"><?php echo lang('SQL'); ?></option><option value="csv"><?php echo lang('CSV'); ?></option></select>
|
|
</p>
|
|
|
|
<?php
|
|
echo "<table border='1' cellspacing='0' cellpadding='2'>\n<thead><tr><th>" . lang('Database') . "</th>";
|
|
foreach (array('', 'USE', 'DROP, CREATE', 'CREATE', 'CREATE, ALTER') as $val) {
|
|
echo "<th onclick=\"check(this, /^databases/, '$val');\" style='cursor: pointer;'>" . ($val ? $val : lang('skip')) . "</th>";
|
|
}
|
|
echo "</tr></thead>\n";
|
|
if (!strlen($_GET["db"]) && !isset($_SESSION["databases"][$_GET["server"]])) {
|
|
$_SESSION["databases"][$_GET["server"]] = get_vals("SHOW DATABASES");
|
|
}
|
|
foreach ((strlen($_GET["db"]) ? array($_GET["db"]) : $_SESSION["databases"][$_GET["server"]]) as $db) {
|
|
if ($db != "information_schema" || $mysql->server_info < 5) {
|
|
echo "<tr><td>" . htmlspecialchars($db) . "</td>";
|
|
foreach (array('', 'USE', 'DROP, CREATE', 'CREATE', 'CREATE, ALTER') as $val) {
|
|
echo '<td><input type="radio" name="databases[' . htmlspecialchars(bracket_escape($db)) . ']"' . ($val == (strlen($_GET["db"]) ? '' : 'CREATE') ? " checked='checked'" : "") . " value='$val' /></td>";
|
|
}
|
|
echo "</tr>\n";
|
|
}
|
|
}
|
|
echo "</table>\n";
|
|
|
|
echo "<table border='1' cellspacing='0' cellpadding='2'>\n<thead><tr><th rowspan='2'>" . lang('Tables') . "</th><th colspan='4'>" . lang('Structure') . "</th><th colspan='4'>" . lang('Data') . "</th></tr><tr>";
|
|
foreach (array('', 'DROP, CREATE', 'CREATE', 'CREATE, ALTER') as $val) {
|
|
echo "<th onclick=\"check(this, /^tables/, '$val');\" style='cursor: pointer;'>" . ($val ? $val : lang('skip')) . "</th>";
|
|
}
|
|
foreach (array('', 'TRUNCATE, INSERT', 'INSERT', 'UPDATE') as $val) {
|
|
echo "<th onclick=\"check(this, /^data/, '$val');\" style='cursor: pointer;'>" . ($val ? $val : lang('skip')) . "</th>";
|
|
}
|
|
echo "</tr></thead>\n";
|
|
$views = "";
|
|
$result = $mysql->query(strlen($_GET["db"]) ? "SHOW TABLE STATUS" : "SELECT 'Engine'");
|
|
while ($row = $result->fetch_assoc()) {
|
|
$print = "<tr><td>" . htmlspecialchars($row["Name"]) . "</td>";
|
|
foreach (array('', 'DROP, CREATE', 'CREATE', 'CREATE, ALTER') as $val) {
|
|
$print .= '<td><input type="radio" name="tables[' . htmlspecialchars(bracket_escape($row["Name"])) . ']"' . ($val == (strlen($_GET["dump"]) && $row["Name"] != $_GET["dump"] ? '' : 'DROP, CREATE') ? " checked='checked'" : "") . " value='$val' /></td>";
|
|
}
|
|
if (!$row["Engine"]) {
|
|
$views .= "$print</tr>\n";
|
|
} else {
|
|
foreach (array('', 'TRUNCATE, INSERT', 'INSERT', 'UPDATE') as $val) {
|
|
$print .= '<td><input type="radio" name="data[' . htmlspecialchars(bracket_escape($row["Name"])) . ']"' . ($val == ((strlen($_GET["dump"]) && $row["Name"] != $_GET["dump"]) || !$row["Engine"] ? '' : 'INSERT') ? " checked='checked'" : "") . " value='$val' /></td>";
|
|
}
|
|
echo "$print</tr>\n";
|
|
}
|
|
}
|
|
echo "$views</table>\n";
|
|
?>
|
|
<input type="submit" value="<?php echo lang('Export'); ?>" />
|
|
</form>
|