Translated using Weblate (Italian)
[phpmyadmin.git] / db_operations.php
blob487a4a97003ee14f7e14b3d052027dc5ab9e8726
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/check_user_privileges.lib.php';
25 require_once 'libraries/operations.lib.php';
27 // add a javascript file for jQuery functions to handle Ajax actions
28 $response = PMA_Response::getInstance();
29 $header = $response->getHeader();
30 $scripts = $header->getScripts();
31 $scripts->addFile('db_operations.js');
33 $sql_query = '';
35 /**
36 * Rename/move or copy database
38 /** @var PMA_String $pmaString */
39 $pmaString = $GLOBALS['PMA_String'];
40 if (/*overload*/mb_strlen($GLOBALS['db'])
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'])
50 || ! /*overload*/mb_strlen($_REQUEST['newname'])
51 ) {
52 $message = PMA_Message::error(__('The database name is empty!'));
53 } else {
54 $_error = false;
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
64 // the db name
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(
76 "export",
77 "sql",
78 'libraries/plugins/export/',
79 array(
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']
90 // copy tables
91 $sqlConstratints = PMA_copyTables(
92 $tables_full, $move, $GLOBALS['db']
95 // handle the views
96 if (! $_error) {
97 PMA_handleTheViews($views, $move, $GLOBALS['db']);
99 unset($views);
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_DRIZZLE && 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_Util::backquote($GLOBALS['db']) . ';';
136 $sql_query .= "\n" . $local_query;
137 $GLOBALS['dbi']->query($local_query);
139 $message = PMA_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_Message::success(
152 __('Database %1$s has been copied to %2$s.')
154 $message->addParam($GLOBALS['db']);
155 $message->addParam($_REQUEST['newname']);
156 } else {
157 $message = PMA_Message::error();
159 $reload = true;
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'];
170 } else {
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_Response} and exit
180 if ($GLOBALS['is_ajax_request'] == true) {
181 $response = PMA_Response::getInstance();
182 $response->isSuccess($message->isSuccess());
183 $response->addJSON('message', $message);
184 $response->addJSON('newname', $_REQUEST['newname']);
185 $response->addJSON(
186 'sql_query',
187 PMA_Util::getMessage(null, $sql_query)
189 $response->addJSON('db', $GLOBALS['db']);
190 exit;
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 .= '&amp;goto=db_operations.php';
211 // Gets the database structure
212 $sub_part = '_structure';
213 require 'libraries/db_info.inc.php';
214 echo "\n";
216 if (isset($message)) {
217 echo PMA_Util::getMessage($message, $sql_query);
218 unset($message);
221 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
222 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
224 $response->addHTML('<div id="boxContainer" data-box-width="300">');
226 if (!$is_information_schema) {
227 if ($cfgRelation['commwork']) {
229 * database comment
231 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
234 $response->addHTML('<div class="operations_half_width">');
235 ob_start();
236 include 'libraries/display_create_table.lib.php';
237 $content = ob_get_contents();
238 ob_end_clean();
239 $response->addHTML($content);
240 $response->addHTML('</div>');
243 * rename database
245 if ($GLOBALS['db'] != 'mysql') {
246 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
249 // Drop link if allowed
250 // Don't even try to drop information_schema.
251 // You won't be able to. Believe me. You won't.
252 // Don't allow to easily drop mysql database, RFE #1327514.
253 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
254 && ! $db_is_system_schema
255 && (PMA_DRIZZLE || $GLOBALS['db'] != 'mysql')
257 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
260 * Copy database
262 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
265 * Change database charset
267 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
269 if (! $cfgRelation['allworks']
270 && $cfg['PmaNoRelation_DisableWarning'] == false
272 $message = PMA_Message::notice(
274 'The phpMyAdmin configuration storage has been deactivated. ' .
275 '%sFind out why%s.'
278 $message->addParam(
279 '<a href="' . $cfg['PmaAbsoluteUri']
280 . 'chk_rel.php' . $url_query . '">',
281 false
283 $message->addParam('</a>', false);
284 /* Show error if user has configured something, notice elsewhere */
285 if (!empty($cfg['Servers'][$server]['pmadb'])) {
286 $message->isError(true);
288 } // end if
289 } // end if (!$is_information_schema)
291 $response->addHTML('</div>');
293 // not sure about displaying the PDF dialog in case db is information_schema
294 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
295 // We only show this if we find something in the new pdf_pages table
296 $test_query = '
297 SELECT *
298 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
299 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
300 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\'';
301 $test_rs = PMA_queryAsControlUser(
302 $test_query,
303 false,
304 PMA_DatabaseInterface::QUERY_STORE
306 } // end if