Translated using Weblate (Interlingua)
[phpmyadmin.git] / db_operations.php
blob284932d0a91132c0853b1ad0c06ce45d2ea60ee5
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 /**
44 * Rename/move or copy database
46 if (strlen($GLOBALS['db']) > 0
47 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
48 ) {
49 if (! empty($_REQUEST['db_rename'])) {
50 $move = true;
51 } else {
52 $move = false;
55 if (! isset($_REQUEST['newname']) || strlen($_REQUEST['newname']) === 0) {
56 $message = Message::error(__('The database name is empty!'));
57 } else {
58 // lower_case_table_names=1 `DB` becomes `db`
59 if ($GLOBALS['dbi']->getLowerCaseNames() === '1') {
60 $_REQUEST['newname'] = mb_strtolower(
61 $_REQUEST['newname']
65 if ($_REQUEST['newname'] === $_REQUEST['db']) {
66 $message = Message::error(
67 __('Cannot copy database to the same name. Change the name and try again.')
69 } else {
70 $_error = false;
71 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
72 Operations::createDbBeforeCopy();
75 // here I don't use DELIMITER because it's not part of the
76 // language; I have to send each statement one by one
78 // to avoid selecting alternatively the current and new db
79 // we would need to modify the CREATE definitions to qualify
80 // the db name
81 Operations::runProcedureAndFunctionDefinitions($GLOBALS['db']);
83 // go back to current db, just in case
84 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
86 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
88 // remove all foreign key constraints, otherwise we can get errors
89 /* @var $export_sql_plugin ExportSql */
90 $export_sql_plugin = Plugins::getPlugin(
91 "export",
92 "sql",
93 'libraries/classes/Plugins/Export/',
94 array(
95 'single_table' => isset($single_table),
96 'export_type' => 'database'
100 // create stand-in tables for views
101 $views = Operations::getViewsAndCreateSqlViewStandIn(
102 $tables_full, $export_sql_plugin, $GLOBALS['db']
105 // copy tables
106 $sqlConstratints = Operations::copyTables(
107 $tables_full, $move, $GLOBALS['db']
110 // handle the views
111 if (! $_error) {
112 Operations::handleTheViews($views, $move, $GLOBALS['db']);
114 unset($views);
116 // now that all tables exist, create all the accumulated constraints
117 if (! $_error && count($sqlConstratints) > 0) {
118 Operations::createAllAccumulatedConstraints($sqlConstratints);
120 unset($sqlConstratints);
122 if ($GLOBALS['dbi']->getVersion() >= 50100) {
123 // here DELIMITER is not used because it's not part of the
124 // language; each statement is sent one by one
126 Operations::runEventDefinitionsForDb($GLOBALS['db']);
129 // go back to current db, just in case
130 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
132 // Duplicate the bookmarks for this db (done once for each db)
133 Operations::duplicateBookmarks($_error, $GLOBALS['db']);
135 if (! $_error && $move) {
136 if (isset($_REQUEST['adjust_privileges'])
137 && ! empty($_REQUEST['adjust_privileges'])
139 Operations::adjustPrivilegesMoveDb($GLOBALS['db'], $_REQUEST['newname']);
143 * cleanup pmadb stuff for this db
145 RelationCleanup::database($GLOBALS['db']);
147 // if someday the RENAME DATABASE reappears, do not DROP
148 $local_query = 'DROP DATABASE '
149 . Util::backquote($GLOBALS['db']) . ';';
150 $sql_query .= "\n" . $local_query;
151 $GLOBALS['dbi']->query($local_query);
153 $message = Message::success(
154 __('Database %1$s has been renamed to %2$s.')
156 $message->addParam($GLOBALS['db']);
157 $message->addParam($_REQUEST['newname']);
158 } elseif (! $_error) {
159 if (isset($_REQUEST['adjust_privileges'])
160 && ! empty($_REQUEST['adjust_privileges'])
162 Operations::adjustPrivilegesCopyDb($GLOBALS['db'], $_REQUEST['newname']);
165 $message = Message::success(
166 __('Database %1$s has been copied to %2$s.')
168 $message->addParam($GLOBALS['db']);
169 $message->addParam($_REQUEST['newname']);
170 } else {
171 $message = Message::error();
173 $reload = true;
175 /* Change database to be used */
176 if (! $_error && $move) {
177 $GLOBALS['db'] = $_REQUEST['newname'];
178 } elseif (! $_error) {
179 if (isset($_REQUEST['switch_to_new'])
180 && $_REQUEST['switch_to_new'] == 'true'
182 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
183 $GLOBALS['db'] = $_REQUEST['newname'];
184 } else {
185 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
192 * Database has been successfully renamed/moved. If in an Ajax request,
193 * generate the output with {@link PhpMyAdmin\Response} and exit
195 if ($response->isAjax()) {
196 $response->setRequestStatus($message->isSuccess());
197 $response->addJSON('message', $message);
198 $response->addJSON('newname', $_REQUEST['newname']);
199 $response->addJSON(
200 'sql_query',
201 Util::getMessage(null, $sql_query)
203 $response->addJSON('db', $GLOBALS['db']);
204 exit;
209 * Settings for relations stuff
212 $cfgRelation = Relation::getRelationsParam();
215 * Check if comments were updated
216 * (must be done before displaying the menu tabs)
218 if (isset($_REQUEST['comment'])) {
219 Relation::setDbComment($GLOBALS['db'], $_REQUEST['comment']);
222 require 'libraries/db_common.inc.php';
223 $url_query .= '&amp;goto=db_operations.php';
225 // Gets the database structure
226 $sub_part = '_structure';
228 list(
229 $tables,
230 $num_tables,
231 $total_num_tables,
232 $sub_part,
233 $is_show_stats,
234 $db_is_system_schema,
235 $tooltip_truename,
236 $tooltip_aliasname,
237 $pos
238 ) = Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
240 echo "\n";
242 if (isset($message)) {
243 echo Util::getMessage($message, $sql_query);
244 unset($message);
247 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
248 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
250 if (!$is_information_schema) {
251 if ($cfgRelation['commwork']) {
253 * database comment
255 $response->addHTML(Operations::getHtmlForDatabaseComment($GLOBALS['db']));
258 $response->addHTML('<div>');
259 $response->addHTML(CreateTable::getHtml($db));
260 $response->addHTML('</div>');
263 * rename database
265 if ($GLOBALS['db'] != 'mysql') {
266 $response->addHTML(Operations::getHtmlForRenameDatabase($GLOBALS['db']));
269 // Drop link if allowed
270 // Don't even try to drop information_schema.
271 // You won't be able to. Believe me. You won't.
272 // Don't allow to easily drop mysql database, RFE #1327514.
273 if (($GLOBALS['dbi']->isSuperuser() || $GLOBALS['cfg']['AllowUserDropDatabase'])
274 && ! $db_is_system_schema
275 && $GLOBALS['db'] != 'mysql'
277 $response->addHTML(Operations::getHtmlForDropDatabaseLink($GLOBALS['db']));
280 * Copy database
282 $response->addHTML(Operations::getHtmlForCopyDatabase($GLOBALS['db']));
285 * Change database charset
287 $response->addHTML(Operations::getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
289 if (! $cfgRelation['allworks']
290 && $cfg['PmaNoRelation_DisableWarning'] == false
292 $message = Message::notice(
294 'The phpMyAdmin configuration storage has been deactivated. ' .
295 '%sFind out why%s.'
298 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
299 $message->addParamHtml('</a>');
300 /* Show error if user has configured something, notice elsewhere */
301 if (!empty($cfg['Servers'][$server]['pmadb'])) {
302 $message->isError(true);
304 } // end if
305 } // end if (!$is_information_schema)