r23173 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r23172‎ | r23173 | r23174 >
Date:19:11, 21 June 2007
Author:robchurch
Status:old
Tags:
Comment:
Some job cleanup:

* Move Jobs left in JobQueue.php to their own file
* Ditch $wgCustomJobs in favour of $wgJobClasses, which acts as a dictionary and allows extensions to add custom jobs
* Standardise Job derivative constructors and update everywhere
* Make sure all overriding implementations of Job::run() return true to avoid bogus "Error" report in runJobs.php
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/AutoLoader.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/EmaillingJob.php (added) (history)
  • /trunk/phase3/includes/EnotifNotifyJob.php (added) (history)
  • /trunk/phase3/includes/HTMLCacheUpdate.php (modified) (history)
  • /trunk/phase3/includes/JobQueue.php (modified) (history)
  • /trunk/phase3/includes/LinksUpdate.php (modified) (history)
  • /trunk/phase3/includes/RefreshLinksJob.php (added) (history)
  • /trunk/phase3/includes/UserMailer.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/RefreshLinksJob.php
@@ -0,0 +1,49 @@
 2+<?php
 3+
 4+/**
 5+ * Background job to update links for a given title.
 6+ */
 7+class RefreshLinksJob extends Job {
 8+
 9+ function __construct( $title, $params = '', $id = 0 ) {
 10+ parent::__construct( 'refreshLinks', $title, $params, $id );
 11+ }
 12+
 13+ /**
 14+ * Run a refreshLinks job
 15+ * @return boolean success
 16+ */
 17+ function run() {
 18+ global $wgParser;
 19+ wfProfileIn( __METHOD__ );
 20+
 21+ $linkCache =& LinkCache::singleton();
 22+ $linkCache->clear();
 23+
 24+ if ( is_null( $this->title ) ) {
 25+ $this->error = "refreshLinks: Invalid title";
 26+ wfProfileOut( __METHOD__ );
 27+ return false;
 28+ }
 29+
 30+ $revision = Revision::newFromTitle( $this->title );
 31+ if ( !$revision ) {
 32+ $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
 33+ wfProfileOut( __METHOD__ );
 34+ return false;
 35+ }
 36+
 37+ wfProfileIn( __METHOD__.'-parse' );
 38+ $options = new ParserOptions;
 39+ $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
 40+ wfProfileOut( __METHOD__.'-parse' );
 41+ wfProfileIn( __METHOD__.'-update' );
 42+ $update = new LinksUpdate( $this->title, $parserOutput, false );
 43+ $update->doUpdate();
 44+ wfProfileOut( __METHOD__.'-update' );
 45+ wfProfileOut( __METHOD__ );
 46+ return true;
 47+ }
 48+}
 49+
 50+?>
\ No newline at end of file
Property changes on: trunk/phase3/includes/RefreshLinksJob.php
___________________________________________________________________
Name: svn:eol-style
151 + native
Index: trunk/phase3/includes/EnotifNotifyJob.php
@@ -0,0 +1,27 @@
 2+<?php
 3+
 4+/**
 5+ * Job for email notification mails
 6+ */
 7+class EnotifNotifyJob extends Job {
 8+
 9+ function __construct( $title, $params, $id = 0 ) {
 10+ parent::__construct( 'enotifNotify', $title, $params, $id );
 11+ }
 12+
 13+ function run() {
 14+ $enotif = new EmailNotification();
 15+ $enotif->actuallyNotifyOnPageChange(
 16+ User::newFromName( $this->params['editor'], false ),
 17+ $this->title,
 18+ $this->params['timestamp'],
 19+ $this->params['summary'],
 20+ $this->params['minorEdit'],
 21+ $this->params['oldid']
 22+ );
 23+ return true;
 24+ }
 25+
 26+}
 27+
 28+?>
\ No newline at end of file
Property changes on: trunk/phase3/includes/EnotifNotifyJob.php
___________________________________________________________________
Name: svn:eol-style
129 + native
Index: trunk/phase3/includes/EmaillingJob.php
@@ -0,0 +1,26 @@
 2+<?php
 3+
 4+/**
 5+ * Old job used for sending single notification emails;
 6+ * kept for backwards-compatibility
 7+ */
 8+class EmaillingJob extends Job {
 9+
 10+ function __construct( $title, $params, $id = 0 ) {
 11+ parent::__construct( 'sendMail', Title::newMainPage(), $params, $id );
 12+ }
 13+
 14+ function run() {
 15+ userMailer(
 16+ $this->params['to'],
 17+ $this->params['from'],
 18+ $this->params['subj'],
 19+ $this->params['body'],
 20+ $this->params['replyto']
 21+ );
 22+ return true;
 23+ }
 24+
 25+}
 26+
 27+?>
