4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
20 * @author Aaron Schulz
24 * This is a simple class to handle action=runjobs and is only used internally
26 * @note: this does not requre "write mode" nor tokens due to the signature check
30 class ApiRunJobs
extends ApiBase
{
31 public function execute() {
33 $this->dieUsage( 'Wiki is in read-only mode', 'read_only', 400 );
36 $params = $this->extractRequestParams();
37 $squery = $this->getRequest()->getValues();
38 unset( $squery['signature'] );
39 $cSig = self
::getQuerySignature( $squery );
40 $rSig = $params['signature'];
42 // Time-insensitive signature verification
43 if ( strlen( $rSig ) !== strlen( $cSig ) ) {
47 for ( $i = 0; $i < strlen( $cSig ); $i++
) {
48 $result |
= ord( $cSig{$i} ) ^
ord( $rSig{$i} );
50 $verified = ( $result == 0 );
53 if ( !$verified ||
$params['sigexpiry'] < time() ) {
54 $this->dieUsage( 'Invalid or stale signature provided', 'bad_signature', 400 );
57 // Client will usually disconnect before checking the response,
58 // but it needs to know when it is safe to disconnect. Until this
59 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
60 ignore_user_abort( true ); // jobs may take a bit of time
61 header( "HTTP/1.0 202 Accepted" );
64 // Once the client receives this response, it can disconnect
66 // Do all of the specified tasks...
67 if ( in_array( 'jobs', $params['tasks'] ) ) {
68 self
::executeJobs( $params['maxjobs'] );
76 public static function getQuerySignature( array $query ) {
79 ksort( $query ); // stable order
80 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $wgSecretKey );
84 * Run jobs from the job queue
86 * @note: also called from Wiki.php
88 * @param integer $maxJobs Maximum number of jobs to run
91 public static function executeJobs( $maxJobs ) {
92 $n = $maxJobs; // number of jobs to run
97 // Fallback to running the jobs here while the user waits
98 $group = JobQueueGroup
::singleton();
100 $job = $group->pop( JobQueueGroup
::USE_CACHE
); // job from any queue
102 $output = $job->toString() . "\n";
103 $t = - microtime( true );
104 wfProfileIn( __METHOD__
. '-' . get_class( $job ) );
105 $success = $job->run();
106 wfProfileOut( __METHOD__
. '-' . get_class( $job ) );
107 $group->ack( $job ); // done
108 $t +
= microtime( true );
109 $t = round( $t * 1000 );
110 if ( $success === false ) {
111 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
113 $output .= "Success, Time: $t ms\n";
115 wfDebugLog( 'jobqueue', $output );
117 } while ( --$n && $job );
118 } catch ( MWException
$e ) {
119 // We don't want exceptions thrown during job execution to
120 // be reported to the user since the output is already sent.
121 // Instead we just log them.
122 MWExceptionHandler
::logException( $e );
126 public function mustBePosted() {
130 public function getAllowedParams() {
133 ApiBase
::PARAM_ISMULTI
=> true,
134 ApiBase
::PARAM_TYPE
=> array( 'jobs' )
137 ApiBase
::PARAM_TYPE
=> 'integer',
138 ApiBase
::PARAM_DFLT
=> 0
140 'signature' => array(
141 ApiBase
::PROP_TYPE
=> 'string',
143 'sigexpiry' => array(
144 ApiBase
::PARAM_TYPE
=> 'integer',
145 ApiBase
::PARAM_DFLT
=> 0 // ~epoch
150 public function getParamDescription() {
152 'tasks' => 'List of task types to perform',
153 'maxjobs' => 'Maximum number of jobs to run',
154 'signature' => 'HMAC Signature that signs the request',
155 'sigexpiry' => 'HMAC signature expiry as a UNIX timestamp'
159 public function getDescription() {
160 return 'Perform periodic tasks or run jobs from the queue';
163 public function getExamples() {
165 'api.php?action=runjobs&tasks=jobs&maxjobs=3' => 'Run up to 3 jobs from the queue',