Translated using Weblate (Belarusian)
[phpmyadmin.git] / db_operations.php
bloba934fc43aa0b1e45421eee764faf35b27274f9d5
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\Response;
15 use PMA\libraries\plugins\export\ExportSql;
17 /**
18 * requirements
20 require_once 'libraries/common.inc.php';
21 require_once 'libraries/display_create_table.lib.php';
23 /**
24 * functions implementation for this script
26 require_once 'libraries/check_user_privileges.lib.php';
27 require_once 'libraries/operations.lib.php';
29 // add a javascript file for jQuery functions to handle Ajax actions
30 $response = Response::getInstance();
31 $header = $response->getHeader();
32 $scripts = $header->getScripts();
33 $scripts->addFile('db_operations.js');
35 $sql_query = '';
37 /**
38 * Rename/move or copy database
40 if (strlen($GLOBALS['db']) > 0
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']) || strlen($_REQUEST['newname']) === 0) {
50 $message = PMA\libraries\Message::error(__('The database name is empty!'));
51 } else {
52 $_error = false;
53 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
54 PMA_createDbBeforeCopy();
57 // here I don't use DELIMITER because it's not part of the
58 // language; I have to send each statement one by one
60 // to avoid selecting alternatively the current and new db
61 // we would need to modify the CREATE definitions to qualify
62 // the db name
63 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
65 // go back to current db, just in case
66 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
68 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
70 include_once "libraries/plugin_interface.lib.php";
71 // remove all foreign key constraints, otherwise we can get errors
72 /* @var $export_sql_plugin ExportSql */
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_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\libraries\Util::backquote($GLOBALS['db']) . ';';
134 $sql_query .= "\n" . $local_query;
135 $GLOBALS['dbi']->query($local_query);
137 $message = PMA\libraries\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\libraries\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\libraries\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\libraries\Response} and exit
178 if ($response->isAjax()) {
179 $response->setRequestStatus($message->isSuccess());
180 $response->addJSON('message', $message);
181 $response->addJSON('newname', $_REQUEST['newname']);
182 $response->addJSON(
183 'sql_query',
184 PMA\libraries\Util::getMessage(null, $sql_query)
186 $response->addJSON('db', $GLOBALS['db']);
187 exit;
192 * Settings for relations stuff
195 $cfgRelation = PMA_getRelationsParam();
198 * Check if comments were updated
199 * (must be done before displaying the menu tabs)
201 if (isset($_REQUEST['comment'])) {
202 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
205 require 'libraries/db_common.inc.php';
206 $url_query .= '&amp;goto=db_operations.php';
208 // Gets the database structure
209 $sub_part = '_structure';
211 list(
212 $tables,
213 $num_tables,
214 $total_num_tables,
215 $sub_part,
216 $is_show_stats,
217 $db_is_system_schema,
218 $tooltip_truename,
219 $tooltip_aliasname,
220 $pos
221 ) = PMA\libraries\Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
223 echo "\n";
225 if (isset($message)) {
226 echo PMA\libraries\Util::getMessage($message, $sql_query);
227 unset($message);
230 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
231 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
233 $response->addHTML('<div id="boxContainer" data-box-width="300">');
235 if (!$is_information_schema) {
236 if ($cfgRelation['commwork']) {
238 * database comment
240 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
243 $response->addHTML('<div class="operations_half_width">');
244 $response->addHTML(PMA_getHtmlForCreateTable($db));
245 $response->addHTML('</div>');
248 * rename database
250 if ($GLOBALS['db'] != 'mysql') {
251 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
254 // Drop link if allowed
255 // Don't even try to drop information_schema.
256 // You won't be able to. Believe me. You won't.
257 // Don't allow to easily drop mysql database, RFE #1327514.
258 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
259 && ! $db_is_system_schema
260 && $GLOBALS['db'] != 'mysql'
262 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
265 * Copy database
267 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
270 * Change database charset
272 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
274 if (! $cfgRelation['allworks']
275 && $cfg['PmaNoRelation_DisableWarning'] == false
277 $message = PMA\libraries\Message::notice(
279 'The phpMyAdmin configuration storage has been deactivated. ' .
280 '%sFind out why%s.'
283 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
284 $message->addParamHtml('</a>');
285 /* Show error if user has configured something, notice elsewhere */
286 if (!empty($cfg['Servers'][$server]['pmadb'])) {
287 $message->isError(true);
289 } // end if
290 } // end if (!$is_information_schema)
292 $response->addHTML('</div>');
294 // not sure about displaying the PDF dialog in case db is information_schema
295 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
296 // We only show this if we find something in the new pdf_pages table
297 $test_query = '
298 SELECT *
299 FROM ' . PMA\libraries\Util::backquote($GLOBALS['cfgRelation']['db'])
300 . '.' . PMA\libraries\Util::backquote($cfgRelation['pdf_pages']) . '
301 WHERE db_name = \'' . $GLOBALS['dbi']->escapeString($GLOBALS['db'])
302 . '\'';
303 $test_rs = PMA_queryAsControlUser(
304 $test_query,
305 false,
306 PMA\libraries\DatabaseInterface::QUERY_STORE
308 } // end if