Translated using Weblate (Norwegian Bokmål)
[phpmyadmin.git] / db_operations.php
blob3ca6cd69866fe20103354affbe23b57dfa93baf6
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';
21 /**
22 * functions implementation for this script
24 require_once 'libraries/operations.lib.php';
26 // add a javascript file for jQuery functions to handle Ajax actions
27 $response = PMA_Response::getInstance();
28 $header = $response->getHeader();
29 $scripts = $header->getScripts();
30 $scripts->addFile('db_operations.js');
32 $sql_query = '';
34 /**
35 * Rename/move or copy database
37 /** @var PMA_String $pmaString */
38 $pmaString = $GLOBALS['PMA_String'];
39 if (/*overload*/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 || ! /*overload*/mb_strlen($_REQUEST['newname'])
50 ) {
51 $message = PMA_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 $export_sql_plugin = PMA_getPlugin(
74 "export",
75 "sql",
76 'libraries/plugins/export/',
77 array(
78 'single_table' => isset($single_table),
79 'export_type' => 'database'
83 // create stand-in tables for views
84 $views = PMA_getViewsAndCreateSqlViewStandIn(
85 $tables_full, $export_sql_plugin, $GLOBALS['db']
88 // copy tables
89 $sqlConstratints = PMA_copyTables(
90 $tables_full, $move, $GLOBALS['db']
93 // handle the views
94 if (! $_error) {
95 PMA_handleTheViews($views, $move, $GLOBALS['db']);
97 unset($views);
99 // now that all tables exist, create all the accumulated constraints
100 if (! $_error && count($sqlConstratints) > 0) {
101 PMA_createAllAccumulatedConstraints($sqlConstratints);
103 unset($sqlConstratints);
105 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
106 // here DELIMITER is not used because it's not part of the
107 // language; each statement is sent one by one
109 PMA_runEventDefinitionsForDb($GLOBALS['db']);
112 // go back to current db, just in case
113 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
115 // Duplicate the bookmarks for this db (done once for each db)
116 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
118 if (! $_error && $move) {
119 if (isset($_REQUEST['adjust_privileges'])
120 && ! empty($_REQUEST['adjust_privileges'])
122 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
126 * cleanup pmadb stuff for this db
128 include_once 'libraries/relation_cleanup.lib.php';
129 PMA_relationsCleanupDatabase($GLOBALS['db']);
131 // if someday the RENAME DATABASE reappears, do not DROP
132 $local_query = 'DROP DATABASE '
133 . PMA_Util::backquote($GLOBALS['db']) . ';';
134 $sql_query .= "\n" . $local_query;
135 $GLOBALS['dbi']->query($local_query);
137 $message = PMA_Message::success(
138 __('Database %1$s has been renamed to %2$s.')
140 $message->addParam($GLOBALS['db']);
141 $message->addParam($_REQUEST['newname']);
142 } elseif (! $_error) {
143 if (isset($_REQUEST['adjust_privileges'])
144 && ! empty($_REQUEST['adjust_privileges'])
146 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
149 $message = PMA_Message::success(
150 __('Database %1$s has been copied to %2$s.')
152 $message->addParam($GLOBALS['db']);
153 $message->addParam($_REQUEST['newname']);
154 } else {
155 $message = PMA_Message::error();
157 $reload = true;
159 /* Change database to be used */
160 if (! $_error && $move) {
161 $GLOBALS['db'] = $_REQUEST['newname'];
162 } elseif (! $_error) {
163 if (isset($_REQUEST['switch_to_new'])
164 && $_REQUEST['switch_to_new'] == 'true'
166 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
167 $GLOBALS['db'] = $_REQUEST['newname'];
168 } else {
169 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
175 * Database has been successfully renamed/moved. If in an Ajax request,
176 * generate the output with {@link PMA_Response} and exit
178 if ($GLOBALS['is_ajax_request'] == true) {
179 $response = PMA_Response::getInstance();
180 $response->isSuccess($message->isSuccess());
181 $response->addJSON('message', $message);
182 $response->addJSON('newname', $_REQUEST['newname']);
183 $response->addJSON(
184 'sql_query',
185 PMA_Util::getMessage(null, $sql_query)
187 $response->addJSON('db', $GLOBALS['db']);
188 exit;
193 * Settings for relations stuff
196 $cfgRelation = PMA_getRelationsParam();
199 * Check if comments were updated
200 * (must be done before displaying the menu tabs)
202 if (isset($_REQUEST['comment'])) {
203 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
206 require 'libraries/db_common.inc.php';
207 $url_query .= '&amp;goto=db_operations.php';
209 // Gets the database structure
210 $sub_part = '_structure';
211 require 'libraries/db_info.inc.php';
212 echo "\n";
214 if (isset($message)) {
215 echo PMA_Util::getMessage($message, $sql_query);
216 unset($message);
219 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
220 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
222 $response->addHTML('<div id="boxContainer" data-box-width="300">');
224 if (!$is_information_schema) {
225 if ($cfgRelation['commwork']) {
227 * database comment
229 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
232 $response->addHTML('<div class="operations_half_width">');
233 ob_start();
234 include 'libraries/display_create_table.lib.php';
235 $content = ob_get_contents();
236 ob_end_clean();
237 $response->addHTML($content);
238 $response->addHTML('</div>');
241 * rename database
243 if ($GLOBALS['db'] != 'mysql') {
244 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
247 // Drop link if allowed
248 // Don't even try to drop information_schema.
249 // You won't be able to. Believe me. You won't.
250 // Don't allow to easily drop mysql database, RFE #1327514.
251 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
252 && ! $db_is_system_schema
253 && (PMA_DRIZZLE || $GLOBALS['db'] != 'mysql')
255 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
258 * Copy database
260 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
263 * Change database charset
265 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
267 if (! $cfgRelation['allworks']
268 && $cfg['PmaNoRelation_DisableWarning'] == false
270 $message = PMA_Message::notice(
271 __('The phpMyAdmin configuration storage has been deactivated. %sFind out why%s.')
273 $message->addParam(
274 '<a href="' . $cfg['PmaAbsoluteUri']
275 . 'chk_rel.php' . $url_query . '">',
276 false
278 $message->addParam('</a>', false);
279 /* Show error if user has configured something, notice elsewhere */
280 if (!empty($cfg['Servers'][$server]['pmadb'])) {
281 $message->isError(true);
283 } // end if
284 } // end if (!$is_information_schema)
286 $response->addHTML('</div>');
288 // not sure about displaying the PDF dialog in case db is information_schema
289 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
290 // We only show this if we find something in the new pdf_pages table
291 $test_query = '
292 SELECT *
293 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
294 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
295 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\'';
296 $test_rs = PMA_queryAsControlUser(
297 $test_query,
298 false,
299 PMA_DatabaseInterface::QUERY_STORE
301 } // end if