MediaWiki master
JobSpecification.php
Go to the documentation of this file.
1<?php
24
43 protected $type;
44
46 protected $params;
47
49 protected $page;
50
52 protected $opts;
53
62 public function __construct(
63 $type, array $params, array $opts = [], PageReference $page = null
64 ) {
65 $params += [
66 'requestId' => Telemetry::getInstance()->getRequestId(),
67 ];
68 $this->validateParams( $params );
69 $this->validateParams( $opts );
70
71 $this->type = $type;
72 if ( $page ) {
73 // Make sure JobQueue classes can pull the title from parameters alone
74 if ( $page->getDBkey() !== '' ) {
75 $params += [
76 'namespace' => $page->getNamespace(),
77 'title' => $page->getDBkey()
78 ];
79 }
80 } else {
81 // We aim to remove the page from job specification and all we need
82 // is namespace/dbkey, so use LOCAL no matter what.
83 $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
84 }
85 $this->params = $params;
86 $this->page = $page;
87 $this->opts = $opts;
88 }
89
93 protected function validateParams( array $params ) {
94 foreach ( $params as $p => $v ) {
95 if ( is_array( $v ) ) {
96 $this->validateParams( $v );
97 } elseif ( !is_scalar( $v ) && $v !== null ) {
98 throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
99 }
100 }
101 }
102
103 public function getType() {
104 return $this->type;
105 }
106
107 public function getParams() {
108 return $this->params;
109 }
110
111 public function getReleaseTimestamp() {
112 return isset( $this->params['jobReleaseTimestamp'] )
113 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
114 : null;
115 }
116
117 public function ignoreDuplicates() {
118 return !empty( $this->opts['removeDuplicates'] );
119 }
120
121 public function getDeduplicationInfo() {
122 $info = [
123 'type' => $this->getType(),
124 'params' => $this->getParams()
125 ];
126 if ( is_array( $info['params'] ) ) {
127 // Identical jobs with different "root" jobs should count as duplicates
128 unset( $info['params']['rootJobSignature'] );
129 unset( $info['params']['rootJobTimestamp'] );
130 // Likewise for jobs with different delay times
131 unset( $info['params']['jobReleaseTimestamp'] );
132 // Identical jobs from different requests should count as duplicates
133 unset( $info['params']['requestId'] );
134 if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
135 foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
136 unset( $info['params'][$field] );
137 }
138 }
139 }
140
141 return $info;
142 }
143
144 public function getRootJobParams() {
145 return [
146 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
147 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
148 ];
149 }
150
151 public function hasRootJobParams() {
152 return isset( $this->params['rootJobSignature'] )
153 && isset( $this->params['rootJobTimestamp'] );
154 }
155
156 public function isRootJob() {
157 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
158 }
159
165 public function toSerializableArray() {
166 wfDeprecated( __METHOD__, '1.41' );
167 return [
168 'type' => $this->type,
169 'params' => $this->params,
170 'opts' => $this->opts,
171 'title' => [
172 'ns' => $this->page->getNamespace(),
173 'key' => $this->page->getDBkey()
174 ]
175 ];
176 }
177
183 public static function newFromArray( array $map ) {
184 return new self(
185 $map['type'],
186 $map['params'],
187 $map['opts'],
188 PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] )
189 );
190 }
191}
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.
array $params
The job parameters.
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.
Service for handling telemetry data.
Definition Telemetry.php:29
Immutable value object representing a page reference.
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.