2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
14 use PhpMyAdmin\DatabaseInterface
;
15 use PhpMyAdmin\Display\CreateTable
;
16 use PhpMyAdmin\Message
;
17 use PhpMyAdmin\Operations
;
18 use PhpMyAdmin\Plugins
;
19 use PhpMyAdmin\Plugins\Export\ExportSql
;
20 use PhpMyAdmin\Relation
;
21 use PhpMyAdmin\RelationCleanup
;
22 use PhpMyAdmin\Response
;
28 require_once 'libraries/common.inc.php';
31 * functions implementation for this script
33 require_once 'libraries/check_user_privileges.inc.php';
35 // add a javascript file for jQuery functions to handle Ajax actions
36 $response = Response
::getInstance();
37 $header = $response->getHeader();
38 $scripts = $header->getScripts();
39 $scripts->addFile('db_operations.js');
43 $operations = new Operations();
46 * Rename/move or copy database
48 if (strlen($GLOBALS['db']) > 0
49 && (! empty($_POST['db_rename']) ||
! empty($_POST['db_copy']))
51 if (! empty($_POST['db_rename'])) {
57 if (! isset($_POST['newname']) ||
strlen($_POST['newname']) === 0) {
58 $message = Message
::error(__('The database name is empty!'));
60 // lower_case_table_names=1 `DB` becomes `db`
61 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
62 $_POST['newname'] = mb_strtolower(
67 if ($_POST['newname'] === $_REQUEST['db']) {
68 $message = Message
::error(
69 __('Cannot copy database to the same name. Change the name and try again.')
73 if ($move ||
! empty($_POST['create_database_before_copying'])) {
74 $operations->createDbBeforeCopy();
77 // here I don't use DELIMITER because it's not part of the
78 // language; I have to send each statement one by one
80 // to avoid selecting alternatively the current and new db
81 // we would need to modify the CREATE definitions to qualify
83 $operations->runProcedureAndFunctionDefinitions($GLOBALS['db']);
85 // go back to current db, just in case
86 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
88 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
90 // remove all foreign key constraints, otherwise we can get errors
91 /* @var $export_sql_plugin ExportSql */
92 $export_sql_plugin = Plugins
::getPlugin(
95 'libraries/classes/Plugins/Export/',
97 'single_table' => isset($single_table),
98 'export_type' => 'database'
102 // create stand-in tables for views
103 $views = $operations->getViewsAndCreateSqlViewStandIn(
104 $tables_full, $export_sql_plugin, $GLOBALS['db']
108 $sqlConstratints = $operations->copyTables(
109 $tables_full, $move, $GLOBALS['db']
114 $operations->handleTheViews($views, $move, $GLOBALS['db']);
118 // now that all tables exist, create all the accumulated constraints
119 if (! $_error && count($sqlConstratints) > 0) {
120 $operations->createAllAccumulatedConstraints($sqlConstratints);
122 unset($sqlConstratints);
124 if ($GLOBALS['dbi']->getVersion() >= 50100) {
125 // here DELIMITER is not used because it's not part of the
126 // language; each statement is sent one by one
128 $operations->runEventDefinitionsForDb($GLOBALS['db']);
131 // go back to current db, just in case
132 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
134 // Duplicate the bookmarks for this db (done once for each db)
135 $operations->duplicateBookmarks($_error, $GLOBALS['db']);
137 if (! $_error && $move) {
138 if (isset($_POST['adjust_privileges'])
139 && ! empty($_POST['adjust_privileges'])
141 $operations->adjustPrivilegesMoveDb($GLOBALS['db'], $_POST['newname']);
145 * cleanup pmadb stuff for this db
147 RelationCleanup
::database($GLOBALS['db']);
149 // if someday the RENAME DATABASE reappears, do not DROP
150 $local_query = 'DROP DATABASE '
151 . Util
::backquote($GLOBALS['db']) . ';';
152 $sql_query .= "\n" . $local_query;
153 $GLOBALS['dbi']->query($local_query);
155 $message = Message
::success(
156 __('Database %1$s has been renamed to %2$s.')
158 $message->addParam($GLOBALS['db']);
159 $message->addParam($_POST['newname']);
160 } elseif (! $_error) {
161 if (isset($_POST['adjust_privileges'])
162 && ! empty($_POST['adjust_privileges'])
164 $operations->adjustPrivilegesCopyDb($GLOBALS['db'], $_POST['newname']);
167 $message = Message
::success(
168 __('Database %1$s has been copied to %2$s.')
170 $message->addParam($GLOBALS['db']);
171 $message->addParam($_POST['newname']);
173 $message = Message
::error();
177 /* Change database to be used */
178 if (! $_error && $move) {
179 $GLOBALS['db'] = $_POST['newname'];
180 } elseif (! $_error) {
181 if (isset($_POST['switch_to_new'])
182 && $_POST['switch_to_new'] == 'true'
184 $_SESSION['pma_switch_to_new'] = true;
185 $GLOBALS['db'] = $_POST['newname'];
187 $_SESSION['pma_switch_to_new'] = false;
194 * Database has been successfully renamed/moved. If in an Ajax request,
195 * generate the output with {@link PhpMyAdmin\Response} and exit
197 if ($response->isAjax()) {
198 $response->setRequestStatus($message->isSuccess());
199 $response->addJSON('message', $message);
200 $response->addJSON('newname', $_POST['newname']);
203 Util
::getMessage(null, $sql_query)
205 $response->addJSON('db', $GLOBALS['db']);
211 * Settings for relations stuff
213 $relation = new Relation();
215 $cfgRelation = $relation->getRelationsParam();
218 * Check if comments were updated
219 * (must be done before displaying the menu tabs)
221 if (isset($_POST['comment'])) {
222 $relation->setDbComment($GLOBALS['db'], $_POST['comment']);
225 require 'libraries/db_common.inc.php';
226 $url_query .= '&goto=db_operations.php';
228 // Gets the database structure
229 $sub_part = '_structure';
237 $db_is_system_schema,
241 ) = Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
245 if (isset($message)) {
246 echo Util
::getMessage($message, $sql_query);
250 $db_collation = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
251 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
253 if (!$is_information_schema) {
254 if ($cfgRelation['commwork']) {
258 $response->addHTML($operations->getHtmlForDatabaseComment($GLOBALS['db']));
261 $response->addHTML('<div>');
262 $response->addHTML(CreateTable
::getHtml($db));
263 $response->addHTML('</div>');
268 if ($GLOBALS['db'] != 'mysql') {
269 $response->addHTML($operations->getHtmlForRenameDatabase($GLOBALS['db'], $db_collation));
272 // Drop link if allowed
273 // Don't even try to drop information_schema.
274 // You won't be able to. Believe me. You won't.
275 // Don't allow to easily drop mysql database, RFE #1327514.
276 if (($GLOBALS['dbi']->isSuperuser() ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
277 && ! $db_is_system_schema
278 && $GLOBALS['db'] != 'mysql'
280 $response->addHTML($operations->getHtmlForDropDatabaseLink($GLOBALS['db']));
285 $response->addHTML($operations->getHtmlForCopyDatabase($GLOBALS['db'], $db_collation));
288 * Change database charset
290 $response->addHTML($operations->getHtmlForChangeDatabaseCharset($GLOBALS['db'], $db_collation));
292 if (! $cfgRelation['allworks']
293 && $cfg['PmaNoRelation_DisableWarning'] == false
295 $message = Message
::notice(
297 'The phpMyAdmin configuration storage has been deactivated. ' .
301 $message->addParamHtml('<a href="./chk_rel.php" data-post="' . $url_query . '">');
302 $message->addParamHtml('</a>');
303 /* Show error if user has configured something, notice elsewhere */
304 if (!empty($cfg['Servers'][$server]['pmadb'])) {
305 $message->isError(true);
308 } // end if (!$is_information_schema)