3 * Implements Special:RunJobs
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\Logger\LoggerFactory
;
27 * Special page designed for running background tasks (internal use only)
29 * @ingroup SpecialPage
31 class SpecialRunJobs
extends UnlistedSpecialPage
{
32 public function __construct() {
33 parent
::__construct( 'RunJobs' );
36 public function doesWrites() {
40 public function execute( $par = '' ) {
41 $this->getOutput()->disable();
44 wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
48 // Validate request method
49 if ( !$this->getRequest()->wasPosted() ) {
50 wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
54 // Validate request parameters
55 $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false,
56 'async' => true, 'stats' => false ];
57 $required = array_flip( [ 'title', 'tasks', 'signature', 'sigexpiry' ] );
58 $params = array_intersect_key( $this->getRequest()->getValues(), $required +
$optional );
59 $missing = array_diff_key( $required, $params );
60 if ( count( $missing ) ) {
61 wfHttpError( 400, 'Bad Request',
62 'Missing parameters: ' . implode( ', ', array_keys( $missing ) )
67 // Validate request signature
69 unset( $squery['signature'] );
70 $correctSignature = self
::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
71 $providedSignature = $params['signature'];
72 $verified = is_string( $providedSignature )
73 && hash_equals( $correctSignature, $providedSignature );
74 if ( !$verified ||
$params['sigexpiry'] < time() ) {
75 wfHttpError( 400, 'Bad Request', 'Invalid or stale signature provided.' );
79 // Apply any default parameter values
82 if ( $params['async'] ) {
84 HttpStatus
::header( 202 );
85 // Clients are meant to disconnect without waiting for the full response.
86 // Let the page output happen before the jobs start, so that clients know it's
87 // safe to disconnect. MediaWiki::preOutputCommit() calls ignore_user_abort()
88 // or similar to make sure we stay alive to run the deferred update.
89 DeferredUpdates
::addUpdate(
90 new TransactionRoundDefiningUpdate(
91 function () use ( $params ) {
92 $this->doRun( $params );
96 DeferredUpdates
::POSTSEND
99 $stats = $this->doRun( $params );
101 if ( $params['stats'] ) {
102 $this->getRequest()->response()->header( 'Content-Type: application/json' );
103 print FormatJson
::encode( $stats );
110 protected function doRun( array $params ) {
111 $runner = new JobRunner( LoggerFactory
::getInstance( 'runJobs' ) );
112 return $runner->run( [
113 'type' => $params['type'],
114 'maxJobs' => $params['maxjobs'] ?
: 1,
115 'maxTime' => $params['maxtime'] ?
: 30
120 * @param array $query
121 * @param string $secretKey
124 public static function getQuerySignature( array $query, $secretKey ) {
125 ksort( $query ); // stable order
126 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );