Skip to content

Commit fcffcf4

Browse files
Merge pull request #18015 from MauricioFauth/connection-type
Extract connection type constants to the `Dbal\Connection` class
2 parents 6e1198c + 41dec1e commit fcffcf4

49 files changed

Lines changed: 436 additions & 327 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libraries/classes/Bookmark.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use PhpMyAdmin\ConfigStorage\Features\BookmarkFeature;
1111
use PhpMyAdmin\ConfigStorage\Relation;
12+
use PhpMyAdmin\Dbal\Connection;
1213
use PhpMyAdmin\Dbal\DatabaseName;
1314

1415
use function count;
@@ -125,7 +126,7 @@ public function save(): bool
125126
. $this->dbi->quoteString($this->query) . ', '
126127
. $this->dbi->quoteString($this->label) . ')';
127128

128-
return (bool) $this->dbi->query($query, DatabaseInterface::CONNECT_CONTROL);
129+
return (bool) $this->dbi->query($query, Connection::TYPE_CONTROL);
129130
}
130131

131132
/**
@@ -142,7 +143,7 @@ public function delete(): bool
142143
. '.' . Util::backquote($bookmarkFeature->bookmark)
143144
. ' WHERE id = ' . $this->id;
144145

145-
return (bool) $this->dbi->tryQuery($query, DatabaseInterface::CONNECT_CONTROL);
146+
return (bool) $this->dbi->tryQuery($query, Connection::TYPE_CONTROL);
146147
}
147148

148149
/**
@@ -260,7 +261,7 @@ public static function getList(
260261
$query,
261262
null,
262263
null,
263-
DatabaseInterface::CONNECT_CONTROL
264+
Connection::TYPE_CONTROL
264265
);
265266

266267
$bookmarks = [];
@@ -315,7 +316,7 @@ public static function get(
315316
$query .= ' AND ' . Util::backquote($id_field)
316317
. ' = ' . $dbi->quoteString((string) $id) . ' LIMIT 1';
317318

318-
$result = $dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, DatabaseInterface::CONNECT_CONTROL);
319+
$result = $dbi->fetchSingleRow($query, DatabaseInterface::FETCH_ASSOC, Connection::TYPE_CONTROL);
319320
if ($result !== null) {
320321
return self::createFromRow($dbi, $result);
321322
}

libraries/classes/Common.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\Config\ConfigFile;
88
use PhpMyAdmin\ConfigStorage\Relation;
9+
use PhpMyAdmin\Dbal\Connection;
910
use PhpMyAdmin\Dbal\DatabaseName;
1011
use PhpMyAdmin\Dbal\TableName;
1112
use PhpMyAdmin\Exceptions\AuthenticationPluginException;
@@ -611,11 +612,11 @@ private static function connectToDatabaseServer(DatabaseInterface $dbi, Authenti
611612
*/
612613
$controlConnection = null;
613614
if ($GLOBALS['cfg']['Server']['controluser'] !== '') {
614-
$controlConnection = $dbi->connect(DatabaseInterface::CONNECT_CONTROL);
615+
$controlConnection = $dbi->connect(Connection::TYPE_CONTROL);
615616
}
616617

617618
// Connects to the server (validates user's login)
618-
$userConnection = $dbi->connect(DatabaseInterface::CONNECT_USER);
619+
$userConnection = $dbi->connect(Connection::TYPE_USER);
619620
if ($userConnection === null) {
620621
$auth->showFailure('mysql-denied');
621622
}
@@ -628,7 +629,7 @@ private static function connectToDatabaseServer(DatabaseInterface $dbi, Authenti
628629
* Open separate connection for control queries, this is needed to avoid problems with table locking used in
629630
* main connection and phpMyAdmin issuing queries to configuration storage, which is not locked by that time.
630631
*/
631-
$dbi->connect(DatabaseInterface::CONNECT_USER, null, DatabaseInterface::CONNECT_CONTROL);
632+
$dbi->connect(Connection::TYPE_USER, null, Connection::TYPE_CONTROL);
632633
}
633634

634635
public static function getRequest(): ServerRequest

libraries/classes/Config.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace PhpMyAdmin;
66

77
use PhpMyAdmin\Config\Settings;
8+
use PhpMyAdmin\Dbal\Connection;
89
use PhpMyAdmin\Exceptions\ConfigException;
910
use Throwable;
1011

@@ -69,6 +70,8 @@
6970

