Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / db_operations.php
blob338d38574f874dc56e25022b040f27ff52b3ec87
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.lib.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 PMA_DBI_select_db($db);
67 $tables_full = PMA_DBI_get_tables_full($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 PMA_DBI_select_db($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 PMA_DBI_query($local_query);
129 $message = PMA_Message::success(__('Database %1$s has been renamed to %2$s'));
130 $message->addParam($db);
131 $message->addParam($_REQUEST['newname']);
132 } elseif (! $_error) {
133 $message = PMA_Message::success(__('Database %1$s has been copied to %2$s'));
134 $message->addParam($db);
135 $message->addParam($_REQUEST['newname']);
137 $reload = true;
139 /* Change database to be used */
140 if (! $_error && $move) {
141 $db = $_REQUEST['newname'];
142 } elseif (! $_error) {
143 if (isset($_REQUEST['switch_to_new'])
144 && $_REQUEST['switch_to_new'] == 'true'
146 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', 'true');
147 $db = $_REQUEST['newname'];
148 } else {
149 $GLOBALS['PMA_Config']->setCookie('pma_switch_to_new', '');
153 if ($_error && ! isset($message)) {
154 $message = PMA_Message::error();
159 * Database has been successfully renamed/moved. If in an Ajax request,
160 * generate the output with {@link PMA_Response} and exit
162 if ($GLOBALS['is_ajax_request'] == true) {
163 $response = PMA_Response::getInstance();
164 $response->isSuccess($message->isSuccess());
165 $response->addJSON('message', $message);
166 $response->addJSON('newname', $_REQUEST['newname']);
167 $response->addJSON(
168 'sql_query',
169 PMA_Util::getMessage(null, $sql_query)
171 exit;
176 * Settings for relations stuff
179 $cfgRelation = PMA_getRelationsParam();
182 * Check if comments were updated
183 * (must be done before displaying the menu tabs)
185 if (isset($_REQUEST['comment'])) {
186 PMA_setDbComment($db, $_REQUEST['comment']);
190 * Prepares the tables list if the user where not redirected to this script
191 * because there is no table in the database ($is_info is true)
193 if (empty($is_info)) {
194 include 'libraries/db_common.inc.php';
195 $url_query .= '&amp;goto=db_operations.php';
197 // Gets the database structure
198 $sub_part = '_structure';
199 include 'libraries/db_info.inc.php';
200 echo "\n";
202 if (isset($message)) {
203 echo PMA_Util::getMessage($message, $sql_query);
204 unset($message);
208 $_REQUEST['db_collation'] = PMA_getDbCollation($db);
209 $is_information_schema = PMA_is_system_schema($db);
211 if (!$is_information_schema) {
212 if ($cfgRelation['commwork']) {
214 * database comment
216 $response->addHTML(PMA_getHtmlForDatabaseComment($db));
219 $response->addHTML('<div class="operations_half_width">');
220 ob_start();
221 include 'libraries/display_create_table.lib.php';
222 $content = ob_get_contents();
223 ob_end_clean();
224 $response->addHTML($content);
225 $response->addHTML('</div>');
228 * rename database
230 if ($db != 'mysql') {
231 $response->addHTML(PMA_getHtmlForRenameDatabase($db));
234 // Drop link if allowed
235 // Don't even try to drop information_schema.
236 // You won't be able to. Believe me. You won't.
237 // Don't allow to easily drop mysql database, RFE #1327514.
238 if (($is_superuser || $GLOBALS['cfg']['AllowUserDropDatabase'])
239 && ! $db_is_information_schema
240 && (PMA_DRIZZLE || $db != 'mysql')
242 $response->addHTML(PMA_getHtmlForDropDatabaseLink($db));
245 * Copy database
247 $response->addHTML(PMA_getHtmlForCopyDatabase($db));
250 * Change database charset
252 $response->addHTML(PMA_getHtmlForChangeDatabaseCharset($db, $table));
254 if ($num_tables > 0
255 && ! $cfgRelation['allworks']
256 && $cfg['PmaNoRelation_DisableWarning'] == false
258 $message = PMA_Message::notice(
259 __('The phpMyAdmin configuration storage has been deactivated. To find out why click %shere%s.')
261 $message->addParam(
262 '<a href="' . $cfg['PmaAbsoluteUri'] . 'chk_rel.php?' . $url_query . '">',
263 false
265 $message->addParam('</a>', false);
266 /* Show error if user has configured something, notice elsewhere */
267 if (!empty($cfg['Servers'][$server]['pmadb'])) {
268 $message->isError(true);
270 $response->addHTML('<div class="operations_full_width">');
271 $response->addHTML($message->getDisplay());
272 $response->addHTML('</div>');
273 } // end if
274 } // end if (!$is_information_schema)
277 // not sure about displaying the PDF dialog in case db is information_schema
278 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
279 // We only show this if we find something in the new pdf_pages table
280 $test_query = '
281 SELECT *
282 FROM ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
283 . '.' . PMA_Util::backquote($cfgRelation['pdf_pages']) . '
284 WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\'';
285 $test_rs = PMA_queryAsControlUser($test_query, null, PMA_DBI_QUERY_STORE);
288 * Export Relational Schema View
290 $response->addHTML(PMA_getHtmlForExportRelationalSchemaView($url_query));
291 } // end if