| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use HttpStatus; |
| 24 |
use JobRunner; |
| 25 |
use MediaWiki\Deferred\DeferredUpdates; |
| 26 |
use MediaWiki\Deferred\TransactionRoundDefiningUpdate; |
| 27 |
use MediaWiki\Json\FormatJson; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\SpecialPage\UnlistedSpecialPage; |
| 30 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class SpecialRunJobs extends UnlistedSpecialPage { |
| 40 |
|
| 41 |
private JobRunner $jobRunner; |
| 42 |
private ReadOnlyMode $readOnlyMode; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __construct( |
| 49 |
JobRunner $jobRunner, |
| 50 |
ReadOnlyMode $readOnlyMode |
| 51 |
) { |
| 52 |
parent::__construct( 'RunJobs' ); |
| 53 |
$this->jobRunner = $jobRunner; |
| 54 |
$this->readOnlyMode = $readOnlyMode; |
| 55 |
} |
| 56 |
|
| 57 |
public function doesWrites() { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
public function execute( $par ) { |
| 62 |
$this->getOutput()->disable(); |
| 63 |
|
| 64 |
if ( $this->readOnlyMode->isReadOnly() ) { |
| 65 |
wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' ); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
if ( !$this->getRequest()->wasPosted() ) { |
| 71 |
wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' ); |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
$optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, |
| 77 |
'async' => true, 'stats' => false ]; |
| 78 |
$required = array_fill_keys( [ 'title', 'tasks', 'signature', 'sigexpiry' ], true ); |
| 79 |
$params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional ); |
| 80 |
$missing = array_diff_key( $required, $params ); |
| 81 |
if ( count( $missing ) ) { |
| 82 |
wfHttpError( 400, 'Bad Request', |
| 83 |
'Missing parameters: ' . implode( ', ', array_keys( $missing ) ) |
| 84 |
); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
$squery = $params; |
| 90 |
unset( $squery['signature'] ); |
| 91 |
$correctSignature = self::getQuerySignature( $squery, |
| 92 |
$this->getConfig()->get( MainConfigNames::SecretKey ) ); |
| 93 |
$providedSignature = $params['signature']; |
| 94 |
$verified = is_string( $providedSignature ) |
| 95 |
&& hash_equals( $correctSignature, $providedSignature ); |
| 96 |
if ( !$verified || $params['sigexpiry'] < time() ) { |
| 97 |
wfHttpError( 400, 'Bad Request', 'Invalid or stale signature provided.' ); |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
$params += $optional; |
| 103 |
|
| 104 |
if ( $params['async'] ) { |
| 105 |
|
| 106 |
HttpStatus::header( 202 ); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
DeferredUpdates::addUpdate( |
| 112 |
new TransactionRoundDefiningUpdate( |
| 113 |
function () use ( $params ) { |
| 114 |
$this->doRun( $params ); |
| 115 |
}, |
| 116 |
__METHOD__ |
| 117 |
), |
| 118 |
DeferredUpdates::POSTSEND |
| 119 |
); |
| 120 |
} else { |
| 121 |
$stats = $this->doRun( $params ); |
| 122 |
|
| 123 |
if ( $params['stats'] ) { |
| 124 |
$this->getRequest()->response()->header( 'Content-Type: application/json' ); |
| 125 |
print FormatJson::encode( $stats ); |
| 126 |
} else { |
| 127 |
print "Done\n"; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
protected function doRun( array $params ) { |
| 133 |
return $this->jobRunner->run( [ |
| 134 |
'type' => $params['type'], |
| 135 |
'maxJobs' => $params['maxjobs'] ?: 1, |
| 136 |
'maxTime' => $params['maxtime'] ?: 30 |
| 137 |
] ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static function getQuerySignature( array $query, $secretKey ) { |
| 146 |
ksort( $query ); |
| 147 |
return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
class_alias( SpecialRunJobs::class, 'SpecialRunJobs' ); |