Translated using Weblate (Latvian)
[phpmyadmin.git] / db_operations.php
blob174a36f09b593ef92ca5eb36c6cb0885cb04443e
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\Message;
16 use PhpMyAdmin\Operations;
17 use PhpMyAdmin\Plugins\Export\ExportSql;
18 use PhpMyAdmin\Relation;
19 use PhpMyAdmin\Response;
20 use PhpMyAdmin\Util;
22 /**
23 * requirements
25 require_once 'libraries/common.inc.php';
26 require_once 'libraries/display_create_table.lib.php';
28 /**
29 * functions implementation for this script
31 require_once 'libraries/check_user_privileges.lib.php';
33 // add a javascript file for jQuery functions to handle Ajax actions
34 $response = Response::getInstance();
35 $header = $response->getHeader();
36 $scripts = $header->getScripts();
37 $scripts->addFile('db_operations.js');
39 $sql_query = '';
41 /**
42 * Rename/move or copy database
44 if (strlen($GLOBALS['db']) > 0
45 && (! empty($_REQUEST['db_rename']) || ! empty($_REQUEST['db_copy']))
46 ) {
47 if (! empty($_REQUEST['db_rename'])) {
48 $move = true;
49 } else {
50 $move = false;
53 if (! isset($_REQUEST['newname']) || strlen($_REQUEST['newname']) === 0) {
54 $message = Message::error(__('The database name is empty!'));
55 } else if($_REQUEST['newname'] === $_REQUEST['db']) {
56 $message = Message::error(
57 __('Cannot copy database to the same name. Change the name and try again.')
59 } else {
60 $_error = false;
61 if ($move || ! empty($_REQUEST['create_database_before_copying'])) {
62 Operations::createDbBeforeCopy();
65 // here I don't use DELIMITER because it's not part of the
66 // language; I have to send each statement one by one
68 // to avoid selecting alternatively the current and new db
69 // we would need to modify the CREATE definitions to qualify
70 // the db name
71 Operations::runProcedureAndFunctionDefinitions($GLOBALS['db']);
73 // go back to current db, just in case
74 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
76 $tables_full = $GLOBALS['dbi']->getTablesFull($GLOBALS['db']);
78 include_once "libraries/plugin_interface.lib.php";
79 // remove all foreign key constraints, otherwise we can get errors
80 /* @var $export_sql_plugin ExportSql */
81 $export_sql_plugin = PMA_getPlugin(
82 "export",
83 "sql",
84 'libraries/classes/Plugins/Export/',
85 array(
86 'single_table' => isset($single_table),
87 'export_type' => 'database'
91 // create stand-in tables for views
92 $views = Operations::getViewsAndCreateSqlViewStandIn(
93 $tables_full, $export_sql_plugin, $GLOBALS['db']
96 // copy tables
97 $sqlConstratints = Operations::copyTables(
98 $tables_full, $move, $GLOBALS['db']
101 // handle the views
102 if (! $_error) {
103 Operations::handleTheViews($views, $move, $GLOBALS['db']);
105 unset($views);
107 // now that all tables exist, create all the accumulated constraints
108 if (! $_error && count($sqlConstratints) > 0) {
109 Operations::createAllAccumulatedConstraints($sqlConstratints);
111 unset($sqlConstratints);
113 if ($GLOBALS['dbi']->getVersion() >= 50100) {
114 // here DELIMITER is not used because it's not part of the
115 // language; each statement is sent one by one
117 Operations::runEventDefinitionsForDb($GLOBALS['db']);
120 // go back to current db, just in case
121 $GLOBALS['dbi']->selectDb($GLOBALS['db']);
123 // Duplicate the bookmarks for this db (done once for each db)
124 Operations::duplicateBookmarks($_error, $GLOBALS['db']);
126 if (! $_error && $move) {
127 if (isset($_REQUEST['adjust_privileges'])
128 && ! empty($_REQUEST['adjust_privileges'])
130 Operations::adjustPrivilegesMoveDb($GLOBALS['db'], $_REQUEST['newname']);
134 * cleanup pmadb stuff for this db
136 include_once 'libraries/relation_cleanup.lib.php';
137 PMA_relationsCleanupDatabase($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(PMA_getHtmlForCreateTable($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 (($is_superuser || $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)
298 // not sure about displaying the PDF dialog in case db is information_schema
299 if ($cfgRelation['pdfwork'] && $num_tables > 0) {
300 // We only show this if we find something in the new pdf_pages table
301 $test_query = '
302 SELECT *
303 FROM ' . Util::backquote($GLOBALS['cfgRelation']['db'])
304 . '.' . Util::backquote($cfgRelation['pdf_pages']) . '
305 WHERE db_name = \'' . $GLOBALS['dbi']->escapeString($GLOBALS['db'])
306 . '\'';
307 $test_rs = Relation::queryAsControlUser(
308 $test_query,
309 false,
310 DatabaseInterface::QUERY_STORE
312 } // end if