MediaWiki r23173 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r23172‎ | r23173 (on ViewVC)‎ | 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:

Diff [purge]

Index: trunk/phase3/includes/RefreshLinksJob.php
===================================================================
--- trunk/phase3/includes/RefreshLinksJob.php	(revision 0)
+++ trunk/phase3/includes/RefreshLinksJob.php	(revision 23173)
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * Background job to update links for a given title.
+ */
+class RefreshLinksJob extends Job {
+
+	function __construct( $title, $params = '', $id = 0 ) {
+		parent::__construct( 'refreshLinks', $title, $params, $id );
+	}
+
+	/**
+	 * Run a refreshLinks job
+	 * @return boolean success
+	 */
+	function run() {
+		global $wgParser;
+		wfProfileIn( __METHOD__ );
+
+		$linkCache =& LinkCache::singleton();
+		$linkCache->clear();
+
+		if ( is_null( $this->title ) ) {
+			$this->error = "refreshLinks: Invalid title";
+			wfProfileOut( __METHOD__ );
+			return false;
+		}
+
+		$revision = Revision::newFromTitle( $this->title );
+		if ( !$revision ) {
+			$this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
+			wfProfileOut( __METHOD__ );
+			return false;
+		}
+
+		wfProfileIn( __METHOD__.'-parse' );
+		$options = new ParserOptions;
+		$parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
+		wfProfileOut( __METHOD__.'-parse' );
+		wfProfileIn( __METHOD__.'-update' );
+		$update = new LinksUpdate( $this->title, $parserOutput, false );
+		$update->doUpdate();
+		wfProfileOut( __METHOD__.'-update' );
+		wfProfileOut( __METHOD__ );
+		return true;
+	}
+}
+
+?>
\ No newline at end of file

Property changes on: trunk/phase3/includes/RefreshLinksJob.php
___________________________________________________________________
Name: svn:eol-style
   + native

Index: trunk/phase3/includes/EnotifNotifyJob.php
===================================================================
--- trunk/phase3/includes/EnotifNotifyJob.php	(revision 0)
+++ trunk/phase3/includes/EnotifNotifyJob.php	(revision 23173)
@@ -0,0 +1,27 @@
+<?php
+
+/**
+ * Job for email notification mails
+ */
+class EnotifNotifyJob extends Job {
+
+	function __construct( $title, $params, $id = 0 ) {
+		parent::__construct( 'enotifNotify', $title, $params, $id );
+	}
+
+	function run() {
+		$enotif = new EmailNotification();
+		$enotif->actuallyNotifyOnPageChange(
+			User::newFromName( $this->params['editor'], false ),
+				$this->title,
+				$this->params['timestamp'],
+				$this->params['summary'],
+				$this->params['minorEdit'],
+				$this->params['oldid']
+		);
+		return true;
+	}
+	
+}
+
+?>
\ No newline at end of file

Property changes on: trunk/phase3/includes/EnotifNotifyJob.php
___________________________________________________________________
Name: svn:eol-style
   + native

Index: trunk/phase3/includes/EmaillingJob.php
===================================================================
--- trunk/phase3/includes/EmaillingJob.php	(revision 0)
+++ trunk/phase3/includes/EmaillingJob.php	(revision 23173)
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * Old job used for sending single notification emails;
+ * kept for backwards-compatibility
+ */
+class EmaillingJob extends Job {
+
+	function __construct( $title, $params, $id = 0 ) {
+		parent::__construct( 'sendMail', Title::newMainPage(), $params, $id );
+	}
+
+	function run() {
+		userMailer(
+			$this->params['to'],
+			$this->params['from'],
+			$this->params['subj'],
+			$this->params['body'],
+			$this->params['replyto']
+		);
+		return true;
+	}
+	
+}
+
+?>
\ No newline at end of file

Property changes on: trunk/phase3/includes/EmaillingJob.php
___________________________________________________________________
Name: svn:eol-style
   + native

Index: trunk/phase3/includes/UserMailer.php
===================================================================
--- trunk/phase3/includes/UserMailer.php	(revision 23172)
+++ trunk/phase3/includes/UserMailer.php	(revision 23173)
@@ -247,7 +247,7 @@
 				"summary" => $summary,
 				"minorEdit" => $minorEdit,
 				"oldid" => $oldid);
