MediaWiki master
JobSpecification.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\JobQueue;
8
12use UnexpectedValueException;
13
33 protected $type;
34
36 protected $params;
37
39 protected $page;
40
42 protected $opts;
43
54 public function __construct(
55 $type, array $params, array $opts = [], ?PageReference $page = null
56 ) {
57 $params += [
58 'requestId' => Telemetry::getInstance()->getRequestId(),
59 ];
60 $this->validateParams( $params );
61 $this->validateParams( $opts );
62
63 $this->type = $type;
64 if ( $page ) {
65 // Make sure JobQueue classes can pull the title from parameters alone
66 if ( $page->getDBkey() !== '' ) {
67 $params += [
68 'namespace' => $page->getNamespace(),
69 'title' => $page->getDBkey()
70 ];
71 }
72 } else {
73 // We aim to remove the page from job specification and all we need
74 // is namespace/dbkey, so use LOCAL no matter what.
75 $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
76 }
77 $this->params = $params;
78 $this->page = $page;
79 $this->opts = $opts;
80 }
81
82 protected function validateParams( array $params ) {
83 foreach ( $params as $p => $v ) {
84 if ( is_array( $v ) ) {
85 $this->validateParams( $v );
86 } elseif ( !is_scalar( $v ) && $v !== null ) {
87 throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
88 }
89 }
90 }
91
93 public function getType() {
94 return $this->type;
95 }
96
98 public function getParams() {
99 return $this->params;
100 }
101
103 public function getReleaseTimestamp() {
104 return isset( $this->params['jobReleaseTimestamp'] )
105 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
106 : null;
107 }
108
110 public function ignoreDuplicates() {
111 return !empty( $this->opts['removeDuplicates'] );
112 }
113
115 public function getDeduplicationInfo() {
116 $info = [
117 'type' => $this->getType(),
118 'params' => $this->getParams()
119 ];
120 if ( is_array( $info['params'] ) ) {
121 // Identical jobs with different "root" jobs should count as duplicates
122 unset( $info['params']['rootJobSignature'] );
123 unset( $info['params']['rootJobTimestamp'] );
124 // Likewise for jobs with different delay times
125 unset( $info['params']['jobReleaseTimestamp'] );
126 // Identical jobs from different requests should count as duplicates
127 unset( $info['params']['requestId'] );
128 if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
129 foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
130 unset( $info['params'][$field] );
131 }
132 }
133 }
134
135 return $info;
136 }
137
139 public function getRootJobParams() {
140 return [
141 'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
142 'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
143 ];
144 }
145
147 public function hasRootJobParams() {
148 return isset( $this->params['rootJobSignature'] )
149 && isset( $this->params['rootJobTimestamp'] );
150 }
151
153 public function isRootJob() {
154 return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
155 }
156
162 public static function newFromArray( array $map ) {
163 return new self(
164 $map['type'],
165 $map['params'],
166 $map['opts'],
167 PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] )
168 );
169 }
170}
171
173class_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.