2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
14 declare(strict_types
=1);
16 use PhpMyAdmin\DatabaseInterface
;
17 use PhpMyAdmin\Display\CreateTable
;
18 use PhpMyAdmin\Message
;
19 use PhpMyAdmin\Operations
;
20 use PhpMyAdmin\Plugins
;
21 use PhpMyAdmin\Plugins\Export\ExportSql
;
22 use PhpMyAdmin\Relation
;
23 use PhpMyAdmin\RelationCleanup
;
24 use PhpMyAdmin\Response
;
30 require_once 'libraries/common.inc.php';
33 * functions implementation for this script
35 require_once 'libraries/check_user_privileges.inc.php';
37 // add a javascript file for jQuery functions to handle Ajax actions
38 $response = Response
::getInstance();
39 $header = $response->getHeader();
40 $scripts = $header->getScripts();
41 $scripts->addFile('db_operations.js');
45 $operations = new Operations();
48 * Rename/move or copy database
50 if (strlen($GLOBALS['db']) > 0
51 && (! empty($_REQUEST['db_rename']) ||
! empty($_REQUEST['db_copy']))
53 if (! empty($_REQUEST['db_rename'])) {
59 if (! isset($_REQUEST['newname']) ||
strlen($_REQUEST['newname']) === 0) {
60 $message = Message
::error(__('The database name is empty!'));
62 // lower_case_table_names=1 `DB` becomes `db`
63 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
64 $_REQUEST['newname'] = mb_strtolower(
69 if ($_REQUEST['newname'] === $_REQUEST['db']) {
70 $message = Message
::error(
71 __('Cannot copy database to the same name. Change the name and try again.')
75 if ($move ||
! empty($_REQUEST['create_database_before_copying'])) {
76 $operations->createDbBeforeCopy();
79 // here I don't use DELIMITER because it's not part of the
80 // language; I have to send each statement one by one
82 // to avoid selecting alternatively the current and new db
83 // we would need to modify the CREATE definitions to qualify
85 $operations->runProcedureAndFunctionDefinitions($GLOBALS['db']);
87 // go back to current db, just in case
88 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
90 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
92 // remove all foreign key constraints, otherwise we can get errors
93 /* @var $export_sql_plugin ExportSql */
94 $export_sql_plugin = Plugins
::getPlugin(
97 'libraries/classes/Plugins/Export/',
99 'single_table' => isset($single_table),
100 'export_type' => 'database'
104 // create stand-in tables for views
105 $views = $operations->getViewsAndCreateSqlViewStandIn(
112 $sqlConstratints = $operations->copyTables(
120 $operations->handleTheViews($views, $move, $GLOBALS['db']);
124 // now that all tables exist, create all the accumulated constraints
125 if (! $_error && count($sqlConstratints) > 0) {
126 $operations->createAllAccumulatedConstraints($sqlConstratints);
128 unset($sqlConstratints);
130 if ($GLOBALS['dbi']->getVersion() >= 50100) {
131 // here DELIMITER is not used because it's not part of the
132 // language; each statement is sent one by one
134 $operations->runEventDefinitionsForDb($GLOBALS['db']);
137 // go back to current db, just in case
138 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
140 // Duplicate the bookmarks for this db (done once for each db)
141 $operations->duplicateBookmarks($_error, $GLOBALS['db']);
143 if (! $_error && $move) {
144 if (isset($_REQUEST['adjust_privileges'])
145 && ! empty($_REQUEST['adjust_privileges'])
147 $operations->adjustPrivilegesMoveDb($GLOBALS['db'], $_REQUEST['newname']);
151 * cleanup pmadb stuff for this db
153 RelationCleanup
::database($GLOBALS['db']);
155 // if someday the RENAME DATABASE reappears, do not DROP
156 $local_query = 'DROP DATABASE '
157 . Util
::backquote($GLOBALS['db']) . ';';
158 $sql_query .= "\n" . $local_query;
159 $GLOBALS['dbi']->query($local_query);
161 $message = Message
::success(
162 __('Database %1$s has been renamed to %2$s.')
164 $message->addParam($GLOBALS['db']);
165 $message->addParam($_REQUEST['newname']);
166 } elseif (! $_error) {
167 if (isset($_REQUEST['adjust_privileges'])
168 && ! empty($_REQUEST['adjust_privileges'])
170 $operations->adjustPrivilegesCopyDb($GLOBALS['db'], $_REQUEST['newname']);
173 $message = Message
::success(
174 __('Database %1$s has been copied to %2$s.')
176 $message->addParam($GLOBALS['db']);
177 $message->addParam($_REQUEST['newname']);
179 $message = Message
::error();
183 /* Change database to be used */
184 if (! $_error && $move) {
185 $GLOBALS['db'] = $_REQUEST['newname'];
186 } elseif (! $_error) {
187 if (isset($_REQUEST['switch_to_new'])
188 && $_REQUEST['switch_to_new'] == 'true'
190 $_SESSION['pma_switch_to_new'] = true;
191 $GLOBALS['db'] = $_REQUEST['newname'];
193 $_SESSION['pma_switch_to_new'] = false;
200 * Database has been successfully renamed/moved. If in an Ajax request,
201 * generate the output with {@link PhpMyAdmin\Response} and exit
203 if ($response->isAjax()) {
204 $response->setRequestStatus($message->isSuccess());
205 $response->addJSON('message', $message);
206 $response->addJSON('newname', $_REQUEST['newname']);
209 Util
::getMessage(null, $sql_query)
211 $response->addJSON('db', $GLOBALS['db']);
217 * Settings for relations stuff
219 $relation = new Relation();
221 $cfgRelation = $relation->getRelationsParam();
224 * Check if comments were updated
225 * (must be done before displaying the menu tabs)
227 if (isset($_REQUEST['comment'])) {
228 $relation->setDbComment($GLOBALS['db'], $_REQUEST['comment']);
231 require 'libraries/db_common.inc.php';
232 $url_query .= '&goto=db_operations.php';
234 // Gets the database structure
235 $sub_part = '_structure';
243 $db_is_system_schema,
247 ) = Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
251 if (isset($message)) {
252 echo Util
::getMessage($message, $sql_query);
256 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
257 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
259 if (!$is_information_schema) {
260 if ($cfgRelation['commwork']) {
264 $response->addHTML($operations->getHtmlForDatabaseComment($GLOBALS['db']));
267 $response->addHTML('<div>');
268 $response->addHTML(CreateTable
::getHtml($db));
269 $response->addHTML('</div>');
274 if ($GLOBALS['db'] != 'mysql') {
275 $response->addHTML($operations->getHtmlForRenameDatabase($GLOBALS['db']));
278 // Drop link if allowed
279 // Don't even try to drop information_schema.
280 // You won't be able to. Believe me. You won't.
281 // Don't allow to easily drop mysql database, RFE #1327514.
282 if (($GLOBALS['dbi']->isSuperuser() ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
283 && ! $db_is_system_schema
284 && $GLOBALS['db'] != 'mysql'
286 $response->addHTML($operations->getHtmlForDropDatabaseLink($GLOBALS['db']));
291 $response->addHTML($operations->getHtmlForCopyDatabase($GLOBALS['db']));
294 * Change database charset
296 $response->addHTML($operations->getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
298 if (! $cfgRelation['allworks']
299 && $cfg['PmaNoRelation_DisableWarning'] == false
301 $message = Message
::notice(
303 'The phpMyAdmin configuration storage has been deactivated. ' .
307 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
308 $message->addParamHtml('</a>');
309 /* Show error if user has configured something, notice elsewhere */
310 if (!empty($cfg['Servers'][$server]['pmadb'])) {
311 $message->isError(true);
314 } // end if (!$is_information_schema)