From 9b4938c40d02c43f8cec7471e73634532ab9b388 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 29 Mar 2019 13:15:58 -0700 Subject: [PATCH] jobqueue: simplify the signature of Job::factory() and Job::__construct() Remove the $title argument from these methods to simplify subclasses that do not have a meaningful title to use. The Job::getTitle() method can be overriden by subclasses to return something meaningful. The old call signature is still supported for backwards compatibility. This will automatically determine what getTitle() returns as before. Use "Blankpage" as the "not applicable" title for jobs instead of one that looks like some error occured. Change-Id: I3d5bd012d9cef1e7daaccfb0d5d319552eb89fb6 --- includes/jobqueue/Job.php | 35 ++++++++++++++++++-------- includes/jobqueue/JobSpecification.php | 2 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/includes/jobqueue/Job.php b/includes/jobqueue/Job.php index d624acf97f..93066571e0 100644 --- a/includes/jobqueue/Job.php +++ b/includes/jobqueue/Job.php @@ -41,7 +41,7 @@ abstract class Job implements IJobSpecification { protected $title; /** @var bool Expensive jobs may set this to true */ - protected $removeDuplicates; + protected $removeDuplicates = false; /** @var string Text for error that occurred last */ protected $error; @@ -65,14 +65,22 @@ abstract class Job implements IJobSpecification { * Create the appropriate object to handle a specific job * * @param string $command Job command - * @param Title $title Associated title * @param array $params Job parameters * @throws InvalidArgumentException * @return Job */ - public static function factory( $command, Title $title, $params = [] ) { + public static function factory( $command, $params = [] ) { global $wgJobClasses; + if ( $params instanceof Title ) { + // Backwards compatibility for old signature ($command, $title, $params) + $title = $params; + $params = func_num_args() >= 3 ? func_get_arg( 2 ) : []; + } else { + // Subclasses can override getTitle() to return something more meaningful + $title = Title::makeTitle( NS_SPECIAL, 'Blankpage' ); + } + if ( isset( $wgJobClasses[$command] ) ) { $handler = $wgJobClasses[$command]; @@ -86,9 +94,10 @@ abstract class Job implements IJobSpecification { if ( $job instanceof Job ) { $job->command = $command; + return $job; } else { - throw new InvalidArgumentException( "Cannot instantiate job '$command': bad spec!" ); + throw new InvalidArgumentException( "Could instantiate job '$command': bad spec!" ); } } @@ -97,17 +106,21 @@ abstract class Job implements IJobSpecification { /** * @param string $command - * @param Title $title - * @param array|bool $params Can not be === true + * @param array $params */ - public function __construct( $command, $title, $params = false ) { + public function __construct( $command, $params = [] ) { + if ( $params instanceof Title ) { + // Backwards compatibility for old signature ($command, $title, $params) + $title = $params; + $params = func_num_args() >= 3 ? func_get_arg( 2 ) : []; + } else { + // Subclasses can override getTitle() to return something more meaningful + $title = Title::makeTitle( NS_SPECIAL, 'Blankpage' ); + } + $this->command = $command; $this->title = $title; $this->params = is_array( $params ) ? $params : []; // sanity - - // expensive jobs may set this to true - $this->removeDuplicates = false; - if ( !isset( $this->params['requestId'] ) ) { $this->params['requestId'] = WebRequest::getRequestId(); } diff --git a/includes/jobqueue/JobSpecification.php b/includes/jobqueue/JobSpecification.php index 4abbc6db2f..b04aa83808 100644 --- a/includes/jobqueue/JobSpecification.php +++ b/includes/jobqueue/JobSpecification.php @@ -64,7 +64,7 @@ class JobSpecification implements IJobSpecification { $this->type = $type; $this->params = $params; - $this->title = $title ?: Title::makeTitle( NS_SPECIAL, 'Badtitle/' . static::class ); + $this->title = $title ?: Title::makeTitle( NS_SPECIAL, 'Blankpage' ); $this->opts = $opts; } -- 2.20.1