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