Merge pull request #19144 from kamil-tekiela/UiProperty

Add UiProperty enum
This commit is contained in:
Maurício Meneghini Fauth 2024-05-07 21:33:33 -03:00 committed by GitHub
commit 2a58575683
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 64 additions and 67 deletions

View File

@ -11228,8 +11228,8 @@
<code><![CDATA[$oneField]]></code>
<code><![CDATA[$options['expr']]]></code>
<code><![CDATA[$row['Type']]]></code>
<code><![CDATA[$this->uiprefs[$property]]]></code>
<code><![CDATA[$this->uiprefs[$property]]]></code>
<code><![CDATA[$this->uiprefs[$property->value]]]></code>
<code><![CDATA[$this->uiprefs[$property->value]]]></code>
<code><![CDATA[$value]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>

View File

@ -11,7 +11,7 @@ use PhpMyAdmin\Http\Response;
use PhpMyAdmin\Http\ServerRequest;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use function array_map;
use function explode;
@ -36,14 +36,14 @@ final class ColumnPreferencesController implements InvocableController
$colorder = $request->getParsedBodyParam('col_order');
if (is_string($colorder)) {
$propertyValue = array_map(intval(...), explode(',', $colorder));
$status = $tableObject->setUiProp(Table::PROP_COLUMN_ORDER, $propertyValue, $tableCreateTime);
$status = $tableObject->setUiProp(UiProperty::ColumnOrder, $propertyValue, $tableCreateTime);
}
// set column visibility
$colvisib = $request->getParsedBodyParam('col_visib');
if ($status === true && is_string($colvisib)) {
$propertyValue = array_map(intval(...), explode(',', $colvisib));
$status = $tableObject->setUiProp(Table::PROP_COLUMN_ORDER, $propertyValue, $tableCreateTime);
$status = $tableObject->setUiProp(UiProperty::ColumnVisibility, $propertyValue, $tableCreateTime);
}
if ($status instanceof Message) {

View File

@ -18,6 +18,7 @@ use PhpMyAdmin\Index;
use PhpMyAdmin\Message;
use PhpMyAdmin\ResponseRenderer;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Transformations;
use PhpMyAdmin\Url;
use PhpMyAdmin\UserPrivileges;
@ -102,11 +103,11 @@ final class SaveController implements InvocableController
);
// find the remembered sort expression
$sortedCol = $this->tableObj->getUiProp(Table::PROP_SORTED_COLUMN);
$sortedCol = $this->tableObj->getUiProp(UiProperty::SortedColumn);
// if the old column name is part of the remembered sort expression
if (str_contains((string) $sortedCol, Util::backquote($_POST['field_orig'][$i]))) {
// delete the whole remembered sort expression
$this->tableObj->removeUiProp(Table::PROP_SORTED_COLUMN);
$this->tableObj->removeUiProp(UiProperty::SortedColumn);
}
if (

View File

@ -29,6 +29,7 @@ use PhpMyAdmin\SqlParser\Utils\Query;
use PhpMyAdmin\SqlParser\Utils\StatementInfo;
use PhpMyAdmin\SqlParser\Utils\StatementType;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Template;
use PhpMyAdmin\Theme\ThemeManager;
use PhpMyAdmin\Transformations;
@ -2391,26 +2392,26 @@ class Results
if ($this->isSelect($statementInfo)) {
$pmatable = new Table($this->table, $this->db, $this->dbi);
$colOrder = $pmatable->getUiProp(Table::PROP_COLUMN_ORDER);
$colOrder = $pmatable->getUiProp(UiProperty::ColumnOrder);
$fieldsCount = count($this->fieldsMeta);
/* Validate the value */
if (is_array($colOrder)) {
foreach ($colOrder as $value) {
if ($value >= $fieldsCount) {
$pmatable->removeUiProp(Table::PROP_COLUMN_ORDER);
$pmatable->removeUiProp(UiProperty::ColumnOrder);
break;
}
}
if ($fieldsCount !== count($colOrder)) {
$pmatable->removeUiProp(Table::PROP_COLUMN_ORDER);
$pmatable->removeUiProp(UiProperty::ColumnOrder);
$colOrder = false;
}
}
$colVisib = $pmatable->getUiProp(Table::PROP_COLUMN_VISIB);
$colVisib = $pmatable->getUiProp(UiProperty::ColumnVisibility);
if (is_array($colVisib) && $fieldsCount !== count($colVisib)) {
$pmatable->removeUiProp(Table::PROP_COLUMN_VISIB);
$pmatable->removeUiProp(UiProperty::ColumnVisibility);
$colVisib = false;
}
}

View File

@ -24,6 +24,7 @@ use PhpMyAdmin\SqlParser\Utils\Query;
use PhpMyAdmin\SqlParser\Utils\StatementInfo;
use PhpMyAdmin\SqlParser\Utils\StatementType;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Utils\ForeignKey;
use function __;
@ -82,7 +83,7 @@ class Sql
if (! $statementInfo->flags->order) {
// Retrieving the name of the column we should sort after.
$sortCol = $tableObject->getUiProp(Table::PROP_SORTED_COLUMN);
$sortCol = $tableObject->getUiProp(UiProperty::SortedColumn);
if (empty($sortCol)) {
return $statementInfo;
}
@ -106,7 +107,7 @@ class Sql
} else {
// Store the remembered table into session.
$tableObject->setUiProp(
Table::PROP_SORTED_COLUMN,
UiProperty::SortedColumn,
Query::getClause(
$statementInfo->statement,
$statementInfo->parser->list,

View File

@ -72,13 +72,6 @@ use function trim;
*/
class Table implements Stringable
{
/**
* UI preferences properties
*/
public const PROP_SORTED_COLUMN = 'sorted_col';
public const PROP_COLUMN_ORDER = 'col_order';
public const PROP_COLUMN_VISIB = 'col_visib';
/** @var mixed[] UI preferences */
public array $uiprefs = [];
@ -1677,28 +1670,22 @@ class Table implements Stringable
/**
* Get a property from UI preferences.
* Return false if the property is not found.
* Available property:
* - PROP_SORTED_COLUMN
* - PROP_COLUMN_ORDER
* - PROP_COLUMN_VISIB
*
* @param string $property property
*/
public function getUiProp(string $property): mixed
public function getUiProp(UiProperty $property): mixed
{
if ($this->uiprefs === []) {
$this->loadUiPrefs();
}
// do checking based on property
if ($property === self::PROP_SORTED_COLUMN) {
if (! isset($this->uiprefs[$property])) {
if ($property === UiProperty::SortedColumn) {
if (! isset($this->uiprefs[$property->value])) {
return false;
}
if (! isset($_POST['discard_remembered_sort'])) {
// check if the column name exists in this table
$tmp = explode(' ', $this->uiprefs[$property]);
$tmp = explode(' ', $this->uiprefs[$property->value]);
$colname = $tmp[0];
//remove backquoting from colname
$colname = str_replace('`', '', $colname);
@ -1708,7 +1695,7 @@ class Table implements Stringable
foreach ($availColumns as $eachCol) {
// check if $each_col ends with $colname
if (substr_compare($eachCol, $colname, mb_strlen($eachCol) - mb_strlen($colname)) === 0) {
return $this->uiprefs[$property];
return $this->uiprefs[$property->value];
}
}
}
@ -1719,47 +1706,43 @@ class Table implements Stringable
return false;
}
if ($property === self::PROP_COLUMN_ORDER || $property === self::PROP_COLUMN_VISIB) {
if ($this->isView() || ! isset($this->uiprefs[$property])) {
return false;
}
// check if the table has not been modified
if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) {
return array_map(intval(...), $this->uiprefs[$property]);
}
// remove the property, since the table has been modified
$this->removeUiProp($property);
if ($this->isView() || ! isset($this->uiprefs[$property->value])) {
return false;
}
// default behaviour for other property:
return $this->uiprefs[$property] ?? false;
// check if the table has not been modified
if ($this->getStatusInfo('Create_time') == $this->uiprefs['CREATE_TIME']) {
return array_map(intval(...), $this->uiprefs[$property->value]);
}
// remove the property, since the table has been modified
$this->removeUiProp($property);
return false;
}
/**
* Set a property from UI preferences.
* If pmadb and table_uiprefs is set, it will save the UI preferences to
* phpMyAdmin database.
* Available property:
* - PROP_SORTED_COLUMN
* - PROP_COLUMN_ORDER
* - PROP_COLUMN_VISIB
*
* @param string $property Property
* @param mixed $value Value for the property
* @param string|null $tableCreateTime Needed for PROP_COLUMN_ORDER and PROP_COLUMN_VISIB
* @param int[]|string $value Value for the property
* @param string|null $tableCreateTime Needed for PROP_COLUMN_ORDER and PROP_COLUMN_VISIB
*/
public function setUiProp(string $property, mixed $value, string|null $tableCreateTime = null): bool|Message
{
public function setUiProp(
UiProperty $property,
array|string $value,
string|null $tableCreateTime = null,
): bool|Message {
if ($this->uiprefs === []) {
$this->loadUiPrefs();
}
// we want to save the create time if the property is PROP_COLUMN_ORDER
if (! $this->isView() && ($property === self::PROP_COLUMN_ORDER || $property === self::PROP_COLUMN_VISIB)) {
if (
! $this->isView()
&& ($property === UiProperty::ColumnOrder || $property === UiProperty::ColumnVisibility)
) {
$currCreateTime = $this->getStatusInfo('CREATE_TIME');
if ($tableCreateTime === null || $tableCreateTime != $currCreateTime) {
// there is no $table_create_time, or
@ -1772,7 +1755,7 @@ class Table implements Stringable
'not be persistent after you refresh this page. ' .
'Please check if the table structure has been changed.',
),
$property,
$property->value,
),
);
}
@ -1781,7 +1764,7 @@ class Table implements Stringable
}
// save the value
$this->uiprefs[$property] = $value;
$this->uiprefs[$property->value] = $value;
// check if pmadb is set
$uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature;
@ -1795,18 +1778,16 @@ class Table implements Stringable
/**
* Remove a property from UI preferences.
*
* @param string $property the property
*
* @return true|Message
*/
public function removeUiProp(string $property): bool|Message
public function removeUiProp(UiProperty $property): bool|Message
{
if ($this->uiprefs === []) {
$this->loadUiPrefs();
}
if (isset($this->uiprefs[$property])) {
unset($this->uiprefs[$property]);
if (isset($this->uiprefs[$property->value])) {
unset($this->uiprefs[$property->value]);
// check if pmadb is set
$uiPreferencesFeature = $this->relation->getRelationParameters()->uiPreferencesFeature;

12
src/Table/UiProperty.php Normal file
View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Table;
enum UiProperty: string
{
case SortedColumn = 'sorted_col';
case ColumnOrder = 'col_order';
case ColumnVisibility = 'col_visib';
}

View File

@ -13,6 +13,7 @@ use PhpMyAdmin\ListDatabase;
use PhpMyAdmin\Query\Cache;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\Table\Table;
use PhpMyAdmin\Table\UiProperty;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\FieldHelper;
use PhpMyAdmin\Tests\Stubs\DbiDummy;
@ -1281,17 +1282,17 @@ class TableTest extends AbstractTestCase
$table = new Table($tableName, $db, $this->mockedDbi);
$property = Table::PROP_COLUMN_ORDER;
$property = UiProperty::ColumnOrder;
$value = 'UiProp_value';
$tableCreateTime = null;
$table->setUiProp($property, $value, $tableCreateTime);
//set UI prop successfully
self::assertSame($value, $table->uiprefs[$property]);
self::assertSame($value, $table->uiprefs[$property->value]);
//removeUiProp
$table->removeUiProp($property);
$isDefineProperty = isset($table->uiprefs[$property]);
$isDefineProperty = isset($table->uiprefs[$property->value]);
self::assertFalse($isDefineProperty);
//getUiProp after removeUiProp