Remove errant return, breaks switch statement for 'htmlCacheUpdate' jobs
[lhc/web/wiklou.git] / includes / JobQueue.php
1 <?php
2
3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
5 }
6
7 require_once('UserMailer.php');
8
9 /**
10 * Class to both describe a background job and handle jobs.
11 */
12 abstract class Job {
13 var $command,
14 $title,
15 $params,
16 $id,
17 $removeDuplicates,
18 $error;
19
20 /*-------------------------------------------------------------------------
21 * Abstract functions
22 *------------------------------------------------------------------------*/
23
24 /**
25 * Run the job
26 * @return boolean success
27 */
28 abstract function run();
29
30 /*-------------------------------------------------------------------------
31 * Static functions
32 *------------------------------------------------------------------------*/
33
34 /**
35 * @deprecated use LinksUpdate::queueRecursiveJobs()
36 */
37 /**
38 * static function queueLinksJobs( $titles ) {}
39 */
40
41 /**
42 * Pop a job of a certain type. This tries less hard than pop() to
43 * actually find a job; it may be adversely affected by concurrent job
44 * runners.
45 */
46 static function pop_type($type) {
47 wfProfilein( __METHOD__ );
48
49 $dbw = wfGetDB( DB_MASTER );
50
51
52 $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__,
53 array( 'LIMIT' => 1 ));
54
55 if ($row === false) {
56 wfProfileOut( __METHOD__ );
57 return false;
58 }
59
60 /* Ensure we "own" this row */
61 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
62 $affected = $dbw->affectedRows();
63
64 if ($affected == 0) {
65 wfProfileOut( __METHOD__ );
66 return false;
67 }
68
69 $namespace = $row->job_namespace;
70 $dbkey = $row->job_title;
71 $title = Title::makeTitleSafe( $namespace, $dbkey );
72 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
73
74 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
75 $dbw->immediateCommit();
76
77 wfProfileOut( __METHOD__ );
78 return $job;
79 }
80
81 /**
82 * Pop a job off the front of the queue
83 * @static
84 * @param $offset Number of jobs to skip
85 * @return Job or false if there's no jobs
86 */
87 static function pop($offset=0) {
88 wfProfileIn( __METHOD__ );
89
90 $dbr = wfGetDB( DB_SLAVE );
91
92 /* Get a job from the slave, start with an offset,
93 scan full set afterwards, avoid hitting purged rows
94
95 NB: If random fetch previously was used, offset
96 will always be ahead of few entries
97 */
98
99 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
100 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
101
102 // Refetching without offset is needed as some of job IDs could have had delayed commits
103 // and have lower IDs than jobs already executed, blame concurrency :)
104 //
105 if ( $row === false) {
106 if ($offset!=0)
107 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
108 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
109
110 if ($row === false ) {
111 wfProfileOut( __METHOD__ );
112 return false;
113 }
114 }
115 $offset = $row->job_id;
116
117 // Try to delete it from the master
118 $dbw = wfGetDB( DB_MASTER );
119 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
120 $affected = $dbw->affectedRows();
121 $dbw->immediateCommit();
122
123 if ( !$affected ) {
124 // Failed, someone else beat us to it
125 // Try getting a random row
126 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
127 'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__ );
128 if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
129 // No jobs to get
130 wfProfileOut( __METHOD__ );
131 return false;
132 }
133 // Get the random row
134 $row = $dbw->selectRow( 'job', '*',
135 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
136 if ( $row === false ) {
137 // Random job gone before we got the chance to select it
138 // Give up
139 wfProfileOut( __METHOD__ );
140 return false;
141 }
142 // Delete the random row
143 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
144 $affected = $dbw->affectedRows();
145 $dbw->immediateCommit();
146
147 if ( !$affected ) {
148 // Random job gone before we exclusively deleted it
149 // Give up
150 wfProfileOut( __METHOD__ );
151 return false;
152 }
153 }
154
155 // If execution got to here, there's a row in $row that has been deleted from the database
156 // by this thread. Hence the concurrent pop was successful.
157 $namespace = $row->job_namespace;
158 $dbkey = $row->job_title;
159 $title = Title::makeTitleSafe( $namespace, $dbkey );
160 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
161
162 // Remove any duplicates it may have later in the queue
163 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
164
165 wfProfileOut( __METHOD__ );
166 return $job;
167 }
168
169 /**
170 * Create an object of a subclass
171 */
172 static function factory( $command, $title, $params = false, $id = 0 ) {
173 switch ( $command ) {
174 case 'refreshLinks':
175 return new RefreshLinksJob( $title, $params, $id );
176 case 'htmlCacheUpdate':
177 case 'html_cache_update': # BC
178 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
179 case 'sendMail':
180 return new EmaillingJob( $params );
181 case 'enotifNotify':
182 return new EnotifNotifyJob( $title, $params );
183 }
184 // OK, check if this is a custom job
185 global $wgCustomJobs;
186 wfLoadAllExtensions(); // This may be for an extension
187
188 if( isset($wgCustomJobs[$command]) ) {
189 $class = $wgCustomJobs[$command];
190 return new $class($title, $params, $id);
191 } else {
192 throw new MWException( "Invalid job command \"$command\"" );
193 }
194 }
195
196 static function makeBlob( $params ) {
197 if ( $params !== false ) {
198 return serialize( $params );
199 } else {
200 return '';
201 }
202 }
203
204 static function extractBlob( $blob ) {
205 if ( (string)$blob !== '' ) {
206 return unserialize( $blob );
207 } else {
208 return false;
209 }
210 }
211
212 /**
213 * Batch-insert a group of jobs into the queue.
214 * This will be wrapped in a transaction with a forced commit.
215 *
216 * This may add duplicate at insert time, but they will be
217 * removed later on, when the first one is popped.
218 *
219 * @param $jobs array of Job objects
220 */
221 static function batchInsert( $jobs ) {
222 if( count( $jobs ) ) {
223 $dbw = wfGetDB( DB_MASTER );
224 $dbw->begin();
225 foreach( $jobs as $job ) {
226 $rows[] = $job->insertFields();
227 }
228 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
229 $dbw->commit();
230 }
231 }
232
233 /*-------------------------------------------------------------------------
234 * Non-static functions
235 *------------------------------------------------------------------------*/
236
237 function __construct( $command, $title, $params = false, $id = 0 ) {
238 $this->command = $command;
239 $this->title = $title;
240 $this->params = $params;
241 $this->id = $id;
242
243 // A bit of premature generalisation
244 // Oh well, the whole class is premature generalisation really
245 $this->removeDuplicates = true;
246 }
247
248 /**
249 * Insert a single job into the queue.
250 */
251 function insert() {
252 $fields = $this->insertFields();
253
254 $dbw = wfGetDB( DB_MASTER );
255
256 if ( $this->removeDuplicates ) {
257 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
258 if ( $dbw->numRows( $res ) ) {
259 return;
260 }
261 }
262 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
263 $dbw->insert( 'job', $fields, __METHOD__ );
264 }
265
266 protected function insertFields() {
267 return array(
268 'job_cmd' => $this->command,
269 'job_namespace' => $this->title->getNamespace(),
270 'job_title' => $this->title->getDBkey(),
271 'job_params' => Job::makeBlob( $this->params )
272 );
273 }
274
275 function toString() {
276 $paramString = '';
277 if ( $this->params ) {
278 foreach ( $this->params as $key => $value ) {
279 if ( $paramString != '' ) {
280 $paramString .= ' ';
281 }
282 $paramString .= "$key=$value";
283 }
284 }
285
286 if ( is_object( $this->title ) ) {
287 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
288 if ( $paramString !== '' ) {
289 $s .= ' ' . $paramString;
290 }
291 return $s;
292 } else {
293 return "{$this->command} $paramString";
294 }
295 }
296
297 function getLastError() {
298 return $this->error;
299 }
300 }
301
302
303 /**
304 * Background job to update links for a given title.
305 */
306 class RefreshLinksJob extends Job {
307 function __construct( $title, $params = '', $id = 0 ) {
308 parent::__construct( 'refreshLinks', $title, $params, $id );
309 }
310
311 /**
312 * Run a refreshLinks job
313 * @return boolean success
314 */
315 function run() {
316 global $wgParser;
317 wfProfileIn( __METHOD__ );
318
319 $linkCache =& LinkCache::singleton();
320 $linkCache->clear();
321
322 if ( is_null( $this->title ) ) {
323 $this->error = "refreshLinks: Invalid title";
324 wfProfileOut( __METHOD__ );
325 return false;
326 }
327
328 $revision = Revision::newFromTitle( $this->title );
329 if ( !$revision ) {
330 $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
331 wfProfileOut( __METHOD__ );
332 return false;
333 }
334
335 wfProfileIn( __METHOD__.'-parse' );
336 $options = new ParserOptions;
337 $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
338 wfProfileOut( __METHOD__.'-parse' );
339 wfProfileIn( __METHOD__.'-update' );
340 $update = new LinksUpdate( $this->title, $parserOutput, false );
341 $update->doUpdate();
342 wfProfileOut( __METHOD__.'-update' );
343 wfProfileOut( __METHOD__ );
344 return true;
345 }
346 }
347
348 class EmaillingJob extends Job {
349 function __construct($params) {
350 parent::__construct('sendMail', Title::newMainPage(), $params);
351 }
352
353 function run() {
354 userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
355 $this->params['body'], $this->params['replyto']);
356 }
357 }
358
359 class EnotifNotifyJob extends Job {
360 function __construct($title, $params) {
361 parent::__construct('enotifNotify', $title, $params);
362 }
363
364 function run() {
365 $enotif = new EmailNotification();
366 $enotif->actuallyNotifyOnPageChange( User::newFromName($this->params['editor'], false),
367 $this->title, $this->params['timestamp'],
368 $this->params['summary'], $this->params['minorEdit'],
369 $this->params['oldid']);
370 }
371 }
372
373
374 ?>