3 * Common includes for the database level views
7 declare(strict_types
=1);
10 use PhpMyAdmin\Message
;
11 use PhpMyAdmin\Operations
;
12 use PhpMyAdmin\Relation
;
13 use PhpMyAdmin\Response
;
17 if (! defined('PHPMYADMIN')) {
21 Util
::checkParameters(['db']);
23 global $cfg, $db, $is_show_stats, $db_is_system_schema, $err_url, $message, $dbi, $url_query, $errno, $is_db;
25 $response = Response
::getInstance();
26 $is_show_stats = $cfg['ShowStats'];
28 $db_is_system_schema = $dbi->isSystemSchema($db);
29 if ($db_is_system_schema) {
30 $is_show_stats = false;
33 $relation = new Relation($dbi);
34 $operations = new Operations($dbi, $relation);
37 * Defines the urls to return to in case of error in a sql statement
39 $err_url_0 = Url
::getFromRoute('/');
41 $err_url = Util
::getScriptNameForOption(
42 $cfg['DefaultTabDatabase'],
45 $err_url .= Url
::getCommon(['db' => $db], strpos($err_url, '?') === false ?
'?' : '&');
48 * Ensures the database exists (else move to the "parent" script) and displays
51 if (! isset($is_db) ||
! $is_db) {
52 if (strlen($db) > 0) {
53 $is_db = $dbi->selectDb($db);
54 // This "Command out of sync" 2014 error may happen, for example
55 // after calling a MySQL procedure; at this point we can't select
56 // the db but it's not necessarily wrong
57 if ($dbi->getError() && $errno == 2014) {
64 // Not a valid db name -> back to the welcome page
65 $params = ['reload' => '1'];
66 if (isset($message)) {
67 $params['message'] = $message;
69 $uri = './index.php?route=/' . Url
::getCommonRaw($params, '&');
70 if (strlen($db) === 0 ||
! $is_db) {
71 $response = Response
::getInstance();
72 if ($response->isAjax()) {
73 $response->setRequestStatus(false);
76 Message
::error(__('No databases selected.'))
79 Core
::sendHeaderLocation($uri);
83 } // end if (ensures db exists)
86 * Changes database charset if requested by the user
88 if (isset($_POST['submitcollation'], $_POST['db_collation']) && ! empty($_POST['db_collation'])) {
89 list($db_charset) = explode('_', $_POST['db_collation']);
90 $sql_query = 'ALTER DATABASE '
91 . Util
::backquote($db)
92 . ' DEFAULT' . Util
::getCharsetQueryPart($_POST['db_collation']);
93 $result = $dbi->query($sql_query);
94 $message = Message
::success();
97 * Changes tables charset if requested by the user
99 if (isset($_POST['change_all_tables_collations']) &&
100 $_POST['change_all_tables_collations'] === 'on'
102 list($tables, , , , , , , ,) = Util
::getDbInfo($db, null);
103 foreach ($tables as $tableName => $data) {
104 if ($dbi->getTable($db, $tableName)->isView()) {
105 // Skip views, we can not change the collation of a view.
109 $sql_query = 'ALTER TABLE '
110 . Util
::backquote($db)
112 . Util
::backquote($tableName)
114 . Util
::getCharsetQueryPart($_POST['db_collation']);
115 $dbi->query($sql_query);
118 * Changes columns charset if requested by the user
120 if (isset($_POST['change_all_tables_columns_collations']) &&
121 $_POST['change_all_tables_columns_collations'] === 'on'
123 $operations->changeAllColumnsCollation($db, $tableName, $_POST['db_collation']);
130 * If we are in an Ajax request, let us stop the execution here. Necessary for
131 * db charset change action on /database/operations. If this causes a bug on
132 * other pages, we might have to move this to a different location.
134 if ($response->isAjax()) {
135 $response->setRequestStatus($message->isSuccess());
136 $response->addJSON('message', $message);
139 } elseif (isset($_POST['submitcollation'], $_POST['db_collation']) && empty($_POST['db_collation'])) {
140 $response = Response
::getInstance();
141 if ($response->isAjax()) {
142 $response->setRequestStatus(false);
145 Message
::error(__('No collation provided.'))
151 * Set parameters for links
153 $url_query = Url
::getCommon(['db' => $db]);