adminerevo/adminer/dump.inc.php

191 lines
7.6 KiB
PHP
Raw Normal View History

<?php
$TABLE = $_GET["dump"];
if ($_POST) {
$cookie = "";
2011-01-19 13:32:35 +00:00
foreach (array("output", "format", "db_style", "routines", "events", "table_style", "auto_increment", "triggers", "data_style") as $key) {
$cookie .= "&$key=" . urlencode($_POST[$key]);
}
cookie("adminer_export", substr($cookie, 1));
2011-02-17 10:43:21 +00:00
$ext = dump_headers(($TABLE != "" ? $TABLE : DB), (DB == "" || count((array) $_POST["tables"] + (array) $_POST["data"]) > 1));
2013-04-04 01:49:05 +00:00
$is_sql = ereg('sql', $_POST["format"]);
2010-10-22 19:27:53 +00:00
if ($is_sql) {
echo "-- Adminer $VERSION " . $drivers[DRIVER] . " dump
2010-05-07 13:44:22 +00:00
" . ($jush != "sql" ? "" : "SET NAMES utf8;
2012-08-12 05:37:30 +00:00
" . ($_POST["data_style"] ? "SET foreign_key_checks = 0;
SET time_zone = " . q(substr(preg_replace('~^[^-]~', '+\0', $connection->result("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP)")), 0, 6)) . ";
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
2012-08-12 05:37:30 +00:00
" : "") . "
");
}
$style = $_POST["db_style"];
$databases = array(DB);
if (DB == "") {
$databases = $_POST["databases"];
if (is_string($databases)) {
$databases = explode("\n", rtrim(str_replace("\r", "", $databases), "\n"));
}
}
foreach ((array) $databases as $db) {
2013-04-04 01:49:05 +00:00
$adminer->dumpDatabase($db);
if ($connection->select_db($db)) {
2010-10-22 19:27:53 +00:00
if ($is_sql && ereg('CREATE', $style) && ($create = $connection->result("SHOW CREATE DATABASE " . idf_escape($db), 1))) {
if ($style == "DROP+CREATE") {
echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n";
}
2013-04-04 01:49:05 +00:00
echo "$create;\n";
}
2010-10-22 19:27:53 +00:00
if ($is_sql) {
if ($style) {
echo use_sql($db) . ";\n\n";
}
$out = "";
if ($_POST["routines"]) {
foreach (array("FUNCTION", "PROCEDURE") as $routine) {
2010-10-13 16:59:15 +00:00
foreach (get_rows("SHOW $routine STATUS WHERE Db = " . q($db), null, "-- ") as $row) {
$out .= ($style != 'DROP+CREATE' ? "DROP $routine IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
2012-08-09 16:39:12 +00:00
. remove_definer($connection->result("SHOW CREATE $routine " . idf_escape($row["Name"]), 2)) . ";;\n\n";
}
}
}
if ($_POST["events"]) {
2010-10-13 16:59:15 +00:00
foreach (get_rows("SHOW EVENTS", null, "-- ") as $row) {
$out .= ($style != 'DROP+CREATE' ? "DROP EVENT IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
2012-08-09 16:39:12 +00:00
. remove_definer($connection->result("SHOW CREATE EVENT " . idf_escape($row["Name"]), 3)) . ";;\n\n";
}
}
if ($out) {
echo "DELIMITER ;;\n\n$out" . "DELIMITER ;\n\n";
}
}
if ($_POST["table_style"] || $_POST["data_style"]) {
$views = array();
2011-08-08 16:00:26 +00:00
foreach (table_status() as $table_status) {
$table = (DB == "" || in_array($table_status["Name"], (array) $_POST["tables"]));
$data = (DB == "" || in_array($table_status["Name"], (array) $_POST["data"]));
if ($table || $data) {
2011-08-08 16:00:26 +00:00
if (!is_view($table_status)) {
if ($ext == "tar") {
ob_start();
}
2011-08-08 16:00:26 +00:00
$adminer->dumpTable($table_status["Name"], ($table ? $_POST["table_style"] : ""));
if ($data) {
2011-08-08 16:00:26 +00:00
$adminer->dumpData($table_status["Name"], $_POST["data_style"], "SELECT * FROM " . table($table_status["Name"]));
}
2011-08-08 16:00:26 +00:00
if ($is_sql && $_POST["triggers"] && $table && ($triggers = trigger_sql($table_status["Name"], $_POST["table_style"]))) {
echo "\nDELIMITER ;;\n$triggers\nDELIMITER ;\n";
}
if ($ext == "tar") {
2011-08-08 16:00:26 +00:00
echo tar_file((DB != "" ? "" : "$db/") . "$table_status[Name].csv", ob_get_clean());
2010-10-22 19:27:53 +00:00
} elseif ($is_sql) {
echo "\n";
}
2010-10-22 19:27:53 +00:00
} elseif ($is_sql) {
2011-08-08 16:00:26 +00:00
$views[] = $table_status["Name"];
}
}
}
foreach ($views as $view) {
2010-10-29 15:03:02 +00:00
$adminer->dumpTable($view, $_POST["table_style"], true);
}
if ($ext == "tar") {
echo pack("x512");
}
}
}
}
2010-10-22 19:27:53 +00:00
if ($is_sql) {
echo "-- " . $connection->result("SELECT NOW()") . "\n";
}
exit;
}
page_header(lang('Export'), "", ($_GET["export"] != "" ? array("table" => $_GET["export"]) : array()), DB);
?>
<form action="" method="post">
<table cellspacing="0">
<?php
$db_style = array('', 'USE', 'DROP+CREATE', 'CREATE');
$table_style = array('', 'DROP+CREATE', 'CREATE');
2010-05-27 14:10:17 +00:00
$data_style = array('', 'TRUNCATE+INSERT', 'INSERT');
2013-04-04 01:49:05 +00:00
if ($jush == "sql") { //! use insert_update() in all drivers
2010-05-27 14:10:17 +00:00
$data_style[] = 'INSERT+UPDATE';
}
parse_str($_COOKIE["adminer_export"], $row);
if (!$row) {
$row = array("output" => "text", "format" => "sql", "db_style" => (DB != "" ? "" : "CREATE"), "table_style" => "DROP+CREATE", "data_style" => "INSERT");
}
2011-01-19 13:32:35 +00:00
if (!isset($row["events"])) { // backwards compatibility
$row["routines"] = $row["events"] = ($_GET["dump"] == "");
2011-04-20 15:15:16 +00:00
$row["triggers"] = $row["table_style"];
2011-01-19 13:32:35 +00:00
}
echo "<tr><th>" . lang('Output') . "<td>" . html_select("output", $adminer->dumpOutput(), $row["output"], 0) . "\n"; // 0 - radio
echo "<tr><th>" . lang('Format') . "<td>" . html_select("format", $adminer->dumpFormat(), $row["format"], 0) . "\n"; // 0 - radio
2010-05-27 14:10:17 +00:00
echo ($jush == "sqlite" ? "" : "<tr><th>" . lang('Database') . "<td>" . html_select('db_style', $db_style, $row["db_style"])
2011-01-19 13:32:35 +00:00
. (support("routine") ? checkbox("routines", 1, $row["routines"], lang('Routines')) : "")
. (support("event") ? checkbox("events", 1, $row["events"], lang('Events')) : "")
2010-05-27 14:10:17 +00:00
);
echo "<tr><th>" . lang('Tables') . "<td>" . html_select('table_style', $table_style, $row["table_style"])
2011-01-19 13:32:35 +00:00
. checkbox("auto_increment", 1, $row["auto_increment"], lang('Auto Increment'))
. (support("trigger") ? checkbox("triggers", 1, $row["triggers"], lang('Triggers')) : "")
;
echo "<tr><th>" . lang('Data') . "<td>" . html_select('data_style', $data_style, $row["data_style"]);
?>
</table>
<p><input type="submit" value="<?php echo lang('Export'); ?>">
<table cellspacing="0">
<?php
$prefixes = array();
if (DB != "") {
$checked = ($TABLE != "" ? "" : " checked");
echo "<thead><tr>";
echo "<th style='text-align: left;'><label><input type='checkbox' id='check-tables'$checked onclick='formCheck(this, /^tables\\[/);'>" . lang('Tables') . "</label>";
echo "<th style='text-align: right;'><label>" . lang('Data') . "<input type='checkbox' id='check-data'$checked onclick='formCheck(this, /^data\\[/);'></label>";
echo "</thead>\n";
$views = "";
//! defer number of rows to JavaScript
2011-08-08 16:00:26 +00:00
foreach (table_status() as $table_status) {
$name = $table_status["Name"];
$prefix = ereg_replace("_.*", "", $name);
$checked = ($TABLE == "" || $TABLE == (substr($TABLE, -1) == "%" ? "$prefix%" : $name)); //! % may be part of table name
2012-08-08 16:23:46 +00:00
$print = "<tr><td>" . checkbox("tables[]", $name, $checked, $name, "checkboxClick(event, this); formUncheck('check-tables');");
2011-08-08 16:00:26 +00:00
if (is_view($table_status)) {
$views .= "$print\n";
} else {
2012-08-08 16:23:46 +00:00
echo "$print<td align='right'><label>" . ($table_status["Engine"] == "InnoDB" && $table_status["Rows"] ? "~ " : "") . $table_status["Rows"] . checkbox("data[]", $name, $checked, "", "checkboxClick(event, this); formUncheck('check-data');") . "</label>\n";
}
$prefixes[$prefix]++;
}
echo $views;
} else {
echo "<thead><tr><th style='text-align: left;'><label><input type='checkbox' id='check-databases'" . ($TABLE == "" ? " checked" : "") . " onclick='formCheck(this, /^databases\\[/);'>" . lang('Database') . "</label></thead>\n";
2012-02-24 06:54:48 +00:00
$databases = $adminer->databases();
if ($databases) {
foreach ($databases as $db) {
if (!information_schema($db)) {
$prefix = ereg_replace("_.*", "", $db);
echo "<tr><td>" . checkbox("databases[]", $db, $TABLE == "" || $TABLE == "$prefix%", $db, "formUncheck('check-databases');") . "</label>\n";
$prefixes[$prefix]++;
}
}
} else {
2011-03-21 12:53:48 +00:00
echo "<tr><td><textarea name='databases' rows='10' cols='20'></textarea>";
}
}
?>
</table>
</form>
<?php
$first = true;
foreach ($prefixes as $key => $val) {
if ($key != "" && $val > 1) {
echo ($first ? "<p>" : " ") . "<a href='" . h(ME) . "dump=" . urlencode("$key%") . "'>" . h($key) . "</a>";
$first = false;
}
}