Skip to content

Commit

Permalink
Move getSystemSchemas and isSystemSchema to Query\Utilities
Browse files Browse the repository at this point in the history
If you think the class name could be better, feel free to improve it.
Everybody know naming classes is difficult ^^
Signed-off-by: William Desportes <williamdes@wdes.fr>
  • Loading branch information
williamdes committed May 30, 2020
1 parent 312bf57 commit 761045b
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 80 deletions.
3 changes: 2 additions & 1 deletion libraries/classes/CheckUserPrivileges.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace PhpMyAdmin;

use PhpMyAdmin\Query\Utilities;
use function mb_strpos;
use function mb_substr;
use function preg_match;
Expand Down Expand Up @@ -206,7 +207,7 @@ private function analyseShowGrant(): void
$GLOBALS['is_reload_priv'] = false;
$GLOBALS['db_to_create'] = '';
$GLOBALS['dbs_where_create_table_allowed'] = [];
$GLOBALS['dbs_to_test'] = $this->dbi->getSystemSchemas();
$GLOBALS['dbs_to_test'] = Utilities::getSystemSchemas();
$GLOBALS['proc_priv'] = false;
$GLOBALS['db_priv'] = false;
$GLOBALS['col_priv'] = false;
Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpMyAdmin;

use PhpMyAdmin\Query\Utilities;
use function explode;
use function strlen;
use function strpos;
Expand Down Expand Up @@ -59,7 +60,7 @@ public static function database(): void
$response = Response::getInstance();
$is_show_stats = $cfg['ShowStats'];

$db_is_system_schema = $dbi->isSystemSchema($db);
$db_is_system_schema = Utilities::isSystemSchema($db);
if ($db_is_system_schema) {
$is_show_stats = false;
}
Expand Down Expand Up @@ -192,7 +193,7 @@ public static function table(): void

Util::checkParameters(['db', 'table']);

$db_is_system_schema = $dbi->isSystemSchema($db);
$db_is_system_schema = Utilities::isSystemSchema($db);

/**
* Set parameters for links
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpMyAdmin\Operations;
use PhpMyAdmin\Plugins;
use PhpMyAdmin\Plugins\Export\ExportSql;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\Relation;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\Response;
Expand Down Expand Up @@ -292,7 +293,7 @@ public function index(): void
}

$db_collation = $this->dbi->getDbCollation($db);
$is_information_schema = $this->dbi->isSystemSchema($db);
$is_information_schema = Utilities::isSystemSchema($db);

if ($is_information_schema) {
return;
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/Controllers/Server/DatabasesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpMyAdmin\DatabaseInterface;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Message;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\RelationCleanup;
use PhpMyAdmin\ReplicationInfo;
use PhpMyAdmin\Response;
Expand Down Expand Up @@ -438,7 +439,7 @@ private function getDatabases(array $replicationTypes): array
'collation' => [],
'statistics' => $statistics,
'replication' => $replication,
'is_system_schema' => $this->dbi->isSystemSchema(
'is_system_schema' => Utilities::isSystemSchema(
$database['SCHEMA_NAME'],
true
),
Expand Down
47 changes: 2 additions & 45 deletions libraries/classes/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PhpMyAdmin\Query\Cache;
use PhpMyAdmin\Query\Compatibility;
use PhpMyAdmin\Query\Generator as QueryGenerator;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\SqlParser\Context;
use const E_USER_WARNING;
use const LOG_INFO;
Expand Down Expand Up @@ -2156,50 +2157,6 @@ public function getLowerCaseNames()
return $this->_lower_case_table_names;
}

/**
* Get the list of system schemas
*
* @return array list of system schemas
*/
public function getSystemSchemas(): array
{
$schemas = [
'information_schema',
'performance_schema',
'mysql',
'sys',
];
$systemSchemas = [];
foreach ($schemas as $schema) {
if (! $this->isSystemSchema($schema, true)) {
continue;
}

$systemSchemas[] = $schema;
}

return $systemSchemas;
}

/**
* Checks whether given schema is a system schema
*
* @param string $schema_name Name of schema (database) to test
* @param bool $testForMysqlSchema Whether 'mysql' schema should
* be treated the same as IS and DD
*/
public function isSystemSchema(
string $schema_name,
bool $testForMysqlSchema = false
): bool {
$schema_name = strtolower($schema_name);

return $schema_name == 'information_schema'
|| $schema_name == 'performance_schema'
|| ($schema_name == 'mysql' && $testForMysqlSchema)
|| $schema_name == 'sys';
}

