mirror of
https://github.com/vrana/adminer.git
synced 2025-08-17 20:01:25 +02:00
Compress export and import
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1032 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
//! memory consumption, speed
|
||||
|
||||
function dump_table($table, $style, $is_view = false) {
|
||||
global $dbh;
|
||||
if ($_POST["format"] == "csv") {
|
||||
echo "\xef\xbb\xbf"; // UTF-8 byte order mark
|
||||
dump("\xef\xbb\xbf"); // UTF-8 byte order mark
|
||||
if ($style) {
|
||||
dump_csv(array_keys(fields($table)));
|
||||
}
|
||||
@@ -10,16 +12,15 @@ function dump_table($table, $style, $is_view = false) {
|
||||
$result = $dbh->query("SHOW CREATE TABLE " . idf_escape($table));
|
||||
if ($result) {
|
||||
if ($style == "DROP+CREATE") {
|
||||
echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . idf_escape($table) . ";\n";
|
||||
dump("DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . idf_escape($table) . ";\n");
|
||||
}
|
||||
$create = $dbh->result($result, 1);
|
||||
echo ($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n";
|
||||
dump(($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n");
|
||||
}
|
||||
if ($style == "CREATE+ALTER" && !$is_view) {
|
||||
// create procedure which iterates over original columns and adds new and removes old
|
||||
$query = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . $dbh->quote($table) . " ORDER BY ORDINAL_POSITION";
|
||||
?>
|
||||
DELIMITER ;;
|
||||
dump("DELIMITER ;;
|
||||
CREATE PROCEDURE adminer_alter () BEGIN
|
||||
DECLARE _column_name, _collation_name, _column_type, after varchar(64) DEFAULT '';
|
||||
DECLARE _column_default longtext;
|
||||
@@ -27,7 +28,7 @@ CREATE PROCEDURE adminer_alter () BEGIN
|
||||
DECLARE _extra varchar(20);
|
||||
DECLARE _column_comment varchar(255);
|
||||
DECLARE done, set_after bool DEFAULT 0;
|
||||
DECLARE add_columns text DEFAULT '<?php
|
||||
DECLARE add_columns text DEFAULT '");
|
||||
$fields = array();
|
||||
$result = $dbh->query($query);
|
||||
$after = "";
|
||||
@@ -43,12 +44,12 @@ CREATE PROCEDURE adminer_alter () BEGIN
|
||||
. ($row["COLUMN_COMMENT"] ? " COMMENT " . $dbh->quote($row["COLUMN_COMMENT"]) : "")
|
||||
. ($after ? " AFTER " . idf_escape($after) : " FIRST")
|
||||
);
|
||||
echo ", ADD $row[alter]";
|
||||
dump(", ADD $row[alter]");
|
||||
$fields[] = $row;
|
||||
$after = $row["COLUMN_NAME"];
|
||||
}
|
||||
?>';
|
||||
DECLARE columns CURSOR FOR <?php echo $query; ?>;
|
||||
dump("';
|
||||
DECLARE columns CURSOR FOR $query;
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
||||
SET @alter_table = '';
|
||||
OPEN columns;
|
||||
@@ -56,17 +57,16 @@ CREATE PROCEDURE adminer_alter () BEGIN
|
||||
FETCH columns INTO _column_name, _column_default, _is_nullable, _collation_name, _column_type, _extra, _column_comment;
|
||||
IF NOT done THEN
|
||||
SET set_after = 1;
|
||||
CASE _column_name<?php
|
||||
CASE _column_name");
|
||||
foreach ($fields as $row) {
|
||||
echo "
|
||||
dump("
|
||||
WHEN " . $dbh->quote($row["COLUMN_NAME"]) . " THEN
|
||||
SET add_columns = REPLACE(add_columns, ', ADD $row[alter]', '');
|
||||
IF NOT (_column_default <=> $row[default]) OR _is_nullable != '$row[IS_NULLABLE]' OR _collation_name != '$row[COLLATION_NAME]' OR _column_type != '$row[COLUMN_TYPE]' OR _extra != '$row[EXTRA]' OR _column_comment != " . $dbh->quote($row["COLUMN_COMMENT"]) . " OR after != $row[after] THEN
|
||||
SET @alter_table = CONCAT(@alter_table, ', MODIFY $row[alter]');
|
||||
END IF;"; //! don't replace in comment
|
||||
END IF;"); //! don't replace in comment
|
||||
}
|
||||
?>
|
||||
|
||||
dump("
|
||||
ELSE
|
||||
SET @alter_table = CONCAT(@alter_table, ', DROP ', _column_name);
|
||||
SET set_after = 0;
|
||||
@@ -78,7 +78,7 @@ CREATE PROCEDURE adminer_alter () BEGIN
|
||||
UNTIL done END REPEAT;
|
||||
CLOSE columns;
|
||||
IF @alter_table != '' OR add_columns != '' THEN
|
||||
SET @alter_table = CONCAT('ALTER TABLE <?php echo idf_escape($table); ?>', SUBSTR(CONCAT(add_columns, @alter_table), 2));
|
||||
SET @alter_table = CONCAT('ALTER TABLE " . idf_escape($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2));
|
||||
PREPARE alter_command FROM @alter_table;
|
||||
EXECUTE alter_command;
|
||||
DROP PREPARE alter_command;
|
||||
@@ -88,7 +88,7 @@ DELIMITER ;
|
||||
CALL adminer_alter;
|
||||
DROP PROCEDURE adminer_alter;
|
||||
|
||||
<?php
|
||||
");
|
||||
//! indexes
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ function dump_data($table, $style, $select = "") {
|
||||
global $dbh, $max_packet;
|
||||
if ($style) {
|
||||
if ($_POST["format"] != "csv" && $style == "TRUNCATE+INSERT") {
|
||||
echo "TRUNCATE " . idf_escape($table) . ";\n";
|
||||
dump("TRUNCATE " . idf_escape($table) . ";\n");
|
||||
}
|
||||
$result = $dbh->query(($select ? $select : "SELECT * FROM " . idf_escape($table))); //! enum and set as numbers, microtime
|
||||
if ($result) {
|
||||
@@ -119,18 +119,18 @@ function dump_data($table, $style, $select = "") {
|
||||
foreach ($row2 as $key => $val) {
|
||||
$set[] = idf_escape($key) . " = $val";
|
||||
}
|
||||
echo "$insert ($s) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
|
||||
dump("$insert ($s) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n");
|
||||
} else {
|
||||
$s = "\n($s)";
|
||||
if (!$length) {
|
||||
echo $insert . $s;
|
||||
dump($insert . $s);
|
||||
$length = strlen($insert) + strlen($s);
|
||||
} else {
|
||||
$length += 1 + strlen($s); // 1 - separator length
|
||||
if ($length < $max_packet) {
|
||||
echo ",$s";
|
||||
dump(",$s");
|
||||
} else {
|
||||
echo ";\n$insert$s";
|
||||
dump(";\n$insert$s");
|
||||
$length = strlen($insert) + strlen($s);
|
||||
}
|
||||
}
|
||||
@@ -138,7 +138,7 @@ function dump_data($table, $style, $select = "") {
|
||||
}
|
||||
}
|
||||
if ($_POST["format"] != "csv" && $style != "INSERT+UPDATE" && $result->num_rows) {
|
||||
echo ";\n";
|
||||
dump(";\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,13 +147,22 @@ function dump_data($table, $style, $select = "") {
|
||||
function dump_headers($identifier, $multi_table = false) {
|
||||
$filename = (strlen($identifier) ? friendly_url($identifier) : "dump");
|
||||
$ext = ($_POST["format"] == "sql" ? "sql" : ($multi_table ? "tar" : "csv")); // multiple CSV packed to TAR
|
||||
header("Content-Type: " . ($ext == "tar" ? "application/x-tar" : ($ext == "sql" || $_POST["output"] != "file" ? "text/plain" : "text/csv")) . "; charset=utf-8");
|
||||
if ($_POST["output"] == "file") {
|
||||
header("Content-Disposition: attachment; filename=$filename.$ext");
|
||||
header("Content-Type: " . ($_POST["compress"] == "gz" ? "application/x-gzip" : ($ext == "tar" ? "application/x-tar" : ($ext == "sql" || $_POST["output"] != "file" ? "text/plain" : "text/csv")) . "; charset=utf-8"));
|
||||
if ($_POST["output"] == "file" || $_POST["compress"]) {
|
||||
header("Content-Disposition: attachment; filename=$filename.$ext" . ($_POST["compress"] == "gz" ? ".gz" : ""));
|
||||
}
|
||||
ob_flush();
|
||||
flush();
|
||||
return $ext;
|
||||
}
|
||||
|
||||
$compress = array();
|
||||
if (function_exists('gzencode')) {
|
||||
$compress['gz'] = 'GZIP';
|
||||
}
|
||||
// bzcompress can't be called repetitively, bzopen requires temporary file
|
||||
// ZipArchive requires temporary file, ZIP can be created by gzcompress - see PEAR File_Archive
|
||||
$dump_output = "<select name='output'>" . optionlist(array('text' => lang('open'), 'file' => lang('save'))) . "</select>";
|
||||
$dump_format = "<select name='format'>" . optionlist(array('sql' => 'SQL', 'csv' => 'CSV')) . "</select>";
|
||||
$dump_compress = ($compress ? "<select name='compress'><option>" . optionlist($compress) . "</select>" : "");
|
||||
$max_packet = 1048576; // default, minimum is 1024
|
||||
|
@@ -151,14 +151,22 @@ function pagination($page) {
|
||||
return " " . ($page == $_GET["page"] ? $page + 1 : '<a href="' . h(remove_from_uri("page") . ($page ? "&page=$page" : "")) . '">' . ($page + 1) . "</a>");
|
||||
}
|
||||
|
||||
function get_file($key) {
|
||||
function get_file($key, $decompress = false) {
|
||||
// returns int for error, string otherwise
|
||||
if (isset($_POST["files"][$key])) {
|
||||
$file = $_POST["files"][$key];
|
||||
if (isset($file)) {
|
||||
// get the file from hidden field if the user was logged out
|
||||
$length = strlen($_POST["files"][$key]);
|
||||
return ($length && $length < 4 ? intval($_POST["files"][$key]) : base64_decode($_POST["files"][$key]));
|
||||
$length = strlen($file);
|
||||
if ($length && $length < 4) {
|
||||
return intval($file);
|
||||
}
|
||||
return base64_decode($file);
|
||||
}
|
||||
return (!$_FILES[$key] || $_FILES[$key]["error"] ? $_FILES[$key]["error"] : file_get_contents($_FILES[$key]["tmp_name"]));
|
||||
$file = $_FILES[$key];
|
||||
if (!$file || $file["error"]) {
|
||||
return $file["error"];
|
||||
}
|
||||
return file_get_contents($decompress && ereg('\\.gz$', $file["name"]) ? "compress.zlib://$file[tmp_name]" : $file["tmp_name"]); //! may not be reachable because of open_basedir
|
||||
}
|
||||
|
||||
function upload_error($error) {
|
||||
@@ -352,13 +360,26 @@ function process_input($field) {
|
||||
}
|
||||
}
|
||||
|
||||
function dump($string = null) { // null $string forces sending of buffer
|
||||
static $buffer = "";
|
||||
if ($_POST["compress"] == "gz") {
|
||||
$buffer .= $string;
|
||||
if (!isset($string) || strlen($buffer) > 1e6) {
|
||||
echo gzencode($buffer);
|
||||
$buffer = "";
|
||||
}
|
||||
} else {
|
||||
echo $string;
|
||||
}
|
||||
}
|
||||
|
||||
function dump_csv($row) {
|
||||
foreach ($row as $key => $val) {
|
||||
if (preg_match("~[\"\n,]~", $val) || (isset($val) && !strlen($val))) {
|
||||
$row[$key] = '"' . str_replace('"', '""', $val) . '"';
|
||||
}
|
||||
}
|
||||
echo implode(",", $row) . "\n";
|
||||
dump(implode(",", $row) . "\n");
|
||||
}
|
||||
|
||||
function apply_sql_function($function, $column) {
|
||||
|
@@ -119,7 +119,7 @@ if (extension_loaded("mysqli")) {
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
mysql_free_result($this->_result);
|
||||
mysql_free_result($this->_result); //! is not called in PHP 4 which is a problem with mysql.trace_mode
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user