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!'));
57 } else if($_REQUEST['newname'] === $_REQUEST['db']) {
58 $message = Message
::error(
59 __('Cannot copy database to the same name. Change the name and try again.')
63 if ($move ||
! empty($_REQUEST['create_database_before_copying'])) {
64 Operations
::createDbBeforeCopy();
67 // here I don't use DELIMITER because it's not part of the
68 // language; I have to send each statement one by one
70 // to avoid selecting alternatively the current and new db
71 // we would need to modify the CREATE definitions to qualify
73 Operations
::runProcedureAndFunctionDefinitions($GLOBALS['db']);
75 // go back to current db, just in case
76 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
78 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
80 // remove all foreign key constraints, otherwise we can get errors
81 /* @var $export_sql_plugin ExportSql */
82 $export_sql_plugin = Plugins
::getPlugin(
85 'libraries/classes/Plugins/Export/',
87 'single_table' => isset($single_table),
88 'export_type' => 'database'
92 // create stand-in tables for views
93 $views = Operations
::getViewsAndCreateSqlViewStandIn(
94 $tables_full, $export_sql_plugin, $GLOBALS['db']
98 $sqlConstratints = Operations
::copyTables(
99 $tables_full, $move, $GLOBALS['db']
104 Operations
::handleTheViews($views, $move, $GLOBALS['db']);
108 // now that all tables exist, create all the accumulated constraints
109 if (! $_error && count($sqlConstratints) > 0) {
110 Operations
::createAllAccumulatedConstraints($sqlConstratints);
112 unset($sqlConstratints);
114 if ($GLOBALS['dbi']->getVersion() >= 50100) {
115 // here DELIMITER is not used because it's not part of the
116 // language; each statement is sent one by one
118 Operations
::runEventDefinitionsForDb($GLOBALS['db']);
121 // go back to current db, just in case
122 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
124 // Duplicate the bookmarks for this db (done once for each db)
125 Operations
::duplicateBookmarks($_error, $GLOBALS['db']);
127 if (! $_error && $move) {
128 if (isset($_REQUEST['adjust_privileges'])
129 && ! empty($_REQUEST['adjust_privileges'])
131 Operations
::adjustPrivilegesMoveDb($GLOBALS['db'], $_REQUEST['newname']);
135 * cleanup pmadb stuff for this db
137 RelationCleanup
::database($GLOBALS['db']);
139 // if someday the RENAME DATABASE reappears, do not DROP
140 $local_query = 'DROP DATABASE '
141 . Util
::backquote($GLOBALS['db']) . ';';
142 $sql_query .= "\n" . $local_query;
143 $GLOBALS['dbi']->query($local_query);
145 $message = Message
::success(
146 __('Database %1$s has been renamed to %2$s.')
148 $message->addParam($GLOBALS['db']);
149 $message->addParam($_REQUEST['newname']);
150 } elseif (! $_error) {
151 if (isset($_REQUEST['adjust_privileges'])
152 && ! empty($_REQUEST['adjust_privileges'])
154 Operations
::adjustPrivilegesCopyDb($GLOBALS['db'], $_REQUEST['newname']);
157 $message = Message
::success(
158 __('Database %1$s has been copied to %2$s.')
160 $message->addParam($GLOBALS['db']);
161 $message->addParam($_REQUEST['newname']);
163 $message = Message
::error();
167 /* Change database to be used */
168 if (! $_error && $move) {
169 $GLOBALS['db'] = $_REQUEST['newname'];
170 } elseif (! $_error) {
171 if (isset($_REQUEST['switch_to_new'])
172 && $_REQUEST['switch_to_new'] == 'true'
174 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
175 $GLOBALS['db'] = $_REQUEST['newname'];
177 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
183 * Database has been successfully renamed/moved. If in an Ajax request,
184 * generate the output with {@link PhpMyAdmin\Response} and exit
186 if ($response->isAjax()) {
187 $response->setRequestStatus($message->isSuccess());
188 $response->addJSON('message', $message);
189 $response->addJSON('newname', $_REQUEST['newname']);
192 Util
::getMessage(null, $sql_query)
194 $response->addJSON('db', $GLOBALS['db']);
200 * Settings for relations stuff
203 $cfgRelation = Relation
::getRelationsParam();
206 * Check if comments were updated
207 * (must be done before displaying the menu tabs)
209 if (isset($_REQUEST['comment'])) {
210 Relation
::setDbComment($GLOBALS['db'], $_REQUEST['comment']);
213 require 'libraries/db_common.inc.php';
214 $url_query .= '&goto=db_operations.php';
216 // Gets the database structure
217 $sub_part = '_structure';
225 $db_is_system_schema,
229 ) = Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
233 if (isset($message)) {
234 echo Util
::getMessage($message, $sql_query);
238 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
239 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
241 if (!$is_information_schema) {
242 if ($cfgRelation['commwork']) {
246 $response->addHTML(Operations
::getHtmlForDatabaseComment($GLOBALS['db']));
249 $response->addHTML('<div>');
250 $response->addHTML(CreateTable
::getHtml($db));
251 $response->addHTML('</div>');
256 if ($GLOBALS['db'] != 'mysql') {
257 $response->addHTML(Operations
::getHtmlForRenameDatabase($GLOBALS['db']));
260 // Drop link if allowed
261 // Don't even try to drop information_schema.
262 // You won't be able to. Believe me. You won't.
263 // Don't allow to easily drop mysql database, RFE #1327514.
264 if (($GLOBALS['dbi']->isSuperuser() ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
265 && ! $db_is_system_schema
266 && $GLOBALS['db'] != 'mysql'
268 $response->addHTML(Operations
::getHtmlForDropDatabaseLink($GLOBALS['db']));
273 $response->addHTML(Operations
::getHtmlForCopyDatabase($GLOBALS['db']));
276 * Change database charset
278 $response->addHTML(Operations
::getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
280 if (! $cfgRelation['allworks']
281 && $cfg['PmaNoRelation_DisableWarning'] == false
283 $message = Message
::notice(
285 'The phpMyAdmin configuration storage has been deactivated. ' .
289 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
290 $message->addParamHtml('</a>');
291 /* Show error if user has configured something, notice elsewhere */
292 if (!empty($cfg['Servers'][$server]['pmadb'])) {
293 $message->isError(true);
296 } // end if (!$is_information_schema)