Translated using Weblate (Slovenian)
[phpmyadmin.git] / db_operations.php
blobff60697f20dfee4e18ed658297f9e1cfc8f53c96
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\plugins\export\ExportSql;
16 /**
17 * requirements
19 require_once 'libraries/common.inc.php';
20 require_once 'libraries/display_create_table.lib.php';
22 /**
23 * functions implementation for this script
25 require_once 'libraries/check_user_privileges.lib.php';
26 require_once 'libraries/operations.lib.php';
28 // add a javascript file for jQuery functions to handle Ajax actions
29 $response = PMA\libraries\Response::getInstance();
30 $header = $response->getHeader();
31 $scripts = $header->getScripts();
32 $scripts->addFile('db_operations.js');
34 $sql_query = '';
36 /**
37 * Rename/move or copy database
39 if (strlen($GLOBALS['db'] > 0)
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']) || strlen($_REQUEST['newname']) === 0) {
49 $message = PMA\libraries\Message::error(__('The database name is empty!'));
50 } else {
51 $_error = false;
52 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
53 PMA_createDbBeforeCopy();
56 // here I don't use DELIMITER because it's not part of the
57 // language; I have to send each statement one by one
59 // to avoid selecting alternatively the current and new db
60 // we would need to modify the CREATE definitions to qualify
61 // the db name
62 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
64 // go back to current db, just in case
65 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
67 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
69 include_once "libraries/plugin_interface.lib.php";
70 // remove all foreign key constraints, otherwise we can get errors
71 /* @var $export_sql_plugin ExportSql */
72 $export_sql_plugin = PMA_getPlugin(
73 "export",
74 "sql",
75 'libraries/plugins/export/',
76 array(
77 'single_table' => isset($single_table),
78 'export_type' => 'database'
82 // create stand-in tables for views
83 $views = PMA_getViewsAndCreateSqlViewStandIn(
84 $tables_full, $export_sql_plugin, $GLOBALS['db']
87 // copy tables
88 $sqlConstratints = PMA_copyTables(
89 $tables_full, $move, $GLOBALS['db']
92 // handle the views
93 if (! $_error) {
94 PMA_handleTheViews($views, $move, $GLOBALS['db']);
96 unset($views);
98 // now that all tables exist, create all the accumulated constraints
99 if (! $_error && count($sqlConstratints) > 0) {
100 PMA_createAllAccumulatedConstraints($sqlConstratints);
102 unset($sqlConstratints);
104 if (PMA_MYSQL_INT_VERSION >= 50100) {
105 // here DELIMITER is not used because it's not part of the
106 // language; each statement is sent one by one
108 PMA_runEventDefinitionsForDb($GLOBALS['db']);
111 // go back to current db, just in case
112 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
114 // Duplicate the bookmarks for this db (done once for each db)
115 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
117 if (! $_error && $move) {
118 if (isset($_REQUEST['adjust_privileges'])
119 && ! empty($_REQUEST['adjust_privileges'])
121 PMA_AdjustPrivileges_moveDB($GLOBALS['db'], $_REQUEST['newname']);
125 * cleanup pmadb stuff for this db
127 include_once 'libraries/relation_cleanup.lib.php';
128 PMA_relationsCleanupDatabase($GLOBALS['db']);
130 // if someday the RENAME DATABASE reappears, do not DROP
131 $local_query = 'DROP DATABASE '
132 . PMA\libraries\Util::backquote($GLOBALS['db']) . ';';
133 $sql_query .= "\n" . $local_query;
134 $GLOBALS['dbi']->query($local_query);
136 $message = PMA\libraries\Message::success(
137 __('Database %1$s has been renamed to %2$s.')
139 $message->addParam($GLOBALS['db']);
140 $message->addParam($_REQUEST['newname']);
141 } elseif (! $_error) {
142 if (isset($_REQUEST['adjust_privileges'])
143 && ! empty($_REQUEST['adjust_privileges'])
145 PMA_AdjustPrivileges_copyDB($GLOBALS['db'], $_REQUEST['newname']);
148 $message = PMA\libraries\Message::success(
149 __('Database %1$s has been copied to %2$s.')
151 $message->addParam($GLOBALS['db']);
152 $message->addParam($_REQUEST['newname']);
153 } else {
154 $message = PMA\libraries\Message::error();
156 $reload = true;
158 /* Change database to be used */
159 if (! $_error && $move) {
160 $GLOBALS['db'] = $_REQUEST['newname'];
161 } elseif (! $_error) {
162 if (isset($_REQUEST['switch_to_new'])
163 && $_REQUEST['switch_to_new'] == 'true'
165 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
166 $GLOBALS['db'] = $_REQUEST['newname'];
167 } else {
168 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
174 * Database has been successfully renamed/moved. If in an Ajax request,
175 * generate the output with {@link PMA\libraries\Response} and exit
177 if ($GLOBALS['is_ajax_request'] == true) {
178 $response = PMA\libraries\Response::getInstance();
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