MediaWiki master
JobSpecification.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\JobQueue;
8
12use UnexpectedValueException;
13use Wikimedia\Timestamp\TimestampFormat as TS;
14
34 protected $type;
35
37 protected $params;
38
40 protected $page;
41
43 protected $opts;
44
55 public function __construct(
56 $type, array $params, array $opts = [], ?PageReference $page = null
57 ) {
58 $params += [
59 'requestId' => Telemetry::getInstance()->getRequestId(),
60 ];
61 $this->validateParams( $params );
62 $this->validateParams( $opts );
63
64 $this->type = $type;
65 if ( $page ) {
66 // Make sure JobQueue classes can pull the title from parameters alone
67 if ( $page->getDBkey() !== '' ) {
68 $params += [
69 'namespace' => $page->getNamespace(),
70 'title' => $page->getDBkey()
71 ];
72 }
73 } else {
74 // We aim to remove the page from job specification and all we need
75 // is namespace/dbkey, so use LOCAL no matter what.
76 $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
77 }
78 $this->params = $params;
79 $this->page = $page;
80 $this->opts = $opts;
81 }
82
83 protected function validateParams( array $params ) {
84 foreach ( $params as $p => $v ) {
85 if ( is_array( $v ) ) {
86 $this->validateParams( $v );
87 } elseif ( !is_scalar( $v ) && $v !== null ) {
88 throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
89 }
90 }
91 }
92
94 public function getType() {
95 return $this->type;
96 }
97
99 public function getParams() {
100 return $this->params;
101 }
102
104 public function getReleaseTimestamp() {
105 return isset( $this->params['jobReleaseTimestamp'] )
106 ? wfTimestampOrNull( TS::UNIX, $this->params['jobReleaseTimestamp'] )
107 : null;
108 }
109
111 public function ignoreDuplicates() {
112 return !empty( $this->opts['removeDuplicates'] );
113 }
114
116 public function getDeduplicationInfo() {
117 $info = [
118 'type' => $this->getType(),
119 'params' => $this->getParams()
120 ];
121 if ( is_array( $info['params'] ) ) {
122 // Identical jobs with different "root" jobs should count as duplicates
123 unset( $info['params']['rootJobSignature'] );
124 unset( $info['params']['rootJobTimestamp'] );
125 // Likewise for jobs with different delay times
126 unset( $info['params']['jobReleaseTimestamp'] );
127 // Identical jobs from different requests should count as duplicates
128 unset( $info['params']['requestId'] );
129 if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
130 foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
131 unset( $info['params'][$field] );
132 }
133 }
134 }
135
136 return $info;
137 }
138
140 public function getRootJobParams() {
141 return [
142 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
143 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
144 ];
145 }
146
148 public function hasRootJobParams() {
149 return isset( $this->params['rootJobSignature'] )
150 && isset( $this->params['rootJobTimestamp'] );
151 }
152
154 public function isRootJob() {
155 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
156 }
157
163 public static function newFromArray( array $map ) {
164 return new self(
165 $map['type'],
166 $map['params'],
167 $map['opts'],
168 PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] )
169 );
170 }
171}
172
174class_alias( JobSpecification::class, 'JobSpecification' );
const NS_SPECIAL
Definition Defines.php:40
wfTimestampOrNull( $outputtype=TS::UNIX, $ts=null)
Return a formatted timestamp, or null if input is null.
Service for handling telemetry data.
Definition Telemetry.php:15
Job queue task description base code.
getType()
string Job type that defines what sort of changes this job makes
ignoreDuplicates()
bool Whether only one of each identical set of jobs should be run
__construct( $type, array $params, array $opts=[], ?PageReference $page=null)
getRootJobParams()
JobQueue::deduplicateRootJob() array 1.26
array $params
Array of job parameters or false if none.
hasRootJobParams()
JobQueue::deduplicateRootJob() bool 1.22
getParams()
array Parameters that specify sources, targets, and options for execution
getReleaseTimestamp()
int|null UNIX timestamp to delay running this job until, otherwise null
getDeduplicationInfo()
Subclasses may need to override this to make duplication detection work.The resulting map conveys eve...
isRootJob()
JobQueue::deduplicateRootJob() bool Whether this is job is a root job
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.