Migrate away from deprecated wfShellExec

Change-Id: Iae3fdea224d896c1a7b53ce1fe7d849cb35a0401
This commit is contained in:
Amir Sarabadani 2024-01-16 00:12:43 +01:00 committed by Reedy
parent 28f297f1f7
commit e1c7392a8e

View file

@ -31,6 +31,7 @@ require_once "$IP/maintenance/Maintenance.php";
use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha; use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha;
use MediaWiki\Extension\ConfirmEdit\Hooks; use MediaWiki\Extension\ConfirmEdit\Hooks;
use MediaWiki\Shell\Shell;
use MediaWiki\Status\Status; use MediaWiki\Status\Status;
/** /**
@ -101,29 +102,38 @@ class GenerateFancyCaptchas extends Maintenance {
$captchaScript = 'captcha-old.py'; $captchaScript = 'captcha-old.py';
} }
$cmd = sprintf( "python3 %s --key %s --output %s --count %s --dirs %s", $cmd = [
wfEscapeShellArg( dirname( __DIR__ ) . '/' . $captchaScript ), "python3",
wfEscapeShellArg( $wgCaptchaSecret ), dirname( __DIR__ ) . '/' . $captchaScript,
wfEscapeShellArg( $tmpDir ), "--key",
wfEscapeShellArg( (string)$countGen ), $wgCaptchaSecret,
wfEscapeShellArg( $wgCaptchaDirectoryLevels ) "--output",
); $tmpDir,
"--count",
(string)$countGen,
"--dirs",
$wgCaptchaDirectoryLevels
];
foreach ( foreach (
[ 'wordlist', 'font', 'font-size', 'blacklist', 'badwordlist', 'verbose', 'threads' ] as $par [ 'wordlist', 'font', 'font-size', 'blacklist', 'badwordlist', 'verbose', 'threads' ] as $par
) { ) {
if ( $this->hasOption( $par ) ) { if ( $this->hasOption( $par ) ) {
$cmd .= " --$par " . wfEscapeShellArg( $this->getOption( $par ) ); $cmd[] = "--$par";
$cmd[] = $this->getOption( $par );
} }
} }
$this->output( "Generating $countGen new captchas.." ); $this->output( "Generating $countGen new captchas.." );
$retVal = 1;
$captchaTime = -microtime( true ); $captchaTime = -microtime( true );
wfShellExec( $cmd, $retVal, [], [ 'time' => 0 ] ); $result = Shell::command( [] )
if ( $retVal != 0 ) { ->unsafeParams( $cmd )
->limits( [ 'time' => 0 ] )
->restrict( Shell::RESTRICT_NONE )
->execute();
if ( $result->getExitCode() != 0 ) {
$this->output( " Failed.\n" ); $this->output( " Failed.\n" );
wfRecursiveRemoveDir( $tmpDir ); wfRecursiveRemoveDir( $tmpDir );
$this->fatalError( "An error occured when running $captchaScript.\n", 1 ); $this->fatalError( "An error occurred when running $captchaScript.\n", 1 );
} }
$captchaTime += microtime( true ); $captchaTime += microtime( true );
@ -170,7 +180,7 @@ class GenerateFancyCaptchas extends Maintenance {
if ( !$fileInfo->isFile() ) { if ( !$fileInfo->isFile() ) {
continue; continue;
} }
list( $salt, $hash ) = $instance->hashFromImageName( $fileInfo->getBasename() ); [ $salt, $hash ] = $instance->hashFromImageName( $fileInfo->getBasename() );
$dest = $instance->imagePath( $salt, $hash ); $dest = $instance->imagePath( $salt, $hash );
$backend->prepare( [ 'dir' => dirname( $dest ) ] ); $backend->prepare( [ 'dir' => dirname( $dest ) ] );
$filesToStore[] = [ $filesToStore[] = [