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