3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Specials
;
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
;
33 * Special page designed for running background tasks (internal use only)
36 * @ingroup SpecialPage
39 class SpecialRunJobs
extends UnlistedSpecialPage
{
41 private JobRunner
$jobRunner;
42 private ReadOnlyMode
$readOnlyMode;
45 * @param JobRunner $jobRunner
46 * @param ReadOnlyMode $readOnlyMode
48 public function __construct(
50 ReadOnlyMode
$readOnlyMode
52 parent
::__construct( 'RunJobs' );
53 $this->jobRunner
= $jobRunner;
54 $this->readOnlyMode
= $readOnlyMode;
57 public function doesWrites() {
61 public function execute( $par ) {
62 $this->getOutput()->disable();
64 if ( $this->readOnlyMode
->isReadOnly() ) {
65 wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
69 // Validate request method
70 if ( !$this->getRequest()->wasPosted() ) {
71 wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
75 // Validate request parameters
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 ) )
88 // Validate request signature
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.' );
101 // Apply any default parameter values
102 $params +
= $optional;
104 if ( $params['async'] ) {
106 HttpStatus
::header( 202 );
107 // Clients are meant to disconnect without waiting for the full response.
108 // Let the page output happen before the jobs start, so that clients know it's
109 // safe to disconnect. MediaWiki::preOutputCommit() calls ignore_user_abort()
110 // or similar to make sure we stay alive to run the deferred update.
111 DeferredUpdates
::addUpdate(
112 new TransactionRoundDefiningUpdate(
113 function () use ( $params ) {
114 $this->doRun( $params );
118 DeferredUpdates
::POSTSEND
121 $stats = $this->doRun( $params );
123 if ( $params['stats'] ) {
124 $this->getRequest()->response()->header( 'Content-Type: application/json' );
125 print FormatJson
::encode( $stats );
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
141 * @param array $query
142 * @param string $secretKey
145 public static function getQuerySignature( array $query, $secretKey ) {
146 ksort( $query ); // stable order
147 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
152 * Retain the old class name for backwards compatibility.
153 * @deprecated since 1.41
155 class_alias( SpecialRunJobs
::class, 'SpecialRunJobs' );