Translated using Weblate (Turkish)
[phpmyadmin.git] / libraries / db_common.inc.php
blob2228894196c32a01ad840de775da130dc024dab3
1 <?php
2 /**
3 * Common includes for the database level views
5 * @package PhpMyAdmin
6 */
7 declare(strict_types=1);
9 use PhpMyAdmin\Core;
10 use PhpMyAdmin\Message;
11 use PhpMyAdmin\Operations;
12 use PhpMyAdmin\Relation;
13 use PhpMyAdmin\Response;
14 use PhpMyAdmin\Url;
15 use PhpMyAdmin\Util;
17 if (! defined('PHPMYADMIN')) {
18 exit;
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);
36 /**
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'],
43 'database'
45 $err_url .= Url::getCommon(['db' => $db], strpos($err_url, '?') === false ? '?' : '&');
47 /**
48 * Ensures the database exists (else move to the "parent" script) and displays
49 * headers
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) {
58 $is_db = true;
59 unset($errno);
61 } else {
62 $is_db = false;
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);
74 $response->addJSON(
75 'message',
76 Message::error(__('No databases selected.'))
78 } else {
79 Core::sendHeaderLocation($uri);
81 exit;
83 } // end if (ensures db exists)
85 /**
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();
96 /**
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.
106 // issue #15283
107 continue;
109 $sql_query = 'ALTER TABLE '
110 . Util::backquote($db)
111 . '.'
112 . Util::backquote($tableName)
113 . ' DEFAULT '
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']);
127 unset($db_charset);
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);
137 exit;
139 } elseif (isset($_POST['submitcollation'], $_POST['db_collation']) && empty($_POST['db_collation'])) {
140 $response = Response::getInstance();
141 if ($response->isAjax()) {
142 $response->setRequestStatus(false);
143 $response->addJSON(
144 'message',
145 Message::error(__('No collation provided.'))
151 * Set parameters for links
153 $url_query = Url::getCommon(['db' => $db]);