MediaWiki master
JobSpecification.php
Go to the documentation of this file.
1<?php
24
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 $params += [
67 'requestId' => Telemetry::getInstance()->getRequestId(),
68 ];
69 $this->validateParams( $params );
70 $this->validateParams( $opts );
71
72 $this->type = $type;
73 if ( $page ) {
74 // Make sure JobQueue classes can pull the title from parameters alone
75 if ( $page->getDBkey() !== '' ) {
76 $params += [
77 'namespace' => $page->getNamespace(),
78 'title' => $page->getDBkey()
79 ];
80 }
81 } else {
82 // We aim to remove the page from job specification and all we need
83 // is namespace/dbkey, so use LOCAL no matter what.
84 $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
85 }
86 $this->params = $params;
87 $this->page = $page;
88 $this->opts = $opts;
89 }
90
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
105 public function getParams() {
106 return $this->params;
107 }
108
109 public function getReleaseTimestamp() {
110 return isset( $this->params['jobReleaseTimestamp'] )
111 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
112 : null;
113 }
114
115 public function ignoreDuplicates() {
116 return !empty( $this->opts['removeDuplicates'] );
117 }
118
119 public function getDeduplicationInfo() {
120 $info = [
121 'type' => $this->getType(),
122 'params' => $this->getParams()
123 ];
124 if ( is_array( $info['params'] ) ) {
125 // Identical jobs with different "root" jobs should count as duplicates
126 unset( $info['params']['rootJobSignature'] );
127 unset( $info['params']['rootJobTimestamp'] );
128 // Likewise for jobs with different delay times
129 unset( $info['params']['jobReleaseTimestamp'] );
130 // Identical jobs from different requests should count as duplicates
131 unset( $info['params']['requestId'] );
132 if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
133 foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
134 unset( $info['params'][$field] );
135 }
136 }
137 }
138
139 return $info;
140 }
141
142 public function getRootJobParams() {
143 return [
144 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
145 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
146 ];
147 }
148
149 public function hasRootJobParams() {
150 return isset( $this->params['rootJobSignature'] )
151 && isset( $this->params['rootJobTimestamp'] );
152 }
153
154 public function isRootJob() {
155 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
156 }
157
163 public function toSerializableArray() {
164 wfDeprecated( __METHOD__, '1.41' );
165 return [
166 'type' => $this->type,
167 'params' => $this->params,
168 'opts' => $this->opts,
169 'title' => [
170 'ns' => $this->page->getNamespace(),
171 'key' => $this->page->getDBkey()
172 ]
173 ];
174 }
175
181 public static function newFromArray( array $map ) {
182 return new self(
183 $map['type'],
184 $map['params'],
185 $map['opts'],
186 PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] )
187 );
188 }
189}
const NS_SPECIAL
Definition Defines.php:54
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
__construct( $type, array $params, array $opts=[], ?PageReference $page=null)
array $params
Array of job parameters or false if none.
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.