Export Output in select.inc.php

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@447 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2008-08-18 15:18:16 +00:00
parent ec23248c51
commit 9890b6c22d
4 changed files with 39 additions and 29 deletions

View file

@ -75,22 +75,24 @@ function dump($db, $style) {
echo ($_POST["format"] != "csv" ? "USE " . idf_escape($db) . ";\n" : ""); echo ($_POST["format"] != "csv" ? "USE " . idf_escape($db) . ";\n" : "");
if (!strlen($_GET["db"])) { if (!strlen($_GET["db"])) {
$views = array(); $views = array();
$result = $mysql->query("SHOW TABLE STATUS"); if ($_POST["tables"][0] || $_POST["data"][0]) {
while ($row = $result->fetch_assoc()) { $result = $mysql->query("SHOW TABLE STATUS");
if (isset($row["Engine"])) { while ($row = $result->fetch_assoc()) {
if ($_POST["format"] == "csv") { if (isset($row["Engine"])) {
ob_start(); if ($_POST["format"] == "csv") {
ob_start();
}
dump_table($row["Name"], $_POST["tables"][0]);
dump_data($row["Name"], $_POST["data"][0]);
if ($_POST["format"] == "csv") {
echo tar_file("$db/$row[Name].csv", ob_get_clean());
}
} else {
$views[] = $row["Name"];
} }
dump_table($row["Name"], $_POST["tables"][0]);
dump_data($row["Name"], $_POST["data"][0]);
if ($_POST["format"] == "csv") {
echo tar_file("$db/$row[Name].csv", ob_get_clean());
}
} else {
$views[] = $row["Name"];
} }
$result->free();
} }
$result->free();
if ($_POST["format"] != "csv") { if ($_POST["format"] != "csv") {
foreach ($views as $view) { foreach ($views as $view) {
dump_table($view, $_POST["tables"][0]); dump_table($view, $_POST["tables"][0]);
@ -112,10 +114,7 @@ function tar_file($filename, $contents) {
} }
if ($_POST) { if ($_POST) {
$filename = (strlen($_GET["db"]) ? preg_replace('~[^a-z0-9_]~i', '-', (strlen($_GET["dump"]) ? $_GET["dump"] : $_GET["db"])) : "dump"); $ext = dump_headers((strlen($_GET["dump"]) ? $_GET["dump"] : $_GET["db"]), (!strlen($_GET["db"]) || count(array_filter($_POST["tables"]) + array_filter($_POST["data"])) > 1));
$ext = ($_POST["format"] == "sql" ? "sql" : (!strlen($_GET["db"]) || count(array_filter($_POST["tables"]) + array_filter($_POST["data"])) > 1 ? "tar" : "csv"));
header("Content-Type: " . ($ext == "tar" ? "application/x-tar" : ($ext == "sql" || $_POST["output"] != "file" ? "text/plain" : "text/csv")) . "; charset=utf-8");
header("Content-Disposition: " . ($_POST["output"] == "file" ? "attachment" : "inline") . "; filename=$filename.$ext");
if ($_POST["format"] != "csv") { if ($_POST["format"] != "csv") {
$max_packet = 16777216; $max_packet = 16777216;
echo "SET NAMES utf8;\n"; echo "SET NAMES utf8;\n";
@ -133,14 +132,16 @@ if ($_POST) {
} }
if (strlen($_GET["db"])) { if (strlen($_GET["db"])) {
foreach ($_POST["tables"] as $key => $style) { foreach ($_POST["tables"] as $key => $style) {
$table = bracket_escape($key, "back"); if ($style || $_POST["data"][$key]) {
if ($ext == "tar") { $table = bracket_escape($key, "back");
ob_start(); if ($ext == "tar") {
} ob_start();
dump_table($table, $style); }
dump_data($table, $_POST["data"][$key]); dump_table($table, $style);
if ($ext == "tar") { dump_data($table, $_POST["data"][$key]);
echo tar_file("$table.csv", ob_get_clean()); if ($ext == "tar") {
echo tar_file("$table.csv", ob_get_clean());
}
} }
} }
dump_routines($_GET["db"]); dump_routines($_GET["db"]);
@ -163,7 +164,7 @@ function check(td, name, value) {
</script> </script>
<form action="" method="post"> <form action="" method="post">
<p><?php echo lang('Output') . ": <select name='output'><option value='text'>" . lang('open') . "</option><option value='file'>" . lang('save') . "</option></select> " . $dump_options; ?></p> <p><?php echo $dump_options; ?></p>
<?php <?php
echo "<table border='1' cellspacing='0' cellpadding='2'>\n<thead><tr><th>" . lang('Database') . "</th>"; echo "<table border='1' cellspacing='0' cellpadding='2'>\n<thead><tr><th>" . lang('Database') . "</th>";

View file

@ -56,5 +56,13 @@ function dump_data($table, $style, $from = "") {
} }
} }
$dump_options = lang('Format') . ": <select name='format'><option value='sql'>" . lang('SQL') . "</option><option value='csv'>" . lang('CSV') . "</option></select>"; function dump_headers($identifier, $multi_table = false) {
$filename = (strlen($identifier) ? preg_replace('~[^a-z0-9_]~i', '-', $identifier) : "dump");
$ext = ($_POST["format"] == "sql" ? "sql" : ($multi_table ? "tar" : "csv"));
header("Content-Type: " . ($ext == "tar" ? "application/x-tar" : ($ext == "sql" || $_POST["output"] != "file" ? "text/plain" : "text/csv")) . "; charset=utf-8");
header("Content-Disposition: " . ($_POST["output"] == "file" ? "attachment" : "inline") . "; filename=$filename.$ext");
return $ext;
}
$dump_options = lang('Output') . ": <select name='output'><option value='text'>" . lang('open') . "</option><option value='file'>" . lang('save') . "</option></select> " . lang('Format') . ": <select name='format'><option value='sql'>" . lang('SQL') . "</option><option value='csv'>" . lang('CSV') . "</option></select>";
$max_packet = 0; $max_packet = 0;

View file

@ -172,4 +172,6 @@ $translations = array(
'Tables' => 'Tabulky', 'Tables' => 'Tabulky',
'Structure' => 'Struktura', 'Structure' => 'Struktura',
'Data' => 'Data', 'Data' => 'Data',
'Export selected' => 'Exportovat označené',
'Export result' => 'Exportovat výsledek',
); );

View file

@ -64,8 +64,7 @@ if ($_POST && !$error) {
$result = true; $result = true;
$deleted = 0; $deleted = 0;
if ($_POST["export"] || $_POST["export_result"]) { if ($_POST["export"] || $_POST["export_result"]) {
header("Content-Type: text/plain; charset=utf-8"); dump_headers($_GET["select"]);
header("Content-Disposition: inline; filename=" . preg_replace('~[^a-z0-9_]~i', '-', $_GET["select"]) . "." . ($_POST["format"] == "sql" ? "sql" : "csv"));
} }
if (isset($_POST["truncate"])) { if (isset($_POST["truncate"])) {
$result = $mysql->query($where ? "DELETE FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", $where) : "TRUNCATE " . idf_escape($_GET["select"])); $result = $mysql->query($where ? "DELETE FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", $where) : "TRUNCATE " . idf_escape($_GET["select"]));