7071
/**
7172
* Configuration handling
73+
*
74+
* @psalm-import-type ConnectionType from Connection
7275
*/
7376
class Config
7477
{
@@ -1213,9 +1216,9 @@ public function checkServers(): void
12131216
/**
12141217
* Return connection parameters for the database server
12151218
*
1216-
* @param int $mode Connection mode on of CONNECT_USER, CONNECT_CONTROL
1217-
* or CONNECT_AUXILIARY.
1219+
* @param int $mode Connection mode.
12181220
* @param array|null $server Server information like host/port/socket/persistent
1221+
* @psalm-param ConnectionType $mode
12191222
*
12201223
* @return array user, host and server settings array
12211224
*/
@@ -1224,11 +1227,11 @@ public static function getConnectionParams(int $mode, ?array $server = null): ar
12241227
$user = null;
12251228
$password = null;
12261229

1227-
if ($mode == DatabaseInterface::CONNECT_USER) {
1230+
if ($mode == Connection::TYPE_USER) {
12281231
$user = $GLOBALS['cfg']['Server']['user'];
12291232
$password = $GLOBALS['cfg']['Server']['password'];
12301233
$server = $GLOBALS['cfg']['Server'];
1231-
} elseif ($mode == DatabaseInterface::CONNECT_CONTROL) {
1234+
} elseif ($mode == Connection::TYPE_CONTROL) {
12321235
$user = $GLOBALS['cfg']['Server']['controluser'];
12331236
$password = $GLOBALS['cfg']['Server']['controlpass'];
12341237

libraries/classes/ConfigStorage/Relation.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpMyAdmin\ConfigStorage\Features\PdfFeature;
88
use PhpMyAdmin\DatabaseInterface;
9+
use PhpMyAdmin\Dbal\Connection;
910
use PhpMyAdmin\Dbal\DatabaseName;
1011
use PhpMyAdmin\Dbal\TableName;
1112
use PhpMyAdmin\InternalRelations;
@@ -268,7 +269,7 @@ private function checkRelationsParam(): array
268269
if (
269270
$GLOBALS['server'] == 0
270271
|| empty($GLOBALS['cfg']['Server']['pmadb'])
271-
|| ! $this->dbi->selectDb($GLOBALS['cfg']['Server']['pmadb'], DatabaseInterface::CONNECT_CONTROL)
272+
|| ! $this->dbi->selectDb($GLOBALS['cfg']['Server']['pmadb'], Connection::TYPE_CONTROL)
272273
) {
273274
// No server selected -> no bookmark table
274275
// we return the array with the falses in it,
@@ -387,16 +388,16 @@ public function tryUpgradeTransformations(): bool
387388
],
388389
(string) $query
389390
);
390-
$this->dbi->tryMultiQuery($query, DatabaseInterface::CONNECT_CONTROL);
391+
$this->dbi->tryMultiQuery($query, Connection::TYPE_CONTROL);
391392
// skips result sets of query as we are not interested in it
392393
do {
393394
$hasResult = (
394-
$this->dbi->moreResults(DatabaseInterface::CONNECT_CONTROL)
395-
&& $this->dbi->nextResult(DatabaseInterface::CONNECT_CONTROL)
395+
$this->dbi->moreResults(Connection::TYPE_CONTROL)
396+
&& $this->dbi->nextResult(Connection::TYPE_CONTROL)
396397
);
397398
} while ($hasResult);
398399

399-
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
400+
$error = $this->dbi->getError(Connection::TYPE_CONTROL);
400401

401402
// return true if no error exists otherwise false
402403
return empty($error);
@@ -434,7 +435,7 @@ public function getForeigners($db, $table, $column = '', $source = 'both')
434435
$rel_query .= ' AND `master_field` = ' . $this->dbi->quoteString($column);
435436
}
436437

437-
$foreign = $this->dbi->fetchResult($rel_query, 'master_field', null, DatabaseInterface::CONNECT_CONTROL);
438+
$foreign = $this->dbi->fetchResult($rel_query, 'master_field', null, Connection::TYPE_CONTROL);
438439
}
439440

