Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / db_operations.php
blob6561764ca30417d0b254c84d7fa40f65cab9bccc
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * handles miscellaneous db operations:
5 * - move/rename
6 * - copy
7 * - changing collation
8 * - changing comment
9 * - adding tables
10 * - viewing PDF schemas
12 * @package PhpMyAdmin
15 /**
16 * requirements
18 require_once 'libraries/common.inc.php';
19 require_once 'libraries/mysql_charsets.inc.php';
21 /**
22 * functions implementation for this script
24 require_once 'libraries/operations.lib.php';
26 // add a javascript file for jQuery functions to handle Ajax actions
27 $response = PMA_Response::getInstance();
28 $header = $response->getHeader();
29 $scripts = $header->getScripts();
30 $scripts->addFile('db_operations.js');
32 /**
33 * Rename/move or copy database
35 if (strlen($db)
36 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
37 ) {
38 if (! empty($_REQUEST['db_rename'])) {
39 $move = true;
40 } else {
41 $move = false;
44 if (! isset($_REQUEST['newname']) || ! strlen($_REQUEST['newname'])) {
45 $message = PMA_Message::error(__('The database name is empty!'));
46 } else {
47 $sql_query = ''; // in case target db exists
48 $_error = false;
49 if ($move
50 || (isset($_REQUEST['create_database_before_copying'])
51 && $_REQUEST['create_database_before_copying'])
52 ) {
53 $sql_query = PMA_getSqlQueryAndCreateDbBeforeCopy();
56 // here I don't use DELIMITER because it's not part of the
57 // language; I have to send each statement one by one
59 // to avoid selecting alternatively the current and new db
60 // we would need to modify the CREATE definitions to qualify
61 // the db name
62 PMA_runProcedureAndFunctionDefinitions($db);
64 // go back to current db, just in case
65 $GLOBALS['dbi']->selectDb($db);
67 $tables_full = $GLOBALS['dbi']->getTablesFull($db);
69 include_once "libraries/plugin_interface.lib.php";
70 // remove all foreign key constraints, otherwise we can get errors
71 $export_sql_plugin = PMA_getPlugin(
72 "export",
73 "sql",
74 'libraries/plugins/export/',
75 array(
76 'single_table' => isset($single_table),
77 'export_type' => 'database'
80 $GLOBALS['sql_constraints_query_full_db']
81 = PMA_getSqlConstraintsQueryForFullDb(
82 $tables_full, $export_sql_plugin, $move, $db
85 $views = PMA_getViewsAndCreateSqlViewStandIn(
86 $tables_full, $export_sql_plugin, $db
89 list($sql_query, $_error) = PMA_getSqlQueryForCopyTable(
90 $tables_full, $sql_query, $move, $db
93 // handle the views
94 if (! $_error) {
95 $_error = PMA_handleTheViews($views, $move, $db);
97 unset($views);
99 // now that all tables exist, create all the accumulated constraints
100 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
101 PMA_createAllAccumulatedConstraints();
104 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
105 // here DELIMITER is not used because it's not part of the
106 // language; each statement is sent one by one
108 PMA_runEventDefinitionsForDb($db);
111 // go back to current db, just in case
112 $GLOBALS['dbi']->selectDb($db);
114 // Duplicate the bookmarks for this db (done once for each db)
115 PMA_duplicateBookmarks($_error, $db);
117 if (! $_error && $move) {
119 * cleanup pmadb stuff for this db
121 include_once 'libraries/relation_cleanup.lib.php';
122 PMA_relationsCleanupDatabase($db);
124 // if someday the RENAME DATABASE reappears, do not DROP
125 $local_query = 'DROP DATABASE ' . PMA_Util::backquote($db) . ';';
126 $sql_query .= "\n" . $local_query;
127 $GLOBALS['dbi']->query($local_query);
129 $message = PMA_Message::success(__('Database %1$s has been renamed to %2$s'));
130 $message->addParam($db);
131 $message->addParam($_REQUEST['newname']);
132 } elseif (! $_error) {
133 $message = PMA_Message::success(__('Database %1$s has been copied to %2$s'));
134 $message->addParam($db);
135 $message->addParam($_REQUEST['newname']);
137 $reload = true;
139 /* Change database to be used */
140 if (! $_error && $move) {
141 $db = $_REQUEST['newname'];
142 } elseif (! $_error) {
143 if (isset($_REQUEST['switch_to_new'])
144 && $_REQUEST['switch_to_new'] == 'true'
146 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
147 $db = $_REQUEST['newname'];
148 } else {
149 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
153 if ($_error && ! isset($message)) {
154 $message = PMA_Message::error();
159 * Database has been successfully renamed/moved. If in an Ajax request,
160 * generate the output with {@link PMA_Response} and exit
162 if ($GLOBALS['is_ajax_request'] == true) {
163 $response = PMA_Response::getInstance();
164 $response->isSuccess($message->isSuccess());
165 $response->addJSON('message', $message);
166 $response->addJSON('newname', $_REQUEST['newname']);
167 $response->addJSON(
168 'sql_query',
169 PMA_Util::getMessage(null, $sql_query)
171 exit;
176 * Settings for relations stuff
179 $cfgRelation = PMA_getRelationsParam();
182 * Check if comments were updated
183 * (must be done before displaying the menu tabs)
185 if (isset($_REQUEST['comment'])) {
186 PMA_setDbComment($db, $_REQUEST['comment']);
190 * Prepares the tables list if the user where not redirected to this script
191 * because there is no table in the database ($is_info is true)
193 if (empty($is_info)) {
194 include 'libraries/db_common.inc.php';
195 $url_query .= '&amp;goto=db_operations.php';
197 // Gets the database structure
198 $sub_part = '_structure';
199 include 'libraries/db_info.inc.php';
200 echo "\n";
202 if (isset($message)) {
203 echo PMA_Util::getMessage($message, $sql_query);
204 unset($message);
208 $_REQUEST['db_collation'] = PMA_getDbCollation($db);
209 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($db);
211 $response->addHTML('<div id="boxContainer" data-box-width="300">');
213 if (!$is_information_schema) {
214 if ($cfgRelation['commwork']) {
216 * database comment
218 $response->addHTML(PMA_getHtmlForDatabaseComment($db));
221 $response->addHTML('<div class="operations_half_width">');
222 ob_start();
223 include 'libraries/display_create_table.lib.php';
224 $content = ob_get_contents();
225 ob_end_clean();
226 $response->addHTML($content);
227 $response->addHTML('</div>');
230 * rename database
232 if ($db != 'mysql') {
233 $response->addHTML(PMA_getHtmlForRenameDatabase($db));
236 // Drop link if allowed
237 // Don't even try to drop information_schema.
238 // You won't be able to. Believe me. You won't.
239 // Don't allow to easily drop mysql database, RFE #1327514.
240 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
241 && ! $db_is_information_schema
242 && (PMA_DRIZZLE || $db != 'mysql')
244 $response->addHTML(PMA_getHtmlForDropDatabaseLink($db));
247 * Copy database
249 $response->addHTML(PMA_getHtmlForCopyDatabase($db));
252 * Change database charset
254 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($db, $table));
256 if ($num_tables > 0
257 && ! $cfgRelation['allworks']
258 && $cfg['PmaNoRelation_DisableWarning'] == false
260 $message = PMA_Message::notice(
261 __('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.')
263 $message->addParam(
264 '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">',
265 false
267 $message->addParam('</a>', false);
268 /* Show error if user has configured something, notice elsewhere */
269 if (!empty($cfg['Servers'][$server]['pmadb'])) {
270 $message->isError(true);
272 $response->addHTML('<div class="operations_full_width">');
273 $response->addHTML($message->getDisplay());
274 $response->addHTML('</div>');
275 } // end if
276 } // end if (!$is_information_schema)
278 $response->addHTML('</div>');
280 // not sure about displaying the PDF dialog in case db is information_schema
281 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
282 // We only show this if we find something in the new pdf_pages table
283 $test_query = '
284 SELECT *
285 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
286 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
287 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
288 $test_rs = PMA_queryAsControlUser(
289 $test_query,
290 null,
291 PMA_DatabaseInterface::QUERY_STORE
295 * Export Relational Schema View
297 $response->addHTML(PMA_getHtmlForExportRelationalSchemaView($url_query));
298 } // end if