Skip to content

Commit 271ce8c

Browse files
Merge pull request #18990 from MauricioFauth/table-relation-dbi-removal
Remove dbi object from table/relation/common_form.twig
2 parents e07c692 + bf806ad commit 271ce8c

6 files changed

Lines changed: 86 additions & 112 deletions

File tree

phpstan-baseline.neon

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4601,57 +4601,57 @@ parameters:
46014601
path: src/Controllers/Table/RelationController.php
46024602

46034603
-
4604-
message: "#^Cannot access offset 'ref_db_name' on mixed\\.$#"
4604+
message: "#^Cannot access offset 'foreign_db' on mixed\\.$#"
46054605
count: 1
46064606
path: src/Controllers/Table/RelationController.php
46074607

46084608
-
4609-
message: "#^Cannot access offset 'ref_table_name' on mixed\\.$#"
4609+
message: "#^Cannot access offset 'foreign_field' on mixed\\.$#"
46104610
count: 1
46114611
path: src/Controllers/Table/RelationController.php
46124612

46134613
-
4614-
message: "#^Parameter \\#1 \\$dbName of method PhpMyAdmin\\\\DatabaseInterface\\:\\:getTable\\(\\) expects string, mixed given\\.$#"
4614+
message: "#^Cannot access offset 'foreign_table' on mixed\\.$#"
46154615
count: 1
46164616
path: src/Controllers/Table/RelationController.php
46174617

46184618
-
4619-
message: "#^Parameter \\#1 \\$destinationForeignDb of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateForeignKeys\\(\\) expects array, mixed given\\.$#"
4619+
message: "#^Cannot access offset 'ref_db_name' on mixed\\.$#"
46204620
count: 1
46214621
path: src/Controllers/Table/RelationController.php
46224622

46234623
-
4624-
message: "#^Parameter \\#1 \\$displayField of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateDisplayField\\(\\) expects string, mixed given\\.$#"
4624+
message: "#^Cannot access offset 'ref_table_name' on mixed\\.$#"
46254625
count: 1
46264626
path: src/Controllers/Table/RelationController.php
46274627

46284628
-
4629-
message: "#^Parameter \\#1 \\$foreignDb of method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getTables\\(\\) expects string, mixed given\\.$#"
4629+
message: "#^Parameter \\#1 \\$dbName of method PhpMyAdmin\\\\DatabaseInterface\\:\\:getTable\\(\\) expects string, mixed given\\.$#"
46304630
count: 1
46314631
path: src/Controllers/Table/RelationController.php
46324632

46334633
-
4634-
message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#"
4635-
count: 2
4634+
message: "#^Parameter \\#1 \\$destinationForeignDb of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateForeignKeys\\(\\) expects array, mixed given\\.$#"
4635+
count: 1
46364636
path: src/Controllers/Table/RelationController.php
46374637

46384638
-
4639-
message: "#^Parameter \\#1 \\$multiEditColumnsName of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateInternalRelations\\(\\) expects array, mixed given\\.$#"
4639+
message: "#^Parameter \\#1 \\$displayField of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateDisplayField\\(\\) expects string, mixed given\\.$#"
46404640
count: 1
46414641
path: src/Controllers/Table/RelationController.php
46424642

46434643
-
4644-
message: "#^Parameter \\#1 \\$name of class PhpMyAdmin\\\\Table\\\\Table constructor expects string, mixed given\\.$#"
4645-
count: 1
4644+
message: "#^Parameter \\#1 \\$identifier of static method PhpMyAdmin\\\\Util\\:\\:backquote\\(\\) expects string\\|Stringable\\|null, mixed given\\.$#"
4645+
count: 2
46464646
path: src/Controllers/Table/RelationController.php
46474647

46484648
-
4649-
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(string\\|null, string\\|null\\)\\: int, Closure\\(string, string\\)\\: int\\<\\-1, 1\\> given\\.$#"
4649+
message: "#^Parameter \\#1 \\$multiEditColumnsName of method PhpMyAdmin\\\\Table\\\\Table\\:\\:updateInternalRelations\\(\\) expects array, mixed given\\.$#"
46504650
count: 1
46514651
path: src/Controllers/Table/RelationController.php
46524652

