Issue #3397575 by kim.pepper, sourabhjain, alexpott, longwave: Deprecate file_progress_implementation() and replace with extension_loaded('uploadprogress')

merge-requests/5841/head
Alex Pott 2023-12-16 16:58:44 +00:00
parent de1a11ea78
commit 0f7d46f9ff
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
8 changed files with 162 additions and 64 deletions

View File

@ -65,53 +65,43 @@ function file_schema() {
function file_requirements($phase) {
$requirements = [];
// Check the server's ability to indicate upload progress.
if ($phase == 'runtime') {
$description = NULL;
$implementation = file_progress_implementation();
// The environment variable might be missing. Fallback to an empty string to
// prevent passing NULL to preg_match(), a few lines later.
$server_software = \Drupal::request()->server->get('SERVER_SOFTWARE', '');
// Test the web server identity.
if (preg_match("/Nginx/i", $server_software)) {
$is_nginx = TRUE;
$is_apache = FALSE;
$fastcgi = FALSE;
}
elseif (preg_match("/Apache/i", $server_software)) {
$is_nginx = FALSE;
$is_apache = TRUE;
$fastcgi = str_contains($server_software, 'mod_fastcgi') || str_contains($server_software, 'mod_fcgi');
}
else {
$is_nginx = FALSE;
$is_apache = FALSE;
$fastcgi = FALSE;
}
if (!$is_apache && !$is_nginx) {
$value = t('Not enabled');
$description = t('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php or Nginx with PHP-FPM.');
}
elseif ($fastcgi) {
$value = t('Not enabled');
$description = t('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php or PHP-FPM and not as FastCGI.');
}
elseif (!$implementation) {
$value = t('Not enabled');
$description = t('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a>.');
}
elseif ($implementation == 'uploadprogress') {
$value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)');
}
$requirements['file_progress'] = [
'title' => t('Upload progress'),
'value' => $value,
'description' => $description,
];
if ($phase != 'runtime') {
return $requirements;
}
$server_software = \Drupal::request()->server->get('SERVER_SOFTWARE', '');
// Get the web server identity.
$is_nginx = preg_match("/Nginx/i", $server_software);
$is_apache = preg_match("/Apache/i", $server_software);
$fastcgi = $is_apache && ((str_contains($server_software, 'mod_fastcgi') || str_contains($server_software, 'mod_fcgi')));
// Check the uploadprogress extension is loaded.
if (extension_loaded('uploadprogress')) {
$value = t('Enabled (<a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress</a>)');
$description = NULL;
}
else {
$value = t('Not enabled');
$description = t('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a>.');
}
// Adjust the requirement depending on what the server supports.
if (!$is_apache && !$is_nginx) {
$value = t('Not enabled');
$description = t('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php or Nginx with PHP-FPM.');
}
elseif ($fastcgi) {
$value = t('Not enabled');
$description = t('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php or PHP-FPM and not as FastCGI.');
}
$requirements['file_progress'] = [
'title' => t('Upload progress'),
'value' => $value,
'description' => $description,
];
return $requirements;
}

View File

@ -723,8 +723,14 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL
* @return string|false
* A string indicating which upload progress system is available. Either "apc"
* or "uploadprogress". If neither are available, returns FALSE.
*
* @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use
* extension_loaded('uploadprogress') instead.
*
* @see https://www.drupal.org/node/3397577
*/
function file_progress_implementation() {
@trigger_error(__FUNCTION__ . '() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use extension_loaded(\'uploadprogress\') instead. See https://www.drupal.org/node/3397577', E_USER_DEPRECATED);
static $implementation;
if (!isset($implementation)) {
$implementation = FALSE;

View File

@ -27,8 +27,7 @@ class FileWidgetAjaxController {
'percentage' => -1,
];
$implementation = file_progress_implementation();
if ($implementation == 'uploadprogress') {
if (extension_loaded('uploadprogress')) {
$status = uploadprogress_get_info($key);
if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
$progress['message'] = t('Uploading... (@current of @total)', [

View File

@ -276,19 +276,17 @@ class ManagedFile extends FormElement {
];
// Add progress bar support to the upload if possible.
if ($element['#progress_indicator'] == 'bar' && $implementation = file_progress_implementation()) {
if ($element['#progress_indicator'] == 'bar' && extension_loaded('uploadprogress')) {
$upload_progress_key = mt_rand();
if ($implementation == 'uploadprogress') {
$element['UPLOAD_IDENTIFIER'] = [
'#type' => 'hidden',
'#value' => $upload_progress_key,
'#attributes' => ['class' => ['file-progress']],
// Uploadprogress extension requires this field to be at the top of
// the form.
'#weight' => -20,
];
}
$element['UPLOAD_IDENTIFIER'] = [
'#type' => 'hidden',
'#value' => $upload_progress_key,
'#attributes' => ['class' => ['file-progress']],
// Uploadprogress extension requires this field to be at the top of
// the form.
'#weight' => -20,
];
// Add the upload progress callback.
$element['upload_button']['#ajax']['progress']['url'] = Url::fromRoute('file.ajax_progress', ['key' => $upload_progress_key]);

View File

@ -72,7 +72,7 @@ class FileWidget extends WidgetBase {
'#default_value' => $this->getSetting('progress_indicator'),
'#description' => $this->t('The throbber display does not show the status of uploads but takes up less space. The progress bar is helpful for monitoring progress on large uploads.'),
'#weight' => 16,
'#access' => file_progress_implementation(),
'#access' => extension_loaded('uploadprogress'),
];
return $element;
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests file module deprecations.
*
* @group legacy
* @group file
*/
class LegacyFileModuleTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['file'];
/**
* @covers ::file_progress_implementation
*/
public function testFileProgressDeprecation() {
$this->expectDeprecation('file_progress_implementation() is deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. Use extension_loaded(\'uploadprogress\') instead. See https://www.drupal.org/node/3397577');
$this->assertFalse(\file_progress_implementation());
}
}

View File

@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Tests the file requirements.
*
* @group file
*/
class RequirementsTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['file'];
/**
* Tests the file upload requirements.
*/
public function testUploadRequirements(): void {
if (\extension_loaded('uploadprogress')) {
$this->markTestSkipped('We are testing only when the uploadprogress extension is not loaded.');
}
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler */
$moduleHandler = $this->container->get('module_handler');
$moduleHandler->loadInclude('file', 'install');
// Test unspecified server software.
$this->setServerSoftware(NULL);
$requirements = \file_requirements('runtime');
$this->assertNotEmpty($requirements);
$this->assertEquals('Upload progress', (string) $requirements['file_progress']['title']);
$this->assertEquals('Not enabled', (string) $requirements['file_progress']['value']);
$this->assertEquals('Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php or Nginx with PHP-FPM.', (string) $requirements['file_progress']['description']);
// Test Apache + mod_php.
$this->setServerSoftware('Apache mod_php');
$requirements = \file_requirements('runtime');
$this->assertNotEmpty($requirements);
$this->assertEquals('Not enabled', (string) $requirements['file_progress']['value']);
$this->assertEquals('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a>.', (string) $requirements['file_progress']['description']);
// Test Apache + mod_fastcgi.
$this->setServerSoftware('Apache mod_fastcgi');
$requirements = \file_requirements('runtime');
$this->assertNotEmpty($requirements);
$this->assertEquals('Not enabled', (string) $requirements['file_progress']['value']);
$this->assertEquals('Your server is not capable of displaying file upload progress. File upload progress requires PHP be run with mod_php or PHP-FPM and not as FastCGI.', (string) $requirements['file_progress']['description']);
// Test Nginx.
$this->setServerSoftware('Nginx');
$requirements = \file_requirements('runtime');
$this->assertNotEmpty($requirements);
$this->assertEquals('Not enabled', (string) $requirements['file_progress']['value']);
$this->assertEquals('Your server is capable of displaying file upload progress, but does not have the required libraries. It is recommended to install the <a href="http://pecl.php.net/package/uploadprogress">PECL uploadprogress library</a>.', (string) $requirements['file_progress']['description']);
}
/**
* Sets the server software attribute in the request.
*/
private function setServerSoftware(?string $software): void {
$request = new Request();
if (is_string($software)) {
$request->server->set('SERVER_SOFTWARE', $software);
}
$requestStack = new RequestStack();
$requestStack->push($request);
$this->container->set('request_stack', $requestStack);
}
}

View File

@ -1446,11 +1446,6 @@ parameters:
count: 1
path: modules/field_ui/src/Form/FieldStorageConfigEditForm.php
-
message: "#^Variable \\$value might not be defined\\.$#"
count: 1
path: modules/file/file.install
-
message: "#^Variable \\$file_upload in empty\\(\\) always exists and is not falsy\\.$#"
count: 1