3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Controllers\Database
;
7 use PhpMyAdmin\Charsets
;
8 use PhpMyAdmin\CheckUserPrivileges
;
9 use PhpMyAdmin\ConfigStorage\Relation
;
10 use PhpMyAdmin\ConfigStorage\RelationCleanup
;
11 use PhpMyAdmin\Controllers\AbstractController
;
12 use PhpMyAdmin\DatabaseInterface
;
13 use PhpMyAdmin\Html\Generator
;
14 use PhpMyAdmin\Message
;
15 use PhpMyAdmin\Operations
;
16 use PhpMyAdmin\Plugins
;
17 use PhpMyAdmin\Query\Utilities
;
18 use PhpMyAdmin\ResponseRenderer
;
19 use PhpMyAdmin\Template
;
25 use function mb_strtolower
;
29 * Handles miscellaneous database operations.
31 class OperationsController
extends AbstractController
33 /** @var Operations */
36 /** @var CheckUserPrivileges */
37 private $checkUserPrivileges;
42 /** @var RelationCleanup */
43 private $relationCleanup;
45 /** @var DatabaseInterface */
48 public function __construct(
49 ResponseRenderer
$response,
51 Operations
$operations,
52 CheckUserPrivileges
$checkUserPrivileges,
54 RelationCleanup
$relationCleanup,
55 DatabaseInterface
$dbi
57 parent
::__construct($response, $template);
58 $this->operations
= $operations;
59 $this->checkUserPrivileges
= $checkUserPrivileges;
60 $this->relation
= $relation;
61 $this->relationCleanup
= $relationCleanup;
65 public function __invoke(): void
67 $this->checkUserPrivileges
->getPrivileges();
69 $this->addScriptFiles(['database/operations.js']);
71 $GLOBALS['sql_query'] = '';
74 * Rename/move or copy database
76 if (strlen($GLOBALS['db']) > 0 && (! empty($_POST['db_rename']) ||
! empty($_POST['db_copy']))) {
77 if (! empty($_POST['db_rename'])) {
78 $GLOBALS['move'] = true;
80 $GLOBALS['move'] = false;
83 if (! isset($_POST['newname']) ||
strlen($_POST['newname']) === 0) {
84 $GLOBALS['message'] = Message
::error(__('The database name is empty!'));
86 // lower_case_table_names=1 `DB` becomes `db`
87 if ($this->dbi
->getLowerCaseNames() === '1') {
88 $_POST['newname'] = mb_strtolower($_POST['newname']);
91 if ($_POST['newname'] === $_REQUEST['db']) {
92 $GLOBALS['message'] = Message
::error(
93 __('Cannot copy database to the same name. Change the name and try again.')
97 if ($GLOBALS['move'] ||
! empty($_POST['create_database_before_copying'])) {
98 $this->operations
->createDbBeforeCopy();
101 // here I don't use DELIMITER because it's not part of the
102 // language; I have to send each statement one by one
104 // to avoid selecting alternatively the current and new db
105 // we would need to modify the CREATE definitions to qualify
107 $this->operations
->runProcedureAndFunctionDefinitions($GLOBALS['db']);
109 // go back to current db, just in case
110 $this->dbi
->selectDb($GLOBALS['db']);
112 $GLOBALS['tables_full'] = $this->dbi
->getTablesFull($GLOBALS['db']);
114 // remove all foreign key constraints, otherwise we can get errors
115 $GLOBALS['export_sql_plugin'] = Plugins
::getPlugin('export', 'sql', [
116 'export_type' => 'database',
117 'single_table' => isset($GLOBALS['single_table']),
120 // create stand-in tables for views
121 $GLOBALS['views'] = $this->operations
->getViewsAndCreateSqlViewStandIn(
122 $GLOBALS['tables_full'],
123 $GLOBALS['export_sql_plugin'],
128 $GLOBALS['sqlConstratints'] = $this->operations
->copyTables(
129 $GLOBALS['tables_full'],
136 $this->operations
->handleTheViews($GLOBALS['views'], $GLOBALS['move'], $GLOBALS['db']);
139 unset($GLOBALS['views']);
141 // now that all tables exist, create all the accumulated constraints
142 if (! $_error && count($GLOBALS['sqlConstratints']) > 0) {
143 $this->operations
->createAllAccumulatedConstraints($GLOBALS['sqlConstratints']);
146 unset($GLOBALS['sqlConstratints']);
148 if ($this->dbi
->getVersion() >= 50100) {
149 // here DELIMITER is not used because it's not part of the
150 // language; each statement is sent one by one
152 $this->operations
->runEventDefinitionsForDb($GLOBALS['db']);
155 // go back to current db, just in case
156 $this->dbi
->selectDb($GLOBALS['db']);
158 // Duplicate the bookmarks for this db (done once for each db)
159 $this->operations
->duplicateBookmarks($_error, $GLOBALS['db']);
161 if (! $_error && $GLOBALS['move']) {
162 if (isset($_POST['adjust_privileges']) && ! empty($_POST['adjust_privileges'])) {
163 $this->operations
->adjustPrivilegesMoveDb($GLOBALS['db'], $_POST['newname']);
167 * cleanup pmadb stuff for this db
169 $this->relationCleanup
->database($GLOBALS['db']);
171 // if someday the RENAME DATABASE reappears, do not DROP
172 $GLOBALS['local_query'] = 'DROP DATABASE '
173 . Util
::backquote($GLOBALS['db']) . ';';
174 $GLOBALS['sql_query'] .= "\n" . $GLOBALS['local_query'];
175 $this->dbi
->query($GLOBALS['local_query']);
177 $GLOBALS['message'] = Message
::success(
178 __('Database %1$s has been renamed to %2$s.')
180 $GLOBALS['message']->addParam($GLOBALS['db']);
181 $GLOBALS['message']->addParam($_POST['newname']);
182 } elseif (! $_error) {
183 if (isset($_POST['adjust_privileges']) && ! empty($_POST['adjust_privileges'])) {
184 $this->operations
->adjustPrivilegesCopyDb($GLOBALS['db'], $_POST['newname']);
187 $GLOBALS['message'] = Message
::success(
188 __('Database %1$s has been copied to %2$s.')
190 $GLOBALS['message']->addParam($GLOBALS['db']);
191 $GLOBALS['message']->addParam($_POST['newname']);
193 $GLOBALS['message'] = Message
::error();
196 $GLOBALS['reload'] = true;
198 /* Change database to be used */
199 if (! $_error && $GLOBALS['move']) {
200 $GLOBALS['db'] = $_POST['newname'];
201 } elseif (! $_error) {
202 if (isset($_POST['switch_to_new']) && $_POST['switch_to_new'] === 'true') {
203 $_SESSION['pma_switch_to_new'] = true;
204 $GLOBALS['db'] = $_POST['newname'];
206 $_SESSION['pma_switch_to_new'] = false;
213 * Database has been successfully renamed/moved. If in an Ajax request,
214 * generate the output with {@link ResponseRenderer} and exit
216 if ($this->response
->isAjax()) {
217 $this->response
->setRequestStatus($GLOBALS['message']->isSuccess());
218 $this->response
->addJSON('message', $GLOBALS['message']);
219 $this->response
->addJSON('newname', $_POST['newname']);
220 $this->response
->addJSON(
222 Generator
::getMessage('', $GLOBALS['sql_query'])
224 $this->response
->addJSON('db', $GLOBALS['db']);
230 $relationParameters = $this->relation
->getRelationParameters();
233 * Check if comments were updated
234 * (must be done before displaying the menu tabs)
236 if (isset($_POST['comment'])) {
237 $this->relation
->setDbComment($GLOBALS['db'], $_POST['comment']);
240 Util
::checkParameters(['db']);
242 $GLOBALS['errorUrl'] = Util
::getScriptNameForOption($GLOBALS['cfg']['DefaultTabDatabase'], 'database');
243 $GLOBALS['errorUrl'] .= Url
::getCommon(['db' => $GLOBALS['db']], '&');
245 if (! $this->hasDatabase()) {
249 $GLOBALS['urlParams']['goto'] = Url
::getFromRoute('/database/operations');
251 // Gets the database structure
252 $GLOBALS['sub_part'] = '_structure';
256 $GLOBALS['num_tables'],
257 $GLOBALS['total_num_tables'],
258 $GLOBALS['sub_part'],,
260 $GLOBALS['tooltip_truename'],
261 $GLOBALS['tooltip_aliasname'],
263 ] = Util
::getDbInfo($GLOBALS['db'], $GLOBALS['sub_part']);
266 if (isset($GLOBALS['message'])) {
267 $oldMessage = Generator
::getMessage($GLOBALS['message'], $GLOBALS['sql_query']);
268 unset($GLOBALS['message']);
271 $GLOBALS['db_collation'] = $this->dbi
->getDbCollation($GLOBALS['db']);
272 $GLOBALS['is_information_schema'] = Utilities
::isSystemSchema($GLOBALS['db']);
274 if ($GLOBALS['is_information_schema']) {
278 $databaseComment = '';
279 if ($relationParameters->columnCommentsFeature
!== null) {
280 $databaseComment = $this->relation
->getDbComment($GLOBALS['db']);
283 $hasAdjustPrivileges = $GLOBALS['db_priv'] && $GLOBALS['table_priv']
284 && $GLOBALS['col_priv'] && $GLOBALS['proc_priv'] && $GLOBALS['is_reload_priv'];
286 $isDropDatabaseAllowed = ($this->dbi
->isSuperUser() ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
287 && ! $isSystemSchema && $GLOBALS['db'] !== 'mysql';
289 $switchToNew = isset($_SESSION['pma_switch_to_new']) && $_SESSION['pma_switch_to_new'];
291 $charsets = Charsets
::getCharsets($this->dbi
, $GLOBALS['cfg']['Server']['DisableIS']);
292 $collations = Charsets
::getCollations($this->dbi
, $GLOBALS['cfg']['Server']['DisableIS']);
294 if (! $relationParameters->hasAllFeatures() && $GLOBALS['cfg']['PmaNoRelation_DisableWarning'] == false) {
295 $GLOBALS['message'] = Message
::notice(
297 'The phpMyAdmin configuration storage has been deactivated. %sFind out why%s.'
300 $GLOBALS['message']->addParamHtml(
301 '<a href="' . Url
::getFromRoute('/check-relations')
302 . '" data-post="' . Url
::getCommon(['db' => $GLOBALS['db']]) . '">'
304 $GLOBALS['message']->addParamHtml('</a>');
305 /* Show error if user has configured something, notice elsewhere */
306 if (! empty($GLOBALS['cfg']['Servers'][$GLOBALS['server']]['pmadb'])) {
307 $GLOBALS['message']->isError(true);
311 $this->render('database/operations/index', [
312 'message' => $oldMessage,
313 'db' => $GLOBALS['db'],
314 'has_comment' => $relationParameters->columnCommentsFeature
!== null,
315 'db_comment' => $databaseComment,
316 'db_collation' => $GLOBALS['db_collation'],
317 'has_adjust_privileges' => $hasAdjustPrivileges,
318 'is_drop_database_allowed' => $isDropDatabaseAllowed,
319 'switch_to_new' => $switchToNew,
320 'charsets' => $charsets,
321 'collations' => $collations,