MediaWiki master
SpecialRunJobs.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
23use HttpStatus;
24use JobRunner;
31
40
41 private JobRunner $jobRunner;
42 private ReadOnlyMode $readOnlyMode;
43
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 // Validate request method
70 if ( !$this->getRequest()->wasPosted() ) {
71 wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
72 return;
73 }
74
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 ) )
84 );
85 return;
86 }
87
88 // Validate request signature
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 // Apply any default parameter values
102 $params += $optional;
103
104 if ( $params['async'] ) {
105 // HTTP 202 Accepted
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(
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
145 public static function getQuerySignature( array $query, $secretKey ) {
146 ksort( $query ); // stable order
147 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
148 }
149}
150
155class_alias( SpecialRunJobs::class, 'SpecialRunJobs' );
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
array $params
The job parameters.
static header( $code)
Output an HTTP status code header.
Job queue runner utility methods.
Definition JobRunner.php:39
Defer callable updates to run later in the PHP process.
Deferrable update that must run outside of any explicit LBFactory transaction round.
JSON formatter wrapper class.
A class containing constants representing the names of configuration variables.
const SecretKey
Name constant for the SecretKey setting, for use with Config::get()
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
getOutput()
Get the OutputPage being used for this instance.
Shortcut to construct a special page which is unlisted by default.
Special page designed for running background tasks (internal use only)
execute( $par)
Default execute method Checks user permissions.
static getQuerySignature(array $query, $secretKey)
doesWrites()
Indicates whether this special page may perform database writes.
__construct(JobRunner $jobRunner, ReadOnlyMode $readOnlyMode)
Determine whether a site is currently in read-only mode.