MediaWiki  master
SpecialRunJobs.php
Go to the documentation of this file.
1 <?php
30 
32  private $jobRunner;
33 
35  private $readOnlyMode;
36 
41  public function __construct(
44  ) {
45  parent::__construct( 'RunJobs' );
46  $this->jobRunner = $jobRunner;
47  $this->readOnlyMode = $readOnlyMode;
48  }
49 
50  public function doesWrites() {
51  return true;
52  }
53 
54  public function execute( $par = '' ) {
55  $this->getOutput()->disable();
56 
57  if ( $this->readOnlyMode->isReadOnly() ) {
58  wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
59  return;
60  }
61 
62  // Validate request method
63  if ( !$this->getRequest()->wasPosted() ) {
64  wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
65  return;
66  }
67 
68  // Validate request parameters
69  $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false,
70  'async' => true, 'stats' => false ];
71  $required = array_flip( [ 'title', 'tasks', 'signature', 'sigexpiry' ] );
72  $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
73  $missing = array_diff_key( $required, $params );
74  if ( count( $missing ) ) {
75  wfHttpError( 400, 'Bad Request',
76  'Missing parameters: ' . implode( ', ', array_keys( $missing ) )
77  );
78  return;
79  }
80 
81  // Validate request signature
82  $squery = $params;
83  unset( $squery['signature'] );
84  $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
85  $providedSignature = $params['signature'];
86  $verified = is_string( $providedSignature )
87  && hash_equals( $correctSignature, $providedSignature );
88  if ( !$verified || $params['sigexpiry'] < time() ) {
89  wfHttpError( 400, 'Bad Request', 'Invalid or stale signature provided.' );
90  return;
91  }
92 
93  // Apply any default parameter values
94  $params += $optional;
95 
96  if ( $params['async'] ) {
97  // HTTP 202 Accepted
98  HttpStatus::header( 202 );
99  // Clients are meant to disconnect without waiting for the full response.
100  // Let the page output happen before the jobs start, so that clients know it's
101  // safe to disconnect. MediaWiki::preOutputCommit() calls ignore_user_abort()
102  // or similar to make sure we stay alive to run the deferred update.
105  function () use ( $params ) {
106  $this->doRun( $params );
107  },
108  __METHOD__
109  ),
111  );
112  } else {
113  $stats = $this->doRun( $params );
114 
115  if ( $params['stats'] ) {
116  $this->getRequest()->response()->header( 'Content-Type: application/json' );
117  print FormatJson::encode( $stats );
118  } else {
119  print "Done\n";
120  }
121  }
122  }
123 
124  protected function doRun( array $params ) {
125  return $this->jobRunner->run( [
126  'type' => $params['type'],
127  'maxJobs' => $params['maxjobs'] ?: 1,
128  'maxTime' => $params['maxtime'] ?: 30
129  ] );
130  }
131 
137  public static function getQuerySignature( array $query, $secretKey ) {
138  ksort( $query ); // stable order
139  return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
140  }
141 }
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:788
UnlistedSpecialPage
Shortcut to construct a special page which is unlisted by default.
Definition: UnlistedSpecialPage.php:31
ReadOnlyMode
A service class for fetching the wiki's current read-only mode.
Definition: ReadOnlyMode.php:11
DeferredUpdates\addUpdate
static addUpdate(DeferrableUpdate $update, $stage=self::POSTSEND)
Add an update to the deferred update queue for execution at the appropriate time.
Definition: DeferredUpdates.php:106
SpecialRunJobs\__construct
__construct(JobRunner $jobRunner, ReadOnlyMode $readOnlyMode)
Definition: SpecialRunJobs.php:41
SpecialRunJobs\getQuerySignature
static getQuerySignature(array $query, $secretKey)
Definition: SpecialRunJobs.php:137
SpecialRunJobs\doRun
doRun(array $params)
Definition: SpecialRunJobs.php:124
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
SpecialRunJobs\$jobRunner
JobRunner $jobRunner
Definition: SpecialRunJobs.php:32
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:866
DeferredUpdates\POSTSEND
const POSTSEND
Definition: DeferredUpdates.php:85
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:778
SpecialRunJobs\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialRunJobs.php:50
SpecialRunJobs\$readOnlyMode
ReadOnlyMode $readOnlyMode
Definition: SpecialRunJobs.php:35
wfHttpError
wfHttpError( $code, $label, $desc)
Provide a simple HTTP error.
Definition: GlobalFunctions.php:1605
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:10
SpecialRunJobs
Special page designed for running background tasks (internal use only)
Definition: SpecialRunJobs.php:29
SpecialRunJobs\execute
execute( $par='')
Default execute method Checks user permissions.
Definition: SpecialRunJobs.php:54
JobRunner
Job queue runner utility methods.
Definition: JobRunner.php:41
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:349