Translated using Weblate (Afrikaans)
[phpmyadmin.git] / db_operations.php
blob7f0e2b9df52ec60cbf684d653594a4134503e476
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
15 /**
16 * requirements
18 require_once 'libraries/common.inc.php';
19 require_once 'libraries/mysql_charsets.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_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 /** @var PMA_String $pmaString */
40 $pmaString = $GLOBALS['PMA_String'];
41 if (/*overload*/mb_strlen($GLOBALS['db'])
42 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
43 ) {
44 if (! empty($_REQUEST['db_rename'])) {
45 $move = true;
46 } else {
47 $move = false;
50 if (! isset($_REQUEST['newname'])
51 || ! /*overload*/mb_strlen($_REQUEST['newname'])
52 ) {
53 $message = PMA_Message::error(__('The database name is empty!'));
54 } else {
55 $_error = false;
56 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
57 PMA_createDbBeforeCopy();
60 // here I don't use DELIMITER because it's not part of the
61 // language; I have to send each statement one by one
63 // to avoid selecting alternatively the current and new db
64 // we would need to modify the CREATE definitions to qualify
65 // the db name
66 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
68 // go back to current db, just in case
69 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
71 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
73 include_once "libraries/plugin_interface.lib.php";
74 // remove all foreign key constraints, otherwise we can get errors
75 /* @var $export_sql_plugin ExportSql */
76 $export_sql_plugin = PMA_getPlugin(
77 "export",
78 "sql",
79 'libraries/plugins/export/',
80 array(
81 'single_table' => isset($single_table),
82 'export_type' => 'database'
86 // create stand-in tables for views
87 $views = PMA_getViewsAndCreateSqlViewStandIn(
88 $tables_full, $export_sql_plugin, $GLOBALS['db']
91 // copy tables
92 $sqlConstratints = PMA_copyTables(
93 $tables_full, $move, $GLOBALS['db']
96 // handle the views
97 if (! $_error) {
98 PMA_handleTheViews($views, $move, $GLOBALS['db']);
100 unset($views);
102 // now that all tables exist, create all the accumulated constraints
103 if (! $_error && count($sqlConstratints) > 0) {
104 PMA_createAllAccumulatedConstraints($sqlConstratints);
106 unset($sqlConstratints);
108 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
109 // here DELIMITER is not used because it's not part of the
110 // language; each statement is sent one by one
112 PMA_runEventDefinitionsForDb($GLOBALS['db']);
115 // go back to current db, just in case
116 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
118 // Duplicate the bookmarks for this db (done once for each db)
119 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
121 if (! $_error && $move) {
122 if (isset($_REQUEST['adjust_privileges'])
123 && ! empty($_REQUEST['adjust_privileges'])
125 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
129 * cleanup pmadb stuff for this db
131 include_once 'libraries/relation_cleanup.lib.php';
132 PMA_relationsCleanupDatabase($GLOBALS['db']);
134 // if someday the RENAME DATABASE reappears, do not DROP
135 $local_query = 'DROP DATABASE '
136 . PMA_Util::backquote($GLOBALS['db']) . ';';
137 $sql_query .= "\n" . $local_query;
138 $GLOBALS['dbi']->query($local_query);
140 $message = PMA_Message::success(
141 __('Database %1$s has been renamed to %2$s.')
143 $message->addParam($GLOBALS['db']);
144 $message->addParam($_REQUEST['newname']);
145 } elseif (! $_error) {
146 if (isset($_REQUEST['adjust_privileges'])
147 && ! empty($_REQUEST['adjust_privileges'])
149 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
152 $message = PMA_Message::success(
153 __('Database %1$s has been copied to %2$s.')
155 $message->addParam($GLOBALS['db']);
156 $message->addParam($_REQUEST['newname']);
157 } else {
158 $message = PMA_Message::error();
160 $reload = true;
162 /* Change database to be used */
163 if (! $_error && $move) {
164 $GLOBALS['db'] = $_REQUEST['newname'];
165 } elseif (! $_error) {
166 if (isset($_REQUEST['switch_to_new'])
167 && $_REQUEST['switch_to_new'] == 'true'
169 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
170 $GLOBALS['db'] = $_REQUEST['newname'];
171 } else {
172 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
178 * Database has been successfully renamed/moved. If in an Ajax request,
179 * generate the output with {@link PMA_Response} and exit
181 if ($GLOBALS['is_ajax_request'] == true) {
182 $response = PMA_Response::getInstance();
183 $response->isSuccess($message->isSuccess());
184 $response->addJSON('message', $message);
185 $response->addJSON('newname', $_REQUEST['newname']);
186 $response->addJSON(
187 'sql_query',
188 PMA_Util::getMessage(null, $sql_query)
190 $response->addJSON('db', $GLOBALS['db']);
191 exit;
196 * Settings for relations stuff
199 $cfgRelation = PMA_getRelationsParam();
202 * Check if comments were updated
203 * (must be done before displaying the menu tabs)
205 if (isset($_REQUEST['comment'])) {
206 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
209 require 'libraries/db_common.inc.php';
210 $url_query .= '&amp;goto=db_operations.php';
212 // Gets the database structure
213 $sub_part = '_structure';
215 list(
216 $tables,
217 $num_tables,
218 $total_num_tables,
219 $sub_part,
220 $is_show_stats,
221 $db_is_system_schema,
222 $tooltip_truename,
223 $tooltip_aliasname,
224 $pos
225 ) = PMA_Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
227 echo "\n";
229 if (isset($message)) {
230 echo PMA_Util::getMessage($message, $sql_query);
231 unset($message);
234 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
235 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
237 $response->addHTML('<div id="boxContainer" data-box-width="300">');
239 if (!$is_information_schema) {
240 if ($cfgRelation['commwork']) {
242 * database comment
244 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
247 $response->addHTML('<div class="operations_half_width">');
248 $response->addHTML(PMA_getHtmlForCreateTable($db));
249 $response->addHTML('</div>');
252 * rename database
254 if ($GLOBALS['db'] != 'mysql') {
255 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
258 // Drop link if allowed
259 // Don't even try to drop information_schema.
260 // You won't be able to. Believe me. You won't.
261 // Don't allow to easily drop mysql database, RFE #1327514.
262 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
263 && ! $db_is_system_schema
264 && (PMA_DRIZZLE || $GLOBALS['db'] != 'mysql')
266 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
269 * Copy database
271 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
274 * Change database charset
276 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
278 if (! $cfgRelation['allworks']
279 && $cfg['PmaNoRelation_DisableWarning'] == false
281 $message = PMA_Message::notice(
283 'The phpMyAdmin configuration storage has been deactivated. ' .
284 '%sFind out why%s.'
287 $message->addParam(
288 '<a href="' . $cfg['PmaAbsoluteUri']
289 . 'chk_rel.php' . $url_query . '">',
290 false
292 $message->addParam('</a>', false);
293 /* Show error if user has configured something, notice elsewhere */
294 if (!empty($cfg['Servers'][$server]['pmadb'])) {
295 $message->isError(true);
297 } // end if
298 } // end if (!$is_information_schema)
300 $response->addHTML('</div>');
302 // not sure about displaying the PDF dialog in case db is information_schema
303 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
304 // We only show this if we find something in the new pdf_pages table
305 $test_query = '
306 SELECT *
307 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
308 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
309 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\'';
310 $test_rs = PMA_queryAsControlUser(
311 $test_query,
312 false,
313 PMA_DatabaseInterface::QUERY_STORE
315 } // end if