46534653
-
4654-
message: "#^Parameter \\#2 \\$dbName of class PhpMyAdmin\\\\Table\\\\Table constructor expects string, mixed given\\.$#"
4654+
message: "#^Parameter \\#2 \\$callback of function usort expects callable\\(string\\|null, string\\|null\\)\\: int, Closure\\(string, string\\)\\: int\\<\\-1, 1\\> given\\.$#"
46554655
count: 1
46564656
path: src/Controllers/Table/RelationController.php
46574657

psalm-baseline.xml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,20 +3483,19 @@
34833483
<code>usort($tables, strnatcasecmp(...))</code>
34843484
</InvalidArgument>
34853485
<MixedArgument>
3486-
<code>$foreignDb</code>
3487-
<code>$foreignTable</code>
34883486
<code><![CDATA[array_key_exists('foreign_keys_data', $relationsForeign)
34893487
? $relationsForeign['foreign_keys_data']
34903488
: []]]></code>
34913489
</MixedArgument>
34923490
<MixedArrayAccess>
34933491
<code><![CDATA[$oneKey['ref_db_name']]]></code>
34943492
<code><![CDATA[$oneKey['ref_table_name']]]></code>
3493+
<code><![CDATA[$relations[$column->field]['foreign_db']]]></code>
3494+
<code><![CDATA[$relations[$column->field]['foreign_field']]]></code>
3495+
<code><![CDATA[$relations[$column->field]['foreign_table']]]></code>
34953496
</MixedArrayAccess>
34963497
<MixedAssignment>
34973498
<code>$existrelForeign</code>
3498-
<code>$foreignDb</code>
3499-
<code>$foreignTable</code>
35003499
<code>$oneKey</code>
35013500
</MixedAssignment>
35023501
<PossiblyInvalidArgument>
@@ -3523,11 +3522,6 @@
35233522
<code>$foreignTable</code>
35243523
<code>$foreignTable</code>
35253524
</PossiblyInvalidCast>
3526-
<RiskyTruthyFalsyComparison>
3527-
<code>$foreignDb</code>
3528-
<code>$foreignDb</code>
3529-
<code>$foreignTable</code>
3530-
</RiskyTruthyFalsyComparison>
35313525
</file>
35323526
<file src="src/Controllers/Table/ReplaceController.php">
35333527
<InvalidArgument>

