*
* @ingroup JobQueue
*/
-abstract class Job implements IJobSpecification {
+abstract class Job implements RunnableJob {
/** @var string */
public $command;
/** @var int Job must not be wrapped in the usual explicit LBFactory transaction round */
const JOB_NO_EXPLICIT_TRX_ROUND = 1;
- /**
- * Run the job
- * @return bool Success
- */
- abstract public function run();
-
/**
* Create the appropriate object to handle a specific job
*
// Backwards compatibility for old signature ($command, $title, $params)
$title = $params;
$params = func_num_args() >= 3 ? func_get_arg( 2 ) : [];
+ } elseif ( isset( $params['namespace'] ) && isset( $params['title'] ) ) {
+ // Handle job classes that take title as constructor parameter.
+ // If a newer classes like GenericParameterJob uses these parameters,
+ // then this happens in Job::__construct instead.
+ $title = Title::makeTitle( $params['namespace'], $params['title'] );
} else {
- // Subclasses can override getTitle() to return something more meaningful
+ // Default title for job classes not implementing GenericParameterJob.
+ // This must be a valid title because it not directly passed to
+ // our Job constructor, but rather it's subclasses which may expect
+ // to be able to use it.
$title = Title::makeTitle( NS_SPECIAL, 'Blankpage' );
}
if ( is_callable( $handler ) ) {
$job = call_user_func( $handler, $title, $params );
} elseif ( class_exists( $handler ) ) {
- $job = new $handler( $title, $params );
+ if ( is_subclass_of( $handler, GenericParameterJob::class ) ) {
+ $job = new $handler( $params );
+ } else {
+ $job = new $handler( $title, $params );
+ }
} else {
$job = null;
}
return $job;
} else {
- throw new InvalidArgumentException( "Could instantiate job '$command': bad spec!" );
+ throw new InvalidArgumentException(
+ "Could not instantiate job '$command': bad spec!"
+ );
}
}
if ( $params instanceof Title ) {
// Backwards compatibility for old signature ($command, $title, $params)
$title = $params;
- $params = func_get_arg( 2 );
+ $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' );
+ // Newer jobs may choose to not have a top-level title (e.g. GenericParameterJob)
+ $title = null;
+ }
+
+ if ( !is_array( $params ) ) {
+ throw new InvalidArgumentException( '$params must be an array' );
+ }
+
+ if (
+ $title &&
+ !isset( $params['namespace'] ) &&
+ !isset( $params['title'] )
+ ) {
+ // When constructing this class for submitting to the queue,
+ // normalise the $title arg of old job classes as part of $params.
+ $params['namespace'] = $title->getNamespace();
+ $params['title'] = $title->getDBKey();
}
$this->command = $command;
- $this->title = $title;
- $this->params = is_array( $params ) ? $params : [];
- if ( !isset( $this->params['requestId'] ) ) {
- $this->params['requestId'] = WebRequest::getRequestId();
+ $this->params = $params + [ 'requestId' => WebRequest::getRequestId() ];
+
+ if ( $this->title === null ) {
+ // Set this field for access via getTitle().
+ $this->title = ( isset( $params['namespace'] ) && isset( $params['title'] ) )
+ ? Title::makeTitle( $params['namespace'], $params['title'] )
+ // GenericParameterJob classes without namespace/title params
+ // should not use getTitle(). Set an invalid title as placeholder.
+ : Title::makeTitle( NS_SPECIAL, '' );
}
}
/**
* @return Title
*/
- public function getTitle() {
+ final public function getTitle() {
return $this->title;
}
public function getDeduplicationInfo() {
$info = [
'type' => $this->getType(),
- 'namespace' => $this->getTitle()->getNamespace(),
- 'title' => $this->getTitle()->getDBkey(),
'params' => $this->getParams()
];
if ( is_array( $info['params'] ) ) {