Refactored ConfigFile class so that it is no longer a singleton
[phpmyadmin.git] / db_operations.php
blob6d0db1982ae7c3bc90cedd002bb92883065a3ad4
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 /**
33 * Rename/move or copy database
35 if (strlen($db)
36 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
37 ) {
38 if (! empty($_REQUEST['db_rename'])) {
39 $move = true;
40 } else {
41 $move = false;
44 if (! isset($_REQUEST['newname']) || ! strlen($_REQUEST['newname'])) {
45 $message = PMA_Message::error(__('The database name is empty!'));
46 } else {
47 $sql_query = ''; // in case target db exists
48 $_error = false;
49 if ($move
50 || (isset($_REQUEST['create_database_before_copying'])
51 && $_REQUEST['create_database_before_copying'])
52 ) {
53 $sql_query = PMA_getSqlQueryAndCreateDbBeforeCopy();
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($db);
64 // go back to current db, just in case
65 $GLOBALS['dbi']->selectDb($db);
67 $tables_full = $GLOBALS['dbi']->getTablesFull($db);
69 include_once "libraries/plugin_interface.lib.php";
70 // remove all foreign key constraints, otherwise we can get errors
71 $export_sql_plugin = PMA_getPlugin(
72 "export",
73 "sql",
74 'libraries/plugins/export/',
75 array(
76 'single_table' => isset($single_table),
77 'export_type' => 'database'
80 $GLOBALS['sql_constraints_query_full_db']
81 = PMA_getSqlConstraintsQueryForFullDb(
82 $tables_full, $export_sql_plugin, $move, $db
85 $views = PMA_getViewsAndCreateSqlViewStandIn(
86 $tables_full, $export_sql_plugin, $db
89 list($sql_query, $_error) = PMA_getSqlQueryForCopyTable(
90 $tables_full, $sql_query, $move, $db
93 // handle the views
94 if (! $_error) {
95 $_error = PMA_handleTheViews($views, $move, $db);
97 unset($views);
99 // now that all tables exist, create all the accumulated constraints
100 if (! $_error && count($GLOBALS['sql_constraints_query_full_db']) > 0) {
101 PMA_createAllAccumulatedConstraints();
104 if (! PMA_DRIZZLE && 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($db);
111 // go back to current db, just in case
112 $GLOBALS['dbi']->selectDb($db);
114 // Duplicate the bookmarks for this db (done once for each db)
115 PMA_duplicateBookmarks($_error, $db);
117 if (! $_error && $move) {
119 * cleanup pmadb stuff for this db
121 include_once 'libraries/relation_cleanup.lib.php';
122 PMA_relationsCleanupDatabase($db);
124 // if someday the RENAME DATABASE reappears, do not DROP
125 $local_query = 'DROP DATABASE ' . PMA_Util::backquote($db) . ';';
126 $sql_query .= "\n" . $local_query;
127 $GLOBALS['dbi']->query($local_query);
129 $message = PMA_Message::success(
130 __('Database %1$s has been renamed to %2$s')
132 $message->addParam($db);
133 $message->addParam($_REQUEST['newname']);
134 } elseif (! $_error) {
135 $message = PMA_Message::success(
136 __('Database %1$s has been copied to %2$s')
138 $message->addParam($db);
139 $message->addParam($_REQUEST['newname']);
141 $reload = true;
143 /* Change database to be used */
144 if (! $_error && $move) {
145 $db = $_REQUEST['newname'];
146 } elseif (! $_error) {
147 if (isset($_REQUEST['switch_to_new'])
148 && $_REQUEST['switch_to_new'] == 'true'
150 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
151 $db = $_REQUEST['newname'];
152 } else {
153 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
157 if ($_error && ! isset($message)) {
158 $message = PMA_Message::error();
163 * Database has been successfully renamed/moved. If in an Ajax request,
164 * generate the output with {@link PMA_Response} and exit
166 if ($GLOBALS['is_ajax_request'] == true) {
167 $response = PMA_Response::getInstance();
168 $response->isSuccess($message->isSuccess());
169 $response->addJSON('message', $message);
170 $response->addJSON('newname', $_REQUEST['newname']);
171 $response->addJSON(
172 'sql_query',
173 PMA_Util::getMessage(null, $sql_query)
175 $response->addJSON('db', $db);
176 exit;
181 * Settings for relations stuff
184 $cfgRelation = PMA_getRelationsParam();
187 * Check if comments were updated
188 * (must be done before displaying the menu tabs)
190 if (isset($_REQUEST['comment'])) {
191 PMA_setDbComment($db, $_REQUEST['comment']);
194 require 'libraries/db_common.inc.php';
195 $url_query .= '&amp;goto=db_operations.php';
197 // Gets the database structure
198 $sub_part = '_structure';
199 require 'libraries/db_info.inc.php';
200 echo "\n";
202 if (isset($message)) {
203 echo PMA_Util::getMessage($message, $sql_query);
204 unset($message);
207 $_REQUEST['db_collation'] = PMA_getDbCollation($db);
208 $is_information_schema = $GLOBALS['dbi']->isSystemSchema($db);
210 $response->addHTML('<div id="boxContainer" data-box-width="300">');
212 if (!$is_information_schema) {
213 if ($cfgRelation['commwork']) {
215 * database comment
217 $response->addHTML(PMA_getHtmlForDatabaseComment($db));
220 $response->addHTML('<div class="operations_half_width">');
221 ob_start();
222 include 'libraries/display_create_table.lib.php';
223 $content = ob_get_contents();
224 ob_end_clean();
225 $response->addHTML($content);
226 $response->addHTML('</div>');
229 * rename database
231 if ($db != 'mysql') {
232 $response->addHTML(PMA_getHtmlForRenameDatabase($db));
235 // Drop link if allowed
236 // Don't even try to drop information_schema.
237 // You won't be able to. Believe me. You won't.
238 // Don't allow to easily drop mysql database, RFE #1327514.
239 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
240 && ! $db_is_information_schema
241 && (PMA_DRIZZLE || $db != 'mysql')
243 $response->addHTML(PMA_getHtmlForDropDatabaseLink($db));
246 * Copy database
248 $response->addHTML(PMA_getHtmlForCopyDatabase($db));
251 * Change database charset
253 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($db, $table));
255 if ($num_tables > 0
256 && ! $cfgRelation['allworks']
257 && $cfg['PmaNoRelation_DisableWarning'] == false
259 $message = PMA_Message::notice(
260 __('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.')
262 $message->addParam(
263 '<a href="' . $cfg['PmaAbsoluteUri']
264 . 'chk_rel.php?' . $url_query . '">',
265 false
267 $message->addParam('</a>', false);
268 /* Show error if user has configured something, notice elsewhere */
269 if (!empty($cfg['Servers'][$server]['pmadb'])) {
270 $message->isError(true);
272 $response->addHTML('<div class="operations_full_width">');
273 $response->addHTML($message->getDisplay());
274 $response->addHTML('</div>');
275 } // end if
276 } // end if (!$is_information_schema)
278 $response->addHTML('</div>');
280 // not sure about displaying the PDF dialog in case db is information_schema
281 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
282 // We only show this if we find something in the new pdf_pages table
283 $test_query = '
284 SELECT *
285 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
286 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
287 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
288 $test_rs = PMA_queryAsControlUser(
289 $test_query,
290 null,
291 PMA_DatabaseInterface::QUERY_STORE
295 * Export Relational Schema View
297 $response->addHTML(PMA_getHtmlForExportRelationalSchemaView($url_query));
298 } // end if