Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ForeignData #19134

Merged
merged 1 commit into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Expand Up @@ -140,11 +140,6 @@ parameters:
count: 1
path: src/Bookmarks/BookmarkRepository.php

-
message: "#^Cannot access offset mixed on mixed\\.$#"
count: 2
path: src/BrowseForeigners.php

-
message: "#^Cannot cast mixed to string\\.$#"
count: 1
Expand Down Expand Up @@ -1140,16 +1135,6 @@ parameters:
count: 1
path: src/ConfigStorage/Relation.php

-
message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getForeignData\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/ConfigStorage/Relation.php

-
message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getForeignData\\(\\) should return array\\{foreign_link\\: bool, the_total\\: int\\|string\\|null, foreign_display\\: string, disp_row\\: array\\<int, non\\-empty\\-array\\>\\|null, foreign_field\\: mixed\\} but returns array\\{foreign_link\\: bool, the_total\\: int\\|string\\|null, foreign_display\\: string, disp_row\\: array\\<int, array\\<string\\|null\\>\\>\\|null, foreign_field\\: mixed\\}\\.$#"
count: 1
path: src/ConfigStorage/Relation.php

-
message: "#^Method PhpMyAdmin\\\\ConfigStorage\\\\Relation\\:\\:getTableReplacementNames\\(\\) should return array\\<string, string\\> but returns array\\<string, array\\<array\\<string\\>\\|bool\\|int\\|string\\>\\|int\\<1, max\\>\\|string\\|true\\>\\.$#"
count: 1
Expand Down Expand Up @@ -8745,11 +8730,6 @@ parameters:
count: 1
path: src/InsertEdit.php

-
message: "#^Only booleans are allowed in &&, array\\|false given on the left side\\.$#"
count: 2
path: src/InsertEdit.php

