Translated using Weblate (Bulgarian)
[phpmyadmin.git] / libraries / db_common.inc.php
blob2fb1fa19f5bf732e27a19f47656b72ecf03d6d8f
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 */
8 declare(strict_types=1);
10 use PhpMyAdmin\Core;
11 use PhpMyAdmin\Message;
12 use PhpMyAdmin\Operations;
13 use PhpMyAdmin\Relation;
14 use PhpMyAdmin\Response;
15 use PhpMyAdmin\Url;
16 use PhpMyAdmin\Util;
18 if (! defined('PHPMYADMIN')) {
19 exit;
22 Util::checkParameters(['db']);
24 global $cfg;
25 global $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);
38 /**
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'],
45 'database'
47 . Url::getCommon(['db' => $db]);
49 /**
50 * Ensures the database exists (else move to the "parent" script) and displays
51 * headers
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) {
60 $is_db = true;
61 unset($GLOBALS['errno']);
63 } else {
64 $is_db = false;
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);
76 $response->addJSON(
77 'message',
78 Message::error(__('No databases selected.'))
80 } else {
81 Core::sendHeaderLocation($uri);
83 exit;
85 } // end if (ensures db exists)
87 /**
88 * Changes database charset if requested by the user
90 if (isset($_POST['submitcollation'])
91 && isset($_POST['db_collation'])
92 && ! empty($_POST['db_collation'])
93 ) {
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.
111 // issue #15283
112 continue;
114 $sql_query = 'ALTER TABLE '
115 . Util::backquote($db)
116 . '.'
117 . Util::backquote($tableName)
118 . ' DEFAULT '
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']);
132 unset($db_charset);
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);
142 exit;
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);
151 $response->addJSON(
152 'message',
153 Message::error(__('No collation provided.'))
159 * Set parameters for links
161 $url_query = Url::getCommon(['db' => $db]);