2007-07-02 05:51:26 +00:00
< ? php
2009-08-29 13:57:50 +00:00
$TABLE = $_GET [ " dump " ];
2009-09-01 13:09:14 +00:00
2008-06-20 14:13:37 +00:00
if ( $_POST ) {
2009-08-29 13:57:50 +00:00
$ext = dump_headers (( strlen ( $TABLE ) ? $TABLE : DB ), ( ! strlen ( DB ) || count (( array ) $_POST [ " tables " ] + ( array ) $_POST [ " data " ]) > 1 ));
2009-08-28 11:49:57 +00:00
if ( $_POST [ " format " ] == " sql " ) {
2009-10-02 12:31:28 +00:00
echo " -- Adminer $VERSION dump
2009-09-01 13:09:14 +00:00
SET NAMES utf8 ;
2009-08-28 11:49:57 +00:00
SET foreign_key_checks = 0 ;
2009-09-22 10:51:40 +00:00
SET time_zone = " . $connection->quote ( $connection->result ( $connection->query ( " SELECT @@ time_zone " ))) . " ;
2009-08-28 11:49:57 +00:00
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO' ;
2009-10-02 12:31:28 +00:00
" ;
2008-08-16 12:28:31 +00:00
}
2008-06-20 14:13:37 +00:00
2009-06-01 12:53:19 +00:00
$style = $_POST [ " db_style " ];
2009-08-29 13:54:25 +00:00
foreach (( strlen ( DB ) ? array ( DB ) : ( array ) $_POST [ " databases " ]) as $db ) {
2009-09-22 10:51:40 +00:00
if ( $connection -> select_db ( $db )) {
if ( $_POST [ " format " ] == " sql " && ereg ( 'CREATE' , $style ) && ( $result = $connection -> query ( " SHOW CREATE DATABASE " . idf_escape ( $db )))) {
2009-06-01 12:53:19 +00:00
if ( $style == " DROP+CREATE " ) {
2009-10-02 12:31:28 +00:00
echo " DROP DATABASE IF EXISTS " . idf_escape ( $db ) . " ; \n " ;
2008-08-18 15:18:16 +00:00
}
2009-09-22 10:51:40 +00:00
$create = $connection -> result ( $result , 1 );
2009-10-02 12:31:28 +00:00
echo ( $style == " CREATE+ALTER " ? preg_replace ( '~^CREATE DATABASE ~' , '\\0IF NOT EXISTS ' , $create ) : $create ) . " ; \n " ;
2008-08-19 09:19:18 +00:00
}
2009-09-25 11:29:46 +00:00
if ( $_POST [ " format " ] == " sql " ) {
if ( $style ) {
2009-10-13 12:47:53 +00:00
echo " USE " . idf_escape ( $db ) . " ; \n \n " ;
}
if ( in_array ( " CREATE+ALTER " , array ( $style , $_POST [ " table_style " ]))) {
echo " SET @adminer_alter = ''; \n \n " ;
2009-09-25 11:29:46 +00:00
}
2008-09-01 16:49:38 +00:00
$out = " " ;
2009-09-25 11:29:46 +00:00
if ( $_POST [ " routines " ]) {
2008-08-19 09:19:18 +00:00
foreach ( array ( " FUNCTION " , " PROCEDURE " ) as $routine ) {
2009-09-22 10:51:40 +00:00
$result = $connection -> query ( " SHOW $routine STATUS WHERE Db = " . $connection -> quote ( $db ));
2008-08-19 09:19:18 +00:00
while ( $row = $result -> fetch_assoc ()) {
2009-06-16 05:49:10 +00:00
$out .= ( $style != 'DROP+CREATE' ? " DROP $routine IF EXISTS " . idf_escape ( $row [ " Name " ]) . " ;; \n " : " " )
2009-09-22 10:51:40 +00:00
. $connection -> result ( $connection -> query ( " SHOW CREATE $routine " . idf_escape ( $row [ " Name " ])), 2 ) . " ;; \n \n " ;
2008-08-19 09:19:18 +00:00
}
}
}
2009-09-25 11:29:46 +00:00
if ( $_POST [ " events " ]) {
2009-09-22 10:51:40 +00:00
$result = $connection -> query ( " SHOW EVENTS " );
2008-09-01 16:49:38 +00:00
while ( $row = $result -> fetch_assoc ()) {
2009-06-16 05:49:10 +00:00
$out .= ( $style != 'DROP+CREATE' ? " DROP EVENT IF EXISTS " . idf_escape ( $row [ " Name " ]) . " ;; \n " : " " )
2009-09-22 10:51:40 +00:00
. $connection -> result ( $connection -> query ( " SHOW CREATE EVENT " . idf_escape ( $row [ " Name " ])), 3 ) . " ;; \n \n " ;
2008-09-01 16:49:38 +00:00
}
}
2009-08-28 11:49:57 +00:00
if ( $out ) {
2009-10-02 12:31:28 +00:00
echo " DELIMITER ;; \n \n $out " . " DELIMITER ; \n \n " ;
2009-08-28 11:49:57 +00:00
}
2008-08-19 09:19:18 +00:00
}
2009-06-01 12:53:19 +00:00
if ( $_POST [ " table_style " ] || $_POST [ " data_style " ]) {
2008-08-19 09:19:18 +00:00
$views = array ();
2009-07-01 19:58:22 +00:00
foreach ( table_status () as $row ) {
2009-08-29 13:54:25 +00:00
$table = ( ! strlen ( DB ) || in_array ( $row [ " Name " ], ( array ) $_POST [ " tables " ]));
$data = ( ! strlen ( DB ) || in_array ( $row [ " Name " ], ( array ) $_POST [ " data " ]));
2009-06-01 12:53:19 +00:00
if ( $table || $data ) {
2008-08-19 09:19:18 +00:00
if ( isset ( $row [ " Engine " ])) {
if ( $ext == " tar " ) {
ob_start ();
}
2009-06-01 12:53:19 +00:00
dump_table ( $row [ " Name " ], ( $table ? $_POST [ " table_style " ] : " " ));
if ( $data ) {
dump_data ( $row [ " Name " ], $_POST [ " data_style " ]);
}
2009-06-15 21:47:29 +00:00
if ( $table ) {
dump_triggers ( $row [ " Name " ], $_POST [ " table_style " ]);
}
2008-08-19 09:19:18 +00:00
if ( $ext == " tar " ) {
2009-10-02 12:31:28 +00:00
echo tar_file (( strlen ( DB ) ? " " : " $db / " ) . " $row[Name] .csv " , ob_get_clean ());
2009-08-28 11:49:57 +00:00
} elseif ( $_POST [ " format " ] == " sql " ) {
2009-10-02 12:31:28 +00:00
echo " \n " ;
2008-08-19 09:19:18 +00:00
}
2009-08-28 11:49:57 +00:00
} elseif ( $_POST [ " format " ] == " sql " ) {
2009-06-01 12:53:19 +00:00
$views [] = $row [ " Name " ];
2008-08-19 09:19:18 +00:00
}
}
}
2009-06-01 12:53:19 +00:00
foreach ( $views as $view ) {
dump_table ( $view , $_POST [ " table_style " ], true );
2008-08-18 15:18:16 +00:00
}
2009-08-18 10:29:08 +00:00
if ( $ext == " tar " ) {
2009-10-02 12:31:28 +00:00
echo pack ( " x512 " );
2009-08-18 10:29:08 +00:00
}
2008-08-16 12:28:31 +00:00
}
2008-08-19 10:32:34 +00:00
2009-08-28 11:49:57 +00:00
if ( $style == " CREATE+ALTER " && $_POST [ " format " ] == " sql " ) {
2009-06-21 23:20:32 +00:00
// drop old tables
2008-08-19 10:32:34 +00:00
$query = " SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() " ;
2009-10-02 12:31:28 +00:00
echo " DELIMITER ;;
2009-08-29 13:51:32 +00:00
CREATE PROCEDURE adminer_alter ( INOUT alter_command text ) BEGIN
2008-08-19 10:32:34 +00:00
DECLARE _table_name , _engine , _table_collation varchar ( 64 );
DECLARE _table_comment varchar ( 64 );
DECLARE done bool DEFAULT 0 ;
2009-08-28 11:49:57 +00:00
DECLARE tables CURSOR FOR $query ;
2008-08-19 10:32:34 +00:00
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
OPEN tables ;
REPEAT
FETCH tables INTO _table_name , _engine , _table_collation , _table_comment ;
IF NOT done THEN
2009-10-02 12:31:28 +00:00
CASE _table_name " ;
2009-09-22 10:51:40 +00:00
$result = $connection -> query ( $query );
2009-08-29 13:58:35 +00:00
while ( $row = $result -> fetch_assoc ()) {
2009-09-22 10:51:40 +00:00
$comment = $connection -> quote ( $row [ " ENGINE " ] == " InnoDB " ? preg_replace ( '~(?:(.+); )?InnoDB free: .*~' , '\\1' , $row [ " TABLE_COMMENT " ]) : $row [ " TABLE_COMMENT " ]);
2009-10-02 12:31:28 +00:00
echo "
2009-09-22 10:51:40 +00:00
WHEN " . $connection->quote ( $row["TABLE_NAME"] ) . " THEN
2009-06-22 00:53:51 +00:00
" . (isset( $row["ENGINE"] ) ? " IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != $comment THEN
ALTER TABLE " . idf_escape( $row["TABLE_NAME"] ) . " ENGINE = $row [ ENGINE ] COLLATE = $row [ TABLE_COLLATION ] COMMENT = $comment ;
2009-10-02 12:31:28 +00:00
END IF " : " BEGIN END " ) . " ; " ;
2009-08-29 13:58:35 +00:00
}
2009-10-02 12:31:28 +00:00
echo "
2008-08-19 10:32:34 +00:00
ELSE
2009-08-29 13:51:32 +00:00
SET alter_command = CONCAT ( alter_command , 'DROP TABLE `' , REPLACE ( _table_name , '`' , '``' ), '`;\\n' );
2008-08-19 10:32:34 +00:00
END CASE ;
END IF ;
UNTIL done END REPEAT ;
CLOSE tables ;
END ;;
DELIMITER ;
2009-08-29 13:51:32 +00:00
CALL adminer_alter ( @ adminer_alter );
DROP PROCEDURE adminer_alter ;
2009-10-02 12:31:28 +00:00
" ;
2008-08-19 10:32:34 +00:00
}
2009-08-29 13:51:32 +00:00
if ( in_array ( " CREATE+ALTER " , array ( $style , $_POST [ " table_style " ])) && $_POST [ " format " ] == " sql " ) {
2009-10-02 12:31:28 +00:00
echo " SELECT @adminer_alter; \n " ;
2009-08-29 13:51:32 +00:00
}
2008-08-08 15:00:42 +00:00
}
2008-06-20 14:13:37 +00:00
}
exit ;
}
2007-08-04 19:40:11 +00:00
2009-08-29 13:54:25 +00:00
page_header ( lang ( 'Export' ), " " , ( strlen ( $_GET [ " export " ]) ? array ( " table " => $_GET [ " export " ]) : array ()), DB );
2008-06-20 14:13:37 +00:00
?>
< form action = " " method = " post " >
2009-06-01 12:53:19 +00:00
< table cellspacing = " 0 " >
2009-05-31 22:31:09 +00:00
< ? php
2009-08-25 15:23:19 +00:00
$db_style = array ( '' , 'USE' , 'DROP+CREATE' , 'CREATE' );
$table_style = array ( '' , 'DROP+CREATE' , 'CREATE' );
$data_style = array ( '' , 'TRUNCATE+INSERT' , 'INSERT' , 'INSERT+UPDATE' );
2009-09-22 10:51:40 +00:00
if ( $connection -> server_info >= 5 ) {
2009-06-01 12:53:19 +00:00
$db_style [] = 'CREATE+ALTER' ;
$table_style [] = 'CREATE+ALTER' ;
}
2009-10-02 12:21:55 +00:00
echo " <tr><th> " . lang ( 'Output' ) . " <td><input type='hidden' name='token' value=' $token '> " . $adminer -> dumpOutput ( 0 ) . " \n " ; // token is not needed but checked in bootstrap for all POST data
echo " <tr><th> " . lang ( 'Format' ) . " <td> " . $adminer -> dumpFormat ( 0 ) . " \n " ;
2009-10-02 13:21:34 +00:00
echo " <tr><th> " . lang ( 'Database' ) . " <td> " . html_select ( 'db_style' , $db_style , ( strlen ( DB ) ? '' : 'CREATE' ));
2009-09-25 11:29:46 +00:00
if ( $connection -> server_info >= 5 ) {
2009-10-07 10:11:27 +00:00
$checked = ! strlen ( $_GET [ " dump " ]);
2009-10-02 13:21:34 +00:00
echo checkbox ( " routines " , 1 , $checked , lang ( 'Routines' ));
2009-09-25 11:29:46 +00:00
if ( $connection -> server_info >= 5.1 ) {
2009-10-02 13:21:34 +00:00
echo checkbox ( " events " , 1 , $checked , lang ( 'Events' ));
2009-09-25 11:29:46 +00:00
}
}
2009-10-02 13:21:34 +00:00
echo " <tr><th> " . lang ( 'Tables' ) . " <td> " . html_select ( 'table_style' , $table_style , 'DROP+CREATE' );
echo " <tr><th> " . lang ( 'Data' ) . " <td> " . html_select ( 'data_style' , $data_style , 'INSERT' );
2009-05-31 22:31:09 +00:00
?>
</ table >
2009-09-11 17:08:18 +00:00
< p >< input type = " submit " value = " <?php echo lang('Export'); ?> " ></ p >
2008-06-20 14:13:37 +00:00
2009-06-21 23:49:04 +00:00
< table cellspacing = " 0 " >
2008-06-20 14:13:37 +00:00
< ? php
2009-08-29 13:54:25 +00:00
if ( strlen ( DB )) {
2009-08-29 13:57:50 +00:00
$checked = ( strlen ( $TABLE ) ? " " : " checked " );
2009-06-03 09:17:20 +00:00
echo " <thead><tr> " ;
2009-07-16 17:13:14 +00:00
echo " <th style='text-align: left;'><label><input type='checkbox' id='check-tables' $checked onclick='form_check(this, /^tables \\ [/);'> " . lang ( 'Tables' ) . " </label> " ;
echo " <th style='text-align: right;'><label> " . lang ( 'Data' ) . " <input type='checkbox' id='check-data' $checked onclick='form_check(this, /^data \\ [/);'></label> " ;
2009-07-11 20:30:40 +00:00
echo " </thead> \n " ;
2009-05-31 22:31:09 +00:00
$views = " " ;
2009-07-01 19:58:22 +00:00
foreach ( table_status () as $row ) {
2009-09-26 16:52:32 +00:00
$checked = ! strlen ( $TABLE ) || $row [ " Name " ] == $TABLE ;
2009-09-25 12:03:30 +00:00
$print = " <tr><td> " . checkbox ( " tables[] " , $row [ " Name " ], $checked , $row [ " Name " ], " form_uncheck('check-tables'); " );
2009-05-31 22:31:09 +00:00
if ( ! $row [ " Engine " ]) {
2009-07-11 20:30:40 +00:00
$views .= " $print\n " ;
2009-05-31 22:31:09 +00:00
} else {
2009-09-25 12:03:30 +00:00
echo " $print <td align='right'><label> " . ( $row [ " Engine " ] == " InnoDB " && $row [ " Rows " ] ? lang ( '~ %s' , $row [ " Rows " ]) : $row [ " Rows " ]) . checkbox ( " data[] " , $row [ " Name " ], $checked , " " , " form_uncheck('check-data'); " ) . " </label> \n " ;
2008-07-31 14:28:26 +00:00
}
2008-06-20 14:13:37 +00:00
}
2009-06-21 23:49:04 +00:00
echo $views ;
} else {
2009-07-28 16:28:47 +00:00
echo " <thead><tr><th style='text-align: left;'><label><input type='checkbox' id='check-databases' checked onclick='form_check(this, /^databases \\ [/);'> " . lang ( 'Database' ) . " </label></thead> \n " ;
2009-06-21 23:49:04 +00:00
foreach ( get_databases () as $db ) {
2009-06-22 09:58:58 +00:00
if ( ! information_schema ( $db )) {
2009-09-25 12:03:30 +00:00
echo " <tr><td> " . checkbox ( " databases[] " , $db , 1 , $db , " form_uncheck('check-databases'); " ) . " </label> \n " ;
2009-06-21 23:49:04 +00:00
}
}
2008-06-20 14:13:37 +00:00
}
?>
2009-06-21 23:49:04 +00:00
</ table >
2008-06-20 14:13:37 +00:00
</ form >