Merge "resourceloader: Omit empty parameters from mw.loader.implement calls"
[lhc/web/wiklou.git] / includes / specials / SpecialRunJobs.php
1 <?php
2 /**
3 * Implements Special:RunJobs
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Aaron Schulz
23 */
24
25 use MediaWiki\Logger\LoggerFactory;
26
27 /**
28 * Special page designed for running background tasks (internal use only)
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialRunJobs extends UnlistedSpecialPage {
33 public function __construct() {
34 parent::__construct( 'RunJobs' );
35 }
36
37 public function execute( $par = '' ) {
38 $this->getOutput()->disable();
39
40 if ( wfReadOnly() ) {
41 header( "HTTP/1.0 423 Locked" );
42 print 'Wiki is in read-only mode';
43
44 return;
45 } elseif ( !$this->getRequest()->wasPosted() ) {
46 header( "HTTP/1.0 400 Bad Request" );
47 print 'Request must be POSTed';
48
49 return;
50 }
51
52 $optional = array( 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, 'async' => true );
53 $required = array_flip( array( 'title', 'tasks', 'signature', 'sigexpiry' ) );
54
55 $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
56 $missing = array_diff_key( $required, $params );
57 if ( count( $missing ) ) {
58 header( "HTTP/1.0 400 Bad Request" );
59 print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
60
61 return;
62 }
63
64 $squery = $params;
65 unset( $squery['signature'] );
66 $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
67 $providedSignature = $params['signature'];
68
69 $verified = is_string( $providedSignature )
70 && hash_equals( $correctSignature, $providedSignature );
71 if ( !$verified || $params['sigexpiry'] < time() ) {
72 header( "HTTP/1.0 400 Bad Request" );
73 print 'Invalid or stale signature provided';
74
75 return;
76 }
77
78 // Apply any default parameter values
79 $params += $optional;
80
81 if ( $params['async'] ) {
82 // Client will usually disconnect before checking the response,
83 // but it needs to know when it is safe to disconnect. Until this
84 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
85 ignore_user_abort( true ); // jobs may take a bit of time
86 header( "HTTP/1.0 202 Accepted" );
87 ob_flush();
88 flush();
89 // Once the client receives this response, it can disconnect
90 }
91
92 // Do all of the specified tasks...
93 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
94 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
95 $response = $runner->run( array(
96 'type' => $params['type'],
97 'maxJobs' => $params['maxjobs'] ? $params['maxjobs'] : 1,
98 'maxTime' => $params['maxtime'] ? $params['maxjobs'] : 30
99 ) );
100 if ( !$params['async'] ) {
101 print FormatJson::encode( $response, true );
102 }
103 }
104 }
105
106 /**
107 * @param array $query
108 * @param string $secretKey
109 * @return string
110 */
111 public static function getQuerySignature( array $query, $secretKey ) {
112 ksort( $query ); // stable order
113 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
114 }
115 }