Translated using Weblate (Ukrainian)
[phpmyadmin.git] / db_operations.php
blob2f86b022924b282dc4fc7b86b3f827421a3c2a6d
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 if($_REQUEST['newname'] === $_REQUEST['db']) {
58 $message = Message::error(
59 __('Cannot copy database to the same name. Change the name and try again.')
61 } else {
62 $_error = false;
63 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
64 Operations::createDbBeforeCopy();
67 // here I don't use DELIMITER because it's not part of the
68 // language; I have to send each statement one by one
70 // to avoid selecting alternatively the current and new db
71 // we would need to modify the CREATE definitions to qualify
72 // the db name
73 Operations::runProcedureAndFunctionDefinitions($GLOBALS['db']);
75 // go back to current db, just in case
76 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
78 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
80 // remove all foreign key constraints, otherwise we can get errors
81 /* @var $export_sql_plugin ExportSql */
82 $export_sql_plugin = Plugins::getPlugin(
83 "export",
84 "sql",
85 'libraries/classes/Plugins/Export/',
86 array(
87 'single_table' => isset($single_table),
88 'export_type' => 'database'
92 // create stand-in tables for views
93 $views = Operations::getViewsAndCreateSqlViewStandIn(
94 $tables_full, $export_sql_plugin, $GLOBALS['db']
97 // copy tables
98 $sqlConstratints = Operations::copyTables(
99 $tables_full, $move, $GLOBALS['db']
102 // handle the views
103 if (! $_error) {
104 Operations::handleTheViews($views, $move, $GLOBALS['db']);
106 unset($views);
108 // now that all tables exist, create all the accumulated constraints
109 if (! $_error && count($sqlConstratints) > 0) {
110 Operations::createAllAccumulatedConstraints($sqlConstratints);
112 unset($sqlConstratints);
114 if ($GLOBALS['dbi']->getVersion() >= 50100) {
115 // here DELIMITER is not used because it's not part of the
116 // language; each statement is sent one by one
118 Operations::runEventDefinitionsForDb($GLOBALS['db']);
121 // go back to current db, just in case
122 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
124 // Duplicate the bookmarks for this db (done once for each db)
125 Operations::duplicateBookmarks($_error, $GLOBALS['db']);
127 if (! $_error && $move) {
128 if (isset($_REQUEST['adjust_privileges'])
129 && ! empty($_REQUEST['adjust_privileges'])
131 Operations::adjustPrivilegesMoveDb($GLOBALS['db'], $_REQUEST['newname']);
135 * cleanup pmadb stuff for this db
137 RelationCleanup::database($GLOBALS['db']);
139 // if someday the RENAME DATABASE reappears, do not DROP
140 $local_query = 'DROP DATABASE '
141 . Util::backquote($GLOBALS['db']) . ';';
142 $sql_query .= "\n" . $local_query;
143 $GLOBALS['dbi']->query($local_query);
145 $message = Message::success(
146 __('Database %1$s has been renamed to %2$s.')
148 $message->addParam($GLOBALS['db']);
149 $message->addParam($_REQUEST['newname']);
150 } elseif (! $_error) {
151 if (isset($_REQUEST['adjust_privileges'])
152 && ! empty($_REQUEST['adjust_privileges'])
154 Operations::adjustPrivilegesCopyDb($GLOBALS['db'], $_REQUEST['newname']);
157 $message = Message::success(
158 __('Database %1$s has been copied to %2$s.')
160 $message->addParam($GLOBALS['db']);
161 $message->addParam($_REQUEST['newname']);
162 } else {
163 $message = Message::error();
165 $reload = true;
167 /* Change database to be used */
168 if (! $_error && $move) {
169 $GLOBALS['db'] = $_REQUEST['newname'];
170 } elseif (! $_error) {
171 if (isset($_REQUEST['switch_to_new'])
172 && $_REQUEST['switch_to_new'] == 'true'
174 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
175 $GLOBALS['db'] = $_REQUEST['newname'];
176 } else {
177 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
183 * Database has been successfully renamed/moved. If in an Ajax request,
184 * generate the output with {@link PhpMyAdmin\Response} and exit
186 if ($response->isAjax()) {
187 $response->setRequestStatus($message->isSuccess());
188 $response->addJSON('message', $message);
189 $response->addJSON('newname', $_REQUEST['newname']);
190 $response->addJSON(
191 'sql_query',
192 Util::getMessage(null, $sql_query)
194 $response->addJSON('db', $GLOBALS['db']);
195 exit;
200 * Settings for relations stuff
203 $cfgRelation = Relation::getRelationsParam();
206 * Check if comments were updated
207 * (must be done before displaying the menu tabs)
209 if (isset($_REQUEST['comment'])) {
210 Relation::setDbComment($GLOBALS['db'], $_REQUEST['comment']);
213 require 'libraries/db_common.inc.php';
214 $url_query .= '&amp;goto=db_operations.php';
216 // Gets the database structure
217 $sub_part = '_structure';
219 list(
220 $tables,
221 $num_tables,
222 $total_num_tables,
223 $sub_part,
224 $is_show_stats,
225 $db_is_system_schema,
226 $tooltip_truename,
227 $tooltip_aliasname,
228 $pos
229 ) = Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
231 echo "\n";
233 if (isset($message)) {
234 echo Util::getMessage($message, $sql_query);
235 unset($message);
238 $_REQUEST['db_collation'] = $GLOBALS['dbi']->getDbCollation($GLOBALS['db']);
239 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
241 if (!$is_information_schema) {
242 if ($cfgRelation['commwork']) {
244 * database comment
246 $response->addHTML(Operations::getHtmlForDatabaseComment($GLOBALS['db']));
249 $response->addHTML('<div>');
250 $response->addHTML(CreateTable::getHtml($db));
251 $response->addHTML('</div>');
254 * rename database
256 if ($GLOBALS['db'] != 'mysql') {
257 $response->addHTML(Operations::getHtmlForRenameDatabase($GLOBALS['db']));
260 // Drop link if allowed
261 // Don't even try to drop information_schema.
262 // You won't be able to. Believe me. You won't.
263 // Don't allow to easily drop mysql database, RFE #1327514.
264 if (($GLOBALS['dbi']->isSuperuser() || $GLOBALS['cfg']['AllowUserDropDatabase'])
265 && ! $db_is_system_schema
266 && $GLOBALS['db'] != 'mysql'
268 $response->addHTML(Operations::getHtmlForDropDatabaseLink($GLOBALS['db']));
271 * Copy database
273 $response->addHTML(Operations::getHtmlForCopyDatabase($GLOBALS['db']));
276 * Change database charset
278 $response->addHTML(Operations::getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
280 if (! $cfgRelation['allworks']
281 && $cfg['PmaNoRelation_DisableWarning'] == false
283 $message = Message::notice(
285 'The phpMyAdmin configuration storage has been deactivated. ' .
286 '%sFind out why%s.'
289 $message->addParamHtml('<a href="./chk_rel.php' . $url_query . '">');
290 $message->addParamHtml('</a>');
291 /* Show error if user has configured something, notice elsewhere */
292 if (!empty($cfg['Servers'][$server]['pmadb'])) {
293 $message->isError(true);
295 } // end if
296 } // end if (!$is_information_schema)