From ef981aa33573ec2ea32fdfa8af735efd483e0446 Mon Sep 17 00:00:00 2001 From: Madhura Jayaratne Date: Fri, 10 Apr 2015 16:00:57 +0530 Subject: [PATCH] rfe #1632 Importing and exporting pMA meta-data Signed-off-by: Madhura Jayaratne --- ChangeLog | 1 + export.php | 1 + libraries/export.lib.php | 56 ++++++++++++ libraries/plugins/ExportPlugin.class.php | 17 ++++ libraries/plugins/export/ExportSql.class.php | 95 ++++++++++++++++++++ 5 files changed, 170 insertions(+) diff --git a/ChangeLog b/ChangeLog index 7064c34015..f17c6ec7fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -22,6 +22,7 @@ phpMyAdmin - ChangeLog + rfe #1651 Allow copying mutiple rows + rfe #1643 No SQL query for loading data + rfe #1653 New data validation feature and datetime type ++ rfe #1632 Importing and exporting pMA meta-data 4.4.2.0 (not yet released) - bug #4835 PMA_hideShowConnection not called after submit_num_fields diff --git a/export.php b/export.php index 3d4a625daf..abbfd782b8 100644 --- a/export.php +++ b/export.php @@ -128,6 +128,7 @@ if (!defined('TESTSUITE')) { 'sql_utc_time', 'sql_drop_database', 'sql_views_as_tables', + 'sql_metadata', 'csv_separator', 'csv_enclosed', 'csv_escaped', diff --git a/libraries/export.lib.php b/libraries/export.lib.php index fecb3d16fe..c1bba9d872 100644 --- a/libraries/export.lib.php +++ b/libraries/export.lib.php @@ -550,6 +550,12 @@ function PMA_exportDatabase( $export_plugin->exportRoutines($db, $aliases); } + if (isset($GLOBALS['sql_metadata'])) { + // Types of metatada to export. + // In the future these can be allowed to be selected by the user + $metadataTypes = PMA_getMetadataTypesToExport(); + } + $views = array(); foreach ($tables as $table) { @@ -634,6 +640,15 @@ function PMA_exportDatabase( break 1; } } + + // now export metadata related to this table + if (isset($GLOBALS['sql_metadata'])) { + if (! $export_plugin->exportMetadata( + $db, $table, $metadataTypes + )) { + return; + } + } } if (isset($GLOBALS['sql_create_view'])) { @@ -658,6 +673,15 @@ function PMA_exportDatabase( if (! $export_plugin->exportDBFooter($db, $db_alias)) { return; } + + // export metadata related to this db + if (isset($GLOBALS['sql_metadata'])) { + if (! $export_plugin->exportMetadata( + $db, null, $metadataTypes + )) { + return; + } + } } /** @@ -775,6 +799,18 @@ function PMA_exportTable( if (! $export_plugin->exportDBFooter($db, $db_alias)) { return; } + + if (isset($GLOBALS['sql_metadata'])) { + // Types of metatada to export. + // In the future these can be allowed to be selected by the user + $metadataTypes = PMA_getMetadataTypesToExport(); + + if (! $export_plugin->exportMetadata( + $db, $table, $metadataTypes + )) { + return; + } + } } /** @@ -886,4 +922,24 @@ function PMA_unlockTables() { return $GLOBALS['dbi']->tryQuery("UNLOCK TABLES"); } + +/** + * Returns the all the metadata types the can be exported with a database or a table + * + * @return array metadata types. + */ +function PMA_getMetadataTypesToExport() +{ + return array( + 'column_info', + 'table_uiprefs', + 'tracking', + 'bookmark', + 'relation', + 'table_coords', + 'pdf_pages', + 'savedsearches', + 'central_columns', + ); +} ?> diff --git a/libraries/plugins/ExportPlugin.class.php b/libraries/plugins/ExportPlugin.class.php index 70f875048a..68e53febef 100644 --- a/libraries/plugins/ExportPlugin.class.php +++ b/libraries/plugins/ExportPlugin.class.php @@ -147,6 +147,23 @@ abstract class ExportPlugin ; } + /** + * Exports metadata from Configuration Storage + * + * @param string $db database being exported + * @param string $table table being exported + * @param array $metadataTypes types of metadata to export + * @param array $targetNames associative array of db and table names of + * target configuraton storage + * + * @return bool Whether it succeeded + */ + public function exportMetadata( + $db, $table, $metadataTypes, $targetNames = array() + ) { + ; + } + /** * Returns a stand-in CREATE definition to resolve view dependencies * diff --git a/libraries/plugins/export/ExportSql.class.php b/libraries/plugins/export/ExportSql.class.php index 91227abcd8..074ba9c26e 100644 --- a/libraries/plugins/export/ExportSql.class.php +++ b/libraries/plugins/export/ExportSql.class.php @@ -155,6 +155,12 @@ class ExportSql extends ExportPlugin $leaf->setText(__('Export views as tables')); $generalOptions->addProperty($leaf); + // export metadata + $leaf = new BoolPropertyItem(); + $leaf->setName("metadata"); + $leaf->setText(__('Export metadata')); + $generalOptions->addProperty($leaf); + // compatibility maximization $compats = $GLOBALS['dbi']->getCompatibilities(); if (count($compats) > 0) { @@ -928,6 +934,95 @@ class ExportSql extends ExportPlugin return $result; } + /** + * Exports metadata from Configuration Storage + * + * @param string $db database being exported + * @param string $table table being exported + * @param array $metadataTypes types of metadata to export + * @param array $targetNames associative array of db and table names of + * target configuraton storage + * + * @return bool Whether it succeeded + */ + public function exportMetadata( + $db, $table, $metadataTypes, $targetNames = array() + ) { + $cfgRelation = PMA_getRelationsParam(); + if (! isset($cfgRelation['db'])) { + return true; + } + + if (isset($table)) { + $types = array( + 'column_info' => 'db_name', + 'table_uiprefs' => 'db_name', + 'tracking' => 'db_name', + ); + } else { + $types = array( + 'bookmark' => 'dbase', + 'relation' => 'master_db', + 'table_coords' => 'db_name', + 'pdf_pages' => 'db_name', + 'savedsearches' => 'db_name', + 'central_columns' => 'db_name', + ); + } + + $aliases = array(); + foreach ($targetNames as $type => $targetName) { + if ($type == 'phpmyadmin') { + $aliases[$cfgRelation['db']] = $targetName; + } else { + if (isset($cfgRelation[$type])) { + $aliases[$cfgRelation['db']]['tables'][$cfgRelation[$type]]['alias'] + = $targetName; + } + } + } + + $comment = $this->_possibleCRLF() + . $this->_exportComment() + . $this->_exportComment( + sprintf( + __('Metadata for %s'), + isset($table) ? $table : $db + ) + ) + . $this->_exportComment(); + if (! PMA_exportOutputHandler($comment)) { + return false; + } + + foreach ($types as $type => $dbNameColumn) { + if (in_array($type, $metadataTypes) && isset($cfgRelation[$type])) { + + $sql_query = "SELECT * FROM " + . PMA_Util::backquote($cfgRelation['db']) + . '.' . PMA_Util::backquote($cfgRelation[$type]) + . " WHERE " . PMA_Util::backquote($dbNameColumn) + . " = '" . PMA_Util::sqlAddSlashes($db) . "'"; + if (isset($table)) { + $sql_query .= " AND `table_name` = '" + . PMA_Util::sqlAddSlashes($table) . "'"; + } + + if (! $this->exportData( + $cfgRelation['db'], + $cfgRelation[$type], + $GLOBALS['crlf'], + '', + $sql_query, + $aliases + )) { + return false; + } + } + } + return true; + } + /** * Returns a stand-in CREATE definition to resolve view dependencies *