Merge "Update the documentation at the top of parserTests.txt"
[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 * @author Aaron Schulz
23 */
24
25 /**
26 * Special page designed for running background tasks (internal use only)
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialRunJobs extends UnlistedSpecialPage {
31 public function __construct() {
32 parent::__construct( 'RunJobs' );
33 }
34
35 public function execute( $par = '' ) {
36 $this->getOutput()->disable();
37
38 if ( wfReadOnly() ) {
39 header( "HTTP/1.0 423 Locked" );
40 print 'Wiki is in read-only mode';
41 return;
42 } elseif ( !$this->getRequest()->wasPosted() ) {
43 header( "HTTP/1.0 400 Bad Request" );
44 print 'Request must be POSTed';
45 return;
46 }
47
48 $optional = array( 'maxjobs' => 0 );
49 $required = array_flip( array( 'title', 'tasks', 'signature', 'sigexpiry' ) );
50
51 $params = array_intersect_key( $this->getRequest()->getValues(), $required + $optional );
52 $missing = array_diff_key( $required, $params );
53 if ( count( $missing ) ) {
54 header( "HTTP/1.0 400 Bad Request" );
55 print 'Missing parameters: ' . implode( ', ', array_keys( $missing ) );
56 return;
57 }
58
59 $squery = $params;
60 unset( $squery['signature'] );
61 $cSig = self::getQuerySignature( $squery ); // correct signature
62 $rSig = $params['signature']; // provided signature
63
64 // Constant-time signature verification
65 // http://www.emerose.com/timing-attacks-explained
66 // @todo: make a common method for this
67 if ( !is_string( $rSig ) || strlen( $rSig ) !== strlen( $cSig ) ) {
68 $verified = false;
69 } else {
70 $result = 0;
71 for ( $i = 0; $i < strlen( $cSig ); $i++ ) {
72 $result |= ord( $cSig[$i] ) ^ ord( $rSig[$i] );
73 }
74 $verified = ( $result == 0 );
75 }
76 if ( !$verified || $params['sigexpiry'] < time() ) {
77 header( "HTTP/1.0 400 Bad Request" );
78 print 'Invalid or stale signature provided';
79 return;
80 }
81
82 // Apply any default parameter values
83 $params += $optional;
84
85 // Client will usually disconnect before checking the response,
86 // but it needs to know when it is safe to disconnect. Until this
87 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
88 ignore_user_abort( true ); // jobs may take a bit of time
89 header( "HTTP/1.0 202 Accepted" );
90 ob_flush();
91 flush();
92 // Once the client receives this response, it can disconnect
93
94 // Do all of the specified tasks...
95 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
96 self::executeJobs( (int)$params['maxjobs'] );
97 }
98 }
99
100 /**
101 * @param array $query
102 * @return string
103 */
104 public static function getQuerySignature( array $query ) {
105 global $wgSecretKey;
106
107 ksort( $query ); // stable order
108 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $wgSecretKey );
109 }
110
111 /**
112 * Run jobs from the job queue
113 *
114 * @note: also called from Wiki.php
115 *
116 * @param integer $maxJobs Maximum number of jobs to run
117 * @return void
118 */
119 public static function executeJobs( $maxJobs ) {
120 $n = $maxJobs; // number of jobs to run
121 if ( $n < 1 ) {
122 return;
123 }
124 try {
125 $group = JobQueueGroup::singleton();
126 $count = $group->executeReadyPeriodicTasks();
127 if ( $count > 0 ) {
128 wfDebugLog( 'jobqueue', "Executed $count periodic queue task(s)." );
129 }
130
131 do {
132 $job = $group->pop( JobQueueGroup::TYPE_DEFAULT, JobQueueGroup::USE_CACHE );
133 if ( $job ) {
134 $output = $job->toString() . "\n";
135 $t = - microtime( true );
136 wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
137 $success = $job->run();
138 wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
139 $group->ack( $job ); // done
140 $t += microtime( true );
141 $t = round( $t * 1000 );
142 if ( $success === false ) {
143 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
144 } else {
145 $output .= "Success, Time: $t ms\n";
146 }
147 wfDebugLog( 'jobqueue', $output );
148 }
149 } while ( --$n && $job );
150 } catch ( MWException $e ) {
151 // We don't want exceptions thrown during job execution to
152 // be reported to the user since the output is already sent.
153 // Instead we just log them.
154 MWExceptionHandler::logException( $e );
155 }
156 }
157 }