AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / SpecialRunJobs.php
blobce5533fbf0133dd15d57065bfa31ff4b46d10053
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Aaron Schulz
25 use MediaWiki\Logger\LoggerFactory;
27 /**
28 * Special page designed for running background tasks (internal use only)
30 * @ingroup SpecialPage
32 class SpecialRunJobs extends UnlistedSpecialPage {
33 public function __construct() {
34 parent::__construct( 'RunJobs' );
37 public function doesWrites() {
38 return true;
41 public function execute( $par = '' ) {
42 $this->getOutput()->disable();
44 if ( wfReadOnly() ) {
45 // HTTP 423 Locked
46 HttpStatus::header( 423 );
47 print 'Wiki is in read-only mode';
49 return;
50 } elseif ( !$this->getRequest()->wasPosted() ) {
51 HttpStatus::header( 400 );
52 print 'Request must be POSTed';
53 return;
56 $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, 'async' => true ];
57 $required = array_flip( [ 'title', 'tasks', 'signature', 'sigexpiry' ] );
59 $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
60 $missing = array_diff_key( $required, $params );
61 if ( count( $missing ) ) {
62 HttpStatus::header( 400 );
63 print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
64 return;
67 $squery = $params;
68 unset( $squery['signature'] );
69 $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
70 $providedSignature = $params['signature'];
72 $verified = is_string( $providedSignature )
73 && hash_equals( $correctSignature, $providedSignature );
74 if ( !$verified || $params['sigexpiry'] < time() ) {
75 HttpStatus::header( 400 );
76 print 'Invalid or stale signature provided';
77 return;
80 // Apply any default parameter values
81 $params += $optional;
83 if ( $params['async'] ) {
84 // Client will usually disconnect before checking the response,
85 // but it needs to know when it is safe to disconnect. Until this
86 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
87 ignore_user_abort( true ); // jobs may take a bit of time
88 // HTTP 202 Accepted
89 HttpStatus::header( 202 );
90 ob_flush();
91 flush();
92 // Once the client receives this response, it can disconnect
93 set_error_handler( function ( $errno, $errstr ) {
94 if ( strpos( $errstr, 'Cannot modify header information' ) !== false ) {
95 return true; // bug T115413
97 // Delegate unhandled errors to the default MediaWiki handler
98 // so that fatal errors get proper logging (T89169)
99 return call_user_func_array(
100 'MWExceptionHandler::handleError', func_get_args()
102 } );
105 // Do all of the specified tasks...
106 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
107 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
108 $response = $runner->run( [
109 'type' => $params['type'],
110 'maxJobs' => $params['maxjobs'] ? $params['maxjobs'] : 1,
111 'maxTime' => $params['maxtime'] ? $params['maxjobs'] : 30
112 ] );
113 if ( !$params['async'] ) {
114 print FormatJson::encode( $response, true );
120 * @param array $query
121 * @param string $secretKey
122 * @return string
124 public static function getQuerySignature( array $query, $secretKey ) {
125 ksort( $query ); // stable order
126 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );