From: Aaron Schulz Date: Fri, 29 Mar 2019 20:15:58 +0000 (-0700) Subject: jobqueue: simplify the signature of Job::factory() and Job::__construct() X-Git-Tag: 1.34.0-rc.0~2237^2 X-Git-Url: http://git.cyclocoop.org/%24image?a=commitdiff_plain;h=9b4938c40d02c;p=lhc%2Fweb%2Fwiklou.git 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 --- 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; }