-			$job = new EnotifNotifyJob($title, $params);
+			$job = new EnotifNotifyJob( $title, $params );
 			$job->insert();
 		} else {
 			$this->actuallyNotifyOnPageChange($editor, $title, $timestamp, $summary, $minorEdit, $oldid);
Index: trunk/phase3/includes/JobQueue.php
===================================================================
--- trunk/phase3/includes/JobQueue.php	(revision 23172)
+++ trunk/phase3/includes/JobQueue.php	(revision 23173)
@@ -167,32 +167,23 @@
 	}
 
 	/**
-	 * Create an object of a subclass
+	 * Create the appropriate object to handle a specific job
+	 *
+	 * @param string $command Job command
+	 * @param Title $title Associated title
+	 * @param array $params Job parameters
+	 * @param int $id Job identifier
+	 * @return Job
 	 */
 	static function factory( $command, $title, $params = false, $id = 0 ) {
-		switch ( $command ) {
-			case 'refreshLinks':
-				return new RefreshLinksJob( $title, $params, $id );
-			case 'htmlCacheUpdate':
-			case 'html_cache_update': # BC
-				return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
-			case 'sendMail':
-				return new EmaillingJob( $params );
-			case 'enotifNotify':
-				return new EnotifNotifyJob( $title, $params );
+		global $wgJobClasses;
+		if( isset( $wgJobClasses[$command] ) ) {
+			$class = $wgJobClasses[$command];
+			return new $class( $title, $params, $id );
 		}
-		// OK, check if this is a custom job
-		global $wgCustomJobs;
-		wfLoadAllExtensions(); // This may be for an extension
-
-		if( isset($wgCustomJobs[$command]) ) {
-			$class = $wgCustomJobs[$command];
-			return new $class($title, $params, $id);
-		} else {
-			throw new MWException( "Invalid job command \"$command\"" );
-		}
+		throw new MWException( "Invalid job command `{$command}`" );
 	}
-
+		
 	static function makeBlob( $params ) {
 		if ( $params !== false ) {
 			return serialize( $params );
@@ -299,76 +290,4 @@
 	}
 }
 
-
-/**
- * Background job to update links for a given title.
- */
-class RefreshLinksJob extends Job {
-	function __construct( $title, $params = '', $id = 0 ) {
-		parent::__construct( 'refreshLinks', $title, $params, $id );
-	}
-
-	/**
-	 * Run a refreshLinks job
-	 * @return boolean success
-	 */
-	function run() {
-		global $wgParser;
-		wfProfileIn( __METHOD__ );
-
-		$linkCache =& LinkCache::singleton();
-		$linkCache->clear();
-
-		if ( is_null( $this->title ) ) {
-			$this->error = "refreshLinks: Invalid title";
-			wfProfileOut( __METHOD__ );
-			return false;
-		}
-
-		$revision = Revision::newFromTitle( $this->title );
-		if ( !$revision ) {
-			$this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
-			wfProfileOut( __METHOD__ );
-			return false;
-		}
-
-		wfProfileIn( __METHOD__.'-parse' );
-		$options = new ParserOptions;
-		$parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
-		wfProfileOut( __METHOD__.'-parse' );
-		wfProfileIn( __METHOD__.'-update' );
-		$update = new LinksUpdate( $this->title, $parserOutput, false );
-		$update->doUpdate();
-		wfProfileOut( __METHOD__.'-update' );
-		wfProfileOut( __METHOD__ );
-		return true;
-	}
-}
-
-class EmaillingJob extends Job {
-	function __construct($params) {
-		parent::__construct('sendMail', Title::newMainPage(), $params);
-	}
-
-	function run() {
-		userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
-				$this->params['body'], $this->params['replyto']);
-	}
-}
-
-class EnotifNotifyJob extends Job {
-	function __construct($title, $params) {
-		parent::__construct('enotifNotify', $title, $params);
-	}
-
-	function run() {
-		$enotif = new EmailNotification();
-		$enotif->actuallyNotifyOnPageChange( User::newFromName($this->params['editor'], false),
-				$this->title, $this->params['timestamp'],
-				$this->params['summary'], $this->params['minorEdit'],
-				$this->params['oldid']);
-	}
-}
-
-
-?>
+?>
\ No newline at end of file
Index: trunk/phase3/includes/LinksUpdate.php
===================================================================
--- trunk/phase3/includes/LinksUpdate.php	(revision 23172)
+++ trunk/phase3/includes/LinksUpdate.php	(revision 23173)
@@ -189,7 +189,7 @@
 					break;
 				}
 				$title = Title::makeTitle( $row->page_namespace, $row->page_title );
-				$jobs[] = Job::factory( 'refreshLinks', $title );
+				$jobs[] = new RefreshLinksJob( $title, '' );
 			}
 			Job::batchInsert( $jobs );
 		}
Index: trunk/phase3/includes/AutoLoader.php
===================================================================
--- trunk/phase3/includes/AutoLoader.php	(revision 23172)
+++ trunk/phase3/includes/AutoLoader.php	(revision 23173)
@@ -94,8 +94,6 @@
 		'HistoryBlobStub' => 'includes/HistoryBlob.php',
 		'HistoryBlobCurStub' => 'includes/HistoryBlob.php',
 		'HTMLCacheUpdate' => 'includes/HTMLCacheUpdate.php',