\ No newline at end of file
Property changes on: trunk/phase3/includes/EmaillingJob.php
___________________________________________________________________
Name: svn:eol-style
128 + native
Index: trunk/phase3/includes/UserMailer.php
@@ -247,7 +247,7 @@
248248 "summary" => $summary,
249249 "minorEdit" => $minorEdit,
250250 "oldid" => $oldid);
251 - $job = new EnotifNotifyJob($title, $params);
 251+ $job = new EnotifNotifyJob( $title, $params );
252252 $job->insert();
253253 } else {
254254 $this->actuallyNotifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid);
Index: trunk/phase3/includes/JobQueue.php
@@ -167,32 +167,23 @@
168168 }
169169
170170 /**
171 - * Create an object of a subclass
 171+ * Create the appropriate object to handle a specific job
 172+ *
 173+ * @param string $command Job command
 174+ * @param Title $title Associated title
 175+ * @param array $params Job parameters
 176+ * @param int $id Job identifier
 177+ * @return Job
172178 */
173179 static function factory( $command, $title, $params = false, $id = 0 ) {
174 - switch ( $command ) {
175 - case 'refreshLinks':
176 - return new RefreshLinksJob( $title, $params, $id );
177 - case 'htmlCacheUpdate':
178 - case 'html_cache_update': # BC
179 - return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
180 - case 'sendMail':
181 - return new EmaillingJob( $params );
182 - case 'enotifNotify':
183 - return new EnotifNotifyJob( $title, $params );
 180+ global $wgJobClasses;
 181+ if( isset( $wgJobClasses[$command] ) ) {
 182+ $class = $wgJobClasses[$command];
 183+ return new $class( $title, $params, $id );
184184 }
185 - // OK, check if this is a custom job
186 - global $wgCustomJobs;
187 - wfLoadAllExtensions(); // This may be for an extension
188 -
189 - if( isset($wgCustomJobs[$command]) ) {
190 - $class = $wgCustomJobs[$command];
191 - return new $class($title, $params, $id);
192 - } else {
193 - throw new MWException( "Invalid job command \"$command\"" );
194 - }
 185+ throw new MWException( "Invalid job command `{$command}`" );
195186 }
196 -
 187+
197188 static function makeBlob( $params ) {
198189 if ( $params !== false ) {
199190 return serialize( $params );
@@ -299,76 +290,4 @@
300291 }
301292 }
302293
303 -
304 -/**
305 - * Background job to update links for a given title.
306 - */
307 -class RefreshLinksJob extends Job {
308 - function __construct( $title, $params = '', $id = 0 ) {
309 - parent::__construct( 'refreshLinks', $title, $params, $id );
310 - }
311 -
312 - /**
313 - * Run a refreshLinks job
314 - * @return boolean success
315 - */
316 - function run() {
317 - global $wgParser;
318 - wfProfileIn( __METHOD__ );
319 -
320 - $linkCache =& LinkCache::singleton();
321 - $linkCache->clear();
322 -
323 - if ( is_null( $this->title ) ) {
324 - $this->error = "refreshLinks: Invalid title";
325 - wfProfileOut( __METHOD__ );
326 - return false;
327 - }
328 -
329 - $revision = Revision::newFromTitle( $this->title );
330 - if ( !$revision ) {
331 - $this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
332 - wfProfileOut( __METHOD__ );
333 - return false;
334 - }
335 -
336 - wfProfileIn( __METHOD__.'-parse' );
337 - $options = new ParserOptions;
338 - $parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
339 - wfProfileOut( __METHOD__.'-parse' );
340 - wfProfileIn( __METHOD__.'-update' );
341 - $update = new LinksUpdate( $this->title, $parserOutput, false );
342 - $update->doUpdate();
343 - wfProfileOut( __METHOD__.'-update' );
344 - wfProfileOut( __METHOD__ );
345 - return true;
346 - }
347 -}
348 -
349 -class EmaillingJob extends Job {
350 - function __construct($params) {
351 - parent::__construct('sendMail', Title::newMainPage(), $params);
352 - }
353 -
354 - function run() {
355 - userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
356 - $this->params['body'], $this->params['replyto']);
357 - }
358 -}
359 -
360 -class EnotifNotifyJob extends Job {
361 - function __construct($title, $params) {
362 - parent::__construct('enotifNotify', $title, $params);
363 - }
364 -
365 - function run() {
366 - $enotif = new EmailNotification();
367 - $enotif->actuallyNotifyOnPageChange( User::newFromName($this->params['editor'], false),
368 - $this->title, $this->params['timestamp'],
369 - $this->params['summary'], $this->params['minorEdit'],
370 - $this->params['oldid']);
371 - }
372 -}
373 -
374 -
375 -?>
 294+?>
\ No newline at end of file
Index: trunk/phase3/includes/LinksUpdate.php
@@ -189,7 +189,7 @@
190190 break;
191191 }
192192 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
193 - $jobs[] = Job::factory( 'refreshLinks', $title );
 193+ $jobs[] = new RefreshLinksJob( $title, '' );
