Translated using Weblate (Greek)
[phpmyadmin.git] / db_operations.php
blob47604a47327e153c2a2197072b41c66e9d31e025
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
15 /**
16 * requirements
18 require_once 'libraries/common.inc.php';
19 require_once 'libraries/mysql_charsets.inc.php';
21 /**
22 * functions implementation for this script
24 require_once 'libraries/operations.lib.php';
26 // add a javascript file for jQuery functions to handle Ajax actions
27 $response = PMA_Response::getInstance();
28 $header = $response->getHeader();
29 $scripts = $header->getScripts();
30 $scripts->addFile('db_operations.js');
32 $sql_query = '';
34 // set export settings we need
35 $GLOBALS['sql_backquotes'] = 1;
37 /**
38 * Rename/move or copy database
40 /** @var PMA_String $pmaString */
41 $pmaString = $GLOBALS['PMA_String'];
42 if (/*overload*/mb_strlen($GLOBALS['db'])
43 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
44 ) {
45 if (! empty($_REQUEST['db_rename'])) {
46 $move = true;
47 } else {
48 $move = false;
51 if (! isset($_REQUEST['newname'])
52 || ! /*overload*/mb_strlen($_REQUEST['newname'])
53 ) {
54 $message = PMA_Message::error(__('The database name is empty!'));
55 } else {
56 $_error = false;
57 if ($move
58 || (isset($_REQUEST['create_database_before_copying'])
59 && $_REQUEST['create_database_before_copying'])
60 ) {
61 $sql_query = PMA_getSqlQueryAndCreateDbBeforeCopy();
64 // here I don't use DELIMITER because it's not part of the
65 // language; I have to send each statement one by one
67 // to avoid selecting alternatively the current and new db
68 // we would need to modify the CREATE definitions to qualify
69 // the db name
70 PMA_runProcedureAndFunctionDefinitions($GLOBALS['db']);
72 // go back to current db, just in case
73 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
75 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
77 include_once "libraries/plugin_interface.lib.php";
78 // remove all foreign key constraints, otherwise we can get errors
79 $export_sql_plugin = PMA_getPlugin(
80 "export",
81 "sql",
82 'libraries/plugins/export/',
83 array(
84 'single_table' => isset($single_table),
85 'export_type' => 'database'
88 $GLOBALS['sql_constraints_query_full_db']
89 = PMA_getSqlConstraintsQueryForFullDb(
90 $tables_full, $export_sql_plugin, $move, $GLOBALS['db']
93 $views = PMA_getViewsAndCreateSqlViewStandIn(
94 $tables_full, $export_sql_plugin, $GLOBALS['db']
97 list($sql_query, $_error) = PMA_getSqlQueryForCopyTable(
98 $tables_full, $sql_query, $move, $GLOBALS['db']
101 // handle the views
102 if (! $_error) {
103 $_error = PMA_handleTheViews($views, $move, $GLOBALS['db']);
105 unset($views);
107 // now that all tables exist, create all the accumulated constraints
108 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
109 PMA_createAllAccumulatedConstraints();
112 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50100) {
113 // here DELIMITER is not used because it's not part of the
114 // language; each statement is sent one by one
116 PMA_runEventDefinitionsForDb($GLOBALS['db']);
119 // go back to current db, just in case
120 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
122 // Duplicate the bookmarks for this db (done once for each db)
123 PMA_duplicateBookmarks($_error, $GLOBALS['db']);
125 if (! $_error && $move) {
127 * cleanup pmadb stuff for this db
129 include_once 'libraries/relation_cleanup.lib.php';
130 PMA_relationsCleanupDatabase($GLOBALS['db']);
132 // if someday the RENAME DATABASE reappears, do not DROP
133 $local_query = 'DROP DATABASE ' . PMA_Util::backquote($GLOBALS['db'])
134 . ';';
135 $sql_query .= "\n" . $local_query;
136 $GLOBALS['dbi']->query($local_query);
138 $message = PMA_Message::success(
139 __('Database %1$s has been renamed to %2$s.')
141 $message->addParam($GLOBALS['db']);
142 $message->addParam($_REQUEST['newname']);
143 } elseif (! $_error) {
144 $message = PMA_Message::success(
145 __('Database %1$s has been copied to %2$s.')
147 $message->addParam($GLOBALS['db']);
148 $message->addParam($_REQUEST['newname']);
149 } else {
150 $message = PMA_Message::error();
152 $reload = true;
154 /* Change database to be used */
155 if (! $_error && $move) {
156 $GLOBALS['db'] = $_REQUEST['newname'];
157 } elseif (! $_error) {
158 if (isset($_REQUEST['switch_to_new'])
159 && $_REQUEST['switch_to_new'] == 'true'
161 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
162 $GLOBALS['db'] = $_REQUEST['newname'];
163 } else {
164 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
170 * Database has been successfully renamed/moved. If in an Ajax request,
171 * generate the output with {@link PMA_Response} and exit
173 if ($GLOBALS['is_ajax_request'] == true) {
174 $response = PMA_Response::getInstance();
175 $response->isSuccess($message->isSuccess());
176 $response->addJSON('message', $message);
177 $response->addJSON('newname', $_REQUEST['newname']);
178 $response->addJSON(
179 'sql_query',
180 PMA_Util::getMessage(null, $sql_query)
182 $response->addJSON('db', $GLOBALS['db']);
183 exit;
188 * Settings for relations stuff
191 $cfgRelation = PMA_getRelationsParam();
194 * Check if comments were updated
195 * (must be done before displaying the menu tabs)
197 if (isset($_REQUEST['comment'])) {
198 PMA_setDbComment($GLOBALS['db'], $_REQUEST['comment']);
201 require 'libraries/db_common.inc.php';
202 $url_query .= '&amp;goto=db_operations.php';
204 // Gets the database structure
205 $sub_part = '_structure';
206 require 'libraries/db_info.inc.php';
207 echo "\n";
209 if (isset($message)) {
210 echo PMA_Util::getMessage($message, $sql_query);
211 unset($message);
214 $_REQUEST['db_collation'] = PMA_getDbCollation($GLOBALS['db']);
215 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($GLOBALS['db']);
217 $response->addHTML('<div id="boxContainer" data-box-width="300">');
219 if (!$is_information_schema) {
220 if ($cfgRelation['commwork']) {
222 * database comment
224 $response->addHTML(PMA_getHtmlForDatabaseComment($GLOBALS['db']));
227 $response->addHTML('<div class="operations_half_width">');
228 ob_start();
229 include 'libraries/display_create_table.lib.php';
230 $content = ob_get_contents();
231 ob_end_clean();
232 $response->addHTML($content);
233 $response->addHTML('</div>');
236 * rename database
238 if ($GLOBALS['db'] != 'mysql') {
239 $response->addHTML(PMA_getHtmlForRenameDatabase($GLOBALS['db']));
242 // Drop link if allowed
243 // Don't even try to drop information_schema.
244 // You won't be able to. Believe me. You won't.
245 // Don't allow to easily drop mysql database, RFE #1327514.
246 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
247 && ! $db_is_system_schema
248 && (PMA_DRIZZLE || $GLOBALS['db'] != 'mysql')
250 $response->addHTML(PMA_getHtmlForDropDatabaseLink($GLOBALS['db']));
253 * Copy database
255 $response->addHTML(PMA_getHtmlForCopyDatabase($GLOBALS['db']));
258 * Change database charset
260 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($GLOBALS['db'], $table));
262 if (! $cfgRelation['allworks']
263 && $cfg['PmaNoRelation_DisableWarning'] == false
265 $message = PMA_Message::notice(
266 __('The phpMyAdmin configuration storage has been deactivated. %sFind out why%s.')
268 $message->addParam(
269 '<a href="' . $cfg['PmaAbsoluteUri']
270 . 'chk_rel.php' . $url_query . '">',
271 false
273 $message->addParam('</a>', false);
274 /* Show error if user has configured something, notice elsewhere */
275 if (!empty($cfg['Servers'][$server]['pmadb'])) {
276 $message->isError(true);
278 } // end if
279 } // end if (!$is_information_schema)
281 $response->addHTML('</div>');
283 // not sure about displaying the PDF dialog in case db is information_schema
284 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
285 // We only show this if we find something in the new pdf_pages table
286 $test_query = '
287 SELECT *
288 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
289 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
290 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($GLOBALS['db']) . '\'';
291 $test_rs = PMA_queryAsControlUser(
292 $test_query,
293 false,
294 PMA_DatabaseInterface::QUERY_STORE
296 } // end if