Merge #20127 - Replace getRealSize with ini_parse_quantity

Pull-request: #20127
Fixes: #20115
Signed-off-by: William Desportes <williamdes@wdes.fr>
This commit is contained in:
William Desportes 2026-02-21 15:02:50 +01:00
commit bc057eb914
No known key found for this signature in database
GPG Key ID: 70684F4717D49A31
8 changed files with 56 additions and 103 deletions

View File

@ -1977,12 +1977,6 @@ parameters:
count: 6
path: src/Controllers/Import/ImportController.php
-
message: '#^Casting to string something that''s already string\.$#'
identifier: cast.useless
count: 1
path: src/Controllers/Import/ImportController.php
-
message: '#^Comparison operation "\<\=" between 0 and int\<1, max\> is always true\.$#'
identifier: smallerOrEqual.alwaysTrue
@ -3924,12 +3918,6 @@ parameters:
count: 1
path: src/Core.php
-
message: '#^Only booleans are allowed in a negated boolean, int\|string given\.$#'
identifier: booleanNot.exprNotBoolean
count: 1
path: src/Core.php
-
message: '#^Parameter \#1 \$value of function count expects array\|Countable, mixed given\.$#'
identifier: argument.type
@ -5784,12 +5772,6 @@ parameters:
count: 2
path: src/Export/Export.php
-
message: '#^Casting to string something that''s already string\.$#'
identifier: cast.useless
count: 1
path: src/Export/Export.php
-
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
identifier: empty.notAllowed

View File

@ -1319,9 +1319,6 @@
</UnusedVariable>
</file>
<file src="src/Controllers/Import/ImportController.php">
<ArgumentTypeCoercion>
<code><![CDATA[$memoryLimit]]></code>
</ArgumentTypeCoercion>
<MixedArgumentTypeCoercion>
<code><![CDATA[UrlParams::$params]]></code>
</MixedArgumentTypeCoercion>
@ -2968,9 +2965,6 @@
<code><![CDATA[Config::getInstance()]]></code>
<code><![CDATA[Config::getInstance()]]></code>
</DeprecatedMethod>
<InvalidOperand>
<code><![CDATA[$matches[1]]]></code>
</InvalidOperand>
<MixedArgument>
<code><![CDATA[$path[$depth + 1]]]></code>
</MixedArgument>
@ -4063,9 +4057,6 @@
</RiskyTruthyFalsyComparison>
</file>
<file src="src/Export/Export.php">
<ArgumentTypeCoercion>
<code><![CDATA[(string) ini_get('memory_limit')]]></code>
</ArgumentTypeCoercion>
<MixedArgument>
<code><![CDATA[$dbAlias]]></code>
<code><![CDATA[$dbAlias]]></code>

View File

