diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index fe34fc5..5c85622 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -5,6 +5,7 @@ */ use Drupal\Component\Utility\Crypt; +use Drupal\Component\Utility\Environment; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Timer; @@ -159,13 +160,6 @@ const DRUPAL_AUTHENTICATED_RID = 'authenticated'; /** - * The number of bytes in a kilobyte. - * - * For more information, visit http://en.wikipedia.org/wiki/Kilobyte. - */ -const DRUPAL_KILOBYTE = 1024; - -/** * The maximum number of characters in a module or theme name. */ const DRUPAL_EXTENSION_NAME_MAX_LENGTH = 50; @@ -2383,18 +2377,12 @@ function _drupal_shutdown_function() { * @return * TRUE if there is sufficient memory to allow the operation, or FALSE * otherwise. + * + * @deprecated 8.0 + * Use \Drupal\Component\Utility\Environment::checkMemoryLimit(). */ function drupal_check_memory_limit($required, $memory_limit = NULL) { - if (!isset($memory_limit)) { - $memory_limit = ini_get('memory_limit'); - } - - // There is sufficient memory if: - // - No memory limit is set. - // - The memory limit is set to unlimited (-1). - // - The memory limit is greater than or equal to the memory required for - // the operation. - return ((!$memory_limit) || ($memory_limit == -1) || (parse_size($memory_limit) >= parse_size($required))); + return Environment::checkMemoryLimit($required, $memory_limit); } /** diff --git a/core/includes/common.inc b/core/includes/common.inc index 79b2224..955f69f 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -11,6 +11,7 @@ use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Yaml; use Drupal\Component\Serialization\Exception\InvalidDataTypeException; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\Number; use Drupal\Component\Utility\SortArray; @@ -699,17 +700,12 @@ function format_plural($count, $singular, $plural, array $args = array(), array * * @return * An integer representation of the size in bytes. + * + * @deprecated 8.0 + * Use \Drupal\Component\Utility\Bytes::toInt(). */ function parse_size($size) { - $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. - $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. - if ($unit) { - // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. - return round($size * pow(DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0]))); - } - else { - return round($size); - } + return Bytes::toInt($size); } /** @@ -725,11 +721,11 @@ function parse_size($size) { * A translated string representation of the size. */ function format_size($size, $langcode = NULL) { - if ($size < DRUPAL_KILOBYTE) { + if ($size < Bytes::KILOBYTE) { return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode)); } else { - $size = $size / DRUPAL_KILOBYTE; // Convert bytes to kilobytes. + $size = $size / Bytes::KILOBYTE; // Convert bytes to kilobytes. $units = array( t('@size KB', array(), array('langcode' => $langcode)), t('@size MB', array(), array('langcode' => $langcode)), @@ -741,8 +737,8 @@ function format_size($size, $langcode = NULL) { t('@size YB', array(), array('langcode' => $langcode)), ); foreach ($units as $unit) { - if (round($size, 2) >= DRUPAL_KILOBYTE) { - $size = $size / DRUPAL_KILOBYTE; + if (round($size, 2) >= Bytes::KILOBYTE) { + $size = $size / Bytes::KILOBYTE; } else { break; diff --git a/core/includes/file.inc b/core/includes/file.inc index fc0b72a..02bd107 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -8,6 +8,7 @@ use Drupal\Component\Utility\UrlHelper; use Drupal\Core\StreamWrapper\LocalStream; use Drupal\Component\PhpStorage\FileStorage; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\String; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; @@ -1271,11 +1272,11 @@ function file_upload_max_size() { if ($max_size < 0) { // Start with post_max_size. - $max_size = parse_size(ini_get('post_max_size')); + $max_size = Bytes::toInt(ini_get('post_max_size')); // If upload_max_size is less, then reduce. Except if upload_max_size is // zero, which indicates no limit. - $upload_max = parse_size(ini_get('upload_max_filesize')); + $upload_max = Bytes::toInt(ini_get('upload_max_filesize')); if ($upload_max > 0 && $upload_max < $max_size) { $max_size = $upload_max; } diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php new file mode 100644 index 0000000..0bcac31 --- /dev/null +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -0,0 +1,47 @@ += Bytes::toInt($required))); + } + +} diff --git a/core/modules/color/color.module b/core/modules/color/color.module index 7809f5f..4f1f1fb 100644 --- a/core/modules/color/color.module +++ b/core/modules/color/color.module @@ -5,6 +5,7 @@ */ use Drupal\Core\Asset\CssOptimizer; +use Drupal\Component\Utility\Bytes; use Drupal\Component\Utility\String; /** @@ -377,7 +378,7 @@ function color_scheme_form_submit($form, &$form_state) { // scheme change based on a faulty memory calculation. $usage = memory_get_usage(TRUE); $memory_limit = ini_get('memory_limit'); - $size = parse_size($memory_limit); + $size = Bytes::toInt($memory_limit); if (!drupal_check_memory_limit($usage + $required, $memory_limit)) { drupal_set_message(t('There is not enough memory available to PHP to change this theme\'s color scheme. You need at least %size more. Check the PHP documentation for more information.', array('%size' => format_size($usage + $required - $size), '@url' => 'http://www.php.net/manual/ini.core.php#ini.sect.resource-limits')), 'error'); return; diff --git a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php index 16ada1e..27ce2ab 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/Field/FieldType/FileItem.php @@ -7,6 +7,7 @@ namespace Drupal\file\Plugin\Field\FieldType; +use Drupal\Component\Utility\Bytes; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\TypedData\DataDefinition; @@ -240,13 +241,13 @@ public static function validateExtensions($element, &$form_state) { * Form API callback. * * Ensures that a size has been entered and that it can be parsed by - * parse_size(). + * \Drupal\Component\Utility\Bytes::toInt(). * * This function is assigned as an #element_validate callback in * instanceSettingsForm(). */ public static function validateMaxFilesize($element, &$form_state) { - if (!empty($element['#value']) && !is_numeric(parse_size($element['#value']))) { + if (!empty($element['#value']) && !is_numeric(Bytes::toInt($element['#value']))) { form_error($element, $form_state, t('The "!name" option must contain a valid value. You may either leave the text field empty or enter a string like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes).', array('!name' => t($element['title'])))); } } @@ -284,9 +285,9 @@ public function getUploadValidators() { $settings = $this->getSettings(); // Cap the upload size according to the PHP limit. - $max_filesize = parse_size(file_upload_max_size()); + $max_filesize = Bytes::toInt(file_upload_max_size()); if (!empty($settings['max_filesize'])) { - $max_filesize = min($max_filesize, parse_size($settings['max_filesize'])); + $max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize'])); } // There is always a file size limit due to the PHP server limit. diff --git a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php deleted file mode 100644 index f94c419..0000000 --- a/core/modules/system/lib/Drupal/system/Tests/Bootstrap/MiscUnitTest.php +++ /dev/null @@ -1,46 +0,0 @@ - 'Miscellaneous bootstrap unit tests', - 'description' => 'Test miscellaneous functions in bootstrap.inc.', - 'group' => 'Bootstrap', - ); - } - - /** - * Tests that the drupal_check_memory_limit() function works as expected. - */ - function testCheckMemoryLimit() { - $memory_limit = ini_get('memory_limit'); - // Test that a very reasonable amount of memory is available. - $this->assertTrue(drupal_check_memory_limit('30MB'), '30MB of memory tested available.'); - - // Get the available memory and multiply it by two to make it unreasonably - // high. - $twice_avail_memory = ($memory_limit * 2) . 'MB'; - // The function should always return true if the memory limit is set to -1. - $this->assertTrue(drupal_check_memory_limit($twice_avail_memory, -1), 'drupal_check_memory_limit() returns TRUE when a limit of -1 (none) is supplied'); - - // Test that even though we have 30MB of memory available - the function - // returns FALSE when given an upper limit for how much memory can be used. - $this->assertFalse(drupal_check_memory_limit('30MB', '16MB'), 'drupal_check_memory_limit() returns FALSE with a 16MB upper limit on a 30MB requirement.'); - - // Test that an equal amount of memory to the amount requested returns TRUE. - $this->assertTrue(drupal_check_memory_limit('30MB', '30MB'), 'drupal_check_memory_limit() returns TRUE when requesting 30MB on a 30MB requirement.'); - } -} diff --git a/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php index a8a2662..c170fc4 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Common/SizeUnitTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Common; +use Drupal\Component\Utility\Bytes; use Drupal\simpletest\UnitTestBase; /** @@ -25,7 +26,7 @@ public static function getInfo() { } function setUp() { - $kb = DRUPAL_KILOBYTE; + $kb = Bytes::KILOBYTE; $this->exact_test_cases = array( '1 byte' => 1, '1 KB' => $kb, @@ -63,46 +64,13 @@ function testCommonFormatSize() { } /** - * Checks that parse_size() returns the proper byte sizes. - */ - function testCommonParseSize() { - foreach ($this->exact_test_cases as $string => $size) { - $this->assertEqual( - $parsed_size = parse_size($string), - $size, - $size . ' == ' . $parsed_size . ' (' . $string . ')' - ); - } - - // Some custom parsing tests - $string = '23476892 bytes'; - $this->assertEqual( - ($parsed_size = parse_size($string)), - $size = 23476892, - $string . ' == ' . $parsed_size . ' bytes' - ); - $string = '76MRandomStringThatShouldBeIgnoredByParseSize.'; // 76 MB - $this->assertEqual( - $parsed_size = parse_size($string), - $size = 79691776, - $string . ' == ' . $parsed_size . ' bytes' - ); - $string = '76.24 Giggabyte'; // Misspeld text -> 76.24 GB - $this->assertEqual( - $parsed_size = parse_size($string), - $size = 81862076662, - $string . ' == ' . $parsed_size . ' bytes' - ); - } - - /** - * Cross-tests parse_size() and format_size(). + * Cross-tests Bytes::toInt() and format_size(). */ function testCommonParseSizeFormatSize() { foreach ($this->exact_test_cases as $size) { $this->assertEqual( $size, - ($parsed_size = parse_size($string = format_size($size, NULL))), + ($parsed_size = Bytes::toInt($string = format_size($size, NULL))), $size . ' == ' . $parsed_size . ' (' . $string . ')' ); } diff --git a/core/tests/Drupal/Tests/Component/Utility/BytesTest.php b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php new file mode 100644 index 0000000..a1c4ca6 --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/BytesTest.php @@ -0,0 +1,57 @@ + 'Bytes utility helpers', + 'description' => '', + 'group' => 'Utility', + ); + } + + /** + * Tests Bytes::toInt(). + * + * @dataProvider providerTestToInt + * @covers ::toInt + */ + public function testToInt($size, $expected_int) { + $this->assertEquals($expected_int, Bytes::toInt($size)); + } + + public function providerTestToInt() { + return array( + array('1 byte', 1), + array('1 KB' , Bytes::KILOBYTE), + array('1 MB' , pow(Bytes::KILOBYTE, 2)), + array('1 GB' , pow(Bytes::KILOBYTE, 3)), + array('1 TB' , pow(Bytes::KILOBYTE, 4)), + array('1 PB' , pow(Bytes::KILOBYTE, 5)), + array('1 EB' , pow(Bytes::KILOBYTE, 6)), + array('1 ZB' , pow(Bytes::KILOBYTE, 7)), + array('1 YB' , pow(Bytes::KILOBYTE, 8)), + array('23476892 bytes', 23476892), + array('76MRandomStringThatShouldBeIgnoredByParseSize.', 79691776), // 76 MB + array('76.24 Giggabyte', 81862076662), // 76.24 GB (with typo) + ); + } + +} diff --git a/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php new file mode 100644 index 0000000..c415d8a --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/EnvironmentTest.php @@ -0,0 +1,57 @@ + 'PHP environment utility helpers', + 'description' => '', + 'group' => 'Utility', + ); + } + + /** + * Tests Environment::checkMemoryLimit(). + * + * @dataProvider providerTestCheckMemoryLimit + * @covers ::checkMemoryLimit + */ + public function testCheckMemoryLimit($required, $custom_memory_limit, $expected) { + $actual = Environment::checkMemoryLimit($required, $custom_memory_limit); + $this->assertEquals($expected, $actual); + } + + public function providerTestCheckMemoryLimit() { + $memory_limit = ini_get('memory_limit'); + $twice_avail_memory = ($memory_limit * 2) . 'MB'; + + return array( + // Minimal amount of memory should be available. + array('30MB', NULL, TRUE), + // Exceed a custom (unlimited) memory limit. + array($twice_avail_memory, -1, TRUE), + // Exceed a custom memory limit. + array('30MB', '16MB', FALSE), + // Available = required. + array('30MB', '30MB', TRUE), + ); + } + +}