-		'HTMLCacheUpdateJob' => 'includes/HTMLCacheUpdate.php',
-		'EnotifNotifyJob' => 'includes/JobQueue.php',
 		'Http' => 'includes/HttpFunctions.php',
 		'IP' => 'includes/IP.php',
 		'ThumbnailImage' => 'includes/Image.php',
@@ -104,6 +102,10 @@
 		'ImageHistoryList' => 'includes/ImagePage.php',
 		'ImageRemote' => 'includes/ImageRemote.php',
 		'Job' => 'includes/JobQueue.php',
+		'EmaillingJob' => 'includes/EmaillingJob.php',
+		'EnotifNotifyJob' => 'includes/EnotifNotifyJob.php',
+		'HTMLCacheUpdateJob' => 'includes/HTMLCacheUpdate.php',
+		'RefreshLinksJob' => 'includes/RefreshLinksJob.php',
 		'Licenses' => 'includes/Licenses.php',
 		'License' => 'includes/Licenses.php',
 		'LinkBatch' => 'includes/LinkBatch.php',
Index: trunk/phase3/includes/HTMLCacheUpdate.php
===================================================================
--- trunk/phase3/includes/HTMLCacheUpdate.php	(revision 23172)
+++ trunk/phase3/includes/HTMLCacheUpdate.php	(revision 23173)
@@ -67,13 +67,13 @@
 					break;
 				}
 			}
-			if ( $id !== false ) {
-				// One less on the end to avoid duplicating the boundary
-				$job = new HTMLCacheUpdateJob( $this->mTitle, $this->mTable, $start, $id - 1 );
-			} else {
-				$job = new HTMLCacheUpdateJob( $this->mTitle, $this->mTable, $start, false );
-			}
-			$jobs[] = $job;
+			
+			$params = array(
+				'table' => $this->mTable,
+				'start' => $start,
+				'end' => ( $id !== false ? $id - 1 : false ),
+			);
+			$jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
 
 			$start = $id;
 		} while ( $start );
@@ -193,20 +193,14 @@
 	/**
 	 * Construct a job
 	 * @param Title $title The title linked to
-	 * @param string $table The name of the link table.
-	 * @param integer $start Beginning page_id or false for open interval
-	 * @param integer $end End page_id or false for open interval
+	 * @param array $params Job parameters (table, start and end page_ids)
 	 * @param integer $id job_id
 	 */
-	function __construct( $title, $table, $start, $end, $id = 0 ) {
-		$params = array(
-			'table' => $table, 
-			'start' => $start, 
-			'end' => $end );
+	function __construct( $title, $params, $id = 0 ) {
 		parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
-		$this->table = $table;
-		$this->start = intval( $start );
-		$this->end = intval( $end );
+		$this->table = $params['table'];
+		$this->start = $params['start'];
+		$this->end = $params['end'];
 	}
 
 	function run() {
Index: trunk/phase3/includes/DefaultSettings.php
===================================================================
--- trunk/phase3/includes/DefaultSettings.php	(revision 23172)
+++ trunk/phase3/includes/DefaultSettings.php	(revision 23173)
@@ -1361,10 +1361,16 @@
 $wgAllowSlowParserFunctions = false;
 
 /**
- * Extra custom jobs can be added to the Job Queue system.
- * This array should consist of job name => job queue subclass pairs
+ * Maps jobs to their handling classes; extensions
+ * can add to this to provide custom jobs
  */
-$wgCustomJobs = array();
+$wgJobClasses = array(
+	'refreshLinks' => 'RefreshLinksJob',
+	'htmlCacheUpdate' => 'HTMLCacheUpdateJob',
+	'html_cache_update' => 'HTMLCacheUpdateJob', // backwards-compatible
+	'sendMail' => 'EmaillingJob',
+	'enotifNotify' => 'EnotifNotifyJob',
+);
 
 /**
  * To use inline TeX, you need to compile 'texvc' (in the 'math' subdirectory of
Index: trunk/phase3/RELEASE-NOTES
===================================================================
--- trunk/phase3/RELEASE-NOTES	(revision 23172)
+++ trunk/phase3/RELEASE-NOTES	(revision 23173)
@@ -97,6 +97,8 @@
   to cause hard-to-track-down interactions between extensions.
 * (bug 9415) Added options to Special:Protect to allow setting of per-page robot
   policies. This can be done only by users with the 'editrobots' permission
+* Use $wgJobClasses to determine the correct Job to instantiate for a particular
+  queued task; allows extensions to introduce custom jobs
 
 == Bugfixes since 1.10 ==
 

Follow-up revisions

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

Status & tagging log

  • 15:19, 12 September 2011 Meno25 (Talk | contribs) changed the status of r23173 [removed: ok added: old]
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox