/** * Exports routines (procedures and functions) * * @param string $db Database * * @return bool Whether it succeeded */ public function exportRoutines($db) { global $crlf; $text = ''; $delimiter = '$$'; $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($procedure_names || $function_names) { $text .= $crlf . 'DELIMITER ' . $delimiter . $crlf; } if ($procedure_names) { $text .= $this->_exportComment() . $this->_exportComment(__('Procedures')) . $this->_exportComment(); foreach ($procedure_names as $procedure_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP PROCEDURE IF EXISTS ' . PMA_Util::backquote($procedure_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf; } } if ($function_names) { $text .= $this->_exportComment() . $this->_exportComment(__('Functions')) . $this->_exportComment(); foreach ($function_names as $function_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP FUNCTION IF EXISTS ' . PMA_Util::backquote($function_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'FUNCTION', $function_name) . $delimiter . $crlf . $crlf; } } if ($procedure_names || $function_names) { $text .= 'DELIMITER ;' . $crlf; } if (!empty($text)) { return PMA_exportOutputHandler($text); } else { return false; } }
// language; I have to send each statement one by one // to avoid selecting alternatively the current and new db // we would need to modify the CREATE definitions to qualify // the db name $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); if ($procedure_names) { foreach ($procedure_names as $procedure_name) { PMA_DBI_select_db($db); $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name); // collect for later display $GLOBALS['sql_query'] .= "\n" . $tmp_query; PMA_DBI_select_db($newname); PMA_DBI_query($tmp_query); } } $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($function_names) { foreach ($function_names as $function_name) { PMA_DBI_select_db($db); $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name); // collect for later display $GLOBALS['sql_query'] .= "\n" . $tmp_query; PMA_DBI_select_db($newname); PMA_DBI_query($tmp_query); } } } // go back to current db, just in case PMA_DBI_select_db($db); // Duplicate the bookmarks for this db (done once for each db) if (!$_error && $db != $newname) {
/** * Outputs export header. It is the first method to be called, so all * the required variables are initialized here. * * @return bool Whether it succeeded */ public function exportHeader() { $this->initSpecificVariables(); global $crlf, $cfg, $db; $table = $this->_getTable(); $tables = $this->_getTables(); $export_struct = isset($GLOBALS['xml_export_functions']) || isset($GLOBALS['xml_export_procedures']) || isset($GLOBALS['xml_export_tables']) || isset($GLOBALS['xml_export_triggers']) || isset($GLOBALS['xml_export_views']); $export_data = isset($GLOBALS['xml_export_contents']) ? true : false; if ($GLOBALS['output_charset_conversion']) { $charset = $GLOBALS['charset_of_file']; } else { $charset = 'utf-8'; } $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . '<!--' . $crlf . '- phpMyAdmin XML Dump' . $crlf . '- version ' . PMA_VERSION . $crlf . '- http://www.phpmyadmin.net' . $crlf . '-' . $crlf . '- ' . __('Host') . ': ' . $cfg['Server']['host']; if (!empty($cfg['Server']['port'])) { $head .= ':' . $cfg['Server']['port']; } $head .= $crlf . '- ' . __('Generation Time') . ': ' . PMA_Util::localisedDate() . $crlf . '- ' . __('Server version') . ': ' . PMA_MYSQL_STR_VERSION . $crlf . '- ' . __('PHP Version') . ': ' . phpversion() . $crlf . '-->' . $crlf . $crlf; $head .= '<pma_xml_export version="1.0"' . ($export_struct ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf; if ($export_struct) { if (PMA_DRIZZLE) { $result = PMA_DBI_fetch_result("SELECT\n 'utf8' AS DEFAULT_CHARACTER_SET_NAME,\n DEFAULT_COLLATION_NAME\n FROM data_dictionary.SCHEMAS\n WHERE SCHEMA_NAME = '" . PMA_Util::sqlAddSlashes($db) . "'"); } else { $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME`' . ' FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME`' . ' = \'' . PMA_Util::sqlAddSlashes($db) . '\' LIMIT 1'); } $db_collation = $result[0]['DEFAULT_COLLATION_NAME']; $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME']; $head .= ' <!--' . $crlf; $head .= ' - Structure schemas' . $crlf; $head .= ' -->' . $crlf; $head .= ' <pma:structure_schemas>' . $crlf; $head .= ' <pma:database name="' . htmlspecialchars($db) . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf; if (count($tables) == 0) { $tables[] = $table; } foreach ($tables as $table) { // Export tables and views $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table), 0); $tbl = $result[$table][1]; $is_view = PMA_Table::isView($db, $table); if ($is_view) { $type = 'view'; } else { $type = 'table'; } if ($is_view && !isset($GLOBALS['xml_export_views'])) { continue; } if (!$is_view && !isset($GLOBALS['xml_export_tables'])) { continue; } $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf; $tbl = " " . htmlspecialchars($tbl); $tbl = str_replace("\n", "\n ", $tbl); $head .= $tbl . ';' . $crlf; $head .= ' </pma:' . $type . '>' . $crlf; if (isset($GLOBALS['xml_export_triggers']) && $GLOBALS['xml_export_triggers']) { // Export triggers $triggers = PMA_DBI_get_triggers($db, $table); if ($triggers) { foreach ($triggers as $trigger) { $code = $trigger['create']; $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf; // Do some formatting $code = substr(rtrim($code), 0, -3); $code = " " . htmlspecialchars($code); $code = str_replace("\n", "\n ", $code); $head .= $code . $crlf; $head .= ' </pma:trigger>' . $crlf; } unset($trigger); unset($triggers); } } } if (isset($GLOBALS['xml_export_functions']) && $GLOBALS['xml_export_functions']) { // Export functions $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($functions) { foreach ($functions as $function) { $head .= ' <pma:function name="' . $function . '">' . $crlf; // Do some formatting $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function); $sql = rtrim($sql); $sql = " " . htmlspecialchars($sql); $sql = str_replace("\n", "\n ", $sql); $head .= $sql . $crlf; $head .= ' </pma:function>' . $crlf; } unset($function); unset($functions); } } if (isset($GLOBALS['xml_export_procedures']) && $GLOBALS['xml_export_procedures']) { // Export procedures $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); if ($procedures) { foreach ($procedures as $procedure) { $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf; // Do some formatting $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure); $sql = rtrim($sql); $sql = " " . htmlspecialchars($sql); $sql = str_replace("\n", "\n ", $sql); $head .= $sql . $crlf; $head .= ' </pma:procedure>' . $crlf; } unset($procedure); unset($procedures); } } unset($result); $head .= ' </pma:database>' . $crlf; $head .= ' </pma:structure_schemas>' . $crlf; if ($export_data) { $head .= $crlf; } } return PMA_exportOutputHandler($head); }
/** * Outputs export header * * @return bool Whether it suceeded * * @access public */ function PMA_exportHeader() { global $crlf; global $cfg; global $what; global $db; global $table; global $tables; $export_struct = isset($GLOBALS[$what . '_export_struc']) ? true : false; $export_data = isset($GLOBALS[$what . '_export_contents']) ? true : false; if ($GLOBALS['output_charset_conversion']) { $charset = $GLOBALS['charset_of_file']; } else { $charset = $GLOBALS['charset']; } $head = '<?xml version="1.0" encoding="' . $charset . '"?>' . $crlf . '<!--' . $crlf . '- phpMyAdmin XML Dump' . $crlf . '- version ' . PMA_VERSION . $crlf . '- http://www.phpmyadmin.net' . $crlf . '-' . $crlf . '- ' . $GLOBALS['strHost'] . ': ' . $cfg['Server']['host']; if (!empty($cfg['Server']['port'])) { $head .= ':' . $cfg['Server']['port']; } $head .= $crlf . '- ' . $GLOBALS['strGenTime'] . ': ' . PMA_localisedDate() . $crlf . '- ' . $GLOBALS['strServerVersion'] . ': ' . substr(PMA_MYSQL_INT_VERSION, 0, 1) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 1, 2) . '.' . (int) substr(PMA_MYSQL_INT_VERSION, 3) . $crlf . '- ' . $GLOBALS['strPHPVersion'] . ': ' . phpversion() . $crlf . '-->' . $crlf . $crlf; $head .= '<pma_xml_export version="1.0"' . ($export_struct ? ' xmlns:pma="http://www.phpmyadmin.net/some_doc_url/"' : '') . '>' . $crlf; if ($export_struct) { $result = PMA_DBI_fetch_result('SELECT `DEFAULT_CHARACTER_SET_NAME`, `DEFAULT_COLLATION_NAME` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = \'' . $db . '\' LIMIT 1'); $db_collation = $result[0]['DEFAULT_COLLATION_NAME']; $db_charset = $result[0]['DEFAULT_CHARACTER_SET_NAME']; $head .= ' <!--' . $crlf; $head .= ' - Structure schemas' . $crlf; $head .= ' -->' . $crlf; $head .= ' <pma:structure_schemas>' . $crlf; $head .= ' <pma:database name="' . $db . '" collation="' . $db_collation . '" charset="' . $db_charset . '">' . $crlf; if (count($tables) == 0) { $tables[] = $table; } foreach ($tables as $table) { // Export tables and views $result = PMA_DBI_fetch_result('SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table), 0); $tbl = $result[$table][1]; $is_view = PMA_isView($db, $table); if ($is_view) { $type = 'view'; } else { $type = 'table'; } if ($is_view && !isset($GLOBALS[$what . '_export_views'])) { continue; } if (!$is_view && !isset($GLOBALS[$what . '_export_tables'])) { continue; } $head .= ' <pma:' . $type . ' name="' . $table . '">' . $crlf; $tbl = " " . $tbl; $tbl = str_replace("\n", "\n ", $tbl); $head .= $tbl . ';' . $crlf; $head .= ' </pma:' . $type . '>' . $crlf; if (isset($GLOBALS[$what . '_export_triggers']) && $GLOBALS[$what . '_export_triggers']) { // Export triggers $triggers = PMA_DBI_get_triggers($db, $table); if ($triggers) { foreach ($triggers as $trigger) { $code = $trigger['create']; $head .= ' <pma:trigger name="' . $trigger['name'] . '">' . $crlf; // Do some formatting $code = substr(rtrim($code), 0, -3); $code = " " . $code; $code = str_replace("\n", "\n ", $code); $head .= $code . $crlf; $head .= ' </pma:trigger>' . $crlf; } unset($trigger); unset($triggers); } } } if (isset($GLOBALS[$what . '_export_functions']) && $GLOBALS[$what . '_export_functions']) { // Export functions $functions = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($functions) { foreach ($functions as $function) { $head .= ' <pma:function name="' . $function . '">' . $crlf; // Do some formatting $sql = PMA_DBI_get_definition($db, 'FUNCTION', $function); $sql = rtrim($sql); $sql = " " . $sql; $sql = str_replace("\n", "\n ", $sql); $head .= $sql . $crlf; $head .= ' </pma:function>' . $crlf; } unset($create_func); unset($function); unset($functions); } } if (isset($GLOBALS[$what . '_export_procedures']) && $GLOBALS[$what . '_export_procedures']) { // Export procedures $procedures = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); if ($procedures) { foreach ($procedures as $procedure) { $head .= ' <pma:procedure name="' . $procedure . '">' . $crlf; // Do some formatting $sql = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure); $sql = rtrim($sql); $sql = " " . $sql; $sql = str_replace("\n", "\n ", $sql); $head .= $sql . $crlf; $head .= ' </pma:procedure>' . $crlf; } unset($create_proc); unset($procedure); unset($procedures); } } unset($result); $head .= ' </pma:database>' . $crlf; $head .= ' </pma:structure_schemas>' . $crlf; if ($export_data) { $head .= $crlf; } } return PMA_exportOutputHandler($head); }
/** * Outputs database footer * * @param string Database name * * @return bool Whether it suceeded * * @access public */ function PMA_exportDBFooter($db) { global $crlf; $result = TRUE; if (isset($GLOBALS['sql_constraints'])) { $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']); unset($GLOBALS['sql_constraints']); } if (PMA_MYSQL_INT_VERSION >= 50000 && isset($GLOBALS['sql_structure']) && isset($GLOBALS['sql_procedure_function'])) { $procs_funcs = ''; $delimiter = '$$'; $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($procedure_names || $function_names) { $procs_funcs .= $crlf . 'DELIMITER ' . $delimiter . $crlf; } if ($procedure_names) { $procs_funcs .= PMA_exportComment() . PMA_exportComment($GLOBALS['strProcedures']) . PMA_exportComment(); foreach ($procedure_names as $procedure_name) { if (!empty($GLOBALS['sql_drop_table'])) { $procs_funcs .= 'DROP PROCEDURE ' . PMA_backquote($procedure_name) . $delimiter . $crlf; } $procs_funcs .= PMA_DBI_get_procedure_or_function_def($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf; } } if ($function_names) { $procs_funcs .= PMA_exportComment() . PMA_exportComment($GLOBALS['strFunctions']) . PMA_exportComment(); foreach ($function_names as $function_name) { if (!empty($GLOBALS['sql_drop_table'])) { $procs_funcs .= 'DROP FUNCTION ' . PMA_backquote($function_name) . $delimiter . $crlf; } $procs_funcs .= PMA_DBI_get_procedure_or_function_def($db, 'FUNCTION', $function_name) . $delimiter . $crlf . $crlf; } } if ($procedure_names || $function_names) { $procs_funcs .= 'DELIMITER ;' . $crlf; } if (!empty($procs_funcs)) { $result = PMA_exportOutputHandler($procs_funcs); } } return $result; }
/** * Outputs database footer * * @param string Database name * * @return bool Whether it suceeded * * @access public */ function PMA_exportDBFooter($db) { global $crlf; $result = TRUE; if (isset($GLOBALS['sql_constraints'])) { $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']); unset($GLOBALS['sql_constraints']); } if (isset($GLOBALS['sql_structure']) && isset($GLOBALS['sql_procedure_function'])) { $text = ''; $delimiter = '$$'; $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if (PMA_MYSQL_INT_VERSION > 50100) { $event_names = PMA_DBI_fetch_result('SELECT EVENT_NAME FROM information_schema.EVENTS WHERE EVENT_SCHEMA= \'' . PMA_sqlAddslashes($db, true) . '\';'); } else { $event_names = array(); } if ($procedure_names || $function_names || $event_names) { $text .= $crlf . 'DELIMITER ' . $delimiter . $crlf; } if ($procedure_names) { $text .= PMA_exportComment() . PMA_exportComment($GLOBALS['strProcedures']) . PMA_exportComment(); foreach ($procedure_names as $procedure_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP PROCEDURE IF EXISTS ' . PMA_backquote($procedure_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf; } } if ($function_names) { $text .= PMA_exportComment() . PMA_exportComment($GLOBALS['strFunctions']) . PMA_exportComment(); foreach ($function_names as $function_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP FUNCTION IF EXISTS ' . PMA_backquote($function_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'FUNCTION', $function_name) . $delimiter . $crlf . $crlf; } } if ($event_names) { $text .= PMA_exportComment() . PMA_exportComment($GLOBALS['strEvents']) . PMA_exportComment(); foreach ($event_names as $event_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP EVENT ' . PMA_backquote($event_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'EVENT', $event_name) . $delimiter . $crlf . $crlf; } } if ($procedure_names || $function_names || $event_names) { $text .= 'DELIMITER ;' . $crlf; } if (!empty($text)) { $result = PMA_exportOutputHandler($text); } } return $result; }
/** * Outputs database footer * * @param string Database name * * @return bool Whether it suceeded * * @access public */ function PMA_exportDBFooter($db) { global $crlf, $comment_marker; $result = TRUE; if (isset($GLOBALS['sql_constraints'])) { $result = PMA_exportOutputHandler($GLOBALS['sql_constraints']); unset($GLOBALS['sql_constraints']); } if (PMA_MYSQL_INT_VERSION >= 50000) { $procs_funcs = ''; $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); if ($procedure_names) { $delimiter = '$$'; $procs_funcs = $crlf . $comment_marker . $crlf . $comment_marker . $GLOBALS['strProcedures'] . $crlf . $comment_marker . $crlf . $comment_marker . 'DELIMITER ' . $delimiter . $crlf . $comment_marker . $crlf; foreach ($procedure_names as $procedure_name) { $procs_funcs .= PMA_DBI_get_procedure_or_function_def($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf; } $procs_funcs .= $comment_marker . $crlf . $comment_marker . 'DELIMITER ;' . $crlf . $comment_marker . $crlf; } $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($function_names) { $procs_funcs .= $comment_marker . $GLOBALS['strFunctions'] . $crlf . $comment_marker . $crlf . $crlf; foreach ($function_names as $function_name) { $procs_funcs .= PMA_DBI_get_procedure_or_function_def($db, 'FUNCTION', $function_name) . $crlf . $crlf; } } if (!empty($procs_funcs)) { $result = PMA_exportOutputHandler($procs_funcs); } } return $result; }
/** * Run the Procedure definitions and function definitions * * to avoid selecting alternatively the current and new db * we would need to modify the CREATE definitions to qualify * the db name * * @param string $db database name * * @return void */ function PMA_runProcedureAndFunctionDefinitions($db) { $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); if ($procedure_names) { foreach ($procedure_names as $procedure_name) { PMA_DBI_select_db($db); $tmp_query = PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name); // collect for later display $GLOBALS['sql_query'] .= "\n" . $tmp_query; PMA_DBI_select_db($_REQUEST['newname']); PMA_DBI_query($tmp_query); } } $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($function_names) { foreach ($function_names as $function_name) { PMA_DBI_select_db($db); $tmp_query = PMA_DBI_get_definition($db, 'FUNCTION', $function_name); // collect for later display $GLOBALS['sql_query'] .= "\n" . $tmp_query; PMA_DBI_select_db($_REQUEST['newname']); PMA_DBI_query($tmp_query); } } }
/** * Outputs CREATE DATABASE database * * @param string Database name * * @return bool Whether it suceeded * * @access public */ function PMA_exportDBCreate($db) { global $crlf; if (isset($GLOBALS['sql_drop_database'])) { if (!PMA_exportOutputHandler('DROP DATABASE ' . (isset($GLOBALS['sql_backquotes']) ? PMA_backquote($db) : $db) . ';' . $crlf)) { return FALSE; } } $create_query = 'CREATE DATABASE ' . (isset($GLOBALS['sql_backquotes']) ? PMA_backquote($db) : $db); $collation = PMA_getDbCollation($db); if (strpos($collation, '_')) { $create_query .= ' DEFAULT CHARACTER SET ' . substr($collation, 0, strpos($collation, '_')) . ' COLLATE ' . $collation; } else { $create_query .= ' DEFAULT CHARACTER SET ' . $collation; } $create_query .= ';' . $crlf; if (!PMA_exportOutputHandler($create_query)) { return FALSE; } if (isset($GLOBALS['sql_backquotes']) && isset($GLOBALS['sql_compatibility']) && $GLOBALS['sql_compatibility'] == 'NONE') { $result = PMA_exportOutputHandler('USE ' . PMA_backquote($db) . ';' . $crlf); } else { $result = PMA_exportOutputHandler('USE ' . $db . ';' . $crlf); } if ($result && isset($GLOBALS['sql_structure']) && isset($GLOBALS['sql_procedure_function'])) { $text = ''; $delimiter = '$$'; $procedure_names = PMA_DBI_get_procedures_or_functions($db, 'PROCEDURE'); $function_names = PMA_DBI_get_procedures_or_functions($db, 'FUNCTION'); if ($procedure_names || $function_names) { $text .= $crlf . 'DELIMITER ' . $delimiter . $crlf; } if ($procedure_names) { $text .= PMA_exportComment() . PMA_exportComment($GLOBALS['strProcedures']) . PMA_exportComment(); foreach ($procedure_names as $procedure_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP PROCEDURE IF EXISTS ' . PMA_backquote($procedure_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'PROCEDURE', $procedure_name) . $delimiter . $crlf . $crlf; } } if ($function_names) { $text .= PMA_exportComment() . PMA_exportComment($GLOBALS['strFunctions']) . PMA_exportComment(); foreach ($function_names as $function_name) { if (!empty($GLOBALS['sql_drop_table'])) { $text .= 'DROP FUNCTION IF EXISTS ' . PMA_backquote($function_name) . $delimiter . $crlf; } $text .= PMA_DBI_get_definition($db, 'FUNCTION', $function_name) . $delimiter . $crlf . $crlf; } } if ($procedure_names || $function_names) { $text .= 'DELIMITER ;' . $crlf; } if (!empty($text)) { $result = PMA_exportOutputHandler($text); } } return $result; }