3 * Implements Special:RunJobs
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.
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.
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
21 * @ingroup SpecialPage
22 * @author Aaron Schulz
26 * Special page designed for running background tasks (internal use only)
28 * @ingroup SpecialPage
30 class SpecialRunJobs
extends UnlistedSpecialPage
{
31 public function __construct() {
32 parent
::__construct( 'RunJobs' );
35 public function execute( $par = '' ) {
36 $this->getOutput()->disable();
39 header( "HTTP/1.0 423 Locked" );
40 print 'Wiki is in read-only mode';
43 } elseif ( !$this->getRequest()->wasPosted() ) {
44 header( "HTTP/1.0 400 Bad Request" );
45 print 'Request must be POSTed';
50 $optional = array( 'maxjobs' => 0, 'maxtime' => 30, 'type' => false, 'async' => true );
51 $required = array_flip( array( 'title', 'tasks', 'signature', 'sigexpiry' ) );
53 $params = array_intersect_key( $this->getRequest()->getValues(), $required +
$optional );
54 $missing = array_diff_key( $required, $params );
55 if ( count( $missing ) ) {
56 header( "HTTP/1.0 400 Bad Request" );
57 print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
63 unset( $squery['signature'] );
64 $cSig = self
::getQuerySignature( $squery, $this->getConfig()->get( 'SecretKey' ) ); // correct signature
65 $rSig = $params['signature']; // provided signature
67 $verified = is_string( $rSig ) && hash_equals( $cSig, $rSig );
68 if ( !$verified ||
$params['sigexpiry'] < time() ) {
69 header( "HTTP/1.0 400 Bad Request" );
70 print 'Invalid or stale signature provided';
75 // Apply any default parameter values
78 if ( $params['async'] ) {
79 // Client will usually disconnect before checking the response,
80 // but it needs to know when it is safe to disconnect. Until this
81 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
82 ignore_user_abort( true ); // jobs may take a bit of time
83 header( "HTTP/1.0 202 Accepted" );
86 // Once the client receives this response, it can disconnect
89 // Do all of the specified tasks...
90 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
91 $runner = new JobRunner();
92 $response = $runner->run( array(
93 'type' => $params['type'],
94 'maxJobs' => $params['maxjobs'] ?
$params['maxjobs'] : 1,
95 'maxTime' => $params['maxtime'] ?
$params['maxjobs'] : 30
97 if ( !$params['async'] ) {
98 print FormatJson
::encode( $response, true );
104 * @param array $query
105 * @param string $secretKey
108 public static function getQuerySignature( array $query, $secretKey ) {
109 ksort( $query ); // stable order
110 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $secretKey );