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');
44 * Rename/move or copy database
46 if (strlen($GLOBALS['db']) > 0
47 && (! empty($_REQUEST['db_rename']) ||
! empty($_REQUEST['db_copy']))
49 if (! empty($_REQUEST['db_rename'])) {
55 if (! isset($_REQUEST['newname']) ||
strlen($_REQUEST['newname']) === 0) {
56 $message = Message
::error(__('The database name is empty!'));
58 // lower_case_table_names=1 `DB` becomes `db`
59 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
60 $_REQUEST['newname'] = mb_strtolower(
65 if ($_REQUEST['newname'] === $_REQUEST['db']) {
66 $message = Message
::error(
67 __('Cannot copy database to the same name. Change the name and try again.')
71 if ($move ||
! empty($_REQUEST['create_database_before_copying'])) {
72 Operations
::createDbBeforeCopy();
75 // here I don't use DELIMITER because it's not part of the
76 // language; I have to send each statement one by one
78 // to avoid selecting alternatively the current and new db
79 // we would need to modify the CREATE definitions to qualify
81 Operations
::runProcedureAndFunctionDefinitions($GLOBALS['db']);
83 // go back to current db, just in case
84 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
86 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
88 // remove all foreign key constraints, otherwise we can get errors
89 /* @var $export_sql_plugin ExportSql */
90 $export_sql_plugin = Plugins
::getPlugin(
93 'libraries/classes/Plugins/Export/',
95 'single_table' => isset($single_table),
96 'export_type' => 'database'
100 // create stand-in tables for views
101 $views = Operations
::getViewsAndCreateSqlViewStandIn(
102 $tables_full, $export_sql_plugin, $GLOBALS['db']
106 $sqlConstratints = Operations
::copyTables(
107 $tables_full, $move, $GLOBALS['db']
112 Operations
::handleTheViews($views, $move, $GLOBALS['db']);
116 // now that all tables exist, create all the accumulated constraints
117 if (! $_error && count($sqlConstratints) > 0) {
118 Operations
::createAllAccumulatedConstraints($sqlConstratints);
120 unset($sqlConstratints);
122 if ($GLOBALS['dbi']->getVersion() >= 50100) {
123 // here DELIMITER is not used because it's not part of the
124 // language; each statement is sent one by one
126 Operations
::runEventDefinitionsForDb($GLOBALS['db']);
129 // go back to current db, just in case
130 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
132 // Duplicate the bookmarks for this db (done once for each db)
133 Operations
::duplicateBookmarks($_error, $GLOBALS['db']);
135 if (! $_error && $move) {
136 if (isset($_REQUEST['adjust_privileges'])
137 && ! empty($_REQUEST['adjust_privileges'])
139 Operations
::adjustPrivilegesMoveDb($GLOBALS['db'], $_REQUEST['newname']);
143 * cleanup pmadb stuff for this db
145 RelationCleanup
::database($GLOBALS['db']);
147 // if someday the RENAME DATABASE reappears, do not DROP
148 $local_query = 'DROP DATABASE '
149 . Util
::backquote($GLOBALS['db']) . ';';
150 $sql_query .= "\n" . $local_query;
151 $GLOBALS['dbi']->query($local_query);
153 $message = Message
::success(
154 __('Database %1$s has been renamed to %2$s.')
156 $message->addParam($GLOBALS['db']);
157 $message->addParam($_REQUEST['newname']);
158 } elseif (! $_error) {
159 if (isset($_REQUEST['adjust_privileges'])
160 && ! empty($_REQUEST['adjust_privileges'])
162 Operations
::adjustPrivilegesCopyDb($GLOBALS['db'], $_REQUEST['newname']);
165 $message = Message
::success(
166 __('Database %1$s has been copied to %2$s.')
168 $message->addParam($GLOBALS['db']);
169 $message->addParam($_REQUEST['newname']);
171 $message = Message
::error();
175 /* Change database to be used */
176 if (! $_error && $move) {
177 $GLOBALS['db'] = $_REQUEST['newname'];
178 } elseif (! $_error) {
179 if (isset($_REQUEST['switch_to_new'])
180 && $_REQUEST['switch_to_new'] == 'true'
182 $_SESSION['pma_switch_to_new'] = true;
183 $GLOBALS['db'] = $_REQUEST['newname'];
185 $_SESSION['pma_switch_to_new'] = false;
192 * Database has been successfully renamed/moved. If in an Ajax request,
193 * generate the output with {@link PhpMyAdmin\Response} and exit
195 if ($response->isAjax()) {
196 $response->setRequestStatus($message->isSuccess());
197 $response->addJSON('message', $message);
198 $response->addJSON('newname', $_REQUEST['newname']);
201 Util
::getMessage(null, $sql_query)
203 $response->addJSON('db', $GLOBALS['db']);
209 * Settings for relations stuff
212 $cfgRelation = Relation
::getRelationsParam();
215 * Check if comments were updated
216 * (must be done before displaying the menu tabs)
218 if (isset($_REQUEST['comment'])) {
219 Relation
::setDbComment($GLOBALS['db'], $_REQUEST['comment']);
222 require 'libraries/db_common.inc.php';
223 $url_query .= '&goto=db_operations.php';
225 // Gets the database structure
226 $sub_part = '_structure';
234 $db_is_system_schema,
238 ) = Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
242 if (isset($message)) {
243 echo Util
::getMessage($message, $sql_query);
247 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
248 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
250 if (!$is_information_schema) {
251 if ($cfgRelation['commwork']) {
255 $response->addHTML(Operations
::getHtmlForDatabaseComment($GLOBALS['db']));
258 $response->addHTML('<div>');
259 $response->addHTML(CreateTable
::getHtml($db));
260 $response->addHTML('</div>');
265 if ($GLOBALS['db'] != 'mysql') {
266 $response->addHTML(Operations
::getHtmlForRenameDatabase($GLOBALS['db']));
269 // Drop link if allowed
270 // Don't even try to drop information_schema.
271 // You won't be able to. Believe me. You won't.
272 // Don't allow to easily drop mysql database, RFE #1327514.
273 if (($GLOBALS['dbi']->isSuperuser() ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
274 && ! $db_is_system_schema
275 && $GLOBALS['db'] != 'mysql'
277 $response->addHTML(Operations
::getHtmlForDropDatabaseLink($GLOBALS['db']));
282 $response->addHTML(Operations
::getHtmlForCopyDatabase($GLOBALS['db']));
285 * Change database charset
287 $response->addHTML(Operations
::getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
289 if (! $cfgRelation['allworks']
290 && $cfg['PmaNoRelation_DisableWarning'] == false
292 $message = Message
::notice(
294 'The phpMyAdmin configuration storage has been deactivated. ' .
298 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
299 $message->addParamHtml('</a>');
300 /* Show error if user has configured something, notice elsewhere */
301 if (!empty($cfg['Servers'][$server]['pmadb'])) {
302 $message->isError(true);
305 } // end if (!$is_information_schema)