2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * handles miscellaneous db operations:
10 * - viewing PDF schemas
14 use PMA\libraries\plugins\export\ExportSql
;
19 require_once 'libraries/common.inc.php';
20 require_once 'libraries/mysql_charsets.inc.php';
21 require_once 'libraries/display_create_table.lib.php';
24 * functions implementation for this script
26 require_once 'libraries/check_user_privileges.lib.php';
27 require_once 'libraries/operations.lib.php';
29 // add a javascript file for jQuery functions to handle Ajax actions
30 $response = PMA\libraries\Response
::getInstance();
31 $header = $response->getHeader();
32 $scripts = $header->getScripts();
33 $scripts->addFile('db_operations.js');
38 * Rename/move or copy database
40 if (mb_strlen($GLOBALS['db'])
41 && (! empty($_REQUEST['db_rename']) ||
! empty($_REQUEST['db_copy']))
43 if (! empty($_REQUEST['db_rename'])) {
49 if (! isset($_REQUEST['newname'])
50 ||
! mb_strlen($_REQUEST['newname'])
52 $message = PMA\libraries\Message
::error(__('The database name is empty!'));
55 if ($move ||
! empty($_REQUEST['create_database_before_copying'])) {
56 PMA_createDbBeforeCopy();
59 // here I don't use DELIMITER because it's not part of the
60 // language; I have to send each statement one by one
62 // to avoid selecting alternatively the current and new db
63 // we would need to modify the CREATE definitions to qualify
65 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
67 // go back to current db, just in case
68 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
70 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
72 include_once "libraries/plugin_interface.lib.php";
73 // remove all foreign key constraints, otherwise we can get errors
74 /* @var $export_sql_plugin ExportSql */
75 $export_sql_plugin = PMA_getPlugin(
78 'libraries/plugins/export/',
80 'single_table' => isset($single_table),
81 'export_type' => 'database'
85 // create stand-in tables for views
86 $views = PMA_getViewsAndCreateSqlViewStandIn(
87 $tables_full, $export_sql_plugin, $GLOBALS['db']
91 $sqlConstratints = PMA_copyTables(
92 $tables_full, $move, $GLOBALS['db']
97 PMA_handleTheViews($views, $move, $GLOBALS['db']);
101 // now that all tables exist, create all the accumulated constraints
102 if (! $_error && count($sqlConstratints) > 0) {
103 PMA_createAllAccumulatedConstraints($sqlConstratints);
105 unset($sqlConstratints);
107 if (PMA_MYSQL_INT_VERSION
>= 50100) {
108 // here DELIMITER is not used because it's not part of the
109 // language; each statement is sent one by one
111 PMA_runEventDefinitionsForDb($GLOBALS['db']);
114 // go back to current db, just in case
115 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
117 // Duplicate the bookmarks for this db (done once for each db)
118 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
120 if (! $_error && $move) {
121 if (isset($_REQUEST['adjust_privileges'])
122 && ! empty($_REQUEST['adjust_privileges'])
124 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
128 * cleanup pmadb stuff for this db
130 include_once 'libraries/relation_cleanup.lib.php';
131 PMA_relationsCleanupDatabase($GLOBALS['db']);
133 // if someday the RENAME DATABASE reappears, do not DROP
134 $local_query = 'DROP DATABASE '
135 . PMA\libraries\Util
::backquote($GLOBALS['db']) . ';';
136 $sql_query .= "\n" . $local_query;
137 $GLOBALS['dbi']->query($local_query);
139 $message = PMA\libraries\Message
::success(
140 __('Database %1$s has been renamed to %2$s.')
142 $message->addParam($GLOBALS['db']);
143 $message->addParam($_REQUEST['newname']);
144 } elseif (! $_error) {
145 if (isset($_REQUEST['adjust_privileges'])
146 && ! empty($_REQUEST['adjust_privileges'])
148 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
151 $message = PMA\libraries\Message
::success(
152 __('Database %1$s has been copied to %2$s.')
154 $message->addParam($GLOBALS['db']);
155 $message->addParam($_REQUEST['newname']);
157 $message = PMA\libraries\Message
::error();
161 /* Change database to be used */
162 if (! $_error && $move) {
163 $GLOBALS['db'] = $_REQUEST['newname'];
164 } elseif (! $_error) {
165 if (isset($_REQUEST['switch_to_new'])
166 && $_REQUEST['switch_to_new'] == 'true'
168 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
169 $GLOBALS['db'] = $_REQUEST['newname'];
171 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
177 * Database has been successfully renamed/moved. If in an Ajax request,
178 * generate the output with {@link PMA\libraries\Response} and exit
180 if ($GLOBALS['is_ajax_request'] == true) {
181 $response = PMA\libraries\Response
::getInstance();
182 $response->setRequestStatus($message->isSuccess());
183 $response->addJSON('message', $message);
184 $response->addJSON('newname', $_REQUEST['newname']);
187 PMA\libraries\Util
::getMessage(null, $sql_query)
189 $response->addJSON('db', $GLOBALS['db']);
195 * Settings for relations stuff
198 $cfgRelation = PMA_getRelationsParam();
201 * Check if comments were updated
202 * (must be done before displaying the menu tabs)
204 if (isset($_REQUEST['comment'])) {
205 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
208 require 'libraries/db_common.inc.php';
209 $url_query .= '&goto=db_operations.php';
211 // Gets the database structure
212 $sub_part = '_structure';
220 $db_is_system_schema,
224 ) = PMA\libraries\Util
::getDbInfo($db, isset($sub_part) ?
$sub_part : '');
228 if (isset($message)) {
229 echo PMA\libraries\Util
::getMessage($message, $sql_query);
233 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
234 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
236 $response->addHTML('<div id="boxContainer" data-box-width="300">');
238 if (!$is_information_schema) {
239 if ($cfgRelation['commwork']) {
243 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
246 $response->addHTML('<div class="operations_half_width">');
247 $response->addHTML(PMA_getHtmlForCreateTable($db));
248 $response->addHTML('</div>');
253 if ($GLOBALS['db'] != 'mysql') {
254 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
257 // Drop link if allowed
258 // Don't even try to drop information_schema.
259 // You won't be able to. Believe me. You won't.
260 // Don't allow to easily drop mysql database, RFE #1327514.
261 if (($is_superuser ||
$GLOBALS['cfg']['AllowUserDropDatabase'])
262 && ! $db_is_system_schema
263 && $GLOBALS['db'] != 'mysql'
265 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
270 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
273 * Change database charset
275 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
277 if (! $cfgRelation['allworks']
278 && $cfg['PmaNoRelation_DisableWarning'] == false
280 $message = PMA\libraries\Message
::notice(
282 'The phpMyAdmin configuration storage has been deactivated. ' .
288 . './chk_rel.php' . $url_query . '">',
291 $message->addParam('</a>', false);
292 /* Show error if user has configured something, notice elsewhere */
293 if (!empty($cfg['Servers'][$server]['pmadb'])) {
294 $message->isError(true);
297 } // end if (!$is_information_schema)
299 $response->addHTML('</div>');
301 // not sure about displaying the PDF dialog in case db is information_schema
302 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
303 // We only show this if we find something in the new pdf_pages table
306 FROM ' . PMA\libraries\Util
::backquote($GLOBALS['cfgRelation']['db'])
307 . '.' . PMA\libraries\Util
::backquote($cfgRelation['pdf_pages']) . '
308 WHERE db_name = \'' . PMA\libraries\Util
::sqlAddSlashes($GLOBALS['db'])
310 $test_rs = PMA_queryAsControlUser(
313 PMA\libraries\DatabaseInterface
::QUERY_STORE