resources/templates/table/relation/common_form.twig

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,68 +83,33 @@
8383
</tr>
8484
</thead>
8585
<tbody>
86-
{% set saved_row_cnt = save_row|length - 1 %}
87-
{% for i in 0..saved_row_cnt %}
88-
{% set myfield = save_row[i] %}
89-
{# Use an md5 as array index to avoid having special characters in the name attribute (see bug #1746964 ) #}
90-
{% set myfield_md5 = column_hash_array[myfield] ?? null %}
91-
92-
{% set foreign_table = false %}
93-
{% set foreign_column = false %}
94-
95-
{# Database dropdown #}
96-
{% if existrel[myfield] is defined %}
97-
{% set foreign_db = existrel[myfield]['foreign_db'] %}
98-
{% else %}
99-
{% set foreign_db = db %}
100-
{% endif %}
101-
102-
{# Table dropdown #}
103-
{% set tables = [] %}
104-
{% if foreign_db %}
105-
{% if existrel[myfield] is defined %}
106-
{% set foreign_table = existrel[myfield]['foreign_table'] %}
107-
{% endif %}
108-
{% set tables = dbi.getTables(foreign_db) %}
109-
{% endif %}
110-
111-
{# Column dropdown #}
112-
{% set unique_columns = [] %}
113-
{% if foreign_db and foreign_table %}
114-
{% if existrel[myfield] is defined %}
115-
{% set foreign_column = existrel[myfield]['foreign_field'] %}
116-
{% endif %}
117-
{% set table_obj = table_get(foreign_table, foreign_db, dbi) %}
118-
{% set unique_columns = table_obj.getUniqueColumns(false, false) %}
119-
{% endif %}
120-
86+
{% for column in internal_relation_columns %}
12187
<tr>
12288
<td class="align-middle">
123-
<strong>{{ myfield }}</strong>
124-
<input type="hidden" name="fields_name[{{ myfield_md5 }}]"
125-
value="{{ myfield }}">
89+
<strong>{{ column.field }}</strong>
90+
<input type="hidden" name="fields_name[{{ column.field_hash }}]" value="{{ column.field }}">
12691
</td>
12792

12893
<td>
12994
{% include 'table/relation/relational_dropdown.twig' with {
130-
'name': 'destination_db[' ~ myfield_md5 ~ ']',
95+
'name': 'destination_db[' ~ column.field_hash ~ ']',
13196
'title': 'Database'|trans,
13297
'values': databases,
133-
'foreign': foreign_db
98+
'foreign': column.foreign_db
13499
} only %}
135100

136101
{% include 'table/relation/relational_dropdown.twig' with {
137-
'name': 'destination_table[' ~ myfield_md5 ~ ']',
102+
'name': 'destination_table[' ~ column.field_hash ~ ']',
138103
'title': 'Table'|trans,
139-
'values': tables,
140-
'foreign': foreign_table
104+
'values': column.tables,
105+
'foreign': column.foreign_table
141106
} only %}
142107

143108
{% include 'table/relation/relational_dropdown.twig' with {
144-
'name': 'destination_column[' ~ myfield_md5 ~ ']',
109+
'name': 'destination_column[' ~ column.field_hash ~ ']',
145110
'title': 'Column'|trans,
146-
'values': unique_columns,
147-
'foreign': foreign_column
111+
'values': column.unique_columns,
112+
'foreign': column.foreign_column
148113
} only %}
149114
</td>
150115
</tr>
@@ -163,12 +128,9 @@
163128
<label class="form-label" for="displayFieldSelect">{% trans 'Choose column to display:' %}</label>
164129
<select class="form-select" name="display_field" id="displayFieldSelect">
165130
<option value="">---</option>
166-
{% for field in save_row %}
167-
<option value="{{ field }}"
168-
{%- if display_field is defined and field == display_field %}
169-
selected="selected"
170-
{%- endif %}>
171-
{{ field }}
131+
{% for column in internal_relation_columns %}
132+
<option value="{{ column.field }}"{{ display_field is defined and column.field == display_field ? ' selected' }}>
133+
{{ column.field }}
172134
</option>
173135
{% endfor %}
174136
</select>

src/Controllers/Table/RelationController.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use PhpMyAdmin\Utils\ForeignKey;
2323

2424
use function __;
25-
use function array_column;
2625
use function array_key_exists;
2726
use function array_keys;
2827
use function mb_strtoupper;
@@ -168,10 +167,8 @@ public function __invoke(ServerRequest $request): void
168167
$columns = $this->dbi->getColumns(Current::$database, Current::$table);
169168

170169
$columnArray = [];
171-
$columnHashArray = [];
172170
$columnArray[''] = '';
173171
foreach ($columns as $column) {
174-
$columnHashArray[$column->field] = md5($column->field);
175172
if (strtoupper($storageEngine) !== 'INNODB' && $column->key === '') {
176173
continue;
177174
}
@@ -191,17 +188,19 @@ public function __invoke(ServerRequest $request): void
191188
$i = 0;
192189

193190
foreach ($existrelForeign as $oneKey) {
191+
/** @var string $foreignDb */
194192
$foreignDb = $oneKey['ref_db_name'] ?? Current::$database;
195193
$foreignTable = false;
196-
if ($foreignDb) {
194+
if ($foreignDb !== '') {
195+
/** @var string|false $foreignTable */
197196
$foreignTable = $oneKey['ref_table_name'] ?? false;
198197
$tables = $this->relation->getTables($foreignDb, $storageEngine);
199198
} else {
200199
$tables = $this->relation->getTables(Current::$database, $storageEngine);
201200
}
202201

203202
$uniqueColumns = [];
204-
if ($foreignDb && $foreignTable) {
203+
if ($foreignDb !== '' && $foreignTable !== false && $foreignTable !== '') {
205204
$tableObject = new Table($foreignTable, $foreignDb, $this->dbi);
206205
$uniqueColumns = $tableObject->getUniqueColumns(false, false);
207206
}
@@ -241,6 +240,57 @@ public function __invoke(ServerRequest $request): void
241240
'tables' => $tables,
242241
]);
243242

243+
$internalRelationColumns = [];
244+
foreach ($columns as $column) {
245+
// Use a md5 as array index to avoid having special characters in the name attribute
246+
// (see bug https://github.com/phpmyadmin/phpmyadmin/issues/8827)
247+
$fieldHash = md5($column->field);
248+
249+
$foreignTable = false;
250+
$foreignColumn = false;
251+
252+
// Database dropdown
253+
if (isset($relations[$column->field])) {
254+
/** @var string $foreignDb */
255+
$foreignDb = $relations[$column->field]['foreign_db'];
256+
} else {
257+
$foreignDb = Current::$database;
258+
}
259+
260+
// Table dropdown
261+
$foreignTables = [];
262+
if ($foreignDb !== '') {
263+
if (isset($relations[$column->field])) {
264+
/** @var string $foreignTable */
265+
$foreignTable = $relations[$column->field]['foreign_table'];
266+
}
267+
268+
$foreignTables = $this->dbi->getTables($foreignDb);
269+
}
270+
271+
// Column dropdown
272+
$uniqueColumns = [];
273+
if ($foreignDb !== '' && $foreignTable !== false && $foreignTable !== '') {
274+
if (isset($relations[$column->field])) {
275+
/** @var string $foreignColumn */
276+
$foreignColumn = $relations[$column->field]['foreign_field'];
277+
}
278+
279+
$tableObject = new Table($foreignTable, $foreignDb, $this->dbi);
280+
$uniqueColumns = $tableObject->getUniqueColumns(false, false);
281+
}
282+
283+
$internalRelationColumns[] = [
284+
'field' => $column->field,
285+
'field_hash' => $fieldHash,
286+
'foreign_db' => $foreignDb,
287+
'foreign_table' => $foreignTable,
288+
'foreign_column' => $foreignColumn,
289+
'tables' => $foreignTables,
290+
'unique_columns' => $uniqueColumns,
291+
];
292+
}
293+
244294
// common form
245295
$this->render('table/relation/common_form', [
246296
'is_foreign_key_supported' => ForeignKey::isSupported($storageEngine),
@@ -251,12 +301,9 @@ public function __invoke(ServerRequest $request): void
251301
'existrel' => $relations,
252302
'existrel_foreign' => $existrelForeign,
253303
'options_array' => $options,
254-
'column_array' => $columnArray,
255-
'column_hash_array' => $columnHashArray,
256-
'save_row' => array_column($columns, 'field'),
304+
'internal_relation_columns' => $internalRelationColumns,
257305
'url_params' => $GLOBALS['urlParams'],
258306
'databases' => $this->dbi->getDatabaseList(),
259-
'dbi' => $this->dbi,
260307
'default_sliders_state' => $config->settings['InitialSlidersState'],
261308
'route' => $request->getRoute(),
262309
'display_field' => $this->relation->getDisplayField(Current::$database, Current::$table),

src/Template.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpMyAdmin\Twig\I18nExtension;
1313
use PhpMyAdmin\Twig\MessageExtension;
1414
use PhpMyAdmin\Twig\SanitizeExtension;
15-
use PhpMyAdmin\Twig\TableExtension;
1615
use PhpMyAdmin\Twig\TransformationsExtension;
1716
use PhpMyAdmin\Twig\UrlExtension;
1817
use PhpMyAdmin\Twig\UtilExtension;
@@ -83,7 +82,6 @@ public static function getTwigEnvironment(string|null $cacheDir, bool $isDevEnv)
8382
$twig->addExtension(new I18nExtension());
8483
$twig->addExtension(new MessageExtension());
8584
$twig->addExtension(new SanitizeExtension());
86-
$twig->addExtension(new TableExtension());
8785
$twig->addExtension(new TransformationsExtension());
8886
$twig->addExtension(new UrlExtension());
8987
$twig->addExtension(new UtilExtension());

src/Twig/TableExtension.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)