2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
14 use PMA\libraries\plugins\export\ExportSql
;
19 require_once 'libraries/common.inc.php';
20 require_once 'libraries/display_create_table.lib.php';
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.lib.php';
26 require_once 'libraries/operations.lib.php';
28 // add a javascript file for jQuery functions to handle Ajax actions
29 $response = PMA\libraries\Response
::getInstance();
30 $header = $response->getHeader();
31 $scripts = $header->getScripts();
32 $scripts->addFile('db_operations.js');
37 * Rename/move or copy database
39 if (mb_strlen($GLOBALS['db'])
40 && (! empty($_REQUEST['db_rename']) ||
! empty($_REQUEST['db_copy']))
42 if (! empty($_REQUEST['db_rename'])) {
48 if (! isset($_REQUEST['newname'])
49 ||
! mb_strlen($_REQUEST['newname'])
51 $message = PMA\libraries\Message
::error(__('The database name is empty!'));
54 if ($move ||
! empty($_REQUEST['create_database_before_copying'])) {
55 PMA_createDbBeforeCopy();
58 // here I don't use DELIMITER because it's not part of the
59 // language; I have to send each statement one by one
61 // to avoid selecting alternatively the current and new db
62 // we would need to modify the CREATE definitions to qualify
64 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
66 // go back to current db, just in case
67 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
69 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
71 include_once "libraries/plugin_interface.lib.php";
72 // remove all foreign key constraints, otherwise we can get errors
73 /* @var $export_sql_plugin ExportSql */
74 $export_sql_plugin = PMA_getPlugin(
77 'libraries/plugins/export/',
79 'single_table' => isset($single_table),
80 'export_type' => 'database'
84 // create stand-in tables for views
85 $views = PMA_getViewsAndCreateSqlViewStandIn(
86 $tables_full, $export_sql_plugin, $GLOBALS['db']
90 $sqlConstratints = PMA_copyTables(
91 $tables_full, $move, $GLOBALS['db']
96 PMA_handleTheViews($views, $move, $GLOBALS['db']);
100 // now that all tables exist, create all the accumulated constraints
101 if (! $_error && count($sqlConstratints) > 0) {
102 PMA_createAllAccumulatedConstraints($sqlConstratints);
104 unset($sqlConstratints);
106 if (PMA_MYSQL_INT_VERSION
>= 50100) {
107 // here DELIMITER is not used because it's not part of the
108 // language; each statement is sent one by one
110 PMA_runEventDefinitionsForDb($GLOBALS['db']);
113 // go back to current db, just in case
114 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
116 // Duplicate the bookmarks for this db (done once for each db)
117 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
119 if (! $_error && $move) {
120 if (isset($_REQUEST['adjust_privileges'])
121 && ! empty($_REQUEST['adjust_privileges'])
123 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
127 * cleanup pmadb stuff for this db
129 include_once 'libraries/relation_cleanup.lib.php';
130 PMA_relationsCleanupDatabase($GLOBALS['db']);
132 // if someday the RENAME DATABASE reappears, do not DROP
133 $local_query = 'DROP DATABASE '
134 . PMA\libraries\Util
::backquote($GLOBALS['db']) . ';';
135 $sql_query .= "\n" . $local_query;
136 $GLOBALS['dbi']->query($local_query);
138 $message = PMA\libraries\Message
::success(
139 __('Database %1$s has been renamed to %2$s.')
141 $message->addParam($GLOBALS['db']);
142 $message->addParam($_REQUEST['newname']);
143 } elseif (! $_error) {
144 if (isset($_REQUEST['adjust_privileges'])
145 && ! empty($_REQUEST['adjust_privileges'])
147 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
150 $message = PMA\libraries\Message
::success(
151 __('Database %1$s has been copied to %2$s.')
153 $message->addParam($GLOBALS['db']);
154 $message->addParam($_REQUEST['newname']);
156 $message = PMA\libraries\Message
::error();
160 /* Change database to be used */
161 if (! $_error && $move) {
162 $GLOBALS['db'] = $_REQUEST['newname'];
163 } elseif (! $_error) {
164 if (isset($_REQUEST['switch_to_new'])
165 && $_REQUEST['switch_to_new'] == 'true'
167 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
168 $GLOBALS['db'] = $_REQUEST['newname'];
170 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
176 * Database has been successfully renamed/moved. If in an Ajax request,
177 * generate the output with {@link PMA\libraries\Response} and exit
179 if ($GLOBALS['is_ajax_request'] == true) {
180 $response = PMA\libraries\Response
::getInstance();
181 $response->setRequestStatus($message->isSuccess());
182 $response->addJSON('message', $message);
183 $response->addJSON('newname', $_REQUEST['newname']);
186 PMA\libraries\Util
::getMessage(null, $sql_query)
188 $response->addJSON('db', $GLOBALS['db']);
194 * Settings for relations stuff
197 $cfgRelation = PMA_getRelationsParam();
200 * Check if comments were updated
201 * (must be done before displaying the menu tabs)
203 if (isset($_REQUEST['comment'])) {
204 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
207 require 'libraries/db_common.inc.php';
208 $url_query .= '&goto=db_operations.php';
210 // Gets the database structure
211 $sub_part = '_structure';
219 $db_is_system_schema,
223 ) = PMA\libraries\Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
227 if (isset($message)) {
228 echo PMA\libraries\Util
::getMessage($message, $sql_query);
232 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
233 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
235 $response->addHTML('<div id="boxContainer" data-box-width="300">');
237 if (!$is_information_schema) {
238 if ($cfgRelation['commwork']) {
242 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
245 $response->addHTML('<div class="operations_half_width">');
246 $response->addHTML(PMA_getHtmlForCreateTable($db));
247 $response->addHTML('</div>');
252 if ($GLOBALS['db'] != 'mysql') {
253 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
256 // Drop link if allowed
257 // Don't even try to drop information_schema.
258 // You won't be able to. Believe me. You won't.
259 // Don't allow to easily drop mysql database, RFE #1327514.
260 if (($is_superuser ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
261 && ! $db_is_system_schema
262 && $GLOBALS['db'] != 'mysql'
264 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
269 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
272 * Change database charset
274 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
276 if (! $cfgRelation['allworks']
277 && $cfg['PmaNoRelation_DisableWarning'] == false
279 $message = PMA\libraries\Message
::notice(
281 'The phpMyAdmin configuration storage has been deactivated. ' .
285 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
286 $message->addParamHtml('</a>');
287 /* Show error if user has configured something, notice elsewhere */
288 if (!empty($cfg['Servers'][$server]['pmadb'])) {
289 $message->isError(true);
292 } // end if (!$is_information_schema)
294 $response->addHTML('</div>');
296 // not sure about displaying the PDF dialog in case db is information_schema
297 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
298 // We only show this if we find something in the new pdf_pages table
301 FROM ' . PMA\libraries\Util
::backquote($GLOBALS['cfgRelation']['db'])
302 . '.' . PMA\libraries\Util
::backquote($cfgRelation['pdf_pages']) . '
303 WHERE db_name = \'' . PMA\libraries\Util
::sqlAddSlashes($GLOBALS['db'])
305 $test_rs = PMA_queryAsControlUser(
308 PMA\libraries\DatabaseInterface
::QUERY_STORE