From b6b52063ecef17bd008d6f477a67c94defc7799d Mon Sep 17 00:00:00 2001 From: Rob Church Date: Thu, 21 Jun 2007 19:11:24 +0000 Subject: [PATCH] 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 --- RELEASE-NOTES | 2 + includes/AutoLoader.php | 6 +- includes/DefaultSettings.php | 14 +++-- includes/EmaillingJob.php | 26 +++++++++ includes/EnotifNotifyJob.php | 27 +++++++++ includes/HTMLCacheUpdate.php | 30 ++++------ includes/JobQueue.php | 109 +++++------------------------------ includes/LinksUpdate.php | 2 +- includes/RefreshLinksJob.php | 49 ++++++++++++++++ includes/UserMailer.php | 2 +- 10 files changed, 146 insertions(+), 121 deletions(-) create mode 100644 includes/EmaillingJob.php create mode 100644 includes/EnotifNotifyJob.php create mode 100644 includes/RefreshLinksJob.php diff --git a/RELEASE-NOTES b/RELEASE-NOTES index ca3f8b2330..64b3724ba0 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -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 == diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index c41cb18b96..c30ac145ba 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -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', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 2c28d13d22..616b9e25a8 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 index 0000000000..5fea8b41ec --- /dev/null +++ b/includes/EmaillingJob.php @@ -0,0 +1,26 @@ +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 index 0000000000..56d3356461 --- /dev/null +++ b/includes/EnotifNotifyJob.php @@ -0,0 +1,27 @@ +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 diff --git a/includes/HTMLCacheUpdate.php b/includes/HTMLCacheUpdate.php index 9a0b6a0846..94aa6ce283 100644 --- a/includes/HTMLCacheUpdate.php +++ b/includes/HTMLCacheUpdate.php @@ -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() { diff --git a/includes/JobQueue.php b/includes/JobQueue.php index 499e415916..15915d1a57 100644 --- a/includes/JobQueue.php +++ b/includes/JobQueue.php @@ -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 diff --git a/includes/LinksUpdate.php b/includes/LinksUpdate.php index 3c01da97bb..45dab9ae53 100644 --- a/includes/LinksUpdate.php +++ b/includes/LinksUpdate.php @@ -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 index 0000000000..41eb4352cd --- /dev/null +++ b/includes/RefreshLinksJob.php @@ -0,0 +1,49 @@ +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 diff --git a/includes/UserMailer.php b/includes/UserMailer.php index d74416fe7d..c69fe90b36 100644 --- a/includes/UserMailer.php +++ b/includes/UserMailer.php @@ -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); -- 2.20.1