Some job cleanup:
authorRob Church <robchurch@users.mediawiki.org>
Thu, 21 Jun 2007 19:11:24 +0000 (19:11 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Thu, 21 Jun 2007 19:11:24 +0000 (19:11 +0000)
* 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

RELEASE-NOTES
includes/AutoLoader.php
includes/DefaultSettings.php
includes/EmaillingJob.php [new file with mode: 0644]
includes/EnotifNotifyJob.php [new file with mode: 0644]
includes/HTMLCacheUpdate.php
includes/JobQueue.php
includes/LinksUpdate.php
includes/RefreshLinksJob.php [new file with mode: 0644]
includes/UserMailer.php

index ca3f8b2..64b3724 100644 (file)
@@ -97,6 +97,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
   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 ==
 
index c41cb18..c30ac14 100644 (file)
@@ -94,8 +94,6 @@ function __autoload($className) {
                '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 @@ function __autoload($className) {
                '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 2c28d13..616b9e2 100644 (file)
@@ -1361,10 +1361,16 @@ $wgWantedPagesThreshold = 1;
 $wgAllowSlowParserFunctions = false;
 
 /**
- * Extra custom jobs can be added to the Job Queue system.
- * This array should consist of job name => job queue subclass pairs
- */
-$wgCustomJobs = array();
+ * Maps jobs to their handling classes; extensions
+ * can add to this to provide custom jobs
+ */
+$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
diff --git a/includes/EmaillingJob.php b/includes/EmaillingJob.php
new file mode 100644 (file)
index 0000000..5fea8b4
--- /dev/null
@@ -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
diff --git a/includes/EnotifNotifyJob.php b/includes/EnotifNotifyJob.php
new file mode 100644 (file)
index 0000000..56d3356
--- /dev/null
@@ -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
index 9a0b6a0..94aa6ce 100644 (file)
@@ -67,13 +67,13 @@ class HTMLCacheUpdate
                                        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 @@ class HTMLCacheUpdateJob extends Job {
        /**
         * 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 499e415..15915d1 100644 (file)
@@ -167,32 +167,23 @@ abstract class Job {
        }
 
        /**
-        * 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 );
-               }
-               // 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\"" );
+               global $wgJobClasses;
+               if( isset( $wgJobClasses[$command] ) ) {
+                       $class = $wgJobClasses[$command];
+                       return new $class( $title, $params, $id );
                }
+               throw new MWException( "Invalid job command `{$command}`" );
        }
-
+               
        static function makeBlob( $params ) {
                if ( $params !== false ) {
                        return serialize( $params );
@@ -299,76 +290,4 @@ abstract class Job {
        }
 }
 
-
-/**
- * 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 3c01da9..45dab9a 100644 (file)
@@ -189,7 +189,7 @@ class LinksUpdate {
                                        break;
                                }
                                $title = Title::makeTitle( $row->page_namespace, $row->page_title );
-                               $jobs[] = Job::factory( 'refreshLinks', $title );
+                               $jobs[] = new RefreshLinksJob( $title, '' );
                        }
                        Job::batchInsert( $jobs );
                }
diff --git a/includes/RefreshLinksJob.php b/includes/RefreshLinksJob.php
new file mode 100644 (file)
index 0000000..41eb435
--- /dev/null
@@ -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
index d74416f..c69fe90 100644 (file)
@@ -247,7 +247,7 @@ class EmailNotification {
                                "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);