MediaWiki  master
JobSpecification.php
Go to the documentation of this file.
1 <?php
25 
44  protected $type;
45 
47  protected $params;
48 
50  protected $page;
51 
53  protected $opts;
54 
63  public function __construct(
64  $type, array $params, array $opts = [], PageReference $page = null
65  ) {
66  $this->validateParams( $params );
67  $this->validateParams( $opts );
68 
69  $this->type = $type;
70  if ( $page ) {
71  // Make sure JobQueue classes can pull the title from parameters alone
72  if ( $page->getDBkey() !== '' ) {
73  $params += [
74  'namespace' => $page->getNamespace(),
75  'title' => $page->getDBkey()
76  ];
77  }
78  } else {
79  // We aim to remove the page from job specification and all we need
80  // is namespace/dbkey, so use LOCAL no matter what.
81  $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
82  }
83  $this->params = $params;
84  $this->page = $page;
85  $this->opts = $opts;
86  }
87 
91  protected function validateParams( array $params ) {
92  foreach ( $params as $p => $v ) {
93  if ( is_array( $v ) ) {
94  $this->validateParams( $v );
95  } elseif ( !is_scalar( $v ) && $v !== null ) {
96  throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
97  }
98  }
99  }
100 
101  public function getType() {
102  return $this->type;
103  }
104 
109  public function getTitle() {
110  wfDeprecated( __METHOD__, '1.37' );
111  return Title::castFromPageReference( $this->page );
112  }
113 
114  public function getParams() {
115  return $this->params;
116  }
117 
118  public function getReleaseTimestamp() {
119  return isset( $this->params['jobReleaseTimestamp'] )
120  ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
121  : null;
122  }
123 
124  public function ignoreDuplicates() {
125  return !empty( $this->opts['removeDuplicates'] );
126  }
127 
128  public function getDeduplicationInfo() {
129  $info = [
130  'type' => $this->getType(),
131  'params' => $this->getParams()
132  ];
133  if ( is_array( $info['params'] ) ) {
134  // Identical jobs with different "root" jobs should count as duplicates
135  unset( $info['params']['rootJobSignature'] );
136  unset( $info['params']['rootJobTimestamp'] );
137  // Likewise for jobs with different delay times
138  unset( $info['params']['jobReleaseTimestamp'] );
139  if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
140  foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
141  unset( $info['params'][$field] );
142  }
143  }
144  }
145 
146  return $info;
147  }
148 
149  public function getRootJobParams() {
150  return [
151  'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
152  'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
153  ];
154  }
155 
156  public function hasRootJobParams() {
157  return isset( $this->params['rootJobSignature'] )
158  && isset( $this->params['rootJobTimestamp'] );
159  }
160 
161  public function isRootJob() {
162  return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
163  }
164 
169  public function toSerializableArray() {
170  return [
171  'type' => $this->type,
172  'params' => $this->params,
173  'opts' => $this->opts,
174  'title' => [
175  'ns' => $this->page->getNamespace(),
176  'key' => $this->page->getDBkey()
177  ]
178  ];
179  }
180 
186  public static function newFromArray( array $map ) {
187  return new self(
188  $map['type'],
189  $map['params'],
190  $map['opts'],
191  PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] )
192  );
193  }
194 }
JobSpecification\$page
PageReference $page
Definition: JobSpecification.php:50
JobSpecification\toSerializableArray
toSerializableArray()
Definition: JobSpecification.php:169
JobSpecification\getTitle
getTitle()
Definition: JobSpecification.php:109
JobSpecification\newFromArray
static newFromArray(array $map)
Definition: JobSpecification.php:186
JobSpecification\$opts
array $opts
Definition: JobSpecification.php:53
JobSpecification\__construct
__construct( $type, array $params, array $opts=[], PageReference $page=null)
Definition: JobSpecification.php:63
JobSpecification\getType
getType()
Definition: JobSpecification.php:101
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
JobSpecification\getReleaseTimestamp
getReleaseTimestamp()
Definition: JobSpecification.php:118
JobSpecification\isRootJob
isRootJob()
Definition: JobSpecification.php:161
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
Page\PageReference\getNamespace
getNamespace()
Returns the page's namespace number.
wfTimestampOrNull
wfTimestampOrNull( $outputtype=TS_UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Definition: GlobalFunctions.php:1684
JobSpecification\ignoreDuplicates
ignoreDuplicates()
Definition: JobSpecification.php:124
JobSpecification\getParams
getParams()
Definition: JobSpecification.php:114
JobSpecification\$params
array $params
Array of job parameters or false if none.
Definition: JobSpecification.php:47
Page\PageReference\getDBkey
getDBkey()
Get the page title in DB key form.
JobSpecification\validateParams
validateParams(array $params)
Definition: JobSpecification.php:91
JobSpecification\getRootJobParams
getRootJobParams()
Definition: JobSpecification.php:149
JobSpecification\hasRootJobParams
hasRootJobParams()
Definition: JobSpecification.php:156
JobSpecification\$type
string $type
Definition: JobSpecification.php:44
Page\PageReferenceValue
Immutable value object representing a page reference.
Definition: PageReferenceValue.php:42
JobSpecification
Job queue task description base code.
Definition: JobSpecification.php:42
Title\castFromPageReference
static castFromPageReference(?PageReference $pageReference)
Return a Title for a given Reference.
Definition: Title.php:345
JobSpecification\getDeduplicationInfo
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
Definition: JobSpecification.php:128
IJobSpecification
Interface for serializable objects that describe a job queue task.
Definition: IJobSpecification.php:42