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