| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Http\Telemetry; |
| 22 |
use MediaWiki\Page\PageReference; |
| 23 |
use MediaWiki\Page\PageReferenceValue; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class JobSpecification implements IJobSpecification { |
| 43 |
|
| 44 |
protected $type; |
| 45 |
|
| 46 |
|
| 47 |
protected $params; |
| 48 |
|
| 49 |
|
| 50 |
protected $page; |
| 51 |
|
| 52 |
|
| 53 |
protected $opts; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 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 |
|
| 75 |
if ( $page->getDBkey() !== '' ) { |
| 76 |
$params += [ |
| 77 |
'namespace' => $page->getNamespace(), |
| 78 |
'title' => $page->getDBkey() |
| 79 |
]; |
| 80 |
} |
| 81 |
} else { |
| 82 |
|
| 83 |
|
| 84 |
$page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ ); |
| 85 |
} |
| 86 |
$this->params = $params; |
| 87 |
$this->page = $page; |
| 88 |
$this->opts = $opts; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
protected function validateParams( array $params ) { |
| 95 |
foreach ( $params as $p => $v ) { |
| 96 |
if ( is_array( $v ) ) { |
| 97 |
$this->validateParams( $v ); |
| 98 |
} elseif ( !is_scalar( $v ) && $v !== null ) { |
| 99 |
throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." ); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
public function getType() { |
| 105 |
return $this->type; |
| 106 |
} |
| 107 |
|
| 108 |
public function getParams() { |
| 109 |
return $this->params; |
| 110 |
} |
| 111 |
|
| 112 |
public function getReleaseTimestamp() { |
| 113 |
return isset( $this->params['jobReleaseTimestamp'] ) |
| 114 |
? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] ) |
| 115 |
: null; |
| 116 |
} |
| 117 |
|
| 118 |
public function ignoreDuplicates() { |
| 119 |
return !empty( $this->opts['removeDuplicates'] ); |
| 120 |
} |
| 121 |
|
| 122 |
public function getDeduplicationInfo() { |
| 123 |
$info = [ |
| 124 |
'type' => $this->getType(), |
| 125 |
'params' => $this->getParams() |
| 126 |
]; |
| 127 |
if ( is_array( $info['params'] ) ) { |
| 128 |
|
| 129 |
unset( $info['params']['rootJobSignature'] ); |
| 130 |
unset( $info['params']['rootJobTimestamp'] ); |
| 131 |
|
| 132 |
unset( $info['params']['jobReleaseTimestamp'] ); |
| 133 |
|
| 134 |
unset( $info['params']['requestId'] ); |
| 135 |
if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) { |
| 136 |
foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) { |
| 137 |
unset( $info['params'][$field] ); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $info; |
| 143 |
} |
| 144 |
|
| 145 |
public function getRootJobParams() { |
| 146 |
return [ |
| 147 |
'rootJobSignature' => $this->params['rootJobSignature'] ?? null, |
| 148 |
'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null |
| 149 |
]; |
| 150 |
} |
| 151 |
|
| 152 |
public function hasRootJobParams() { |
| 153 |
return isset( $this->params['rootJobSignature'] ) |
| 154 |
&& isset( $this->params['rootJobTimestamp'] ); |
| 155 |
} |
| 156 |
|
| 157 |
public function isRootJob() { |
| 158 |
return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] ); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function toSerializableArray() { |
| 167 |
wfDeprecated( __METHOD__, '1.41' ); |
| 168 |
return [ |
| 169 |
'type' => $this->type, |
| 170 |
'params' => $this->params, |
| 171 |
'opts' => $this->opts, |
| 172 |
'title' => [ |
| 173 |
'ns' => $this->page->getNamespace(), |
| 174 |
'key' => $this->page->getDBkey() |
| 175 |
] |
| 176 |
]; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public static function newFromArray( array $map ) { |
| 185 |
return new self( |
| 186 |
$map['type'], |
| 187 |
$map['params'], |
| 188 |
$map['opts'], |
| 189 |
PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] ) |
| 190 |
); |
| 191 |
} |
| 192 |
} |