MediaWiki REL1_37
SpecialRunJobs.php
Go to the documentation of this file.
1<?php
30
32 private $jobRunner;
33
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_fill_keys( [ 'title', 'tasks', 'signature', 'sigexpiry' ], true );
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.
103 DeferredUpdates::addUpdate(
105 function () use ( $params ) {
106 $this->doRun( $params );
107 },
108 __METHOD__
109 ),
110 DeferredUpdates::POSTSEND
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}
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....
Job queue runner utility methods.
Definition JobRunner.php:41
A service class for fetching the wiki's current read-only mode.
getOutput()
Get the OutputPage being used for this instance.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
Special page designed for running background tasks (internal use only)
JobRunner $jobRunner
__construct(JobRunner $jobRunner, ReadOnlyMode $readOnlyMode)
doRun(array $params)
execute( $par)
Default execute method Checks user permissions.
doesWrites()
Indicates whether this special page may perform database writes.
ReadOnlyMode $readOnlyMode
static getQuerySignature(array $query, $secretKey)
Deferrable update that must run outside of any explicit LBFactory transaction round.
Shortcut to construct a special page which is unlisted by default.
while(( $__line=Maintenance::readconsole()) !==false) print
Definition eval.php:69