Merge branch 'QA_4_3'
[phpmyadmin.git] / db_operations.php
bloba6ec51090a970cd589bf21c0919202649dbc1356
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 $sql_query = '';
34 /**
35 * Rename/move or copy database
37 /** @var PMA_String $pmaString */
38 $pmaString = $GLOBALS['PMA_String'];
39 if (/*overload*/mb_strlen($GLOBALS['db'])
40 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
41 ) {
42 if (! empty($_REQUEST['db_rename'])) {
43 $move = true;
44 } else {
45 $move = false;
48 if (! isset($_REQUEST['newname'])
49 || ! /*overload*/mb_strlen($_REQUEST['newname'])
50 ) {
51 $message = PMA_Message::error(__('The database name is empty!'));
52 } else {
53 $_error = false;
54 if ($move
55 || (isset($_REQUEST['create_database_before_copying'])
56 && $_REQUEST['create_database_before_copying'])
57 ) {
58 $sql_query = PMA_getSqlQueryAndCreateDbBeforeCopy();
61 // here I don't use DELIMITER because it's not part of the
62 // language; I have to send each statement one by one
64 // to avoid selecting alternatively the current and new db
65 // we would need to modify the CREATE definitions to qualify
66 // the db name
67 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
69 // go back to current db, just in case
70 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
72 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
74 include_once "libraries/plugin_interface.lib.php";
75 // remove all foreign key constraints, otherwise we can get errors
76 $export_sql_plugin = PMA_getPlugin(
77 "export",
78 "sql",
79 'libraries/plugins/export/',
80 array(
81 'single_table' => isset($single_table),
82 'export_type' => 'database'
85 $GLOBALS['sql_constraints_query_full_db']
86 = PMA_getSqlConstraintsQueryForFullDb(
87 $tables_full, $export_sql_plugin, $move, $GLOBALS['db']
90 $views = PMA_getViewsAndCreateSqlViewStandIn(
91 $tables_full, $export_sql_plugin, $GLOBALS['db']
94 list($sql_query, $_error) = PMA_getSqlQueryForCopyTable(
95 $tables_full, $sql_query, $move, $GLOBALS['db']
98 // handle the views
99 if (! $_error) {
100 $_error = PMA_handleTheViews($views, $move, $GLOBALS['db']);
102 unset($views);
104 // now that all tables exist, create all the accumulated constraints
105 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
106 PMA_createAllAccumulatedConstraints();
109 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
110 // here DELIMITER is not used because it's not part of the
111 // language; each statement is sent one by one
113 PMA_runEventDefinitionsForDb($GLOBALS['db']);
116 // go back to current db, just in case
117 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
119 // Duplicate the bookmarks for this db (done once for each db)
120 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
122 if (! $_error && $move) {
124 * cleanup pmadb stuff for this db
126 include_once 'libraries/relation_cleanup.lib.php';
127 PMA_relationsCleanupDatabase($GLOBALS['db']);
129 // if someday the RENAME DATABASE reappears, do not DROP
130 $local_query = 'DROP DATABASE ' . PMA_Util::backquote($GLOBALS['db'])
131 . ';';
132 $sql_query .= "\n" . $local_query;
133 $GLOBALS['dbi']->query($local_query);
135 $message = PMA_Message::success(
136 __('Database %1$s has been renamed to %2$s.')
138 $message->addParam($GLOBALS['db']);
139 $message->addParam($_REQUEST['newname']);
140 } elseif (! $_error) {
141 $message = PMA_Message::success(
142 __('Database %1$s has been copied to %2$s.')
144 $message->addParam($GLOBALS['db']);
145 $message->addParam($_REQUEST['newname']);
146 } else {
147 $message = PMA_Message::error();
149 $reload = true;
151 /* Change database to be used */
152 if (! $_error && $move) {
153 $GLOBALS['db'] = $_REQUEST['newname'];
154 } elseif (! $_error) {
155 if (isset($_REQUEST['switch_to_new'])
156 && $_REQUEST['switch_to_new'] == 'true'
158 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
159 $GLOBALS['db'] = $_REQUEST['newname'];
160 } else {
161 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
167 * Database has been successfully renamed/moved. If in an Ajax request,
168 * generate the output with {@link PMA_Response} and exit
170 if ($GLOBALS['is_ajax_request'] == true) {
171 $response = PMA_Response::getInstance();
172 $response->isSuccess($message->isSuccess());
173 $response->addJSON('message', $message);
174 $response->addJSON('newname', $_REQUEST['newname']);
175 $response->addJSON(
176 'sql_query',
177 PMA_Util::getMessage(null, $sql_query)
179 $response->addJSON('db', $GLOBALS['db']);
180 exit;
185 * Settings for relations stuff
188 $cfgRelation = PMA_getRelationsParam();
191 * Check if comments were updated
192 * (must be done before displaying the menu tabs)
194 if (isset($_REQUEST['comment'])) {
195 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
198 require 'libraries/db_common.inc.php';
199 $url_query .= '&amp;goto=db_operations.php';
201 // Gets the database structure
202 $sub_part = '_structure';
203 require 'libraries/db_info.inc.php';
204 echo "\n";
206 if (isset($message)) {
207 echo PMA_Util::getMessage($message, $sql_query);
208 unset($message);
211 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
212 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
214 $response->addHTML('<div id="boxContainer" data-box-width="300">');
216 if (!$is_information_schema) {
217 if ($cfgRelation['commwork']) {
219 * database comment
221 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
224 $response->addHTML('<div class="operations_half_width">');
225 ob_start();
226 include 'libraries/display_create_table.lib.php';
227 $content = ob_get_contents();
228 ob_end_clean();
229 $response->addHTML($content);
230 $response->addHTML('</div>');
233 * rename database
235 if ($GLOBALS['db'] != 'mysql') {
236 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
239 // Drop link if allowed
240 // Don't even try to drop information_schema.
241 // You won't be able to. Believe me. You won't.
242 // Don't allow to easily drop mysql database, RFE #1327514.
243 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
244 && ! $db_is_system_schema
245 && (PMA_DRIZZLE || $GLOBALS['db'] != 'mysql')
247 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
250 * Copy database
252 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
255 * Change database charset
257 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
259 if (! $cfgRelation['allworks']
260 && $cfg['PmaNoRelation_DisableWarning'] == false
262 $message = PMA_Message::notice(
263 __('The phpMyAdmin configuration storage has been deactivated. %sFind out why%s.')
265 $message->addParam(
266 '<a href="' . $cfg['PmaAbsoluteUri']
267 . 'chk_rel.php' . $url_query . '">',
268 false
270 $message->addParam('</a>', false);
271 /* Show error if user has configured something, notice elsewhere */
272 if (!empty($cfg['Servers'][$server]['pmadb'])) {
273 $message->isError(true);
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($GLOBALS['db']) . '\'';
288 $test_rs = PMA_queryAsControlUser(
289 $test_query,
290 false,
291 PMA_DatabaseInterface::QUERY_STORE
293 } // end if