Translated using Weblate (Frisian)
[phpmyadmin.git] / db_operations.php
blobc2e2b4b3dfd373d86d045e70788fce5f955b4abd
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) {
120 * cleanup pmadb stuff for this db
122 include_once 'libraries/relation_cleanup.lib.php';
123 PMA_relationsCleanupDatabase($GLOBALS['db']);
125 // if someday the RENAME DATABASE reappears, do not DROP
126 $local_query = 'DROP DATABASE '
127 . PMA_Util::backquote($GLOBALS['db']) . ';';
128 $sql_query .= "\n" . $local_query;
129 $GLOBALS['dbi']->query($local_query);
131 $message = PMA_Message::success(
132 __('Database %1$s has been renamed to %2$s.')
134 $message->addParam($GLOBALS['db']);
135 $message->addParam($_REQUEST['newname']);
136 } elseif (! $_error) {
137 $message = PMA_Message::success(
138 __('Database %1$s has been copied to %2$s.')
140 $message->addParam($GLOBALS['db']);
141 $message->addParam($_REQUEST['newname']);
142 } else {
143 $message = PMA_Message::error();
145 $reload = true;
147 /* Change database to be used */
148 if (! $_error && $move) {
149 $GLOBALS['db'] = $_REQUEST['newname'];
150 } elseif (! $_error) {
151 if (isset($_REQUEST['switch_to_new'])
152 && $_REQUEST['switch_to_new'] == 'true'
154 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
155 $GLOBALS['db'] = $_REQUEST['newname'];
156 } else {
157 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
163 * Database has been successfully renamed/moved. If in an Ajax request,
164 * generate the output with {@link PMA_Response} and exit
166 if ($GLOBALS['is_ajax_request'] == true) {
167 $response = PMA_Response::getInstance();
168 $response->isSuccess($message->isSuccess());
169 $response->addJSON('message', $message);
170 $response->addJSON('newname', $_REQUEST['newname']);
171 $response->addJSON(
172 'sql_query',
173 PMA_Util::getMessage(null, $sql_query)
175 $response->addJSON('db', $GLOBALS['db']);
176 exit;
181 * Settings for relations stuff
184 $cfgRelation = PMA_getRelationsParam();
187 * Check if comments were updated
188 * (must be done before displaying the menu tabs)
190 if (isset($_REQUEST['comment'])) {
191 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
194 require 'libraries/db_common.inc.php';
195 $url_query .= '&amp;goto=db_operations.php';
197 // Gets the database structure
198 $sub_part = '_structure';
199 require 'libraries/db_info.inc.php';
200 echo "\n";
202 if (isset($message)) {
203 echo PMA_Util::getMessage($message, $sql_query);
204 unset($message);
207 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
208 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
210 $response->addHTML('<div id="boxContainer" data-box-width="300">');
212 if (!$is_information_schema) {
213 if ($cfgRelation['commwork']) {
215 * database comment
217 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
220 $response->addHTML('<div class="operations_half_width">');
221 ob_start();
222 include 'libraries/display_create_table.lib.php';
223 $content = ob_get_contents();
224 ob_end_clean();
225 $response->addHTML($content);
226 $response->addHTML('</div>');
229 * rename database
231 if ($GLOBALS['db'] != 'mysql') {
232 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
235 // Drop link if allowed
236 // Don't even try to drop information_schema.
237 // You won't be able to. Believe me. You won't.
238 // Don't allow to easily drop mysql database, RFE #1327514.
239 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
240 && ! $db_is_system_schema
241 && (PMA_DRIZZLE || $GLOBALS['db'] != 'mysql')
243 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
246 * Copy database
248 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
251 * Change database charset
253 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
255 if (! $cfgRelation['allworks']
256 && $cfg['PmaNoRelation_DisableWarning'] == false
258 $message = PMA_Message::notice(
259 __('The phpMyAdmin configuration storage has been deactivated. %sFind out why%s.')
261 $message->addParam(
262 '<a href="' . $cfg['PmaAbsoluteUri']
263 . 'chk_rel.php' . $url_query . '">',
264 false
266 $message->addParam('</a>', false);
267 /* Show error if user has configured something, notice elsewhere */
268 if (!empty($cfg['Servers'][$server]['pmadb'])) {
269 $message->isError(true);
271 } // end if
272 } // end if (!$is_information_schema)
274 $response->addHTML('</div>');
276 // not sure about displaying the PDF dialog in case db is information_schema
277 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
278 // We only show this if we find something in the new pdf_pages table
279 $test_query = '
280 SELECT *
281 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
282 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
283 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\'';
284 $test_rs = PMA_queryAsControlUser(
285 $test_query,
286 false,
287 PMA_DatabaseInterface::QUERY_STORE
289 } // end if