/**
* Return connection parameters for the database server
*
Expand Down Expand Up @@ -2774,7 +2731,7 @@ public function getTable(string $db_name, string $table_name): Table
*/
public function getDbCollation(string $db): string
{
if ($this->isSystemSchema($db)) {
if (Utilities::isSystemSchema($db)) {
// We don't have to check the collation of the virtual
// information_schema database: We know it!
return 'utf8_general_ci';
Expand Down
17 changes: 0 additions & 17 deletions libraries/classes/Dbal/DbalInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,23 +544,6 @@ public function getCurrentUserAndHost(): array;
*/
public function getLowerCaseNames();

/**
* Get the list of system schemas
*
* @return array list of system schemas
*/
public function getSystemSchemas(): array;

/**
* Checks whether given schema is a system schema
*
* @param string $schema_name Name of schema (database) to test
* @param bool $testForMysqlSchema Whether 'mysql' schema should
* be treated the same as IS and
* DD
*/
public function isSystemSchema(string $schema_name, bool $testForMysqlSchema = false): bool;

/**
* Return connection parameters for the database server
*
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/Display/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpMyAdmin\Message;
use PhpMyAdmin\Plugins;
use PhpMyAdmin\Plugins\ExportPlugin;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Response;
use PhpMyAdmin\Table;
Expand Down Expand Up @@ -79,7 +80,7 @@ public function getHtmlForSelectOptions($tmpSelect = '')

$databases = [];
foreach ($GLOBALS['dblist']->databases as $currentDb) {
if ($GLOBALS['dbi']->isSystemSchema($currentDb, true)) {
if (Utilities::isSystemSchema($currentDb, true)) {
continue;
}
$isSelected = false;
Expand Down
3 changes: 2 additions & 1 deletion libraries/classes/ListAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace PhpMyAdmin;

use ArrayObject;
use PhpMyAdmin\Query\Utilities;
use function in_array;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getList(): array

$list = [];
foreach ($this as $eachItem) {
if ($GLOBALS['dbi']->isSystemSchema($eachItem)) {
if (Utilities::isSystemSchema($eachItem)) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace PhpMyAdmin;

use PhpMyAdmin\Query\Utilities;
use function array_key_exists;
use function count;
use function in_array;
Expand Down Expand Up @@ -261,7 +262,7 @@ private function _getTableTabs()
/** @var DatabaseInterface $dbi */
global $route, $dbi;

$db_is_system_schema = $dbi->isSystemSchema($this->_db);
$db_is_system_schema = Utilities::isSystemSchema($this->_db);
$tbl_is_view = $dbi->getTable($this->_db, $this->_table)
->isView();
$updatable_view = false;
Expand Down Expand Up @@ -389,7 +390,7 @@ private function _getDbTabs()
/** @var DatabaseInterface $dbi */
global $route, $dbi;

$db_is_system_schema = $dbi->isSystemSchema($this->_db);
$db_is_system_schema = Utilities::isSystemSchema($this->_db);
$num_tables = count($dbi->getTables($this->_db));
$is_superuser = $dbi->isSuperuser();
$isCreateOrGrantUser = $dbi->isUserType('grant')
Expand Down
59 changes: 59 additions & 0 deletions libraries/classes/Query/Utilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\Query;

use function strtolower;

/**
* Some helfull functions for common tasks related to SQL results
*/
class Utilities
{
/**
* Get the list of system schemas
*
* @return string[] list of system schemas
*/
public static function getSystemSchemas(): array
{
$schemas = [
'information_schema',
'performance_schema',
'mysql',
'sys',
];
$systemSchemas = [];
foreach ($schemas as $schema) {
if (! self::isSystemSchema($schema, true)) {
continue;
}

$systemSchemas[] = $schema;
}

return $systemSchemas;
}

/**
* Checks whether given schema is a system schema
*
* @param string $schema_name Name of schema (database) to test
* @param bool $testForMysqlSchema Whether 'mysql' schema should
* be treated the same as IS and DD
*/
public static function isSystemSchema(
string $schema_name,
bool $testForMysqlSchema = false
): bool {
$schema_name = strtolower($schema_name);

$isMySqlSystemSchema = $schema_name === 'mysql' && $testForMysqlSchema;

return $schema_name === 'information_schema'
|| $schema_name === 'performance_schema'
|| $isMySqlSystemSchema
|| $schema_name === 'sys';
}
}
3 changes: 2 additions & 1 deletion libraries/classes/ReplicationGui.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace PhpMyAdmin;

use PhpMyAdmin\Query\Utilities;
use function htmlspecialchars;
use function in_array;
use function is_array;
Expand Down Expand Up @@ -204,7 +205,7 @@ public function getHtmlForReplicationDbMultibox()
{
$databases = [];
foreach ($GLOBALS['dblist']->databases as $database) {
if ($GLOBALS['dbi']->isSystemSchema($database)) {
if (Utilities::isSystemSchema($database)) {
continue;
}

Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpMyAdmin\Display\Results as DisplayResults;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\SqlParser\Statements\AlterStatement;
use PhpMyAdmin\SqlParser\Statements\DropStatement;
use PhpMyAdmin\SqlParser\Statements\SelectStatement;
Expand Down Expand Up @@ -1605,7 +1606,7 @@ private function getHtmlForPreviousUpdateQuery(
private function getMessageIfMissingColumnIndex($table, $database, $editable, $hasUniqueKey): string
{
$output = '';
if (! empty($table) && ($GLOBALS['dbi']->isSystemSchema($database) || ! $editable)) {
if (! empty($table) && (Utilities::isSystemSchema($database) || ! $editable)) {
$output = Message::notice(
sprintf(
__(
Expand Down Expand Up @@ -1789,7 +1790,7 @@ private function getQueryResponseForResultsReturned(
'pview_lnk' => '1',
];

if ($GLOBALS['dbi']->isSystemSchema($db) || ! $editable) {
if (Utilities::isSystemSchema($db) || ! $editable) {
$displayParts = [
'edit_lnk' => $displayResultsObject::NO_EDIT_OR_DELETE,
'del_lnk' => $displayResultsObject::NO_EDIT_OR_DELETE,
Expand Down
5 changes: 3 additions & 2 deletions libraries/classes/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Closure;
use PhpMyAdmin\Html\Generator;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\SqlParser\Context;
use PhpMyAdmin\SqlParser\Token;
use phpseclib\Crypt\Random;
Expand Down Expand Up @@ -293,7 +294,7 @@ private static function _checkRowCount($db, array $table)
// set this because Table::countRecords() can use it
$tbl_is_view = $table['TABLE_TYPE'] == 'VIEW';

if ($tbl_is_view || $GLOBALS['dbi']->isSystemSchema($db)) {
if ($tbl_is_view || Utilities::isSystemSchema($db)) {
$rowCount = $GLOBALS['dbi']
->getTable($db, $table['Name'])
->countRecords();
Expand Down Expand Up @@ -2728,7 +2729,7 @@ public static function getDbInfo($db, ?string $sub_part)
*/
$db_is_system_schema = false;

if ($GLOBALS['dbi']->isSystemSchema($db)) {
if (Utilities::isSystemSchema($db)) {
$is_show_stats = false;
$db_is_system_schema = true;
}
Expand Down
3 changes: 2 additions & 1 deletion test/classes/Dbal/DbiDummyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace PhpMyAdmin\Tests\Dbal;

use PhpMyAdmin\Query\Utilities;
use PhpMyAdmin\Tests\AbstractTestCase;

/**
Expand Down Expand Up @@ -60,7 +61,7 @@ public function testFetch()
*/
public function testSystemSchema($schema, $expected): void
{
$this->assertEquals($expected, $GLOBALS['dbi']->isSystemSchema($schema));
$this->assertEquals($expected, Utilities::isSystemSchema($schema));
}

/**
Expand Down
3 changes: 0 additions & 3 deletions test/classes/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ public function exists($name)
$dbi->expects($this->any())->method('getTablesFull')
->will($this->returnValue($databases));

$dbi->expects($this->any())->method('isSystemSchema')
->will($this->returnValue(false));

$dbi->expects($this->any())->method('numRows')
->will($this->returnValue(20));

Expand Down

0 comments on commit 761045b

Please sign in to comment.