public $params;
/** @var array Additional queue metadata */
- public $metadata = array();
+ public $metadata = [];
/** @var Title */
protected $title;
/** @var string Text for error that occurred last */
protected $error;
+ /** @var callable[] */
+ protected $teardownCallbacks = [];
+
/**
* Run the job
* @return bool Success
* @throws MWException
* @return Job
*/
- public static function factory( $command, Title $title, $params = array() ) {
+ public static function factory( $command, Title $title, $params = [] ) {
global $wgJobClasses;
+
if ( isset( $wgJobClasses[$command] ) ) {
$class = $wgJobClasses[$command];
- return new $class( $title, $params );
+ $job = new $class( $title, $params );
+ $job->command = $command;
+
+ return $job;
}
- throw new MWException( "Invalid job command `{$command}`" );
+
+ throw new InvalidArgumentException( "Invalid job command '{$command}'" );
}
/**
public function __construct( $command, $title, $params = false ) {
$this->command = $command;
$this->title = $title;
- $this->params = is_array( $params ) ? $params : array(); // sanity
+ $this->params = is_array( $params ) ? $params : []; // sanity
// expensive jobs may set this to true
$this->removeDuplicates = false;
* @since 1.21
*/
public function getDeduplicationInfo() {
- $info = array(
+ $info = [
'type' => $this->getType(),
'namespace' => $this->getTitle()->getNamespace(),
'title' => $this->getTitle()->getDBkey(),
'params' => $this->getParams()
- );
+ ];
if ( is_array( $info['params'] ) ) {
// Identical jobs with different "root" jobs should count as duplicates
unset( $info['params']['rootJobSignature'] );
* @since 1.21
*/
public static function newRootJobParams( $key ) {
- return array(
+ return [
'rootJobIsSelf' => true,
'rootJobSignature' => sha1( $key ),
'rootJobTimestamp' => wfTimestampNow()
- );
+ ];
}
/**
* @since 1.21
*/
public function getRootJobParams() {
- return array(
+ return [
'rootJobSignature' => isset( $this->params['rootJobSignature'] )
? $this->params['rootJobSignature']
: null,
'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
? $this->params['rootJobTimestamp']
: null
- );
+ ];
}
/**
return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
}
+ /**
+ * @param callable $callback
+ * @since 1.27
+ */
+ protected function addTeardownCallback( $callback ) {
+ $this->teardownCallbacks[] = $callback;
+ }
+
+ /**
+ * Do any final cleanup after run(), deferred updates, and all DB commits happen
+ *
+ * @since 1.27
+ */
+ public function teardown() {
+ foreach ( $this->teardownCallbacks as $callback ) {
+ call_user_func( $callback );
+ }
+ }
+
/**
* Insert a single job into the queue.
* @return bool True on success
$paramString .= ' ';
}
if ( is_array( $value ) ) {
- $filteredValue = array();
+ $filteredValue = [];
foreach ( $value as $k => $v ) {
if ( is_scalar( $v ) ) {
$filteredValue[$k] = $truncFunc( $v );