Translated using Weblate (Czech)
[phpmyadmin.git] / libraries / db_common.inc.php
blobdee9ffcaf979d69655e4c4933e6b643ed80b8b77
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Common includes for the database level views
6 * @package PhpMyAdmin
7 */
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Message;
11 use PhpMyAdmin\Response;
12 use PhpMyAdmin\Url;
13 use PhpMyAdmin\Util;
14 use PhpMyAdmin\Operations;
16 if (! defined('PHPMYADMIN')) {
17 exit;
20 PhpMyAdmin\Util::checkParameters(array('db'));
22 global $cfg;
23 global $db;
25 $response = Response::getInstance();
26 $is_show_stats = $cfg['ShowStats'];
28 $db_is_system_schema = $GLOBALS['dbi']->isSystemSchema($db);
29 if ($db_is_system_schema) {
30 $is_show_stats = false;
33 /**
34 * Defines the urls to return to in case of error in a sql statement
36 $err_url_0 = 'index.php' . Url::getCommon();
38 $err_url = PhpMyAdmin\Util::getScriptNameForOption(
39 $GLOBALS['cfg']['DefaultTabDatabase'], 'database'
41 . Url::getCommon(array('db' => $db));
43 /**
44 * Ensures the database exists (else move to the "parent" script) and displays
45 * headers
47 if (! isset($is_db) || ! $is_db) {
48 if (strlen($db) > 0) {
49 $is_db = $GLOBALS['dbi']->selectDb($db);
50 // This "Command out of sync" 2014 error may happen, for example
51 // after calling a MySQL procedure; at this point we can't select
52 // the db but it's not necessarily wrong
53 if ($GLOBALS['dbi']->getError() && $GLOBALS['errno'] == 2014) {
54 $is_db = true;
55 unset($GLOBALS['errno']);
57 } else {
58 $is_db = false;
60 // Not a valid db name -> back to the welcome page
61 $params = array('reload' => '1');
62 if (isset($message)) {
63 $params['message'] = $message;
65 $uri = './index.php' . Url::getCommonRaw($params);
66 if (strlen($db) === 0 || ! $is_db) {
67 $response = Response::getInstance();
68 if ($response->isAjax()) {
69 $response->setRequestStatus(false);
70 $response->addJSON(
71 'message',
72 Message::error(__('No databases selected.'))
74 } else {
75 Core::sendHeaderLocation($uri);
77 exit;
79 } // end if (ensures db exists)
81 /**
82 * Changes database charset if requested by the user
84 if (isset($_POST['submitcollation'])
85 && isset($_POST['db_collation'])
86 && ! empty($_POST['db_collation'])
87 ) {
88 list($db_charset) = explode('_', $_POST['db_collation']);
89 $sql_query = 'ALTER DATABASE '
90 . PhpMyAdmin\Util::backquote($db)
91 . ' DEFAULT' . Util::getCharsetQueryPart($_POST['db_collation']);
92 $result = $GLOBALS['dbi']->query($sql_query);
93 $message = Message::success();
95 /**
96 * Changes tables charset if requested by the user
98 if (
99 isset($_POST['change_all_tables_collations']) &&
100 $_POST['change_all_tables_collations'] === 'on'
102 list($tables, , , , , , , ,) = PhpMyAdmin\Util::getDbInfo($db, null);
103 foreach($tables as $tableName => $data) {
104 if ($GLOBALS['dbi']->getTable($db, $tableName)->isView()) {
105 // Skip views, we can not change the collation of a view.
106 // issue #15283
107 continue;
109 $sql_query = 'ALTER TABLE '
110 . PhpMyAdmin\Util::backquote($db)
111 . '.'
112 . PhpMyAdmin\Util::backquote($tableName)
113 . ' DEFAULT '
114 . Util::getCharsetQueryPart($_POST['db_collation']);
115 $GLOBALS['dbi']->query($sql_query);
118 * Changes columns charset if requested by the user
120 if (
121 isset($_POST['change_all_tables_columns_collations']) &&
122 $_POST['change_all_tables_columns_collations'] === 'on'
124 $operations = new Operations();
125 $operations->changeAllColumnsCollation($db, $tableName, $_POST['db_collation']);
130 unset($db_charset);
133 * If we are in an Ajax request, let us stop the execution here. Necessary for
134 * db charset change action on db_operations.php. If this causes a bug on
135 * other pages, we might have to move this to a different location.
137 if ($response->isAjax()) {
138 $response->setRequestStatus($message->isSuccess());
139 $response->addJSON('message', $message);
140 exit;
142 } elseif (isset($_POST['submitcollation'])
143 && isset($_POST['db_collation'])
144 && empty($_POST['db_collation'])
146 $response = Response::getInstance();
147 if ($response->isAjax()) {
148 $response->setRequestStatus(false);
149 $response->addJSON(
150 'message',
151 Message::error(__('No collation provided.'))
157 * Set parameters for links
159 $url_query = Url::getCommon(array('db' => $db));