Translated using Weblate (Czech)
[phpmyadmin.git] / db_operations.php
blobc5e99783b51be0ac0460a343847b94c7e2c60b80
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 PhpMyAdmin\DatabaseInterface;
15 use PhpMyAdmin\Display\CreateTable;
16 use PhpMyAdmin\Message;
17 use PhpMyAdmin\Operations;
18 use PhpMyAdmin\Plugins;
19 use PhpMyAdmin\Plugins\Export\ExportSql;
20 use PhpMyAdmin\Relation;
21 use PhpMyAdmin\RelationCleanup;
22 use PhpMyAdmin\Response;
23 use PhpMyAdmin\Util;
25 /**
26 * requirements
28 require_once 'libraries/common.inc.php';
30 /**
31 * functions implementation for this script
33 require_once 'libraries/check_user_privileges.inc.php';
35 // add a javascript file for jQuery functions to handle Ajax actions
36 $response = Response::getInstance();
37 $header = $response->getHeader();
38 $scripts = $header->getScripts();
39 $scripts->addFile('db_operations.js');
41 $sql_query = '';
43 $operations = new Operations();
45 /**
46 * Rename/move or copy database
48 if (strlen($GLOBALS['db']) > 0
49 && (! empty($_POST['db_rename']) || ! empty($_POST['db_copy']))
50 ) {
51 if (! empty($_POST['db_rename'])) {
52 $move = true;
53 } else {
54 $move = false;
57 if (! isset($_POST['newname']) || strlen($_POST['newname']) === 0) {
58 $message = Message::error(__('The database name is empty!'));
59 } else {
60 // lower_case_table_names=1 `DB` becomes `db`
61 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
62 $_POST['newname'] = mb_strtolower(
63 $_POST['newname']
67 if ($_POST['newname'] === $_REQUEST['db']) {
68 $message = Message::error(
69 __('Cannot copy database to the same name. Change the name and try again.')
71 } else {
72 $_error = false;
73 if ($move || ! empty($_POST['create_database_before_copying'])) {
74 $operations->createDbBeforeCopy();
77 // here I don't use DELIMITER because it's not part of the
78 // language; I have to send each statement one by one
80 // to avoid selecting alternatively the current and new db
81 // we would need to modify the CREATE definitions to qualify
82 // the db name
83 $operations->runProcedureAndFunctionDefinitions($GLOBALS['db']);
85 // go back to current db, just in case
86 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
88 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
90 // remove all foreign key constraints, otherwise we can get errors
91 /* @var $export_sql_plugin ExportSql */
92 $export_sql_plugin = Plugins::getPlugin(
93 "export",
94 "sql",
95 'libraries/classes/Plugins/Export/',
96 array(
97 'single_table' => isset($single_table),
98 'export_type' => 'database'
102 // create stand-in tables for views
103 $views = $operations->getViewsAndCreateSqlViewStandIn(
104 $tables_full, $export_sql_plugin, $GLOBALS['db']
107 // copy tables
108 $sqlConstratints = $operations->copyTables(
109 $tables_full, $move, $GLOBALS['db']
112 // handle the views
113 if (! $_error) {
114 $operations->handleTheViews($views, $move, $GLOBALS['db']);
116 unset($views);
118 // now that all tables exist, create all the accumulated constraints
119 if (! $_error && count($sqlConstratints) > 0) {
120 $operations->createAllAccumulatedConstraints($sqlConstratints);
122 unset($sqlConstratints);
124 if ($GLOBALS['dbi']->getVersion() >= 50100) {
125 // here DELIMITER is not used because it's not part of the
126 // language; each statement is sent one by one
128 $operations->runEventDefinitionsForDb($GLOBALS['db']);
131 // go back to current db, just in case
132 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
134 // Duplicate the bookmarks for this db (done once for each db)
135 $operations->duplicateBookmarks($_error, $GLOBALS['db']);
137 if (! $_error && $move) {
138 if (isset($_POST['adjust_privileges'])
139 && ! empty($_POST['adjust_privileges'])
141 $operations->adjustPrivilegesMoveDb($GLOBALS['db'], $_POST['newname']);
145 * cleanup pmadb stuff for this db
147 RelationCleanup::database($GLOBALS['db']);
149 // if someday the RENAME DATABASE reappears, do not DROP
150 $local_query = 'DROP DATABASE '
151 . Util::backquote($GLOBALS['db']) . ';';
152 $sql_query .= "\n" . $local_query;
153 $GLOBALS['dbi']->query($local_query);
155 $message = Message::success(
156 __('Database %1$s has been renamed to %2$s.')
158 $message->addParam($GLOBALS['db']);
159 $message->addParam($_POST['newname']);
160 } elseif (! $_error) {
161 if (isset($_POST['adjust_privileges'])
162 && ! empty($_POST['adjust_privileges'])
164 $operations->adjustPrivilegesCopyDb($GLOBALS['db'], $_POST['newname']);
167 $message = Message::success(
168 __('Database %1$s has been copied to %2$s.')
170 $message->addParam($GLOBALS['db']);
171 $message->addParam($_POST['newname']);
172 } else {
173 $message = Message::error();
175 $reload = true;
177 /* Change database to be used */
178 if (! $_error && $move) {
179 $GLOBALS['db'] = $_POST['newname'];
180 } elseif (! $_error) {
181 if (isset($_POST['switch_to_new'])
182 && $_POST['switch_to_new'] == 'true'
184 $_SESSION['pma_switch_to_new'] = true;
185 $GLOBALS['db'] = $_POST['newname'];
186 } else {
187 $_SESSION['pma_switch_to_new'] = false;
194 * Database has been successfully renamed/moved. If in an Ajax request,
195 * generate the output with {@link PhpMyAdmin\Response} and exit
197 if ($response->isAjax()) {
198 $response->setRequestStatus($message->isSuccess());
199 $response->addJSON('message', $message);
200 $response->addJSON('newname', $_POST['newname']);
201 $response->addJSON(
202 'sql_query',
203 Util::getMessage(null, $sql_query)
205 $response->addJSON('db', $GLOBALS['db']);
206 exit;
211 * Settings for relations stuff
213 $relation = new Relation();
215 $cfgRelation = $relation->getRelationsParam();
218 * Check if comments were updated
219 * (must be done before displaying the menu tabs)
221 if (isset($_POST['comment'])) {
222 $relation->setDbComment($GLOBALS['db'], $_POST['comment']);
225 require 'libraries/db_common.inc.php';
226 $url_query .= '&amp;goto=db_operations.php';
228 // Gets the database structure
229 $sub_part = '_structure';
231 list(
232 $tables,
233 $num_tables,
234 $total_num_tables,
235 $sub_part,
236 $is_show_stats,
237 $db_is_system_schema,
238 $tooltip_truename,
239 $tooltip_aliasname,
240 $pos
241 ) = Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
243 echo "\n";
245 if (isset($message)) {
246 echo Util::getMessage($message, $sql_query);
247 unset($message);
250 $db_collation = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
251 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
253 if (!$is_information_schema) {
254 if ($cfgRelation['commwork']) {
256 * database comment
258 $response->addHTML($operations->getHtmlForDatabaseComment($GLOBALS['db']));
261 $response->addHTML('<div>');
262 $response->addHTML(CreateTable::getHtml($db));
263 $response->addHTML('</div>');
266 * rename database
268 if ($GLOBALS['db'] != 'mysql') {
269 $response->addHTML($operations->getHtmlForRenameDatabase($GLOBALS['db'], $db_collation));
272 // Drop link if allowed
273 // Don't even try to drop information_schema.
274 // You won't be able to. Believe me. You won't.
275 // Don't allow to easily drop mysql database, RFE #1327514.
276 if (($GLOBALS['dbi']->isSuperuser() || $GLOBALS['cfg']['AllowUserDropDatabase'])
277 && ! $db_is_system_schema
278 && $GLOBALS['db'] != 'mysql'
280 $response->addHTML($operations->getHtmlForDropDatabaseLink($GLOBALS['db']));
283 * Copy database
285 $response->addHTML($operations->getHtmlForCopyDatabase($GLOBALS['db'], $db_collation));
288 * Change database charset
290 $response->addHTML($operations->getHtmlForChangeDatabaseCharset($GLOBALS['db'], $db_collation));
292 if (! $cfgRelation['allworks']
293 && $cfg['PmaNoRelation_DisableWarning'] == false
295 $message = Message::notice(
297 'The phpMyAdmin configuration storage has been deactivated. ' .
298 '%sFind out why%s.'
301 $message->addParamHtml('<a href="./chk_rel.php" data-post="' . $url_query . '">');
302 $message->addParamHtml('</a>');
303 /* Show error if user has configured something, notice elsewhere */
304 if (!empty($cfg['Servers'][$server]['pmadb'])) {
305 $message->isError(true);
307 } // end if
308 } // end if (!$is_information_schema)