2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Common includes for the database level views
8 declare(strict_types
=1);
11 use PhpMyAdmin\Message
;
12 use PhpMyAdmin\Operations
;
13 use PhpMyAdmin\Relation
;
14 use PhpMyAdmin\Response
;
18 if (! defined('PHPMYADMIN')) {
22 Util
::checkParameters(['db']);
27 $response = Response
::getInstance();
28 $is_show_stats = $cfg['ShowStats'];
30 $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($db);
31 if ($db_is_system_schema) {
32 $is_show_stats = false;
35 $relation = new Relation($GLOBALS['dbi']);
36 $operations = new Operations($GLOBALS['dbi'], $relation);
39 * Defines the urls to return to in case of error in a sql statement
41 $err_url_0 = 'index.php' . Url
::getCommon();
43 $err_url = Util
::getScriptNameForOption(
44 $GLOBALS['cfg']['DefaultTabDatabase'],
47 . Url
::getCommon(['db' => $db]);
50 * Ensures the database exists (else move to the "parent" script) and displays
53 if (! isset($is_db) ||
! $is_db) {
54 if (strlen($db) > 0) {
55 $is_db = $GLOBALS['dbi']->selectDb($db);
56 // This "Command out of sync" 2014 error may happen, for example
57 // after calling a MySQL procedure; at this point we can't select
58 // the db but it's not necessarily wrong
59 if ($GLOBALS['dbi']->getError() && $GLOBALS['errno'] == 2014) {
61 unset($GLOBALS['errno']);
66 // Not a valid db name -> back to the welcome page
67 $params = ['reload' => '1'];
68 if (isset($message)) {
69 $params['message'] = $message;
71 $uri = './index.php' . Url
::getCommonRaw($params);
72 if (strlen($db) === 0 ||
! $is_db) {
73 $response = Response
::getInstance();
74 if ($response->isAjax()) {
75 $response->setRequestStatus(false);
78 Message
::error(__('No databases selected.'))
81 Core
::sendHeaderLocation($uri);
85 } // end if (ensures db exists)
88 * Changes database charset if requested by the user
90 if (isset($_POST['submitcollation'])
91 && isset($_POST['db_collation'])
92 && ! empty($_POST['db_collation'])
94 list($db_charset) = explode('_', $_POST['db_collation']);
95 $sql_query = 'ALTER DATABASE '
96 . Util
::backquote($db)
97 . ' DEFAULT' . Util
::getCharsetQueryPart($_POST['db_collation']);
98 $result = $GLOBALS['dbi']->query($sql_query);
99 $message = Message
::success();
102 * Changes tables charset if requested by the user
104 if (isset($_POST['change_all_tables_collations']) &&
105 $_POST['change_all_tables_collations'] === 'on'
107 list($tables, , , , , , , ,) = Util
::getDbInfo($db, null);
108 foreach ($tables as $tableName => $data) {
109 if ($GLOBALS['dbi']->getTable($db, $tableName)->isView()) {
110 // Skip views, we can not change the collation of a view.
114 $sql_query = 'ALTER TABLE '
115 . Util
::backquote($db)
117 . Util
::backquote($tableName)
119 . Util
::getCharsetQueryPart($_POST['db_collation']);
120 $GLOBALS['dbi']->query($sql_query);
123 * Changes columns charset if requested by the user
125 if (isset($_POST['change_all_tables_columns_collations']) &&
126 $_POST['change_all_tables_columns_collations'] === 'on'
128 $operations->changeAllColumnsCollation($db, $tableName, $_POST['db_collation']);
135 * If we are in an Ajax request, let us stop the execution here. Necessary for
136 * db charset change action on db_operations.php. If this causes a bug on
137 * other pages, we might have to move this to a different location.
139 if ($response->isAjax()) {
140 $response->setRequestStatus($message->isSuccess());
141 $response->addJSON('message', $message);
144 } elseif (isset($_POST['submitcollation'])
145 && isset($_POST['db_collation'])
146 && empty($_POST['db_collation'])
148 $response = Response
::getInstance();
149 if ($response->isAjax()) {
150 $response->setRequestStatus(false);
153 Message
::error(__('No collation provided.'))
159 * Set parameters for links
161 $url_query = Url
::getCommon(['db' => $db]);