-
message: "#^Only booleans are allowed in &&, string\\|null given on the left side\\.$#"
count: 1
Expand Down
45 changes: 5 additions & 40 deletions psalm-baseline.xml
Expand Up @@ -79,26 +79,14 @@
<MixedArgument>
<code><![CDATA[$descriptions[$indexByDescription]]]></code>
<code><![CDATA[$descriptions[$indexByKeyname]]]></code>
<code><![CDATA[$foreignData['the_total'] / $this->settings->maxRows]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$relrow[$foreignData['foreign_display']]]]></code>
<code><![CDATA[$relrow[$foreignData['foreign_field']]]]></code>
</MixedArrayAccess>
<MixedArrayOffset>
<code><![CDATA[$relrow[$foreignData['foreign_display']]]]></code>
<code><![CDATA[$relrow[$foreignData['foreign_field']]]]></code>
<code><![CDATA[$relrow[$foreignData->foreignField]]]></code>
</MixedArrayOffset>
<MixedAssignment>
<code><![CDATA[$descriptions[]]]></code>
<code><![CDATA[$keys[]]]></code>
<code><![CDATA[$leftKeyname]]></code>
<code><![CDATA[$relrow]]></code>
<code><![CDATA[$rightKeyname]]></code>
</MixedAssignment>
<MixedOperand>
<code><![CDATA[$foreignData['the_total']]]></code>
</MixedOperand>
<PossiblyInvalidArgument>
<code><![CDATA[$_POST['foreign_filter']]]></code>
</PossiblyInvalidArgument>
Expand All @@ -109,9 +97,6 @@
<code><![CDATA[$pos]]></code>
<code><![CDATA[$this->settings->maxRows]]></code>
</PossiblyInvalidOperand>
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$foreignData['foreign_display']]]></code>
</PossiblyUndefinedArrayOffset>
</file>
<file src="src/Cache.php">
<MixedAssignment>
Expand Down Expand Up @@ -694,15 +679,6 @@
<InvalidReturnType>
<code><![CDATA[array<string, string>]]></code>
</InvalidReturnType>
<LessSpecificReturnStatement>
<code><![CDATA[[
'foreign_link' => $foreignLink,
'the_total' => $theTotal,
'foreign_display' => is_string($foreignDisplay) ? $foreignDisplay : '',
'disp_row' => $dispRow,
'foreign_field' => $foreignField,
]]]></code>
</LessSpecificReturnStatement>
<MixedArgument>
<code><![CDATA[$_SESSION['sql_history']]]></code>
<code><![CDATA[$_SESSION['sql_history']]]></code>
Expand Down Expand Up @@ -772,15 +748,6 @@
<code><![CDATA[$column['COLUMN_NAME']]]></code>
<code><![CDATA[$foreigners[$column]]]></code>
</MixedReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[array{
* foreign_link: bool,
* the_total: int|string|null,
* foreign_display: string,
* disp_row: list<non-empty-array>|null,
* foreign_field: mixed
* }]]></code>
</MoreSpecificReturnType>
<PossiblyFalseArgument>
<code><![CDATA[$this->config->selectedServer['column_info']]]></code>
<code><![CDATA[$this->config->selectedServer['column_info']]]></code>
Expand Down Expand Up @@ -3900,7 +3867,7 @@
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<MixedArgument>
<code><![CDATA[$foreignData['foreign_field']]]></code>
<code><![CDATA[$foreignData->foreignField]]></code>
</MixedArgument>
<MixedAssignment>
<code><![CDATA[$isUnsigned]]></code>
Expand Down Expand Up @@ -4285,7 +4252,7 @@
<code><![CDATA[$row[$_POST['criteriaColumnNames'][1]]]]></code>
</InvalidArrayOffset>
<MixedArgument>
<code><![CDATA[$foreignData['foreign_field']]]></code>
<code><![CDATA[$foreignData->foreignField]]></code>
<code><![CDATA[$properties['type']]]></code>
</MixedArgument>
<MixedAssignment>
Expand Down Expand Up @@ -6816,7 +6783,7 @@
<code><![CDATA[$defaultValue]]></code>
<code><![CDATA[$extractedColumnspec['enum_set_values']]]></code>
<code><![CDATA[$extractedColumnspec['spec_in_brackets']]]></code>
<code><![CDATA[$foreignData['foreign_field']]]></code>
<code><![CDATA[$foreignData->foreignField]]></code>
<code><![CDATA[$foreigner['foreign_db']]]></code>
<code><![CDATA[$foreigner['foreign_db']]]></code>
<code><![CDATA[$foreigner['foreign_db']]]></code>
Expand Down Expand Up @@ -6901,8 +6868,6 @@
<RiskyTruthyFalsyComparison>
<code><![CDATA[! $gotoInclude]]></code>
<code><![CDATA[$editField->salt]]></code>
<code><![CDATA[$foreigner]]></code>
<code><![CDATA[$foreigner]]></code>
</RiskyTruthyFalsyComparison>
</file>
<file src="src/IpAllowDeny.php">
Expand Down Expand Up @@ -10853,7 +10818,7 @@
<code><![CDATA[string]]></code>
</InvalidNullableReturnType>
<MixedArgument>
<code><![CDATA[$foreignData['foreign_field']]]></code>
<code><![CDATA[$foreignData->foreignField]]></code>
<code><![CDATA[$numberOfLine / $maxRows]]></code>
<code><![CDATA[$sortCol]]></code>
<code><![CDATA[$statement->expr]]></code>
Expand Down
7 changes: 2 additions & 5 deletions resources/templates/table/browse_foreigners/show_all.twig
@@ -1,5 +1,2 @@
{% if foreign_data.disp_row is iterable and
(show_all and foreign_data.the_total > max_rows) %}
<input class="btn btn-secondary" type="submit" id="foreign_showAll" name="foreign_showAll" value="
{{- t('Show all') }}">
{% endif %}
<input class="btn btn-secondary" type="submit" id="foreign_showAll" name="foreign_showAll" value="
{{- t('Show all') }}">
4 changes: 2 additions & 2 deletions resources/templates/table/search/input_box.twig
@@ -1,11 +1,11 @@
{# Get inputbox based on different column types (Foreign key, geometrical, enum) #}
{% if foreigners and search_column_in_foreigners %}
{% if foreign_data['disp_row'] is iterable %}
{% if foreign_data.dispRow is iterable %}
<select name="criteriaValues[{{ column_index }}]"
id="{{ column_id }}{{ column_index }}">
{{ foreign_dropdown|raw }}
</select>
{% elseif foreign_data['foreign_link'] == true %}
{% elseif foreign_data.foreignLink %}
<input type="text"
id="{{ column_id }}{{ column_index }}"
name="criteriaValues[{{ column_index }}]"
Expand Down
40 changes: 17 additions & 23 deletions src/BrowseForeigners.php
Expand Up @@ -8,6 +8,7 @@
namespace PhpMyAdmin;

use PhpMyAdmin\Config\Settings;
use PhpMyAdmin\ConfigStorage\ForeignData;
use PhpMyAdmin\Theme\ThemeManager;

use function __;
Expand All @@ -16,7 +17,6 @@
use function ceil;
use function floor;
use function htmlspecialchars;
use function is_array;
use function mb_strlen;
use function mb_substr;

Expand Down Expand Up @@ -133,24 +133,25 @@ private function getHtmlForOneKey(
* @param string $db current database
* @param string $table current table
* @param string $field field
* @param mixed[] $foreignData foreign column data
* @param string|null $fieldKey field key
* @param string $currentValue current columns's value
*/
public function getHtmlForRelationalFieldSelection(
string $db,
string $table,
string $field,
array $foreignData,
ForeignData $foreignData,
string|null $fieldKey,
string $currentValue,
): string {
$gotoPage = $this->getHtmlForGotoPage($foreignData);
$foreignShowAll = $this->template->render('table/browse_foreigners/show_all', [
'foreign_data' => $foreignData,
'show_all' => $this->settings->showAll,
'max_rows' => $this->settings->maxRows,
]);
$foreignShowAll = '';
if (
$foreignData->dispRow !== null &&
$this->settings->showAll && $foreignData->theTotal > $this->settings->maxRows
) {
$foreignShowAll = $this->template->render('table/browse_foreigners/show_all');
}

$output = '<form class="ajax" '
. 'id="browse_foreign_form" name="browse_foreign_from" action="'
Expand Down Expand Up @@ -185,7 +186,7 @@ public function getHtmlForRelationalFieldSelection(

$output .= '<table class="table table-striped table-hover" id="browse_foreign_table">' . "\n";

if (! is_array($foreignData['disp_row'])) {
if ($foreignData->dispRow === null) {
return $output . '</tbody>'
. '</table>';
}
Expand All @@ -204,14 +205,9 @@ public function getHtmlForRelationalFieldSelection(

$descriptions = [];
$keys = [];
foreach ($foreignData['disp_row'] as $relrow) {
if ($foreignData['foreign_display'] != false) {
$descriptions[] = $relrow[$foreignData['foreign_display']] ?? '';
} else {
$descriptions[] = '';
}

$keys[] = $relrow[$foreignData['foreign_field']];
foreach ($foreignData->dispRow as $relrow) {
$descriptions[] = $relrow[$foreignData->foreignDisplay] ?? '';
$keys[] = $relrow[$foreignData->foreignField];
}

asort($keys);
Expand Down Expand Up @@ -258,20 +254,18 @@ private function getDescriptionAndTitle(string $description): array

/**
* Function to get html for the goto page option
*
* @param mixed[]|null $foreignData foreign data
*/
private function getHtmlForGotoPage(array|null $foreignData): string
private function getHtmlForGotoPage(ForeignData $foreignData): string
{
isset($_POST['pos']) ? $pos = $_POST['pos'] : $pos = 0;
if ($foreignData === null || ! is_array($foreignData['disp_row'])) {
if ($foreignData->dispRow === null) {
return '';
}

$pageNow = (int) floor($pos / $this->settings->maxRows) + 1;
$nbTotalPage = (int) ceil($foreignData['the_total'] / $this->settings->maxRows);
$nbTotalPage = (int) ceil($foreignData->theTotal / $this->settings->maxRows);

if ($foreignData['the_total'] > $this->settings->maxRows) {
if ($foreignData->theTotal > $this->settings->maxRows) {
return Util::pageselector(
'pos',
$this->settings->maxRows,
Expand Down
18 changes: 18 additions & 0 deletions src/ConfigStorage/ForeignData.php
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\ConfigStorage;

class ForeignData
{
/** @param list<array<array-key, string|null>>|null $dispRow */
public function __construct(
public bool $foreignLink,
public int $theTotal,
public string $foreignDisplay,
public array|null $dispRow,
public mixed $foreignField,
) {
}
}
32 changes: 8 additions & 24 deletions src/ConfigStorage/Relation.php
Expand Up @@ -885,15 +885,6 @@ public function foreignDropdown(
* @param bool $getTotal optional, whether to get total num of rows
* in $foreignData['the_total;]
* (has an effect of performance)
*
* @return array<string, mixed> data about the foreign keys
* @psalm-return array{
* foreign_link: bool,
* the_total: int|string|null,
* foreign_display: string,
* disp_row: list<non-empty-array>|null,
* foreign_field: mixed
* }
*/
public function getForeignData(
array|bool $foreigners,
Expand All @@ -902,7 +893,7 @@ public function getForeignData(
string $foreignFilter,
string $foreignLimit,
bool $getTotal = false,
): array {
): ForeignData {
// we always show the foreign field in the drop-down; if a display
// field is defined, we show it besides the foreign field
$foreignLink = false;
Expand Down Expand Up @@ -966,17 +957,10 @@ public function getForeignData(

if ($foreignFilter !== '') {
$theTotal = $this->dbi->fetchValue('SELECT COUNT(*)' . $fQueryFrom . $fQueryFilter);
if ($theTotal === false) {
$theTotal = 0;
}
}

$disp = $this->dbi->tryQuery($fQueryMain . $fQueryFrom . $fQueryFilter . $fQueryOrder . $fQueryLimit);
if ($disp && $disp->numRows() > 0) {
// If a resultset has been created, pre-cache it in the $disp_row
// array. This helps us from not needing to use mysql_data_seek by
// accessing a pre-cached PHP array. Usually those resultsets are
// not that big, so a performance hit should not be expected.
$dispRow = $disp->fetchAllAssoc();
} else {
// Either no data in the foreign table or
Expand All @@ -996,13 +980,13 @@ public function getForeignData(
->countRecords(true);
}

return [
'foreign_link' => $foreignLink,
'the_total' => $theTotal,
'foreign_display' => is_string($foreignDisplay) ? $foreignDisplay : '',
'disp_row' => $dispRow,
'foreign_field' => $foreignField,
];
return new ForeignData(
$foreignLink,
(int) $theTotal,
is_string($foreignDisplay) ? $foreignDisplay : '',
$dispRow,
$foreignField,
);
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Controllers/Table/SearchController.php
Expand Up @@ -29,7 +29,6 @@
use function __;
use function array_keys;
use function in_array;
use function is_array;
use function mb_strtolower;
use function md5;
use function preg_match;
Expand Down Expand Up @@ -377,12 +376,12 @@ private function getColumnProperties(int $searchIndex, int $columnIndex): array
if (
$this->foreigners
&& $searchColumnInForeigners
&& is_array($foreignData['disp_row'])
&& $foreignData->dispRow !== null
) {
$foreignDropdown = $this->relation->foreignDropdown(
$foreignData['disp_row'],
$foreignData['foreign_field'],
$foreignData['foreign_display'],
$foreignData->dispRow,
$foreignData->foreignField,
$foreignData->foreignDisplay,
'',
Config::getInstance()->settings['ForeignKeyMaxLimit'],
);
Expand Down