194194 }
195195 Job::batchInsert( $jobs );
196196 }
Index: trunk/phase3/includes/AutoLoader.php
@@ -94,8 +94,6 @@
9595 'HistoryBlobStub' => 'includes/HistoryBlob.php',
9696 'HistoryBlobCurStub' => 'includes/HistoryBlob.php',
9797 'HTMLCacheUpdate' => 'includes/HTMLCacheUpdate.php',
98 - 'HTMLCacheUpdateJob' => 'includes/HTMLCacheUpdate.php',
99 - 'EnotifNotifyJob' => 'includes/JobQueue.php',
10098 'Http' => 'includes/HttpFunctions.php',
10199 'IP' => 'includes/IP.php',
102100 'ThumbnailImage' => 'includes/Image.php',
@@ -104,6 +102,10 @@
105103 'ImageHistoryList' => 'includes/ImagePage.php',
106104 'ImageRemote' => 'includes/ImageRemote.php',
107105 'Job' => 'includes/JobQueue.php',
 106+ 'EmaillingJob' => 'includes/EmaillingJob.php',
 107+ 'EnotifNotifyJob' => 'includes/EnotifNotifyJob.php',
 108+ 'HTMLCacheUpdateJob' => 'includes/HTMLCacheUpdate.php',
 109+ 'RefreshLinksJob' => 'includes/RefreshLinksJob.php',
108110 'Licenses' => 'includes/Licenses.php',
109111 'License' => 'includes/Licenses.php',
110112 'LinkBatch' => 'includes/LinkBatch.php',
Index: trunk/phase3/includes/HTMLCacheUpdate.php
@@ -67,13 +67,13 @@
6868 break;
6969 }
7070 }
71 - if ( $id !== false ) {
72 - // One less on the end to avoid duplicating the boundary
73 - $job = new HTMLCacheUpdateJob( $this->mTitle, $this->mTable, $start, $id - 1 );
74 - } else {
75 - $job = new HTMLCacheUpdateJob( $this->mTitle, $this->mTable, $start, false );
76 - }
77 - $jobs[] = $job;
 71+
 72+ $params = array(
 73+ 'table' => $this->mTable,
 74+ 'start' => $start,
 75+ 'end' => ( $id !== false ? $id - 1 : false ),
 76+ );
 77+ $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
7878
7979 $start = $id;
8080 } while ( $start );
@@ -193,20 +193,14 @@
194194 /**
195195 * Construct a job
196196 * @param Title $title The title linked to
197 - * @param string $table The name of the link table.
198 - * @param integer $start Beginning page_id or false for open interval
199 - * @param integer $end End page_id or false for open interval
 197+ * @param array $params Job parameters (table, start and end page_ids)
200198 * @param integer $id job_id
201199 */
202 - function __construct( $title, $table, $start, $end, $id = 0 ) {
203 - $params = array(
204 - 'table' => $table,
205 - 'start' => $start,
206 - 'end' => $end );
 200+ function __construct( $title, $params, $id = 0 ) {
207201 parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
208 - $this->table = $table;
209 - $this->start = intval( $start );
210 - $this->end = intval( $end );
 202+ $this->table = $params['table'];
 203+ $this->start = $params['start'];
 204+ $this->end = $params['end'];
211205 }
212206
213207 function run() {
Index: trunk/phase3/includes/DefaultSettings.php
@@ -1361,10 +1361,16 @@
13621362 $wgAllowSlowParserFunctions = false;
13631363
13641364 /**
1365 - * Extra custom jobs can be added to the Job Queue system.
1366 - * This array should consist of job name => job queue subclass pairs
 1365+ * Maps jobs to their handling classes; extensions
 1366+ * can add to this to provide custom jobs
13671367 */
1368 -$wgCustomJobs = array();
 1368+$wgJobClasses = array(
 1369+ 'refreshLinks' => 'RefreshLinksJob',
 1370+ 'htmlCacheUpdate' => 'HTMLCacheUpdateJob',
 1371+ 'html_cache_update' => 'HTMLCacheUpdateJob', // backwards-compatible
 1372+ 'sendMail' => 'EmaillingJob',
 1373+ 'enotifNotify' => 'EnotifNotifyJob',
 1374+);
13691375
13701376 /**
13711377 * To use inline TeX, you need to compile 'texvc' (in the 'math' subdirectory of
Index: trunk/phase3/RELEASE-NOTES
@@ -97,6 +97,8 @@
9898 to cause hard-to-track-down interactions between extensions.
9999 * (bug 9415) Added options to Special:Protect to allow setting of per-page robot
100100 policies. This can be done only by users with the 'editrobots' permission
 101+* Use $wgJobClasses to determine the correct Job to instantiate for a particular
 102+ queued task; allows extensions to introduce custom jobs
101103
102104 == Bugfixes since 1.10 ==
103105

Follow-up revisions

RevisionCommit summaryAuthorDate
r23203Merged revisions 23120-23202 via svnmerge from...david09:07, 22 June 2007

Status & tagging log