Merge "Function type hints for LinkHolderArray.php"
[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
42 return;
43 } elseif ( !$this->getRequest()->wasPosted() ) {
44 header( "HTTP/1.0 400 Bad Request" );
45 print 'Request must be POSTed';
46
47 return;
48 }
49
50 $optional = array( 'maxjobs' => 0 );
51 $required = array_flip( array( 'title', 'tasks', 'signature', 'sigexpiry' ) );
52
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 ) );
58
59 return;
60 }
61
62 $squery = $params;
63 unset( $squery['signature'] );
64 $cSig = self::getQuerySignature( $squery ); // correct signature
65 $rSig = $params['signature']; // provided signature
66
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';
71
72 return;
73 }
74
75 // Apply any default parameter values
76 $params += $optional;
77
78 // Client will usually disconnect before checking the response,
79 // but it needs to know when it is safe to disconnect. Until this
80 // reaches ignore_user_abort(), it is not safe as the jobs won't run.
81 ignore_user_abort( true ); // jobs may take a bit of time
82 header( "HTTP/1.0 202 Accepted" );
83 ob_flush();
84 flush();
85 // Once the client receives this response, it can disconnect
86
87 // Do all of the specified tasks...
88 if ( in_array( 'jobs', explode( '|', $params['tasks'] ) ) ) {
89 self::executeJobs( (int)$params['maxjobs'] );
90 }
91 }
92
93 /**
94 * @param array $query
95 * @return string
96 */
97 public static function getQuerySignature( array $query ) {
98 global $wgSecretKey;
99
100 ksort( $query ); // stable order
101 return hash_hmac( 'sha1', wfArrayToCgi( $query ), $wgSecretKey );
102 }
103
104 /**
105 * Run jobs from the job queue
106 *
107 * @note: also called from Wiki.php
108 *
109 * @param int $maxJobs Maximum number of jobs to run
110 * @return void
111 */
112 public static function executeJobs( $maxJobs ) {
113 $n = $maxJobs; // number of jobs to run
114 if ( $n < 1 ) {
115 return;
116 }
117 try {
118 $group = JobQueueGroup::singleton();
119 $count = $group->executeReadyPeriodicTasks();
120 if ( $count > 0 ) {
121 wfDebugLog( 'jobqueue', "Executed $count periodic queue task(s)." );
122 }
123
124 do {
125 $job = $group->pop( JobQueueGroup::TYPE_DEFAULT, JobQueueGroup::USE_CACHE );
126 if ( $job ) {
127 $output = $job->toString() . "\n";
128 $t = -microtime( true );
129 wfProfileIn( __METHOD__ . '-' . get_class( $job ) );
130 $success = $job->run();
131 wfProfileOut( __METHOD__ . '-' . get_class( $job ) );
132 $group->ack( $job ); // done
133 $t += microtime( true );
134 $t = round( $t * 1000 );
135 if ( $success === false ) {
136 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
137 } else {
138 $output .= "Success, Time: $t ms\n";
139 }
140 wfDebugLog( 'jobqueue', $output );
141 }
142 } while ( --$n && $job );
143 } catch ( MWException $e ) {
144 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
145 // We don't want exceptions thrown during job execution to
146 // be reported to the user since the output is already sent.
147 // Instead we just log them.
148 MWExceptionHandler::logException( $e );
149 }
150 }
151 }