@ -36,7 +36,6 @@ use function __;
use function _ngettext;
use function in_array;
use function ini_get;
use function ini_parse_quantity;
use function ini_set;
use function is_array;
use function is_link;
@ -343,8 +342,7 @@ final readonly class ImportController implements InvocableController
// We can not read all at once, otherwise we can run out of memory
// Calculate value of the limit
$memoryLimit = (string) ini_get('memory_limit');
$memoryLimit = ini_parse_quantity($memoryLimit);
$memoryLimit = Util::getRealSize(ini_get('memory_limit'));
// 2 MB as default
if ($memoryLimit === 0) {
$memoryLimit = 2 * 1024 * 1024;

View File

@ -119,38 +119,6 @@ class Core
ErrorHandler::getInstance()->addUserError($message, false);
}
/**
* Converts numbers like 10M into bytes
* Used with permission from Moodle (https://moodle.org) by Martin Dougiamas
* (renamed with PMA prefix to avoid double definition when embedded
* in Moodle)
*
* @param string|int $size size (Default = 0)
*/
public static function getRealSize(string|int $size = 0): int
{
if (! $size) {
return 0;
}
$binaryprefixes = [
'T' => 1099511627776,
't' => 1099511627776,
'G' => 1073741824,
'g' => 1073741824,
'M' => 1048576,
'm' => 1048576,
'K' => 1024,
'k' => 1024,
];
if (preg_match('/^([0-9]+)([KMGT])/i', (string) $size, $matches) === 1) {
return (int) ($matches[1] * $binaryprefixes[$matches[2]]);
}
return (int) $size;
}
/**
* Checks if the given $page is index.php and returns true if valid.
* It ignores query parameters in $page (script.php?ignored)

View File

@ -32,7 +32,6 @@ use function http_build_query;
use function implode;
use function in_array;
use function ini_get;
use function ini_parse_quantity;
use function is_array;
use function is_numeric;
use function mb_strlen;
@ -114,7 +113,7 @@ class Export
*/
public function getMemoryLimit(): int
{
$memoryLimit = ini_parse_quantity((string) ini_get('memory_limit'));
$memoryLimit = Util::getRealSize(ini_get('memory_limit'));
// Some of memory is needed for other things and as threshold.
// During export I had allocated (see memory_get_usage function)

View File

@ -40,6 +40,7 @@ use function htmlspecialchars_decode;
use function implode;
use function in_array;
use function ini_get;
use function ini_parse_quantity;
use function is_array;
use function is_object;
use function is_scalar;
@ -1742,16 +1743,29 @@ class Util
$fileSize = '5M';
}
$size = Core::getRealSize($fileSize);
$size = self::getRealSize($fileSize);
$postSize = ini_get('post_max_size');
if ($postSize !== '' && $postSize !== false) {
$size = min($size, Core::getRealSize($postSize));
$size = min($size, self::getRealSize($postSize));
}
return $size;
}
/**
* Converts numbers like 10M into bytes
*/
public static function getRealSize(string $size): int
{
if ($size === '') {
return 0;
}
// Using error suppression operator because ini_parse_quantity() can throw a warning if the value is invalid
return @ini_parse_quantity($size);
}
public static function unquoteDefaultValue(string $value): string
{
if (! str_starts_with($value, "'")) {

View File

@ -14,7 +14,6 @@ use PhpMyAdmin\Tests\Clock\MockClock;
use PhpMyAdmin\Url;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use stdClass;
use function _pgettext;
@ -258,42 +257,6 @@ class CoreTest extends AbstractTestCase
];
}
/**
* Test for Core::getRealSize
*
* @param string $size Size
* @param int $expected Expected value
*/
#[DataProvider('providerTestGetRealSize')]
#[Group('32bit-incompatible')]
public function testGetRealSize(string $size, int $expected): void
{
self::assertSame($expected, Core::getRealSize($size));
}
/**
* Data provider for testGetRealSize
*
* @return array<array{string, int}>
*/
public static function providerTestGetRealSize(): array
{
return [
['0', 0],
['1kb', 1024],
['1024k', 1024 * 1024],
['8m', 8 * 1024 * 1024],
['12gb', 12 * 1024 * 1024 * 1024],
['1024', 1024],
['8000m', 8 * 1000 * 1024 * 1024],
['8G', 8 * 1024 * 1024 * 1024],
['2048', 2048],
['2048K', 2048 * 1024],
['2048K', 2048 * 1024],
['102400K', 102400 * 1024],
];
}
/**
* Test for Core::getPHPDocLink
*/

View File

@ -16,6 +16,7 @@ use PhpMyAdmin\Utils\SessionCache;
use PhpMyAdmin\Version;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use function __;
@ -676,6 +677,43 @@ class UtilTest extends AbstractTestCase
];
}
/**
* Test for Util::getRealSize
*
* @param string $size Size
* @param int $expected Expected value
*/
#[DataProvider('providerTestGetRealSize')]
#[Group('32bit-incompatible')]
public function testGetRealSize(string $size, int $expected): void
{
self::assertSame($expected, Util::getRealSize($size));
}
/**
* Data provider for testGetRealSize
*
* @return array<array{string, int}>
*/
public static function providerTestGetRealSize(): array
{
return [
['', 0],
['0', 0],
['1kb', 1],
['1024k', 1024 * 1024],
['8m', 8 * 1024 * 1024],
['12gb', 12],
['1024', 1024],
['8000m', 8 * 1000 * 1024 * 1024],
['8G', 8 * 1024 * 1024 * 1024],
['2048', 2048],
['2048K', 2048 * 1024],
['2048K', 2048 * 1024],
['102400K', 102400 * 1024],
];
}
/**
* Test for Util::getTitleForTarget
*