440441
if (($source === 'both' || $source === 'foreign') && strlen($table) > 0) {
@@ -505,7 +506,7 @@ public function getDisplayField($db, $table)
505506
$row = $this->dbi->fetchSingleRow(
506507
$disp_query,
507508
DatabaseInterface::FETCH_ASSOC,
508-
DatabaseInterface::CONNECT_CONTROL
509+
Connection::TYPE_CONTROL
509510
);
510511
if (isset($row['display_field'])) {
511512
return $row['display_field'];
@@ -751,7 +752,7 @@ public function getHistory($username)
751752
WHERE `username` = ' . $this->dbi->quoteString($username) . '
752753
ORDER BY `id` DESC';
753754

754-
return $this->dbi->fetchResult($hist_query, null, null, DatabaseInterface::CONNECT_CONTROL);
755+
return $this->dbi->fetchResult($hist_query, null, null, Connection::TYPE_CONTROL);
755756
}
756757

757758
/**
@@ -777,7 +778,7 @@ public function purgeHistory($username): void
777778
ORDER BY `timevalue` DESC
778779
LIMIT ' . $GLOBALS['cfg']['QueryHistoryMax'] . ', 1';
779780

780-
$max_time = $this->dbi->fetchValue($search_query, 0, DatabaseInterface::CONNECT_CONTROL);
781+
$max_time = $this->dbi->fetchValue($search_query, 0, Connection::TYPE_CONTROL);
781782

782783
if (! $max_time) {
783784
return;
@@ -1346,7 +1347,7 @@ public function createPage(?string $newpage, PdfFeature $pdfFeature, $db): int
13461347
. $this->dbi->quoteString($newpage ?: __('no description')) . ')';
13471348
$this->dbi->tryQueryAsControlUser($ins_query);
13481349

1349-
return $this->dbi->insertId(DatabaseInterface::CONNECT_CONTROL);
1350+
return $this->dbi->insertId(Connection::TYPE_CONTROL);
13501351
}
13511352

13521353
/**
@@ -1526,10 +1527,10 @@ public function createPmaDatabase(string $configurationStorageDbName): bool
15261527
{
15271528
$this->dbi->tryQuery(
15281529
'CREATE DATABASE IF NOT EXISTS ' . Util::backquote($configurationStorageDbName),
1529-
DatabaseInterface::CONNECT_CONTROL
1530+
Connection::TYPE_CONTROL
15301531
);
15311532

1532-
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
1533+
$error = $this->dbi->getError(Connection::TYPE_CONTROL);
15331534
if (! $error) {
15341535
// Re-build the cache to show the list of tables created or not
15351536
// This is the case when the DB could be created but no tables just after
@@ -1586,7 +1587,7 @@ public function fixPmaTables($db, $create = true): void
15861587
'pma__export_templates' => 'export_templates',
15871588
];
15881589

1589-
$existingTables = $this->dbi->getTables($db, DatabaseInterface::CONNECT_CONTROL);
1590+
$existingTables = $this->dbi->getTables($db, Connection::TYPE_CONTROL);
15901591

15911592
/** @var array<string, string> $tableNameReplacements */
15921593
$tableNameReplacements = [];
@@ -1617,16 +1618,16 @@ public function fixPmaTables($db, $create = true): void
16171618
if ($create) {
16181619
if ($createQueries == null) { // first create
16191620
$createQueries = $this->getDefaultPmaTableNames($tableNameReplacements);
1620-
if (! $this->dbi->selectDb($db, DatabaseInterface::CONNECT_CONTROL)) {
1621-
$GLOBALS['message'] = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
1621+
if (! $this->dbi->selectDb($db, Connection::TYPE_CONTROL)) {
1622+
$GLOBALS['message'] = $this->dbi->getError(Connection::TYPE_CONTROL);
16221623

16231624
return;
16241625
}
16251626
}
16261627

1627-
$this->dbi->tryQuery($createQueries[$table], DatabaseInterface::CONNECT_CONTROL);
1628+
$this->dbi->tryQuery($createQueries[$table], Connection::TYPE_CONTROL);
16281629

1629-
$error = $this->dbi->getError(DatabaseInterface::CONNECT_CONTROL);
1630+
$error = $this->dbi->getError(Connection::TYPE_CONTROL);
16301631
if ($error) {
16311632
$GLOBALS['message'] = $error;
16321633

libraries/classes/Controllers/Server/DatabasesController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\CheckUserPrivileges;
99
use PhpMyAdmin\Controllers\AbstractController;
1010
use PhpMyAdmin\DatabaseInterface;
11+
use PhpMyAdmin\Dbal\Connection;
1112
use PhpMyAdmin\Http\ServerRequest;
1213
use PhpMyAdmin\Query\Utilities;
1314
use PhpMyAdmin\ReplicationInfo;
@@ -102,7 +103,7 @@ public function __invoke(ServerRequest $request): void
102103
$this->databases = $this->dbi->getDatabasesFull(
103104
null,
104105
$this->hasStatistics,
105-
DatabaseInterface::CONNECT_USER,
106+
Connection::TYPE_USER,
106107
$this->sortBy,
107108
$this->sortOrder,
108109
$this->position,

libraries/classes/Controllers/Server/UserGroupsFormController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpMyAdmin\ConfigStorage\Relation;
1010
use PhpMyAdmin\Controllers\AbstractController;
1111
use PhpMyAdmin\DatabaseInterface;
12+
use PhpMyAdmin\Dbal\Connection;
1213
use PhpMyAdmin\Http\ServerRequest;
1314
use PhpMyAdmin\ResponseRenderer;
1415
use PhpMyAdmin\Template;
@@ -85,7 +86,7 @@ private function getHtmlToChooseUserGroup(
8586
$userTable,
8687
$this->dbi->escapeString($username)
8788
);
88-
$userGroup = $this->dbi->fetchValue($sqlQuery, 0, DatabaseInterface::CONNECT_CONTROL);
89+
$userGroup = $this->dbi->fetchValue($sqlQuery, 0, Connection::TYPE_CONTROL);
8990

9091
$allUserGroups = [];
9192
$sqlQuery = 'SELECT DISTINCT `usergroup` FROM ' . $groupTable;

0 commit comments

Comments
 (0)