MediaWiki  master
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 
58  public function __construct(
59  $type, array $params, array $opts = [], Title $title = null
60  ) {
61  $this->validateParams( $params );
62  $this->validateParams( $opts );
63 
64  $this->type = $type;
65  if ( $title instanceof Title ) {
66  // Make sure JobQueue classes can pull the title from parameters alone
67  if ( $title->getDBkey() !== '' ) {
68  $params += [
69  'namespace' => $title->getNamespace(),
70  'title' => $title->getDBkey()
71  ];
72  }
73  } else {
75  }
76  $this->params = $params;
77  $this->title = $title;
78  $this->opts = $opts;
79  }
80 
84  protected function validateParams( array $params ) {
85  foreach ( $params as $p => $v ) {
86  if ( is_array( $v ) ) {
87  $this->validateParams( $v );
88  } elseif ( !is_scalar( $v ) && $v !== null ) {
89  throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
90  }
91  }
92  }
93 
94  public function getType() {
95  return $this->type;
96  }
97 
98  public function getTitle() {
99  return $this->title;
100  }
101 
102  public function getParams() {
103  return $this->params;
104  }
105 
106  public function getReleaseTimestamp() {
107  return isset( $this->params['jobReleaseTimestamp'] )
108  ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
109  : null;
110  }
111 
112  public function ignoreDuplicates() {
113  return !empty( $this->opts['removeDuplicates'] );
114  }
115 
116  public function getDeduplicationInfo() {
117  $info = [
118  'type' => $this->getType(),
119  'params' => $this->getParams()
120  ];
121  if ( is_array( $info['params'] ) ) {
122  // Identical jobs with different "root" jobs should count as duplicates
123  unset( $info['params']['rootJobSignature'] );
124  unset( $info['params']['rootJobTimestamp'] );
125  // Likewise for jobs with different delay times
126  unset( $info['params']['jobReleaseTimestamp'] );
127  }
128 
129  return $info;
130  }
131 
132  public function getRootJobParams() {
133  return [
134  'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
135  'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
136  ];
137  }
138 
139  public function hasRootJobParams() {
140  return isset( $this->params['rootJobSignature'] )
141  && isset( $this->params['rootJobTimestamp'] );
142  }
143 
144  public function isRootJob() {
145  return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
146  }
147 
152  public function toSerializableArray() {
153  return [
154  'type' => $this->type,
155  'params' => $this->params,
156  'opts' => $this->opts,
157  'title' => [
158  'ns' => $this->title->getNamespace(),
159  'key' => $this->title->getDBkey()
160  ]
161  ];
162  }
163 
169  public static function newFromArray( array $map ) {
170  $title = Title::makeTitle( $map['title']['ns'], $map['title']['key'] );
171 
172  return new self( $map['type'], $map['params'], $map['opts'], $title );
173  }
174 }
validateParams(array $params)
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
const NS_SPECIAL
Definition: Defines.php:49
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
getDBkey()
Get the main part with underscores.
Definition: Title.php:1016
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1040
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
__construct( $type, array $params, array $opts=[], Title $title=null)
array $params
Array of job parameters or false if none.
Interface for serializable objects that describe a job queue task.
static newFromArray(array $map)
Job queue task description base code.