Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / SqlQueryForm.php
blob9ee5c3c3235719d1e597a0451ba4b20a7c3b75d4
1 <?php
2 /**
3 * functions for displaying the sql query form
5 * @usedby /server/sql
6 * @usedby /database/sql
7 * @usedby /table/sql
8 * @usedby /table/structure
9 * @usedby /table/tracking
12 declare(strict_types=1);
14 namespace PhpMyAdmin;
16 use PhpMyAdmin\Bookmarks\BookmarkRepository;
17 use PhpMyAdmin\ConfigStorage\Relation;
18 use PhpMyAdmin\Html\MySQLDocumentation;
19 use PhpMyAdmin\Utils\ForeignKey;
21 use function __;
22 use function htmlspecialchars;
23 use function sprintf;
24 use function str_contains;
26 /**
27 * PhpMyAdmin\SqlQueryForm class
29 class SqlQueryForm
31 private readonly Config $config;
33 public function __construct(
34 private Template $template,
35 private DatabaseInterface $dbi,
36 private readonly BookmarkRepository $bookmarkRepository,
37 ) {
38 $this->config = Config::getInstance();
41 /**
42 * return HTML for the sql query boxes
44 * @param bool|string $query query to display in the textarea
45 * or true to display last executed
46 * @param bool|string $displayTab sql|full|false
47 * what part to display
48 * false if not inside querywindow
49 * @param string $delimiter delimiter
51 * @usedby /server/sql
52 * @usedby /database/sql
53 * @usedby /table/sql
54 * @usedby /table/structure
55 * @usedby /table/tracking
57 public function getHtml(
58 string $db,
59 string $table,
60 bool|string $query = true,
61 bool|string $displayTab = false,
62 string $delimiter = ';',
63 ): string {
64 if ($displayTab === false || $displayTab === '') {
65 $displayTab = 'full';
68 // query to show
69 if ($query === true) {
70 $query = $GLOBALS['sql_query'];
71 if (empty($query) && (isset($_GET['show_query']) || isset($_POST['show_query']))) {
72 $query = $_GET['sql_query'] ?? $_POST['sql_query'] ?? '';
76 if ($db === '') {
77 // prepare for server related
78 $goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/server/sql') : $GLOBALS['goto'];
79 } elseif ($table === '') {
80 // prepare for db related
81 $goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/database/sql') : $GLOBALS['goto'];
82 } else {
83 $goto = empty($GLOBALS['goto']) ? Url::getFromRoute('/table/sql') : $GLOBALS['goto'];
86 if ($displayTab === 'full' || $displayTab === 'sql') {
87 [$legend, $query, $columnsList] = $this->init($query);
90 $relation = new Relation($this->dbi);
91 $bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature;
93 $bookmarks = [];
94 if ($displayTab === 'full' && $bookmarkFeature !== null) {
95 $bookmarkList = $this->bookmarkRepository->getList($this->config->selectedServer['user'], $db);
97 foreach ($bookmarkList as $bookmarkItem) {
98 $bookmarks[] = [
99 'id' => $bookmarkItem->getId(),
100 'variable_count' => $bookmarkItem->getVariableCount(),
101 'label' => $bookmarkItem->getLabel(),
102 'is_shared' => $bookmarkItem->getUser() === '',
107 return $this->template->render('sql/query', [
108 'legend' => $legend ?? '',
109 'textarea_cols' => $this->config->settings['TextareaCols'],
110 'textarea_rows' => $this->config->settings['TextareaRows'],
111 'textarea_auto_select' => $this->config->settings['TextareaAutoSelect'],
112 'columns_list' => $columnsList ?? [],
113 'codemirror_enable' => $this->config->settings['CodemirrorEnable'],
114 'has_bookmark' => $bookmarkFeature !== null,
115 'delimiter' => $delimiter,
116 'retain_query_box' => $this->config->settings['RetainQueryBox'] !== false,
117 'is_upload' => $this->config->get('enable_upload'),
118 'db' => $db,
119 'table' => $table,
120 'goto' => $goto,
121 'query' => $query,
122 'display_tab' => $displayTab,
123 'bookmarks' => $bookmarks,
124 'can_convert_kanji' => Encoding::canConvertKanji(),
125 'is_foreign_key_check' => ForeignKey::isCheckEnabled(),
126 'allow_shared_bookmarks' => $this->config->settings['AllowSharedBookmarks'],
131 * Get initial values for Sql Query Form Insert
133 * @param string $query query to display in the textarea
135 * @return array{string, string, ColumnFull[]}
137 public function init(string $query): array
139 $columnsList = [];
140 if (Current::$database === '') {
141 // prepare for server related
142 $legend = sprintf(
143 __('Run SQL query/queries on server “%s”'),
144 htmlspecialchars(
145 ! empty($this->config->settings['Servers'][Current::$server]['verbose'])
146 ? $this->config->settings['Servers'][Current::$server]['verbose']
147 : $this->config->settings['Servers'][Current::$server]['host'],
150 } elseif (Current::$table === '') {
151 // prepare for db related
152 $db = Current::$database;
153 // if you want navigation:
154 $scriptName = Util::getScriptNameForOption($this->config->settings['DefaultTabDatabase'], 'database');
155 $tmpDbLink = '<a href="' . $scriptName
156 . Url::getCommon(['db' => $db], ! str_contains($scriptName, '?') ? '?' : '&')
157 . '">';
158 $tmpDbLink .= htmlspecialchars($db) . '</a>';
159 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmpDbLink);
160 if ($query === '') {
161 $query = Util::expandUserString($this->config->settings['DefaultQueryDatabase'], Util::backquote(...));
163 } else {
164 $db = Current::$database;
165 $table = Current::$table;
166 // Get the list and number of fields
167 // we do a try_query here, because we could be in the query window,
168 // trying to synchronize and the table has not yet been created
169 $columnsList = $this->dbi->getColumns($db, Current::$table, true);
171 $scriptName = Util::getScriptNameForOption($this->config->settings['DefaultTabTable'], 'table');
172 $tmpTblLink = '<a href="' . $scriptName . Url::getCommon(['db' => $db, 'table' => $table], '&') . '">';
173 $tmpTblLink .= htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>';
174 $legend = sprintf(__('Run SQL query/queries on table %s'), $tmpTblLink);
175 if ($query === '') {
176 $query = Util::expandUserString($this->config->settings['DefaultQueryTable'], Util::backquote(...));
180 $legend .= ': ' . MySQLDocumentation::show('SELECT');
182 return [$legend, $query, $columnsList];