MediaWiki  1.34.0
SpecialRunJobs.php
Go to the documentation of this file.
1 <?php
25 
32  public function __construct() {
33  parent::__construct( 'RunJobs' );
34  }
35 
36  public function doesWrites() {
37  return true;
38  }
39 
40  public function execute( $par = '' ) {
41  $this->getOutput()->disable();
42 
43  if ( wfReadOnly() ) {
44  wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
45  return;
46  }
47 
48  // Validate request method
49  if ( !$this->getRequest()->wasPosted() ) {
50  wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
51  return;
52  }
53 
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 ) )
63  );
64  return;
65  }
66 
67  // Validate request signature
68  $squery = $params;
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.' );
76  return;
77  }
78 
79  // Apply any default parameter values
80  $params += $optional;
81 
82  if ( $params['async'] ) {
83  // HTTP 202 Accepted
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.
91  function () use ( $params ) {
92  $this->doRun( $params );
93  },
94  __METHOD__
95  ),
97  );
98  } else {
99  $stats = $this->doRun( $params );
100 
101  if ( $params['stats'] ) {
102  $this->getRequest()->response()->header( 'Content-Type: application/json' );
103  print FormatJson::encode( $stats );
104  } else {
105  print "Done\n";
106  }
107  }
108  }
109 
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
116  ] );
117  }
118 
124  public static function getQuerySignature( array $query, $secretKey ) {
125  ksort( $query ); // stable order
126  return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
127  }
128 }
SpecialRunJobs\__construct
__construct()
Definition: SpecialRunJobs.php:32
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
UnlistedSpecialPage
Shortcut to construct a special page which is unlisted by default.
Definition: UnlistedSpecialPage.php:29
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred list to be run later by execute()
Definition: DeferredUpdates.php:85
SpecialRunJobs\getQuerySignature
static getQuerySignature(array $query, $secretKey)
Definition: SpecialRunJobs.php:124
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1171
SpecialRunJobs\doRun
doRun(array $params)
Definition: SpecialRunJobs.php:110
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:758
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:70
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
SpecialRunJobs\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialRunJobs.php:36
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1659
HttpStatus\header
static header( $code)
Output an HTTP status code header.
Definition: HttpStatus.php:96
TransactionRoundDefiningUpdate
Deferrable update that must run outside of any explicit LBFactory transaction round.
Definition: TransactionRoundDefiningUpdate.php:8
SpecialRunJobs
Special page designed for running background tasks (internal use only)
Definition: SpecialRunJobs.php:31
SpecialRunJobs\execute
execute( $par='')
Default execute method Checks user permissions.
Definition: SpecialRunJobs.php:40
JobRunner
Job queue runner utility methods.
Definition: JobRunner.php:39
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
Definition: GlobalFunctions.php:347