Require one of page id or page title as params to ApiRollback
[mediawiki.git] / includes / specials / SpecialRunJobs.php
blob63eff36ca3eefb9e84f11cd1db1df27b2ce9a387
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
22 * @author Aaron Schulz
25 /**
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();
38 if ( wfReadOnly() ) {
39 header( "HTTP/1.0 423 Locked" );
40 print 'Wiki is in read-only mode';
42 return;
43 } elseif ( !$this->getRequest()->wasPosted() ) {
44 header( "HTTP/1.0 400 Bad Request" );
45 print 'Request must be POSTed';
47 return;
50 $optional = array( 'maxjobs' => 0 );
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 ) );
59 return;
62 $squery = $params;
63 unset( $squery['signature'] );
64 $cSig = self::getQuerySignature( $squery ); // correct signature
65 $rSig = $params['signature']; // provided signature
67 // Constant-time signature verification
68 // http://www.emerose.com/timing-attacks-explained
69 // @todo Make a common method for this
70 if ( !is_string( $rSig ) || strlen( $rSig ) !== strlen( $cSig ) ) {
71 $verified = false;
72 } else {
73 $result = 0;
74 $cSigLength = strlen( $cSig );
75 for ( $i = 0; $i < $cSigLength; $i++ ) {
76 $result |= ord( $cSig[$i] ) ^ ord( $rSig[$i] );
78 $verified = ( $result == 0 );
80 if ( !$verified || $params['sigexpiry'] < time() ) {
81 header( "HTTP/1.0 400 Bad Request" );
82 print 'Invalid or stale signature provided';
84 return;
87 // Apply any default parameter values
88 $params += $optional;
90 // Client will usually disconnect before checking the response,
91 // but it needs to know when it is safe to disconnect. Until this
92 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
93 ignore_user_abort( true ); // jobs may take a bit of time
94 header( "HTTP/1.0 202 Accepted" );
95 ob_flush();
96 flush();
97 // Once the client receives this response, it can disconnect
99 // Do all of the specified tasks...
100 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
101 self::executeJobs( (int)$params['maxjobs'] );
106 * @param array $query
107 * @return string
109 public static function getQuerySignature( array $query ) {
110 global $wgSecretKey;
112 ksort( $query ); // stable order
113 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $wgSecretKey );
117 * Run jobs from the job queue
119 * @note: also called from Wiki.php
121 * @param int $maxJobs Maximum number of jobs to run
122 * @return void
124 public static function executeJobs( $maxJobs ) {
125 $n = $maxJobs; // number of jobs to run
126 if ( $n < 1 ) {
127 return;
129 try {
130 $group = JobQueueGroup::singleton();
131 $count = $group->executeReadyPeriodicTasks();
132 if ( $count > 0 ) {
133 wfDebugLog( 'jobqueue', "Executed $count periodic queue task(s)." );
136 do {
137 $job = $group->pop( JobQueueGroup::TYPE_DEFAULT, JobQueueGroup::USE_CACHE );
138 if ( $job ) {
139 $output = $job->toString() . "\n";
140 $t = -microtime( true );
141 wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
142 $success = $job->run();
143 wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
144 $group->ack( $job ); // done
145 $t += microtime( true );
146 $t = round( $t * 1000 );
147 if ( $success === false ) {
148 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
149 } else {
150 $output .= "Success, Time: $t ms\n";
152 wfDebugLog( 'jobqueue', $output );
154 } while ( --$n && $job );
155 } catch ( MWException $e ) {
156 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
157 // We don't want exceptions thrown during job execution to
158 // be reported to the user since the output is already sent.
159 // Instead we just log them.
160 MWExceptionHandler::logException( $e );