Prepare customizable export

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@424 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana
2008-06-20 14:13:37 +00:00
parent cfc995f267
commit 80bc4b30dc
3 changed files with 151 additions and 73 deletions

View File

@@ -9,7 +9,7 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") {
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta name="robots" content="noindex" />
<title><?php echo $title . (strlen($title2) ? ": " . htmlspecialchars($title2) : "") . " - " . lang('phpMinAdmin') . " 1.6.2-dev"; ?></title>
<title><?php echo $title . (strlen($title2) ? ": " . htmlspecialchars($title2) : "") . " - " . lang('phpMinAdmin') . " 1.7.0-dev"; ?></title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
<link rel="stylesheet" type="text/css" href="default.css" /><?php // Ondrej Valka, http://valka.info ?>
<?php if ($_COOKIE["highlight"] == "jush") { ?>
@@ -66,10 +66,8 @@ function page_footer($missing = false) {
<form action="" method="post">
<p>
<a href="<?php echo htmlspecialchars($SELF); ?>sql="><?php echo lang('SQL command'); ?></a>
<a href="<?php echo htmlspecialchars($SELF); ?>dump=<?php echo urlencode($_GET["table"]); ?>"><?php echo lang('Dump'); ?></a>
<input type="hidden" name="token" value="<?php
echo $_SESSION["tokens"][$_GET["server"]]["?logout"];
?>" />
<a href="<?php echo htmlspecialchars($SELF); ?>dump=<?php echo urlencode(isset($_GET["table"]) ? $_GET["table"] : $_GET["select"]); ?>"><?php echo lang('Dump'); ?></a>
<input type="hidden" name="token" value="<?php echo $_SESSION["tokens"][$_GET["server"]]["?logout"]; ?>" />
<input type="submit" name="logout" value="<?php echo lang('Logout'); ?>" />
</p>
</form>

View File

@@ -1,9 +1,5 @@
<?php
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");
header("Content-Disposition: inline; filename=$filename.sql");
function dump_table($table, $data = true) {
function dump_table($table, $style) {
global $mysql, $max_packet;
$result = $mysql->query("SHOW CREATE TABLE " . idf_escape($table));
if ($result) {
@@ -20,36 +16,6 @@ function dump_table($table, $data = true) {
}
}
$result->free();
if ($data) {
$result = $mysql->query("SELECT * FROM " . idf_escape($table)); //! enum and set as numbers, binary as _binary, microtime
if ($result) {
if ($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";
}
}
if ($mysql->server_info >= 5) {
$result = $mysql->query("SHOW TRIGGERS LIKE '" . $mysql->escape_string(addcslashes($table, "%_")) . "'");
@@ -64,7 +30,39 @@ function dump_table($table, $data = true) {
}
}
function dump($db) {
function dump_data($table) {
global $mysql, $max_packet;
$result = $mysql->query("SELECT * FROM " . idf_escape($table)); //! enum and set as numbers, binary as _binary, microtime
if ($result) {
if ($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($db, $style) {
global $mysql;
static $routines;
if (!isset($routines)) {
@@ -81,53 +79,135 @@ function dump($db) {
}
}
}
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";
}
foreach ($_POST["tables"] as $table => $val) {
$table = bracket_escape($table, "back");
if ($val) {
dump_table($table, $val);
}
if ($_POST["data"][$table]) {
dump_data($table, $_POST["data"][$table]);
}
}
/*
$views = array();
$result = $mysql->query("SHOW TABLE STATUS");
while ($row = $result->fetch_assoc()) {
if (isset($row["Engine"])) {
dump_table($row["Name"]);
dump_data($row["Name"]);
} else {
$views[] = $row["Name"];
}
}
$result->free();
foreach ($views as $view) {
dump_table($view, false);
dump_table($view);
}
*/
if ($routines[$db]) {
echo "DELIMITER ;;\n\n" . implode("", $routines[$db]) . "DELIMITER ;\n\n";
}
echo "\n\n";
}
$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";
if (!strlen($_GET["db"])) {
$result = $mysql->query("SHOW DATABASES");
while ($row = $result->fetch_assoc()) {
if ($row["Database"] != "information_schema" || $mysql->server_info < 5) {
if ($mysql->select_db($row["Database"])) {
$result1 = $mysql->query("SHOW CREATE DATABASE " . idf_escape($row["Database"]));
if ($result1) {
echo $mysql->result($result1, 1) . ";\n";
$result1->free();
}
echo "USE " . idf_escape($row["Database"]) . ";\n";
dump($row["Database"]);
}
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");
header("Content-Disposition: inline; filename=$filename.sql");
$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);
}
}
$result->free();
} elseif (strlen($_GET["dump"])) {
dump_table($_GET["dump"]);
} else {
dump($_GET["db"]);
/*
} elseif (strlen($_GET["dump"])) {
dump_table($_GET["dump"]);
} else {
dump($_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 (!isset($_GET["db"]) && !isset($_SESSION["databases"][$_GET["server"]])) {
$_SESSION["databases"][$_GET["server"]] = get_vals("SHOW DATABASES");
}
foreach ((isset($_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 == (isset($_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";
foreach ((isset($_GET["db"]) ? get_vals("SHOW TABLES") : $_SESSION["databases"][$_GET["server"]]) as $table) {
echo "<tr><td>" . htmlspecialchars($table) . "</td>";
foreach (array('', 'DROP, CREATE', 'CREATE', 'CREATE, ALTER') as $val) {
echo '<td><input type="radio" name="tables[' . htmlspecialchars(bracket_escape($table)) . ']"' . ($val == (strlen($_GET["dump"]) && $table != $_GET["dump"] ? '' : 'DROP, CREATE') ? " checked='checked'" : "") . " value='$val' /></td>";
}
foreach (array('', 'TRUNCATE, INSERT', 'INSERT', 'UPDATE') as $val) {
echo '<td><input type="radio" name="data[' . htmlspecialchars(bracket_escape($table)) . ']"' . ($val == (strlen($_GET["dump"]) && $table != $_GET["dump"] ? '' : 'INSERT') ? " checked='checked'" : "") . " value='$val' /></td>";
}
echo "</tr>\n";
}
echo "</table>\n";
?>
<input type="submit" value="<?php echo lang('Export'); ?>" />
</form>

View File

@@ -24,9 +24,7 @@ include "./auth.inc.php";
include "./connect.inc.php";
include "./editing.inc.php";
if (isset($_GET["dump"])) {
include "./dump.inc.php";
} elseif (isset($_GET["download"])) {
if (isset($_GET["download"])) {
include "./download.inc.php";
} else { // outputs footer
$on_actions = array("RESTRICT", "CASCADE", "SET NULL", "NO ACTION");
@@ -50,6 +48,8 @@ if (isset($_GET["dump"])) {
include "./view.inc.php";
} elseif (isset($_GET["schema"])) {
include "./schema.inc.php";
} elseif (isset($_GET["dump"])) {
include "./dump.inc.php";
} elseif (isset($_GET["privileges"])) {
include "./privileges.inc.php";
} else { // uses CSRF token