Add SJIS-win to default list of allowed charsets
[phpmyadmin.git] / db_operations.php
blobb6222058c755f59022575e593d804bdfab032f15
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * handles miscellaneous db operations:
5 * - move/rename
6 * - copy
7 * - changing collation
8 * - changing comment
9 * - adding tables
10 * - viewing PDF schemas
12 * @package PhpMyAdmin
14 use PMA\libraries\Response;
15 use PMA\libraries\plugins\export\ExportSql;
17 /**
18 * requirements
20 require_once 'libraries/common.inc.php';
21 require_once 'libraries/display_create_table.lib.php';
23 /**
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 = Response::getInstance();
31 $header = $response->getHeader();
32 $scripts = $header->getScripts();
33 $scripts->addFile('db_operations.js');
35 $sql_query = '';
37 /**
38 * Rename/move or copy database
40 if (strlen($GLOBALS['db']) > 0
41 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
42 ) {
43 if (! empty($_REQUEST['db_rename'])) {
44 $move = true;
45 } else {
46 $move = false;
49 if (! isset($_REQUEST['newname']) || strlen($_REQUEST['newname']) === 0) {
50 $message = PMA\libraries\Message::error(__('The database name is empty!'));
51 } else {
52 // lower_case_table_names=1 `DB` becomes `db`
53 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
54 $_REQUEST['newname'] = mb_strtolower(
55 $_REQUEST['newname']
59 if ($_REQUEST['newname'] === $_REQUEST['db']) {
60 $message = PMA\libraries\Message::error(
61 __('Cannot copy database to the same name. Change the name and try again.')
63 } else {
64 $_error = false;
65 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
66 PMA_createDbBeforeCopy();
69 // here I don't use DELIMITER because it's not part of the
70 // language; I have to send each statement one by one
72 // to avoid selecting alternatively the current and new db
73 // we would need to modify the CREATE definitions to qualify
74 // the db name
75 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
77 // go back to current db, just in case
78 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
80 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
82 include_once "libraries/plugin_interface.lib.php";
83 // remove all foreign key constraints, otherwise we can get errors
84 /* @var $export_sql_plugin ExportSql */
85 $export_sql_plugin = PMA_getPlugin(
86 "export",
87 "sql",
88 'libraries/plugins/export/',
89 array(
90 'single_table' => isset($single_table),
91 'export_type' => 'database'
95 // create stand-in tables for views
96 $views = PMA_getViewsAndCreateSqlViewStandIn(
97 $tables_full, $export_sql_plugin, $GLOBALS['db']
100 // copy tables
101 $sqlConstratints = PMA_copyTables(
102 $tables_full, $move, $GLOBALS['db']
105 // handle the views
106 if (! $_error) {
107 PMA_handleTheViews($views, $move, $GLOBALS['db']);
109 unset($views);
111 // now that all tables exist, create all the accumulated constraints
112 if (! $_error && count($sqlConstratints) > 0) {
113 PMA_createAllAccumulatedConstraints($sqlConstratints);
115 unset($sqlConstratints);
117 if (PMA_MYSQL_INT_VERSION >= 50100) {
118 // here DELIMITER is not used because it's not part of the
119 // language; each statement is sent one by one
121 PMA_runEventDefinitionsForDb($GLOBALS['db']);
124 // go back to current db, just in case
125 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
127 // Duplicate the bookmarks for this db (done once for each db)
128 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
130 if (! $_error && $move) {
131 if (isset($_REQUEST['adjust_privileges'])
132 && ! empty($_REQUEST['adjust_privileges'])
134 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
138 * cleanup pmadb stuff for this db
140 include_once 'libraries/relation_cleanup.lib.php';
141 PMA_relationsCleanupDatabase($GLOBALS['db']);
143 // if someday the RENAME DATABASE reappears, do not DROP
144 $local_query = 'DROP DATABASE '
145 . PMA\libraries\Util::backquote($GLOBALS['db']) . ';';
146 $sql_query .= "\n" . $local_query;
147 $GLOBALS['dbi']->query($local_query);
149 $message = PMA\libraries\Message::success(
150 __('Database %1$s has been renamed to %2$s.')
152 $message->addParam($GLOBALS['db']);
153 $message->addParam($_REQUEST['newname']);
154 } elseif (! $_error) {
155 if (isset($_REQUEST['adjust_privileges'])
156 && ! empty($_REQUEST['adjust_privileges'])
158 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
161 $message = PMA\libraries\Message::success(
162 __('Database %1$s has been copied to %2$s.')
164 $message->addParam($GLOBALS['db']);
165 $message->addParam($_REQUEST['newname']);
166 } else {
167 $message = PMA\libraries\Message::error();
169 $reload = true;
171 /* Change database to be used */
172 if (! $_error && $move) {
173 $GLOBALS['db'] = $_REQUEST['newname'];
174 } elseif (! $_error) {
175 if (isset($_REQUEST['switch_to_new'])
176 && $_REQUEST['switch_to_new'] == 'true'
178 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
179 $GLOBALS['db'] = $_REQUEST['newname'];
180 } else {
181 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
188 * Database has been successfully renamed/moved. If in an Ajax request,
189 * generate the output with {@link PMA\libraries\Response} and exit
191 if ($response->isAjax()) {
192 $response->setRequestStatus($message->isSuccess());
193 $response->addJSON('message', $message);
194 $response->addJSON('newname', $_REQUEST['newname']);
195 $response->addJSON(
196 'sql_query',
197 PMA\libraries\Util::getMessage(null, $sql_query)
199 $response->addJSON('db', $GLOBALS['db']);
200 exit;
205 * Settings for relations stuff
208 $cfgRelation = PMA_getRelationsParam();
211 * Check if comments were updated
212 * (must be done before displaying the menu tabs)
214 if (isset($_REQUEST['comment'])) {
215 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
218 require 'libraries/db_common.inc.php';
219 $url_query .= '&amp;goto=db_operations.php';
221 // Gets the database structure
222 $sub_part = '_structure';
224 list(
225 $tables,
226 $num_tables,
227 $total_num_tables,
228 $sub_part,
229 $is_show_stats,
230 $db_is_system_schema,
231 $tooltip_truename,
232 $tooltip_aliasname,
233 $pos
234 ) = PMA\libraries\Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
236 echo "\n";
238 if (isset($message)) {
239 echo PMA\libraries\Util::getMessage($message, $sql_query);
240 unset($message);
243 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
244 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
246 $response->addHTML('<div id="boxContainer" data-box-width="300">');
248 if (!$is_information_schema) {
249 if ($cfgRelation['commwork']) {
251 * database comment
253 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
256 $response->addHTML('<div class="operations_half_width">');
257 $response->addHTML(PMA_getHtmlForCreateTable($db));
258 $response->addHTML('</div>');
261 * rename database
263 if ($GLOBALS['db'] != 'mysql') {
264 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
267 // Drop link if allowed
268 // Don't even try to drop information_schema.
269 // You won't be able to. Believe me. You won't.
270 // Don't allow to easily drop mysql database, RFE #1327514.
271 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
272 && ! $db_is_system_schema
273 && $GLOBALS['db'] != 'mysql'
275 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
278 * Copy database
280 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
283 * Change database charset
285 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
287 if (! $cfgRelation['allworks']
288 && $cfg['PmaNoRelation_DisableWarning'] == false
290 $message = PMA\libraries\Message::notice(
292 'The phpMyAdmin configuration storage has been deactivated. ' .
293 '%sFind out why%s.'
296 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
297 $message->addParamHtml('</a>');
298 /* Show error if user has configured something, notice elsewhere */
299 if (!empty($cfg['Servers'][$server]['pmadb'])) {
300 $message->isError(true);
302 } // end if
303 } // end if (!$is_information_schema)
305 $response->addHTML('</div>');
307 // not sure about displaying the PDF dialog in case db is information_schema
308 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
309 // We only show this if we find something in the new pdf_pages table
310 $test_query = '
311 SELECT *
312 FROM ' . PMA\libraries\Util::backquote($GLOBALS['cfgRelation']['db'])
313 . '.' . PMA\libraries\Util::backquote($cfgRelation['pdf_pages']) . '
314 WHERE db_name = \'' . $GLOBALS['dbi']->escapeString($GLOBALS['db'])
315 . '\'';
316 $test_rs = PMA_queryAsControlUser(
317 $test_query,
318 false,
319 PMA\libraries\DatabaseInterface::QUERY_STORE
321 } // end if