Translated using Weblate (Portuguese (Brazil))
[phpmyadmin.git] / db_operations.php
blobb236ba589dbb14cdc44a7fb61df449515b1c50d1
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\plugins\export\ExportSql;
16 /**
17 * requirements
19 require_once 'libraries/common.inc.php';
20 require_once 'libraries/display_create_table.lib.php';
22 /**
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.lib.php';
26 require_once 'libraries/operations.lib.php';
28 // add a javascript file for jQuery functions to handle Ajax actions
29 $response = PMA\libraries\Response::getInstance();
30 $header = $response->getHeader();
31 $scripts = $header->getScripts();
32 $scripts->addFile('db_operations.js');
34 $sql_query = '';
36 /**
37 * Rename/move or copy database
39 if (mb_strlen($GLOBALS['db'])
40 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
41 ) {
42 if (! empty($_REQUEST['db_rename'])) {
43 $move = true;
44 } else {
45 $move = false;
48 if (! isset($_REQUEST['newname'])
49 || ! mb_strlen($_REQUEST['newname'])
50 ) {
51 $message = PMA\libraries\Message::error(__('The database name is empty!'));
52 } else {
53 $_error = false;
54 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
55 PMA_createDbBeforeCopy();
58 // here I don't use DELIMITER because it's not part of the
59 // language; I have to send each statement one by one
61 // to avoid selecting alternatively the current and new db
62 // we would need to modify the CREATE definitions to qualify
63 // the db name
64 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
66 // go back to current db, just in case
67 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
69 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
71 include_once "libraries/plugin_interface.lib.php";
72 // remove all foreign key constraints, otherwise we can get errors
73 /* @var $export_sql_plugin ExportSql */
74 $export_sql_plugin = PMA_getPlugin(
75 "export",
76 "sql",
77 'libraries/plugins/export/',
78 array(
79 'single_table' => isset($single_table),
80 'export_type' => 'database'
84 // create stand-in tables for views
85 $views = PMA_getViewsAndCreateSqlViewStandIn(
86 $tables_full, $export_sql_plugin, $GLOBALS['db']
89 // copy tables
90 $sqlConstratints = PMA_copyTables(
91 $tables_full, $move, $GLOBALS['db']
94 // handle the views
95 if (! $_error) {
96 PMA_handleTheViews($views, $move, $GLOBALS['db']);
98 unset($views);
100 // now that all tables exist, create all the accumulated constraints
101 if (! $_error && count($sqlConstratints) > 0) {
102 PMA_createAllAccumulatedConstraints($sqlConstratints);
104 unset($sqlConstratints);
106 if (PMA_MYSQL_INT_VERSION >= 50100) {
107 // here DELIMITER is not used because it's not part of the
108 // language; each statement is sent one by one
110 PMA_runEventDefinitionsForDb($GLOBALS['db']);
113 // go back to current db, just in case
114 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
116 // Duplicate the bookmarks for this db (done once for each db)
117 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
119 if (! $_error && $move) {
120 if (isset($_REQUEST['adjust_privileges'])
121 && ! empty($_REQUEST['adjust_privileges'])
123 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
127 * cleanup pmadb stuff for this db
129 include_once 'libraries/relation_cleanup.lib.php';
130 PMA_relationsCleanupDatabase($GLOBALS['db']);
132 // if someday the RENAME DATABASE reappears, do not DROP
133 $local_query = 'DROP DATABASE '
134 . PMA\libraries\Util::backquote($GLOBALS['db']) . ';';
135 $sql_query .= "\n" . $local_query;
136 $GLOBALS['dbi']->query($local_query);
138 $message = PMA\libraries\Message::success(
139 __('Database %1$s has been renamed to %2$s.')
141 $message->addParam($GLOBALS['db']);
142 $message->addParam($_REQUEST['newname']);
143 } elseif (! $_error) {
144 if (isset($_REQUEST['adjust_privileges'])
145 && ! empty($_REQUEST['adjust_privileges'])
147 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
150 $message = PMA\libraries\Message::success(
151 __('Database %1$s has been copied to %2$s.')
153 $message->addParam($GLOBALS['db']);
154 $message->addParam($_REQUEST['newname']);
155 } else {
156 $message = PMA\libraries\Message::error();
158 $reload = true;
160 /* Change database to be used */
161 if (! $_error && $move) {
162 $GLOBALS['db'] = $_REQUEST['newname'];
163 } elseif (! $_error) {
164 if (isset($_REQUEST['switch_to_new'])
165 && $_REQUEST['switch_to_new'] == 'true'
167 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
168 $GLOBALS['db'] = $_REQUEST['newname'];
169 } else {
170 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
176 * Database has been successfully renamed/moved. If in an Ajax request,
177 * generate the output with {@link PMA\libraries\Response} and exit
179 if ($GLOBALS['is_ajax_request'] == true) {
180 $response = PMA\libraries\Response::getInstance();
181 $response->setRequestStatus($message->isSuccess());
182 $response->addJSON('message', $message);
183 $response->addJSON('newname', $_REQUEST['newname']);
184 $response->addJSON(
185 'sql_query',
186 PMA\libraries\Util::getMessage(null, $sql_query)
188 $response->addJSON('db', $GLOBALS['db']);
189 exit;
194 * Settings for relations stuff
197 $cfgRelation = PMA_getRelationsParam();
200 * Check if comments were updated
201 * (must be done before displaying the menu tabs)
203 if (isset($_REQUEST['comment'])) {
204 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
207 require 'libraries/db_common.inc.php';
208 $url_query .= '&amp;goto=db_operations.php';
210 // Gets the database structure
211 $sub_part = '_structure';
213 list(
214 $tables,
215 $num_tables,
216 $total_num_tables,
217 $sub_part,
218 $is_show_stats,
219 $db_is_system_schema,
220 $tooltip_truename,
221 $tooltip_aliasname,
222 $pos
223 ) = PMA\libraries\Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
225 echo "\n";
227 if (isset($message)) {
228 echo PMA\libraries\Util::getMessage($message, $sql_query);
229 unset($message);
232 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
233 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
235 $response->addHTML('<div id="boxContainer" data-box-width="300">');
237 if (!$is_information_schema) {
238 if ($cfgRelation['commwork']) {
240 * database comment
242 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
245 $response->addHTML('<div class="operations_half_width">');
246 $response->addHTML(PMA_getHtmlForCreateTable($db));
247 $response->addHTML('</div>');
250 * rename database
252 if ($GLOBALS['db'] != 'mysql') {
253 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
256 // Drop link if allowed
257 // Don't even try to drop information_schema.
258 // You won't be able to. Believe me. You won't.
259 // Don't allow to easily drop mysql database, RFE #1327514.
260 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
261 && ! $db_is_system_schema
262 && $GLOBALS['db'] != 'mysql'
264 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
267 * Copy database
269 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
272 * Change database charset
274 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
276 if (! $cfgRelation['allworks']
277 && $cfg['PmaNoRelation_DisableWarning'] == false
279 $message = PMA\libraries\Message::notice(
281 'The phpMyAdmin configuration storage has been deactivated. ' .
282 '%sFind out why%s.'
285 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
286 $message->addParamHtml('</a>');
287 /* Show error if user has configured something, notice elsewhere */
288 if (!empty($cfg['Servers'][$server]['pmadb'])) {
289 $message->isError(true);
291 } // end if
292 } // end if (!$is_information_schema)
294 $response->addHTML('</div>');
296 // not sure about displaying the PDF dialog in case db is information_schema
297 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
298 // We only show this if we find something in the new pdf_pages table
299 $test_query = '
300 SELECT *
301 FROM ' . PMA\libraries\Util::backquote($GLOBALS['cfgRelation']['db'])
302 . '.' . PMA\libraries\Util::backquote($cfgRelation['pdf_pages']) . '
303 WHERE db_name = \'' . PMA\libraries\Util::sqlAddSlashes($GLOBALS['db'])
304 . '\'';
305 $test_rs = PMA_queryAsControlUser(
306 $test_query,
307 false,
308 PMA\libraries\DatabaseInterface::QUERY_STORE
310 } // end if