Merge "Add attributes parameter to ShowSearchHitTitle"
[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
43 if ( wfReadOnly() ) {
44 wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
45 return;
46 }
47
48 // Validate request method
49 if ( !$this->getRequest()->wasPosted() ) {
50 wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
51 return;
52 }
53
54 // Validate request parameters
55 $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, 'async' => true ];
56 $required = array_flip( [ 'title', 'tasks', 'signature', 'sigexpiry' ] );
57 $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
58 $missing = array_diff_key( $required, $params );
59 if ( count( $missing ) ) {
60 wfHttpError( 400, 'Bad Request',
61 'Missing parameters: ' . implode( ', ', array_keys( $missing ) )
62 );
63 return;
64 }
65
66 // Validate request signature
67 $squery = $params;
68 unset( $squery['signature'] );
69 $correctSignature = self::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) );
70 $providedSignature = $params['signature'];
71 $verified = is_string( $providedSignature )
72 && hash_equals( $correctSignature, $providedSignature );
73 if ( !$verified || $params['sigexpiry'] < time() ) {
74 wfHttpError( 400, 'Bad Request', 'Invalid or stale signature provided.' );
75 return;
76 }
77
78 // Apply any default parameter values
79 $params += $optional;
80
81 if ( $params['async'] ) {
82 // HTTP 202 Accepted
83 HttpStatus::header( 202 );
84 // Clients are meant to disconnect without waiting for the full response.
85 // Let the page output happen before the jobs start, so that clients know it's
86 // safe to disconnect. MediaWiki::preOutputCommit() calls ignore_user_abort()
87 // or similar to make sure we stay alive to run the deferred update.
88 DeferredUpdates::addUpdate(
89 new TransactionRoundDefiningUpdate(
90 function () use ( $params ) {
91 $this->doRun( $params );
92 },
93 __METHOD__
94 ),
95 DeferredUpdates::POSTSEND
96 );
97 } else {
98 $this->doRun( $params );
99 print "Done\n";
100 }
101 }
102
103 protected function doRun( array $params ) {
104 $runner = new JobRunner( LoggerFactory::getInstance( 'runJobs' ) );
105 $runner->run( [
106 'type' => $params['type'],
107 'maxJobs' => $params['maxjobs'] ?: 1,
108 'maxTime' => $params['maxtime'] ?: 30
109 ] );
110 }
111
112 /**
113 * @param array $query
114 * @param string $secretKey
115 * @return string
116 */
117 public static function getQuerySignature( array $query, $secretKey ) {
118 ksort( $query ); // stable order
119 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
120 }
121 }