MediaWiki master
JobSpecification.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\JobQueue;
22
26use UnexpectedValueException;
27
47 protected $type;
48
50 protected $params;
51
53 protected $page;
54
56 protected $opts;
57
68 public function __construct(
69 $type, array $params, array $opts = [], ?PageReference $page = null
70 ) {
71 $params += [
72 'requestId' => Telemetry::getInstance()->getRequestId(),
73 ];
74 $this->validateParams( $params );
75 $this->validateParams( $opts );
76
77 $this->type = $type;
78 if ( $page ) {
79 // Make sure JobQueue classes can pull the title from parameters alone
80 if ( $page->getDBkey() !== '' ) {
81 $params += [
82 'namespace' => $page->getNamespace(),
83 'title' => $page->getDBkey()
84 ];
85 }
86 } else {
87 // We aim to remove the page from job specification and all we need
88 // is namespace/dbkey, so use LOCAL no matter what.
89 $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
90 }
91 $this->params = $params;
92 $this->page = $page;
93 $this->opts = $opts;
94 }
95
96 protected function validateParams( array $params ) {
97 foreach ( $params as $p => $v ) {
98 if ( is_array( $v ) ) {
99 $this->validateParams( $v );
100 } elseif ( !is_scalar( $v ) && $v !== null ) {
101 throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
102 }
103 }
104 }
105
106 public function getType() {
107 return $this->type;
108 }
109
110 public function getParams() {
111 return $this->params;
112 }
113
114 public function getReleaseTimestamp() {
115 return isset( $this->params['jobReleaseTimestamp'] )
116 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
117 : null;
118 }
119
120 public function ignoreDuplicates() {
121 return !empty( $this->opts['removeDuplicates'] );
122 }
123
124 public function getDeduplicationInfo() {
125 $info = [
126 'type' => $this->getType(),
127 'params' => $this->getParams()
128 ];
129 if ( is_array( $info['params'] ) ) {
130 // Identical jobs with different "root" jobs should count as duplicates
131 unset( $info['params']['rootJobSignature'] );
132 unset( $info['params']['rootJobTimestamp'] );
133 // Likewise for jobs with different delay times
134 unset( $info['params']['jobReleaseTimestamp'] );
135 // Identical jobs from different requests should count as duplicates
136 unset( $info['params']['requestId'] );
137 if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
138 foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
139 unset( $info['params'][$field] );
140 }
141 }
142 }
143
144 return $info;
145 }
146
147 public function getRootJobParams() {
148 return [
149 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
150 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
151 ];
152 }
153
154 public function hasRootJobParams() {
155 return isset( $this->params['rootJobSignature'] )
156 && isset( $this->params['rootJobTimestamp'] );
157 }
158
159 public function isRootJob() {
160 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
161 }
162
168 public function toSerializableArray() {
169 wfDeprecated( __METHOD__, '1.41' );
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}
195
197class_alias( JobSpecification::class, 'JobSpecification' );
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.
Service for handling telemetry data.
Definition Telemetry.php:29
Job queue task description base code.
__construct( $type, array $params, array $opts=[], ?PageReference $page=null)
array $params
Array of job parameters or false if none.
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.
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.