Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / specials / SpecialRunJobs.php
blob42175537646bc5ab77850c3fdace978c5da1a66a
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 execute( $par = '' ) {
38 $this->getOutput()->disable();
40 if ( wfReadOnly() ) {
41 // HTTP 423 Locked
42 HttpStatus::header( 423 );
43 print 'Wiki is in read-only mode';
45 return;
46 } elseif ( !$this->getRequest()->wasPosted() ) {
47 HttpStatus::header( 400 );
48 print 'Request must be POSTed';
49 return;
52 $optional = array( 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, 'async' => true );
53 $required = array_flip( array( 'title', 'tasks', 'signature', 'sigexpiry' ) );
55 $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
56 $missing = array_diff_key( $required, $params );
57 if ( count( $missing ) ) {
58 HttpStatus::header( 400 );
59 print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
60 return;
63 $squery = $params;
64 unset( $squery['signature'] );
65 $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
66 $providedSignature = $params['signature'];
68 $verified = is_string( $providedSignature )
69 && hash_equals( $correctSignature, $providedSignature );
70 if ( !$verified || $params['sigexpiry'] < time() ) {
71 HttpStatus::header( 400 );
72 print 'Invalid or stale signature provided';
73 return;
76 // Apply any default parameter values
77 $params += $optional;
79 if ( $params['async'] ) {
80 // Client will usually disconnect before checking the response,
81 // but it needs to know when it is safe to disconnect. Until this
82 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
83 ignore_user_abort( true ); // jobs may take a bit of time
84 // HTTP 202 Accepted
85 HttpStatus::header( 202 );
86 ob_flush();
87 flush();
88 // Once the client receives this response, it can disconnect
89 set_error_handler( function ( $errno, $errstr ) {
90 if ( strpos( $errstr, 'Cannot modify header information' ) !== false ) {
91 return true; // bug T115413
93 // Delegate unhandled errors to the default handlers
94 return false;
95 } );
98 // Do all of the specified tasks...
99 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
100 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
101 $response = $runner->run( array(
102 'type' => $params['type'],
103 'maxJobs' => $params['maxjobs'] ? $params['maxjobs'] : 1,
104 'maxTime' => $params['maxtime'] ? $params['maxjobs'] : 30
105 ) );
106 if ( !$params['async'] ) {
107 print FormatJson::encode( $response, true );
113 * @param array $query
114 * @param string $secretKey
115 * @return string
117 public static function getQuerySignature( array $query, $secretKey ) {
118 ksort( $query ); // stable order
119 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );