Skip to content

Commit 81192a1

Browse files
committed
Move DBI getRoutines method to the Routines class
DatabaseInterface::getRoutines -> Database\Routines::getDetails Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
1 parent 746d169 commit 81192a1

7 files changed

Lines changed: 91 additions & 104 deletions

File tree

‎libraries/classes/Controllers/Database/RoutinesController.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __invoke(ServerRequest $request): void
113113
$type = null;
114114
}
115115

116-
$items = $this->dbi->getRoutines($GLOBALS['db'], $type);
116+
$items = Routines::getDetails($this->dbi, $GLOBALS['db'], $type);
117117
$isAjax = $this->response->isAjax() && empty($_REQUEST['ajax_page_request']);
118118

119119
$rows = '';

‎libraries/classes/Database/Routines.php‎

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpMyAdmin\DatabaseInterface;
99
use PhpMyAdmin\Html\Generator;
1010
use PhpMyAdmin\Message;
11+
use PhpMyAdmin\Query\Generator as QueryGenerator;
1112
use PhpMyAdmin\ResponseRenderer;
1213
use PhpMyAdmin\SqlParser\Parser;
1314
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
@@ -18,7 +19,9 @@
1819

1920
use function __;
2021
use function _ngettext;
22+
use function array_column;
2123
use function array_merge;
24+
use function array_multisort;
2225
use function count;
2326
use function explode;
2427
use function htmlentities;
@@ -38,6 +41,7 @@
3841
use function trim;
3942

4043
use const ENT_QUOTES;
44+
use const SORT_ASC;
4145

4246
/**
4347
* Functions for routine management.
@@ -289,7 +293,7 @@ public function handleRequestCreateOrEdit(array $errors, $db)
289293
exit;
290294
}
291295

292-
$routines = $this->dbi->getRoutines($db, $_POST['item_type'], $_POST['item_name']);
296+
$routines = self::getDetails($this->dbi, $db, $_POST['item_type'], $_POST['item_name']);
293297
$routine = $routines[0];
294298
$this->response->addJSON(
295299
'name',
@@ -1597,4 +1601,71 @@ public function export(): void
15971601

15981602
$this->response->addHTML($message->getDisplay());
15991603
}
1604+
1605+
/**
1606+
* returns details about the PROCEDUREs or FUNCTIONs for a specific database
1607+
* or details about a specific routine
1608+
*
1609+
* @param string $db db name
1610+
* @param string|null $which PROCEDURE | FUNCTION or null for both
1611+
* @param string $name name of the routine (to fetch a specific routine)
1612+
*
1613+
* @return array information about PROCEDUREs or FUNCTIONs
1614+
*/
1615+
public static function getDetails(
1616+
DatabaseInterface $dbi,
1617+
string $db,
1618+
?string $which = null,
1619+
string $name = ''
1620+
): array {
1621+
if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1622+
$query = QueryGenerator::getInformationSchemaRoutinesRequest(
1623+
$dbi->escapeString($db),
1624+
isset($which) && in_array($which, ['FUNCTION', 'PROCEDURE']) ? $which : null,
1625+
empty($name) ? null : $dbi->escapeString($name)
1626+
);
1627+
$routines = $dbi->fetchResult($query);
1628+
} else {
1629+
$routines = [];
1630+
1631+
if ($which === 'FUNCTION' || $which == null) {
1632+
$query = 'SHOW FUNCTION STATUS'
1633+
. " WHERE `Db` = '" . $dbi->escapeString($db) . "'";
1634+
if ($name) {
1635+
$query .= " AND `Name` = '"
1636+
. $dbi->escapeString($name) . "'";
1637+
}
1638+
1639+
$routines = $dbi->fetchResult($query);
1640+
}
1641+
1642+
if ($which === 'PROCEDURE' || $which == null) {
1643+
$query = 'SHOW PROCEDURE STATUS'
1644+
. " WHERE `Db` = '" . $dbi->escapeString($db) . "'";
1645+
if ($name) {
1646+
$query .= " AND `Name` = '"
1647+
. $dbi->escapeString($name) . "'";
1648+
}
1649+
1650+
$routines = array_merge($routines, $dbi->fetchResult($query));
1651+
}
1652+
}
1653+
1654+
$ret = [];
1655+
foreach ($routines as $routine) {
1656+
$ret[] = [
1657+
'db' => $routine['Db'],
1658+
'name' => $routine['Name'],
1659+
'type' => $routine['Type'],
1660+
'definer' => $routine['Definer'],
1661+
'returns' => $routine['DTD_IDENTIFIER'] ?? '',
1662+
];
1663+
}
1664+
1665+
// Sort results by name
1666+
$name = array_column($ret, 'name');
1667+
array_multisort($name, SORT_ASC, $ret);
1668+
1669+
return $ret;
1670+
}
16001671
}

