Unverified Commit 55c96a6b authored by alexpott's avatar alexpott

Issue #3000057 by kim.pepper, andypost, claudiu.cristea, voleger, dww, Berdir,...

Issue #3000057 by kim.pepper, andypost, claudiu.cristea, voleger, dww, Berdir, larowlan: Deprecate drupal_set_time_limit() and file_upload_max_size() and move to Environment component
parent 0d835977
......@@ -11,16 +11,17 @@
use Drupal\Component\Gettext\PoItem;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SortArray;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\Element\Link;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
/**
* @defgroup php_wrappers PHP wrapper functions
......@@ -407,15 +408,15 @@ function drupal_http_header_attributes(array $attributes = []) {
* indicates unlimited execution time.
*
* @ingroup php_wrappers
*
* @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\Core\Environment::setTimeLimit() instead.
*
* @see https://www.drupal.org/node/3000058
*/
function drupal_set_time_limit($time_limit) {
if (function_exists('set_time_limit')) {
$current = ini_get('max_execution_time');
// Do not set time limit if it is currently unlimited.
if ($current != 0) {
@set_time_limit($time_limit);
}
}
@trigger_error('drupal_set_time_limit() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Environment::setTimeLimit() instead. See https://www.drupal.org/node/3000058.', E_USER_DEPRECATED);
Environment::setTimeLimit($time_limit);
}
/**
......
......@@ -7,7 +7,7 @@
use Drupal\Component\FileSystem\FileSystem as ComponentFileSystem;
use Drupal\Component\PhpStorage\FileStorage;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\Exception\FileWriteException;
......@@ -1048,22 +1048,13 @@ function file_scan_directory($dir, $mask, $options = [], $depth = 0) {
* @return
* A file size limit in bytes based on the PHP upload_max_filesize and
* post_max_size
*
* @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Component\Utility\Environment::getUploadMaxSize() instead.
*/
function file_upload_max_size() {
static $max_size = -1;
if ($max_size < 0) {
// Start with 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 = Bytes::toInt(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
@trigger_error('file_upload_max_size() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Utility\Environment::getUploadMaxSize() instead. See https://www.drupal.org/node/3000058.', E_USER_DEPRECATED);
return Environment::getUploadMaxSize();
}
/**
......
......@@ -37,4 +37,64 @@ public static function checkMemoryLimit($required, $memory_limit = NULL) {
return ((!$memory_limit) || ($memory_limit == -1) || (Bytes::toInt($memory_limit) >= Bytes::toInt($required)));
}
/**
* Attempts to set the PHP maximum execution time.
*
* This function is a wrapper around the PHP function set_time_limit(). When
* called, set_time_limit() restarts the timeout counter from zero. In other
* words, if the timeout is the default 30 seconds, and 25 seconds into script
* execution a call such as set_time_limit(20) is made, the script will run
* for a total of 45 seconds before timing out.
*
* If the current time limit is not unlimited it is possible to decrease the
* total time limit if the sum of the new time limit and the current time
* spent running the script is inferior to the original time limit. It is
* inherent to the way set_time_limit() works, it should rather be called with
* an appropriate value every time you need to allocate a certain amount of
* time to execute a task than only once at the beginning of the script.
*
* Before calling set_time_limit(), we check if this function is available
* because it could be disabled by the server administrator.
*
* @param int $time_limit
* An integer time limit in seconds, or 0 for unlimited execution time.
*
* @return bool
* Whether set_time_limit() was successful or not.
*/
public static function setTimeLimit($time_limit) {
if (function_exists('set_time_limit')) {
$current = ini_get('max_execution_time');
// Do not set time limit if it is currently unlimited.
if ($current != 0) {
return set_time_limit($time_limit);
}
}
return FALSE;
}
/**
* Determines the maximum file upload size by querying the PHP settings.
*
* @return int
* A file size limit in bytes based on the PHP upload_max_filesize and
* post_max_size settings.
*/
public static function getUploadMaxSize() {
static $max_size = -1;
if ($max_size < 0) {
// Start with 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 = Bytes::toInt(ini_get('upload_max_filesize'));
if ($upload_max > 0 && $upload_max < $max_size) {
$max_size = $upload_max;
}
}
return $max_size;
}
}
......@@ -3,16 +3,17 @@
namespace Drupal\Core;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Timer;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\Core\Queue\RequeueException;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\Core\Queue\RequeueException;
use Drupal\Core\Queue\SuspendQueueException;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\Core\Session\AnonymousUserSession;
use Drupal\Core\State\StateInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
......@@ -120,7 +121,7 @@ public function run() {
$this->accountSwitcher->switchTo(new AnonymousUserSession());
// Try to allocate enough time to run all the hook_cron implementations.
drupal_set_time_limit(240);
Environment::setTimeLimit(240);
$return = FALSE;
......
......@@ -3,6 +3,7 @@
namespace Drupal\Core\Form;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\UrlHelper;
......@@ -1390,7 +1391,7 @@ protected function buttonWasClicked($element, FormStateInterface &$form_state) {
* based on the PHP upload_max_filesize and post_max_size.
*/
protected function getFileUploadMaxSize() {
return file_upload_max_size();
return Environment::getUploadMaxSize();
}
/**
......
......@@ -4,6 +4,7 @@
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Config\Development\ConfigSchemaChecker;
use Drupal\Core\Database\Database;
use Drupal\Core\DrupalKernel;
......@@ -620,7 +621,7 @@ protected function prepareEnvironment() {
'hash_salt' => $this->databasePrefix,
]);
drupal_set_time_limit($this->timeLimit);
Environment::setTimeLimit($this->timeLimit);
// Save and clean the shutdown callbacks array because it is static cached
// and will be changed by the test run. Otherwise it will contain callbacks
......
......@@ -5,6 +5,7 @@
* Administration functions for editor.module.
*/
use Drupal\Component\Utility\Environment;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\editor\Entity\Editor;
......@@ -77,7 +78,7 @@ function editor_image_upload_settings_form(Editor $editor) {
'#states' => $show_if_image_uploads_enabled,
];
$default_max_size = format_size(file_upload_max_size());
$default_max_size = format_size(Environment::getUploadMaxSize());
$form['max_size'] = [
'#type' => 'textfield',
'#default_value' => $image_upload['max_size'],
......
......@@ -3,6 +3,7 @@
namespace Drupal\editor\Form;
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
......@@ -91,8 +92,7 @@ public function buildForm(array $form, FormStateInterface $form_state, Editor $e
else {
$max_dimensions = 0;
}
$max_filesize = min(Bytes::toInt($image_upload['max_size']), file_upload_max_size());
$max_filesize = min(Bytes::toInt($image_upload['max_size']), Environment::getUploadMaxSize());
$existing_file = isset($image_element['data-entity-uuid']) ? \Drupal::service('entity.repository')->loadEntityByUuid('file', $image_element['data-entity-uuid']) : NULL;
$fid = $existing_file ? $existing_file->id() : NULL;
......
......@@ -4,6 +4,7 @@
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
......@@ -178,7 +179,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
'#type' => 'textfield',
'#title' => t('Maximum upload size'),
'#default_value' => $settings['max_filesize'],
'#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', ['%limit' => format_size(file_upload_max_size())]),
'#description' => t('Enter a value like "512" (bytes), "80 KB" (kilobytes) or "50 MB" (megabytes) in order to restrict the allowed file size. If left empty the file sizes will be limited only by PHP\'s maximum post and file upload sizes (current limit <strong>%limit</strong>).', ['%limit' => format_size(Environment::getUploadMaxSize())]),
'#size' => 10,
'#element_validate' => [[get_class($this), 'validateMaxFilesize']],
'#weight' => 5,
......@@ -301,7 +302,7 @@ public function getUploadValidators() {
$settings = $this->getSettings();
// Cap the upload size according to the PHP limit.
$max_filesize = Bytes::toInt(file_upload_max_size());
$max_filesize = Bytes::toInt(Environment::getUploadMaxSize());
if (!empty($settings['max_filesize'])) {
$max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
}
......
......@@ -4,6 +4,7 @@
use Drupal\Component\Utility\Bytes;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
......@@ -527,7 +528,7 @@ protected function getUploadValidators(FieldDefinitionInterface $field_definitio
$settings = $field_definition->getSettings();
// Cap the upload size according to the PHP limit.
$max_filesize = Bytes::toInt(file_upload_max_size());
$max_filesize = Bytes::toInt(Environment::getUploadMaxSize());
if (!empty($settings['max_filesize'])) {
$max_filesize = min($max_filesize, Bytes::toInt($settings['max_filesize']));
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\locale\Form;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -99,7 +100,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
$validators = [
'file_validate_extensions' => ['po'],
'file_validate_size' => [file_upload_max_size()],
'file_validate_size' => [Environment::getUploadMaxSize()],
];
$form['file'] = [
'#type' => 'file',
......
......@@ -8,6 +8,7 @@
* API pattern.
*/
use Drupal\Component\Utility\Environment;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
......@@ -1232,7 +1233,7 @@ function node_access_rebuild($batch_mode = FALSE) {
}
else {
// Try to allocate enough time to rebuild node grants
drupal_set_time_limit(240);
Environment::setTimeLimit(240);
// Rebuild newest nodes first so that recent content becomes available
// quickly.
......
......@@ -3,9 +3,10 @@
namespace Drupal\simpletest;
use Drupal\Component\Assertion\Handle;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Render\MarkupInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Database\Database;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Site\Settings;
......@@ -1178,7 +1179,7 @@ private function prepareEnvironment() {
'container_yamls' => [],
]);
drupal_set_time_limit($this->timeLimit);
Environment::setTimeLimit($this->timeLimit);
}
/**
......
......@@ -5,7 +5,7 @@
use Drupal\KernelTests\KernelTestBase;
/**
* Tests legacy functions in common.inc
* Tests legacy functions in common.inc.
*
* @group Common
* @group legacy
......@@ -23,4 +23,11 @@ public function testFormatDate() {
$this->assertEquals('1970-01-01', $date);
}
/**
* @expectedDeprecation drupal_set_time_limit() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Environment::setTimeLimit() instead. See https://www.drupal.org/node/3000058.
*/
public function testDrupalSetTimeLimit() {
drupal_set_time_limit(1000);
}
}
......@@ -90,4 +90,11 @@ public function testDeprecatedFileCreate() {
$this->assertNotNull(file_create_filename('', ''));
}
/**
* @expectedDeprecation file_upload_max_size() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Utility\Environment::getUploadMaxSize() instead. See https://www.drupal.org/node/3000058.
*/
public function testDeprecatedFileUploadMaxSize() {
$this->assertNotNull(file_upload_max_size());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment