2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
14 use PMA\libraries\Response
;
15 use PMA\libraries\plugins\export\ExportSql
;
20 require_once 'libraries/common.inc.php';
21 require_once 'libraries/display_create_table.lib.php';
24 * functions implementation for this script
26 require_once 'libraries/check_user_privileges.lib.php';
27 require_once 'libraries/operations.lib.php';
29 // add a javascript file for jQuery functions to handle Ajax actions
30 $response = Response
::getInstance();
31 $header = $response->getHeader();
32 $scripts = $header->getScripts();
33 $scripts->addFile('db_operations.js');
38 * Rename/move or copy database
40 if (strlen($GLOBALS['db']) > 0
41 && (! empty($_REQUEST['db_rename']) ||
! empty($_REQUEST['db_copy']))
43 if (! empty($_REQUEST['db_rename'])) {
49 if (! isset($_REQUEST['newname']) ||
strlen($_REQUEST['newname']) === 0) {
50 $message = PMA\libraries\Message
::error(__('The database name is empty!'));
52 // lower_case_table_names=1 `DB` becomes `db`
53 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
54 $_REQUEST['newname'] = mb_strtolower(
59 if ($_REQUEST['newname'] === $_REQUEST['db']) {
60 $message = PMA\libraries\Message
::error(
61 __('Cannot copy database to the same name. Change the name and try again.')
65 if ($move ||
! empty($_REQUEST['create_database_before_copying'])) {
66 PMA_createDbBeforeCopy();
69 // here I don't use DELIMITER because it's not part of the
70 // language; I have to send each statement one by one
72 // to avoid selecting alternatively the current and new db
73 // we would need to modify the CREATE definitions to qualify
75 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
77 // go back to current db, just in case
78 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
80 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
82 include_once "libraries/plugin_interface.lib.php";
83 // remove all foreign key constraints, otherwise we can get errors
84 /* @var $export_sql_plugin ExportSql */
85 $export_sql_plugin = PMA_getPlugin(
88 'libraries/plugins/export/',
90 'single_table' => isset($single_table),
91 'export_type' => 'database'
95 // create stand-in tables for views
96 $views = PMA_getViewsAndCreateSqlViewStandIn(
97 $tables_full, $export_sql_plugin, $GLOBALS['db']
101 $sqlConstratints = PMA_copyTables(
102 $tables_full, $move, $GLOBALS['db']
107 PMA_handleTheViews($views, $move, $GLOBALS['db']);
111 // now that all tables exist, create all the accumulated constraints
112 if (! $_error && count($sqlConstratints) > 0) {
113 PMA_createAllAccumulatedConstraints($sqlConstratints);
115 unset($sqlConstratints);
117 if (PMA_MYSQL_INT_VERSION
>= 50100) {
118 // here DELIMITER is not used because it's not part of the
119 // language; each statement is sent one by one
121 PMA_runEventDefinitionsForDb($GLOBALS['db']);
124 // go back to current db, just in case
125 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
127 // Duplicate the bookmarks for this db (done once for each db)
128 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
130 if (! $_error && $move) {
131 if (isset($_REQUEST['adjust_privileges'])
132 && ! empty($_REQUEST['adjust_privileges'])
134 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
138 * cleanup pmadb stuff for this db
140 include_once 'libraries/relation_cleanup.lib.php';
141 PMA_relationsCleanupDatabase($GLOBALS['db']);
143 // if someday the RENAME DATABASE reappears, do not DROP
144 $local_query = 'DROP DATABASE '
145 . PMA\libraries\Util
::backquote($GLOBALS['db']) . ';';
146 $sql_query .= "\n" . $local_query;
147 $GLOBALS['dbi']->query($local_query);
149 $message = PMA\libraries\Message
::success(
150 __('Database %1$s has been renamed to %2$s.')
152 $message->addParam($GLOBALS['db']);
153 $message->addParam($_REQUEST['newname']);
154 } elseif (! $_error) {
155 if (isset($_REQUEST['adjust_privileges'])
156 && ! empty($_REQUEST['adjust_privileges'])
158 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
161 $message = PMA\libraries\Message
::success(
162 __('Database %1$s has been copied to %2$s.')
164 $message->addParam($GLOBALS['db']);
165 $message->addParam($_REQUEST['newname']);
167 $message = PMA\libraries\Message
::error();
171 /* Change database to be used */
172 if (! $_error && $move) {
173 $GLOBALS['db'] = $_REQUEST['newname'];
174 } elseif (! $_error) {
175 if (isset($_REQUEST['switch_to_new'])
176 && $_REQUEST['switch_to_new'] == 'true'
178 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
179 $GLOBALS['db'] = $_REQUEST['newname'];
181 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
188 * Database has been successfully renamed/moved. If in an Ajax request,
189 * generate the output with {@link PMA\libraries\Response} and exit
191 if ($response->isAjax()) {
192 $response->setRequestStatus($message->isSuccess());
193 $response->addJSON('message', $message);
194 $response->addJSON('newname', $_REQUEST['newname']);
197 PMA\libraries\Util
::getMessage(null, $sql_query)
199 $response->addJSON('db', $GLOBALS['db']);
205 * Settings for relations stuff
208 $cfgRelation = PMA_getRelationsParam();
211 * Check if comments were updated
212 * (must be done before displaying the menu tabs)
214 if (isset($_REQUEST['comment'])) {
215 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
218 require 'libraries/db_common.inc.php';
219 $url_query .= '&goto=db_operations.php';
221 // Gets the database structure
222 $sub_part = '_structure';
230 $db_is_system_schema,
234 ) = PMA\libraries\Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
238 if (isset($message)) {
239 echo PMA\libraries\Util
::getMessage($message, $sql_query);
243 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
244 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
246 $response->addHTML('<div id="boxContainer" data-box-width="300">');
248 if (!$is_information_schema) {
249 if ($cfgRelation['commwork']) {
253 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
256 $response->addHTML('<div class="operations_half_width">');
257 $response->addHTML(PMA_getHtmlForCreateTable($db));
258 $response->addHTML('</div>');
263 if ($GLOBALS['db'] != 'mysql') {
264 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
267 // Drop link if allowed
268 // Don't even try to drop information_schema.
269 // You won't be able to. Believe me. You won't.
270 // Don't allow to easily drop mysql database, RFE #1327514.
271 if (($is_superuser ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
272 && ! $db_is_system_schema
273 && $GLOBALS['db'] != 'mysql'
275 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
280 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
283 * Change database charset
285 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
287 if (! $cfgRelation['allworks']
288 && $cfg['PmaNoRelation_DisableWarning'] == false
290 $message = PMA\libraries\Message
::notice(
292 'The phpMyAdmin configuration storage has been deactivated. ' .
296 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
297 $message->addParamHtml('</a>');
298 /* Show error if user has configured something, notice elsewhere */
299 if (!empty($cfg['Servers'][$server]['pmadb'])) {
300 $message->isError(true);
303 } // end if (!$is_information_schema)
305 $response->addHTML('</div>');
307 // not sure about displaying the PDF dialog in case db is information_schema
308 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
309 // We only show this if we find something in the new pdf_pages table
312 FROM ' . PMA\libraries\Util
::backquote($GLOBALS['cfgRelation']['db'])
313 . '.' . PMA\libraries\Util
::backquote($cfgRelation['pdf_pages']) . '
314 WHERE db_name = \'' . $GLOBALS['dbi']->escapeString($GLOBALS['db'])
316 $test_rs = PMA_queryAsControlUser(
319 PMA\libraries\DatabaseInterface
::QUERY_STORE