MediaWiki REL1_35
JobSpecification.php
Go to the documentation of this file.
1<?php
41 protected $type;
42
44 protected $params;
45
47 protected $title;
48
50 protected $opts;
51
60 public function __construct(
61 $type, array $params, array $opts = [], Title $title = null
62 ) {
63 $this->validateParams( $params );
64 $this->validateParams( $opts );
65
66 $this->type = $type;
67 if ( $title instanceof Title ) {
68 // Make sure JobQueue classes can pull the title from parameters alone
69 if ( $title->getDBkey() !== '' ) {
70 $params += [
71 'namespace' => $title->getNamespace(),
72 'title' => $title->getDBkey()
73 ];
74 }
75 } else {
76 $title = Title::makeTitle( NS_SPECIAL, '' );
77 }
78 $this->params = $params;
79 $this->title = $title;
80 $this->opts = $opts;
81 }
82
86 protected function validateParams( array $params ) {
87 foreach ( $params as $p => $v ) {
88 if ( is_array( $v ) ) {
89 $this->validateParams( $v );
90 } elseif ( !is_scalar( $v ) && $v !== null ) {
91 throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
92 }
93 }
94 }
95
96 public function getType() {
97 return $this->type;
98 }
99
100 public function getTitle() {
101 return $this->title;
102 }
103
104 public function getParams() {
105 return $this->params;
106 }
107
108 public function getReleaseTimestamp() {
109 return isset( $this->params['jobReleaseTimestamp'] )
110 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
111 : null;
112 }
113
114 public function ignoreDuplicates() {
115 return !empty( $this->opts['removeDuplicates'] );
116 }
117
118 public function getDeduplicationInfo() {
119 $info = [
120 'type' => $this->getType(),
121 'params' => $this->getParams()
122 ];
123 if ( is_array( $info['params'] ) ) {
124 // Identical jobs with different "root" jobs should count as duplicates
125 unset( $info['params']['rootJobSignature'] );
126 unset( $info['params']['rootJobTimestamp'] );
127 // Likewise for jobs with different delay times
128 unset( $info['params']['jobReleaseTimestamp'] );
129 if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
130 foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
131 unset( $info['params'][$field] );
132 }
133 }
134 }
135
136 return $info;
137 }
138
139 public function getRootJobParams() {
140 return [
141 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
142 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
143 ];
144 }
145
146 public function hasRootJobParams() {
147 return isset( $this->params['rootJobSignature'] )
148 && isset( $this->params['rootJobTimestamp'] );
149 }
150
151 public function isRootJob() {
152 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
153 }
154
159 public function toSerializableArray() {
160 return [
161 'type' => $this->type,
162 'params' => $this->params,
163 'opts' => $this->opts,
164 'title' => [
165 'ns' => $this->title->getNamespace(),
166 'key' => $this->title->getDBkey()
167 ]
168 ];
169 }
170
176 public static function newFromArray( array $map ) {
177 $title = Title::makeTitle( $map['title']['ns'], $map['title']['key'] );
178
179 return new self( $map['type'], $map['params'], $map['opts'], $title );
180 }
181}
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Job queue task description base code.
array $params
Array of job parameters or false if none.
__construct( $type, array $params, array $opts=[], Title $title=null)
validateParams(array $params)
static newFromArray(array $map)
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Represents a title within MediaWiki.
Definition Title.php:42
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1041
getDBkey()
Get the main part with underscores.
Definition Title.php:1032
const NS_SPECIAL
Definition Defines.php:59
Interface for serializable objects that describe a job queue task.