Merge pull request #18885 from MauricioFauth/database-list-refactor
Refactor ListDatabase class
This commit is contained in:
commit
0211e32549
@ -5099,6 +5099,7 @@
|
||||
<code>Config::getInstance()</code>
|
||||
<code>Config::getInstance()</code>
|
||||
<code>Config::getInstance()</code>
|
||||
<code>Config::getInstance()</code>
|
||||
</DeprecatedMethod>
|
||||
<DeprecatedProperty>
|
||||
<code>Routing::$route</code>
|
||||
@ -7026,13 +7027,6 @@
|
||||
</PossiblyNullOperand>
|
||||
</file>
|
||||
<file src="src/ListDatabase.php">
|
||||
<DeprecatedMethod>
|
||||
<code>Config::getInstance()</code>
|
||||
<code>Config::getInstance()</code>
|
||||
<code>Config::getInstance()</code>
|
||||
<code>DatabaseInterface::getInstance()</code>
|
||||
<code>DatabaseInterface::getInstance()</code>
|
||||
</DeprecatedMethod>
|
||||
<MixedArgument>
|
||||
<code>$db</code>
|
||||
</MixedArgument>
|
||||
@ -7042,6 +7036,9 @@
|
||||
<MixedAssignment>
|
||||
<code>$db</code>
|
||||
</MixedAssignment>
|
||||
<UnusedProperty>
|
||||
<code>$checkUserPrivileges</code>
|
||||
</UnusedProperty>
|
||||
</file>
|
||||
<file src="src/Menu.php">
|
||||
<DeprecatedMethod>
|
||||
@ -14451,7 +14448,6 @@
|
||||
<file src="tests/classes/ListDatabaseTest.php">
|
||||
<DeprecatedMethod>
|
||||
<code>Config::getInstance()</code>
|
||||
<code>Config::getInstance()</code>
|
||||
</DeprecatedMethod>
|
||||
</file>
|
||||
<file src="tests/classes/MenuTest.php">
|
||||
|
||||
@ -2073,7 +2073,7 @@ class DatabaseInterface implements DbalInterface
|
||||
public function getDatabaseList(): ListDatabase
|
||||
{
|
||||
if ($this->databaseList === null) {
|
||||
$this->databaseList = new ListDatabase();
|
||||
$this->databaseList = new ListDatabase($this, Config::getInstance(), new CheckUserPrivileges($this));
|
||||
}
|
||||
|
||||
return $this->databaseList;
|
||||
|
||||
@ -1,42 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin;
|
||||
|
||||
use ArrayObject;
|
||||
|
||||
use function in_array;
|
||||
|
||||
/** @extends ArrayObject<int, string> */
|
||||
abstract class ListAbstract extends ArrayObject
|
||||
{
|
||||
/**
|
||||
* Checks if the given strings exists in the current list, if there is
|
||||
* missing at least one item it returns false otherwise true
|
||||
*/
|
||||
public function exists(string ...$params): bool
|
||||
{
|
||||
$elements = $this->getArrayCopy();
|
||||
foreach ($params as $param) {
|
||||
if (! in_array($param, $elements, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns default item
|
||||
*/
|
||||
public function getDefault(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* builds up the list
|
||||
*/
|
||||
abstract public function build(): void;
|
||||
}
|
||||
@ -4,9 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin;
|
||||
|
||||
use ArrayObject;
|
||||
use PhpMyAdmin\Query\Utilities;
|
||||
|
||||
use function array_merge;
|
||||
use function in_array;
|
||||
use function is_array;
|
||||
use function is_string;
|
||||
use function preg_match;
|
||||
@ -17,22 +19,20 @@ use function strtr;
|
||||
use function usort;
|
||||
|
||||
/**
|
||||
* handles database lists
|
||||
* Handles database lists
|
||||
*
|
||||
* <code>
|
||||
* $ListDatabase = new ListDatabase();
|
||||
* </code>
|
||||
*
|
||||
* @todo this object should be attached to the PMA_Server object
|
||||
* @extends ArrayObject<int, string>
|
||||
*/
|
||||
class ListDatabase extends ListAbstract
|
||||
class ListDatabase extends ArrayObject
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
public function __construct(
|
||||
private readonly DatabaseInterface $dbi,
|
||||
private readonly Config $config,
|
||||
private readonly CheckUserPrivileges $checkUserPrivileges,
|
||||
) {
|
||||
parent::__construct();
|
||||
|
||||
$checkUserPrivileges = new CheckUserPrivileges(DatabaseInterface::getInstance());
|
||||
$checkUserPrivileges->getPrivileges();
|
||||
$this->checkUserPrivileges->getPrivileges();
|
||||
|
||||
$this->build();
|
||||
}
|
||||
@ -40,15 +40,13 @@ class ListDatabase extends ListAbstract
|
||||
/** @return array<int, array<string, bool|string>> */
|
||||
public function getList(): array
|
||||
{
|
||||
$selected = $this->getDefault();
|
||||
|
||||
$list = [];
|
||||
foreach ($this as $eachItem) {
|
||||
if (Utilities::isSystemSchema($eachItem)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$list[] = ['name' => $eachItem, 'is_selected' => $selected === $eachItem];
|
||||
$list[] = ['name' => $eachItem, 'is_selected' => $eachItem === Current::$database];
|
||||
}
|
||||
|
||||
return $list;
|
||||
@ -59,13 +57,12 @@ class ListDatabase extends ListAbstract
|
||||
*/
|
||||
protected function checkHideDatabase(): void
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
if (empty($config->selectedServer['hide_db'])) {
|
||||
if (empty($this->config->selectedServer['hide_db'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->getArrayCopy() as $key => $db) {
|
||||
if (! preg_match('/' . $config->selectedServer['hide_db'] . '/', $db)) {
|
||||
if (! preg_match('/' . $this->config->selectedServer['hide_db'] . '/', $db)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -84,8 +81,7 @@ class ListDatabase extends ListAbstract
|
||||
{
|
||||
$databaseList = [];
|
||||
$command = '';
|
||||
$config = Config::getInstance();
|
||||
if (! $config->selectedServer['DisableIS']) {
|
||||
if (! $this->config->selectedServer['DisableIS']) {
|
||||
$command .= 'SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`';
|
||||
if ($likeDbName !== null) {
|
||||
$command .= " WHERE `SCHEMA_NAME` LIKE '" . $likeDbName . "'";
|
||||
@ -105,10 +101,10 @@ class ListDatabase extends ListAbstract
|
||||
}
|
||||
|
||||
if ($command !== '') {
|
||||
$databaseList = DatabaseInterface::getInstance()->fetchResult($command);
|
||||
$databaseList = $this->dbi->fetchResult($command);
|
||||
}
|
||||
|
||||
if ($config->settings['NaturalOrder']) {
|
||||
if ($this->config->settings['NaturalOrder']) {
|
||||
usort($databaseList, strnatcasecmp(...));
|
||||
} else {
|
||||
// need to sort anyway, otherwise information_schema
|
||||
@ -137,18 +133,19 @@ class ListDatabase extends ListAbstract
|
||||
*/
|
||||
protected function checkOnlyDatabase(): bool
|
||||
{
|
||||
$config = Config::getInstance();
|
||||
if (is_string($config->selectedServer['only_db']) && strlen($config->selectedServer['only_db']) > 0) {
|
||||
$config->selectedServer['only_db'] = [$config->selectedServer['only_db']];
|
||||
if (
|
||||
is_string($this->config->selectedServer['only_db']) && strlen($this->config->selectedServer['only_db']) > 0
|
||||
) {
|
||||
$this->config->selectedServer['only_db'] = [$this->config->selectedServer['only_db']];
|
||||
}
|
||||
|
||||
if (! is_array($config->selectedServer['only_db'])) {
|
||||
if (! is_array($this->config->selectedServer['only_db'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$items = [];
|
||||
|
||||
foreach ($config->selectedServer['only_db'] as $eachOnlyDb) {
|
||||
foreach ($this->config->selectedServer['only_db'] as $eachOnlyDb) {
|
||||
// check if the db name contains wildcard,
|
||||
// thus containing not escaped _ or %
|
||||
if (! preg_match('/(^|[^\\\\])(_|%)/', $eachOnlyDb)) {
|
||||
@ -166,16 +163,18 @@ class ListDatabase extends ListAbstract
|
||||
}
|
||||
|
||||
/**
|
||||
* returns default item
|
||||
*
|
||||
* @return string default item
|
||||
* Checks if the given strings exists in the current list, if there is
|
||||
* missing at least one item it returns false otherwise true
|
||||
*/
|
||||
public function getDefault(): string
|
||||
public function exists(string ...$params): bool
|
||||
{
|
||||
if (Current::$database !== '') {
|
||||
return Current::$database;
|
||||
$elements = $this->getArrayCopy();
|
||||
foreach ($params as $param) {
|
||||
if (! in_array($param, $elements, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return parent::getDefault();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,9 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Tests;
|
||||
|
||||
use PhpMyAdmin\CheckUserPrivileges;
|
||||
use PhpMyAdmin\Config;
|
||||
use PhpMyAdmin\Current;
|
||||
use PhpMyAdmin\DatabaseInterface;
|
||||
use PhpMyAdmin\ListDatabase;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
|
||||
@ -25,20 +25,11 @@ class ListDatabaseTest extends AbstractTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
DatabaseInterface::$instance = $this->createDatabaseInterface();
|
||||
$config = Config::getInstance();
|
||||
$dbi = $this->createDatabaseInterface();
|
||||
$config = new Config();
|
||||
$config->selectedServer['DisableIS'] = false;
|
||||
$config->selectedServer['only_db'] = ['single\\_db'];
|
||||
$this->object = new ListDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for ListDatabase::getDefault
|
||||
*/
|
||||
public function testEmpty(): void
|
||||
{
|
||||
$arr = new ListDatabase();
|
||||
$this->assertEquals('', $arr->getDefault());
|
||||
$this->object = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,13 +37,21 @@ class ListDatabaseTest extends AbstractTestCase
|
||||
*/
|
||||
public function testExists(): void
|
||||
{
|
||||
$arr = new ListDatabase();
|
||||
$dbi = $this->createDatabaseInterface();
|
||||
$config = new Config();
|
||||
$config->selectedServer['DisableIS'] = false;
|
||||
$config->selectedServer['only_db'] = ['single\\_db'];
|
||||
$arr = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi));
|
||||
$this->assertTrue($arr->exists('single_db'));
|
||||
}
|
||||
|
||||
public function testGetList(): void
|
||||
{
|
||||
$arr = new ListDatabase();
|
||||
$dbi = $this->createDatabaseInterface();
|
||||
$config = new Config();
|
||||
$config->selectedServer['DisableIS'] = false;
|
||||
$config->selectedServer['only_db'] = ['single\\_db'];
|
||||
$arr = new ListDatabase($dbi, $config, new CheckUserPrivileges($dbi));
|
||||
|
||||
Current::$database = 'db';
|
||||
$this->assertEquals(
|
||||
@ -83,22 +82,4 @@ class ListDatabaseTest extends AbstractTestCase
|
||||
'',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for getDefault
|
||||
*/
|
||||
public function testGetDefault(): void
|
||||
{
|
||||
Current::$database = '';
|
||||
$this->assertEquals(
|
||||
$this->object->getDefault(),
|
||||
'',
|
||||
);
|
||||
|
||||
Current::$database = 'mysql';
|
||||
$this->assertEquals(
|
||||
$this->object->getDefault(),
|
||||
'mysql',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user