Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialRunJobs
0.00% covered (danger)
0.00%
0 / 55
0.00% covered (danger)
0.00%
0 / 5
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
90
 doRun
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getQuerySignature
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Specials;
22
23use HttpStatus;
24use JobRunner;
25use MediaWiki\Deferred\DeferredUpdates;
26use MediaWiki\Deferred\TransactionRoundDefiningUpdate;
27use MediaWiki\Json\FormatJson;
28use MediaWiki\MainConfigNames;
29use MediaWiki\SpecialPage\UnlistedSpecialPage;
30use Wikimedia\Rdbms\ReadOnlyMode;
31
32/**
33 * Special page designed for running background tasks (internal use only)
34 *
35 * @internal
36 * @ingroup SpecialPage
37 * @ingroup JobQueue
38 */
39class SpecialRunJobs extends UnlistedSpecialPage {
40
41    private JobRunner $jobRunner;
42    private ReadOnlyMode $readOnlyMode;
43
44    /**
45     * @param JobRunner $jobRunner
46     * @param ReadOnlyMode $readOnlyMode
47     */
48    public function __construct(
49        JobRunner $jobRunner,
50        ReadOnlyMode $readOnlyMode
51    ) {
52        parent::__construct( 'RunJobs' );
53        $this->jobRunner = $jobRunner;
54        $this->readOnlyMode = $readOnlyMode;
55    }
56
57    public function doesWrites() {
58        return true;
59    }
60
61    public function execute( $par ) {
62        $this->getOutput()->disable();
63
64        if ( $this->readOnlyMode->isReadOnly() ) {
65            wfHttpError( 423, 'Locked', 'Wiki is in read-only mode.' );
66            return;
67        }
68
69        // Validate request method
70        if ( !$this->getRequest()->wasPosted() ) {
71            wfHttpError( 400, 'Bad Request', 'Request must be POSTed.' );
72            return;
73        }
74
75        // Validate request parameters
76        $optional = [ 'maxjobs' => 0, 'maxtime' => 30, 'type' => false,
77            'async' => true, 'stats' => false ];
78        $required = array_fill_keys( [ 'title', 'tasks', 'signature', 'sigexpiry' ], true );
79        $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
80        $missing = array_diff_key( $required, $params );
81        if ( count( $missing ) ) {
82            wfHttpError( 400, 'Bad Request',
83                'Missing parameters: ' . implode( ', ', array_keys( $missing ) )
84            );
85            return;
86        }
87
88        // Validate request signature
89        $squery = $params;
90        unset( $squery['signature'] );
91        $correctSignature = self::getQuerySignature( $squery,
92            $this->getConfig()->get( MainConfigNames::SecretKey ) );
93        $providedSignature = $params['signature'];
94        $verified = is_string( $providedSignature )
95            && hash_equals( $correctSignature, $providedSignature );
96        if ( !$verified || $params['sigexpiry'] < time() ) {
97            wfHttpError( 400, 'Bad Request', 'Invalid or stale signature provided.' );
98            return;
99        }
100
101        // Apply any default parameter values
102        $params += $optional;
103
104        if ( $params['async'] ) {
105            // HTTP 202 Accepted
106            HttpStatus::header( 202 );
107            // Clients are meant to disconnect without waiting for the full response.
108            // Let the page output happen before the jobs start, so that clients know it's
109            // safe to disconnect. MediaWiki::preOutputCommit() calls ignore_user_abort()
110            // or similar to make sure we stay alive to run the deferred update.
111            DeferredUpdates::addUpdate(
112                new TransactionRoundDefiningUpdate(
113                    function () use ( $params ) {
114                        $this->doRun( $params );
115                    },
116                    __METHOD__
117                ),
118                DeferredUpdates::POSTSEND
119            );
120        } else {
121            $stats = $this->doRun( $params );
122
123            if ( $params['stats'] ) {
124                $this->getRequest()->response()->header( 'Content-Type: application/json' );
125                print FormatJson::encode( $stats );
126            } else {
127                print "Done\n";
128            }
129        }
130    }
131
132    protected function doRun( array $params ) {
133        return $this->jobRunner->run( [
134            'type'     => $params['type'],
135            'maxJobs'  => $params['maxjobs'] ?: 1,
136            'maxTime'  => $params['maxtime'] ?: 30
137        ] );
138    }
139
140    /**
141     * @param array $query
142     * @param string $secretKey
143     * @return string
144     */
145    public static function getQuerySignature( array $query, $secretKey ) {
146        ksort( $query ); // stable order
147        return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );
148    }
149}
150
151/**
152 * Retain the old class name for backwards compatibility.
153 * @deprecated since 1.41
154 */
155class_alias( SpecialRunJobs::class, 'SpecialRunJobs' );