From 5031e4cad8099706893c23dfe9f159357856db20 Mon Sep 17 00:00:00 2001 From: Marc Delisle Date: Sun, 29 Dec 2013 06:31:44 -0500 Subject: [PATCH] Refactor: new PMA_exportServer() Signed-off-by: Marc Delisle --- export.php | 96 +++------------------------------ libraries/export.lib.php | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 90 deletions(-) diff --git a/export.php b/export.php index 44c49892e5..a53dd7d2c9 100644 --- a/export.php +++ b/export.php @@ -372,97 +372,13 @@ if (!defined('TESTSUITE')) { */ // Gets the number of tables if a dump of a database has been required if ($export_type == 'server') { - if (isset($db_select)) { - $tmp_select = implode($db_select, '|'); - $tmp_select = '|' . $tmp_select . '|'; - } - // Walk over databases - foreach ($GLOBALS['pma']->databases as $current_db) { - if (isset($tmp_select) - && strpos(' ' . $tmp_select, '|' . $current_db . '|') - ) { - if (! $export_plugin->exportDBHeader($current_db)) { - break 2; - } - if (! $export_plugin->exportDBCreate($current_db)) { - break 2; - } - if (method_exists($export_plugin, 'exportRoutines') - && strpos($whatStrucOrData, 'structure') !== false - && isset($GLOBALS['sql_procedure_function']) - ) { - $export_plugin->exportRoutines($current_db); - } - - $tables = $GLOBALS['dbi']->getTables($current_db); - $views = array(); - foreach ($tables as $table) { - // if this is a view, collect it for later; - // views must be exported after the tables - $is_view = PMA_Table::isView($current_db, $table); - if ($is_view) { - $views[] = $table; - } - if ($whatStrucOrData == 'structure' - || $whatStrucOrData == 'structure_and_data' - ) { - // for a view, export a stand-in definition of the table - // to resolve view dependencies - if (! $export_plugin->exportStructure( - $current_db, $table, $crlf, $err_url, - $is_view ? 'stand_in' : 'create_table', $export_type, - $do_relation, $do_comments, $do_mime, $do_dates - )) { - break 3; - } - } - // if this is a view or a merge table, don't export data - if (($whatStrucOrData == 'data' - || $whatStrucOrData == 'structure_and_data') - && ! ($is_view || PMA_Table::isMerge($current_db, $table)) - ) { - $local_query = 'SELECT * FROM ' - . PMA_Util::backquote($current_db) - . '.' . PMA_Util::backquote($table); - if (! $export_plugin->exportData( - $current_db, $table, $crlf, $err_url, $local_query - )) { - break 3; - } - } - // now export the triggers (needs to be done after the data - // because triggers can modify already imported tables) - if ($whatStrucOrData == 'structure' - || $whatStrucOrData == 'structure_and_data' - ) { - if (! $export_plugin->exportStructure( - $current_db, $table, $crlf, $err_url, - 'triggers', $export_type, - $do_relation, $do_comments, $do_mime, $do_dates - )) { - break 2; - } - } - } - foreach ($views as $view) { - // no data export for a view - if ($whatStrucOrData == 'structure' - || $whatStrucOrData == 'structure_and_data' - ) { - if (! $export_plugin->exportStructure( - $current_db, $view, $crlf, $err_url, - 'create_view', $export_type, - $do_relation, $do_comments, $do_mime, $do_dates - )) { - break 3; - } - } - } - if (! $export_plugin->exportDBFooter($current_db)) { - break 2; - } - } + if (! isset($db_select)) { + $db_select = ''; } + PMA_exportServer( + $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url, + $export_type, $do_relation, $do_comments, $do_mime, $do_dates + ); } elseif ($export_type == 'database') { if (! $export_plugin->exportDBHeader($db)) { break; diff --git a/libraries/export.lib.php b/libraries/export.lib.php index 3332ad41bb..7dff107e89 100644 --- a/libraries/export.lib.php +++ b/libraries/export.lib.php @@ -445,4 +445,117 @@ function PMA_getHtmlForDisplayedExportHeader($export_type, $db, $table) return array($html, $back_button); } + +/** + * Export at the server level + * + * @param string $db_select the selected databases to export + * @param string $whatStrucOrData structure or data or both + * @param object $export_plugin the selected export plugin + * @param string $crlf end of line character(s) + * @param string $err_url the URL in case of error + * @param string $export_type the export type + * @param string $do_relation whether to export relation info + * @param string $do_comments whether to add comments + * @param string $do_mime whether to add MIME info + * @param string $do_dates whether to add dates + * + * @return void + */ +function PMA_exportServer( + $db_select, $whatStrucOrData, $export_plugin, $crlf, $err_url, + $export_type, $do_relation, $do_comments, $do_mime, $do_dates +) { + if (! empty($db_select)) { + $tmp_select = implode($db_select, '|'); + $tmp_select = '|' . $tmp_select . '|'; + } + // Walk over databases + foreach ($GLOBALS['pma']->databases as $current_db) { + if (isset($tmp_select) + && strpos(' ' . $tmp_select, '|' . $current_db . '|') + ) { + if (! $export_plugin->exportDBHeader($current_db)) { + break 2; + } + if (! $export_plugin->exportDBCreate($current_db)) { + break 2; + } + if (method_exists($export_plugin, 'exportRoutines') + && strpos($whatStrucOrData, 'structure') !== false + && isset($GLOBALS['sql_procedure_function']) + ) { + $export_plugin->exportRoutines($current_db); + } + + $tables = $GLOBALS['dbi']->getTables($current_db); + $views = array(); + foreach ($tables as $table) { + // if this is a view, collect it for later; + // views must be exported after the tables + $is_view = PMA_Table::isView($current_db, $table); + if ($is_view) { + $views[] = $table; + } + if ($whatStrucOrData == 'structure' + || $whatStrucOrData == 'structure_and_data' + ) { + // for a view, export a stand-in definition of the table + // to resolve view dependencies + if (! $export_plugin->exportStructure( + $current_db, $table, $crlf, $err_url, + $is_view ? 'stand_in' : 'create_table', $export_type, + $do_relation, $do_comments, $do_mime, $do_dates + )) { + break 3; + } + } + // if this is a view or a merge table, don't export data + if (($whatStrucOrData == 'data' + || $whatStrucOrData == 'structure_and_data') + && ! ($is_view || PMA_Table::isMerge($current_db, $table)) + ) { + $local_query = 'SELECT * FROM ' + . PMA_Util::backquote($current_db) + . '.' . PMA_Util::backquote($table); + if (! $export_plugin->exportData( + $current_db, $table, $crlf, $err_url, $local_query + )) { + break 3; + } + } + // now export the triggers (needs to be done after the data + // because triggers can modify already imported tables) + if ($whatStrucOrData == 'structure' + || $whatStrucOrData == 'structure_and_data' + ) { + if (! $export_plugin->exportStructure( + $current_db, $table, $crlf, $err_url, + 'triggers', $export_type, + $do_relation, $do_comments, $do_mime, $do_dates + )) { + break 2; + } + } + } + foreach ($views as $view) { + // no data export for a view + if ($whatStrucOrData == 'structure' + || $whatStrucOrData == 'structure_and_data' + ) { + if (! $export_plugin->exportStructure( + $current_db, $view, $crlf, $err_url, + 'create_view', $export_type, + $do_relation, $do_comments, $do_mime, $do_dates + )) { + break 3; + } + } + } + if (! $export_plugin->exportDBFooter($current_db)) { + break 2; + } + } + } // end foreach database +} ?>