‎libraries/classes/DatabaseInterface.php‎

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use function array_diff;
3131
use function array_keys;
3232
use function array_map;
33-
use function array_merge;
3433
use function array_multisort;
3534
use function array_reverse;
3635
use function array_shift;
@@ -41,7 +40,6 @@
4140
use function defined;
4241
use function explode;
4342
use function implode;
44-
use function in_array;
4543
use function is_array;
4644
use function is_int;
4745
use function is_string;
@@ -1525,72 +1523,6 @@ public function getDefinition(
15251523
return is_string($result) ? $result : null;
15261524
}
15271525

1528-
/**
1529-
* returns details about the PROCEDUREs or FUNCTIONs for a specific database
1530-
* or details about a specific routine
1531-
*
1532-
* @param string $db db name
1533-
* @param string|null $which PROCEDURE | FUNCTION or null for both
1534-
* @param string $name name of the routine (to fetch a specific routine)
1535-
*
1536-
* @return array information about PROCEDUREs or FUNCTIONs
1537-
*/
1538-
public function getRoutines(
1539-
string $db,
1540-
?string $which = null,
1541-
string $name = ''
1542-
): array {
1543-
if (! $GLOBALS['cfg']['Server']['DisableIS']) {
1544-
$query = QueryGenerator::getInformationSchemaRoutinesRequest(
1545-
$this->escapeString($db),
1546-
isset($which) && in_array($which, ['FUNCTION', 'PROCEDURE']) ? $which : null,
1547-
empty($name) ? null : $this->escapeString($name)
1548-
);
1549-
$routines = $this->fetchResult($query);
1550-
} else {
1551-
$routines = [];
1552-
1553-
if ($which === 'FUNCTION' || $which == null) {
1554-
$query = 'SHOW FUNCTION STATUS'
1555-
. " WHERE `Db` = '" . $this->escapeString($db) . "'";
1556-
if ($name) {
1557-
$query .= " AND `Name` = '"
1558-
. $this->escapeString($name) . "'";
1559-
}
1560-
1561-
$routines = $this->fetchResult($query);
1562-
}
1563-
1564-
if ($which === 'PROCEDURE' || $which == null) {
1565-
$query = 'SHOW PROCEDURE STATUS'
1566-
. " WHERE `Db` = '" . $this->escapeString($db) . "'";
1567-
if ($name) {
1568-
$query .= " AND `Name` = '"
1569-
. $this->escapeString($name) . "'";
1570-
}
1571-
1572-
$routines = array_merge($routines, $this->fetchResult($query));
1573-
}
1574-
}
1575-
1576-
$ret = [];
1577-
foreach ($routines as $routine) {
1578-
$ret[] = [
1579-
'db' => $routine['Db'],
1580-
'name' => $routine['Name'],
1581-
'type' => $routine['Type'],
1582-
'definer' => $routine['Definer'],
1583-
'returns' => $routine['DTD_IDENTIFIER'] ?? '',
1584-
];
1585-
}
1586-
1587-
// Sort results by name
1588-
$name = array_column($ret, 'name');
1589-
array_multisort($name, SORT_ASC, $ret);
1590-
1591-
return $ret;
1592-
}
1593-
15941526
/**
15951527
* returns details about the EVENTs for a specific database
15961528
*

‎libraries/classes/Dbal/DbalInterface.php‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -461,18 +461,6 @@ public function getDefinition(
461461
$link = DatabaseInterface::CONNECT_USER
462462
): ?string;
463463

464-
/**
465-
* returns details about the PROCEDUREs or FUNCTIONs for a specific database
466-
* or details about a specific routine
467-
*
468-
* @param string $db db name
469-
* @param string|null $which PROCEDURE | FUNCTION or null for both
470-
* @param string $name name of the routine (to fetch a specific routine)
471-
*
472-
* @return array information about ROCEDUREs or FUNCTIONs
473-
*/
474-
public function getRoutines(string $db, ?string $which = null, string $name = ''): array;
475-
476464
/**
477465
* returns details about the EVENTs for a specific database
478466
*

‎libraries/classes/Server/Privileges.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpMyAdmin\ConfigStorage\Features\ConfigurableMenusFeature;
1212
use PhpMyAdmin\ConfigStorage\Relation;
1313
use PhpMyAdmin\ConfigStorage\RelationCleanup;
14+
use PhpMyAdmin\Database\Routines;
1415
use PhpMyAdmin\DatabaseInterface;
1516
use PhpMyAdmin\Dbal\MysqliResult;
1617
use PhpMyAdmin\Dbal\ResultInterface;
@@ -1966,7 +1967,7 @@ public function getHtmlForAllTableSpecificRights(
19661967

19671968
$data['tables'] = $tables;
19681969
} else { // routine
1969-
$routineData = $this->dbi->getRoutines($dbname);
1970+
$routineData = Routines::getDetails($this->dbi, $dbname);
19701971

19711972
$routines = [];
19721973
foreach ($routineData as $routine) {
@@ -3660,7 +3661,7 @@ public function getSqlQueriesForDisplayAndAddUser($username, $hostname, $passwor
36603661
*/
36613662
public function getRoutineType(string $dbname, string $routineName)
36623663
{
3663-
$routineData = $this->dbi->getRoutines($dbname);
3664+
$routineData = Routines::getDetails($this->dbi, $dbname);
36643665
$routineName = mb_strtolower($routineName);
36653666

36663667
foreach ($routineData as $routine) {

‎phpstan-baseline.neon‎

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,6 +2285,11 @@ parameters:
22852285
count: 1
22862286
path: libraries/classes/Database/Routines.php
22872287

2288+
-
2289+
message: "#^Method PhpMyAdmin\\\\Database\\\\Routines\\:\\:getDetails\\(\\) return type has no value type specified in iterable type array\\.$#"
2290+
count: 1
2291+
path: libraries/classes/Database/Routines.php
2292+
22882293
-
22892294
message: "#^Method PhpMyAdmin\\\\Database\\\\Routines\\:\\:getEditorForm\\(\\) has parameter \\$routine with no value type specified in iterable type array\\.$#"
22902295
count: 1
@@ -2545,11 +2550,6 @@ parameters:
25452550
count: 1
25462551
path: libraries/classes/DatabaseInterface.php
25472552

2548-
-
2549-
message: "#^Method PhpMyAdmin\\\\DatabaseInterface\\:\\:getRoutines\\(\\) return type has no value type specified in iterable type array\\.$#"
2550-
count: 1
2551-
path: libraries/classes/DatabaseInterface.php
2552-
25532553
-
25542554
message: "#^Method PhpMyAdmin\\\\DatabaseInterface\\:\\:getTables\\(\\) return type has no value type specified in iterable type array\\.$#"
25552555
count: 1
@@ -2690,11 +2690,6 @@ parameters:
26902690
count: 1
26912691
path: libraries/classes/Dbal/DbalInterface.php
26922692

2693-
-
2694-
message: "#^Method PhpMyAdmin\\\\Dbal\\\\DbalInterface\\:\\:getRoutines\\(\\) return type has no value type specified in iterable type array\\.$#"
2695-
count: 1
2696-
path: libraries/classes/Dbal/DbalInterface.php
2697-
26982693
-
26992694
message: "#^Method PhpMyAdmin\\\\Dbal\\\\DbalInterface\\:\\:getTables\\(\\) return type has no value type specified in iterable type array\\.$#"
27002695
count: 1

‎psalm-baseline.xml‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5251,7 +5251,7 @@
52515251
<code>$stmt-&gt;return-&gt;parameters</code>
52525252
<code>$value</code>
52535253
</MixedArgumentTypeCoercion>
5254-
<MixedArrayAccess occurrences="35">
5254+
<MixedArrayAccess occurrences="40">
52555255
<code>$_POST['funcs'][$routine['item_param_name'][$i]]</code>
52565256
<code>$_POST['funcs'][$routine['item_param_name'][$i]]</code>
52575257
<code>$_POST['params'][$routine['item_param_name'][$i]]</code>
@@ -5263,6 +5263,11 @@
52635263
<code>$priv[5]</code>
52645264
<code>$priv[6]</code>
52655265
<code>$priv[7]</code>
5266+
<code>$routine['DTD_IDENTIFIER']</code>
5267+
<code>$routine['Db']</code>
5268+
<code>$routine['Definer']</code>
5269+
<code>$routine['Name']</code>
5270+
<code>$routine['Type']</code>
52665271
<code>$routine['item_param_dir'][$i]</code>
52675272
<code>$routine['item_param_dir'][$i]</code>
52685273
<code>$routine['item_param_dir'][$routine['item_num_params'] - 1]</code>
@@ -5330,7 +5335,7 @@
53305335
<code>$routine['item_param_type'][$i]</code>
53315336
<code>$routine['item_param_type'][$routine['item_num_params'] - 1]</code>
53325337
</MixedArrayOffset>
5333-
<MixedAssignment occurrences="47">
5338+
<MixedAssignment occurrences="48">
53345339
<code>$GLOBALS['errors']</code>
53355340
<code>$GLOBALS['errors']</code>
53365341
<code>$GLOBALS['errors']</code>
@@ -5368,6 +5373,7 @@
53685373
<code>$retval['item_sqldataaccess']</code>
53695374
<code>$retval['item_type']</code>
53705375
<code>$routine</code>
5376+
<code>$routine</code>
53715377
<code>$routine['item_num_params']</code>
53725378
<code>$routine['item_num_params']</code>
53735379
<code>$routine['item_original_name']</code>
@@ -5624,19 +5630,14 @@
56245630
<code>uksort($eachTables, 'strnatcasecmp')</code>
56255631
<code>usort($tables, 'strnatcasecmp')</code>
56265632
</MixedArgumentTypeCoercion>
5627-
<MixedArrayAccess occurrences="33">
5633+
<MixedArrayAccess occurrences="28">
56285634
<code>$event['Name']</code>
56295635
<code>$event['Status']</code>
56305636
<code>$event['Type']</code>
56315637
<code>$link</code>
56325638
<code>$oneShow['Db']</code>
56335639
<code>$oneShow['Name']</code>
56345640
<code>$oneShow['Type']</code>
5635-
<code>$routine['DTD_IDENTIFIER']</code>
5636-
<code>$routine['Db']</code>
5637-
<code>$routine['Definer']</code>
5638-
<code>$routine['Name']</code>
5639-
<code>$routine['Type']</code>
56405641
<code>$tableData[$sortBy]</code>
56415642
<code>$tableData['Data_length']</code>
56425643
<code>$tableData['Engine']</code>
@@ -5679,7 +5680,7 @@
56795680
<code>$this-&gt;links[$link]</code>
56805681
<code>$this-&gt;links[$link]</code>
56815682
</MixedArrayOffset>
5682-
<MixedAssignment occurrences="36">
5683+
<MixedAssignment occurrences="35">
56835684
<code>$aLength</code>
56845685
<code>$bLength</code>
56855686
<code>$database</code>
@@ -5701,7 +5702,6 @@
57015702
<code>$resultRows[]</code>
57025703
<code>$resultTarget</code>
57035704
<code>$result[]</code>
5704-
<code>$routine</code>
57055705
<code>$table</code>
57065706
<code>$tableData</code>
57075707
<code>$tableData</code>

0 commit comments

Comments
 (0)