Merge "Revert "Use display name in category page subheadings if provided""
[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 doesWrites() {
38 return true;
39 }
40
41 public function execute( $par = '' ) {
42 $this->getOutput()->disable();
43 if ( wfReadOnly() ) {
44 // HTTP 423 Locked
45 HttpStatus::header( 423 );
46 print 'Wiki is in read-only mode';
47
48 return;
49 } elseif ( !$this->getRequest()->wasPosted() ) {
50 HttpStatus::header( 400 );
51 print 'Request must be POSTed';
52 return;
53 }
54
55 $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, 'async' => true ];
56 $required = array_flip( [ 'title', 'tasks', 'signature', 'sigexpiry' ] );
57
58 $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
59 $missing = array_diff_key( $required, $params );
60 if ( count( $missing ) ) {
61 HttpStatus::header( 400 );
62 print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
63 return;
64 }
65
66 $squery = $params;
67 unset( $squery['signature'] );
68 $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
69 $providedSignature = $params['signature'];
70
71 $verified = is_string( $providedSignature )
72 && hash_equals( $correctSignature, $providedSignature );
73 if ( !$verified || $params['sigexpiry'] < time() ) {
74 HttpStatus::header( 400 );
75 print 'Invalid or stale signature provided';
76 return;
77 }
78
79 // Apply any default parameter values
80 $params += $optional;
81
82 if ( $params['async'] ) {
83 // Client will usually disconnect before checking the response,
84 // but it needs to know when it is safe to disconnect. Until this
85 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
86 ignore_user_abort( true ); // jobs may take a bit of time
87 // HTTP 202 Accepted
88 HttpStatus::header( 202 );
89 ob_flush();
90 flush();
91 // Once the client receives this response, it can disconnect
92 set_error_handler( function ( $errno, $errstr ) {
93 if ( strpos( $errstr, 'Cannot modify header information' ) !== false ) {
94 return true; // bug T115413
95 }
96 // Delegate unhandled errors to the default MediaWiki handler
97 // so that fatal errors get proper logging (T89169)
98 return call_user_func_array(
99 'MWExceptionHandler::handleError', func_get_args()
100 );
101 } );
102 }
103
104 // Do all of the specified tasks...
105 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
106 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
107 $response = $runner->run( [
108 'type' => $params['type'],
109 'maxJobs' => $params['maxjobs'] ? $params['maxjobs'] : 1,
110 'maxTime' => $params['maxtime'] ? $params['maxjobs'] : 30
111 ] );
112 if ( !$params['async'] ) {
113 print FormatJson::encode( $response, true );
114 }
115 }
116 }
117
118 /**
119 * @param array $query
120 * @param string $secretKey
121 * @return string
122 */
123 public static function getQuerySignature( array $query, $secretKey ) {
124 ksort( $query ); // stable order
125 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
126 }
127 }