| 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\Json\FormatJson; |
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use MediaWiki\Page\PageReference; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
abstract class Job implements RunnableJob { |
| 39 |
|
| 40 |
public $command; |
| 41 |
|
| 42 |
|
| 43 |
public $params; |
| 44 |
|
| 45 |
|
| 46 |
public $metadata = []; |
| 47 |
|
| 48 |
|
| 49 |
protected $title; |
| 50 |
|
| 51 |
|
| 52 |
protected $removeDuplicates = false; |
| 53 |
|
| 54 |
|
| 55 |
protected $error; |
| 56 |
|
| 57 |
|
| 58 |
protected $teardownCallbacks = []; |
| 59 |
|
| 60 |
|
| 61 |
protected $executionFlags = 0; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function factory( $command, $params = [] ) { |
| 73 |
$factory = MediaWikiServices::getInstance()->getJobFactory(); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
return $factory->newJob( ...func_get_args() ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function __construct( $command, $params = null ) { |
| 87 |
if ( $params instanceof PageReference ) { |
| 88 |
|
| 89 |
$page = $params; |
| 90 |
$params = func_num_args() >= 3 ? func_get_arg( 2 ) : []; |
| 91 |
} else { |
| 92 |
|
| 93 |
$page = null; |
| 94 |
} |
| 95 |
|
| 96 |
if ( !is_array( $params ) ) { |
| 97 |
throw new InvalidArgumentException( '$params must be an array' ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( |
| 101 |
$page && |
| 102 |
!isset( $params['namespace'] ) && |
| 103 |
!isset( $params['title'] ) |
| 104 |
) { |
| 105 |
|
| 106 |
|
| 107 |
$params['namespace'] = $page->getNamespace(); |
| 108 |
$params['title'] = $page->getDBkey(); |
| 109 |
} |
| 110 |
|
| 111 |
$this->command = $command; |
| 112 |
$this->params = $params + [ |
| 113 |
'requestId' => Telemetry::getInstance()->getRequestId(), |
| 114 |
]; |
| 115 |
|
| 116 |
if ( $this->title === null ) { |
| 117 |
|
| 118 |
$this->title = ( isset( $params['namespace'] ) && isset( $params['title'] ) ) |
| 119 |
? Title::makeTitle( $params['namespace'], $params['title'] ) |
| 120 |
|
| 121 |
|
| 122 |
: Title::makeTitle( NS_SPECIAL, '' ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function hasExecutionFlag( $flag ) { |
| 131 |
return ( $this->executionFlags & $flag ) === $flag; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function getType() { |
| 139 |
return $this->command; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
final public function getTitle() { |
| 146 |
return $this->title; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function getParams() { |
| 154 |
return $this->params; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getMetadata( $field = null ) { |
| 164 |
if ( $field === null ) { |
| 165 |
return $this->metadata; |
| 166 |
} |
| 167 |
|
| 168 |
return $this->metadata[$field] ?? null; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function setMetadata( $field, $value ) { |
| 179 |
$old = $this->getMetadata( $field ); |
| 180 |
if ( $value === null ) { |
| 181 |
unset( $this->metadata[$field] ); |
| 182 |
} else { |
| 183 |
$this->metadata[$field] = $value; |
| 184 |
} |
| 185 |
|
| 186 |
return $old; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function getReleaseTimestamp() { |
| 195 |
$time = wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] ?? null ); |
| 196 |
return $time ? (int)$time : null; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public function getQueuedTimestamp() { |
| 204 |
$time = wfTimestampOrNull( TS_UNIX, $this->metadata['timestamp'] ?? null ); |
| 205 |
return $time ? (int)$time : null; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function getRequestId() { |
| 213 |
return $this->params['requestId'] ?? null; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public function getReadyTimestamp() { |
| 221 |
return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp(); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function ignoreDuplicates() { |
| 238 |
return $this->removeDuplicates; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function allowRetries() { |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
public function workItemCount() { |
| 254 |
return 1; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function getDeduplicationInfo() { |
| 268 |
$info = [ |
| 269 |
'type' => $this->getType(), |
| 270 |
'params' => $this->getParams() |
| 271 |
]; |
| 272 |
if ( is_array( $info['params'] ) ) { |
| 273 |
|
| 274 |
unset( $info['params']['rootJobSignature'] ); |
| 275 |
unset( $info['params']['rootJobTimestamp'] ); |
| 276 |
|
| 277 |
unset( $info['params']['jobReleaseTimestamp'] ); |
| 278 |
|
| 279 |
unset( $info['params']['requestId'] ); |
| 280 |
|
| 281 |
ksort( $info['params'] ); |
| 282 |
} |
| 283 |
|
| 284 |
return $info; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public static function newRootJobParams( $key ) { |
| 307 |
return [ |
| 308 |
'rootJobIsSelf' => true, |
| 309 |
'rootJobSignature' => sha1( $key ), |
| 310 |
'rootJobTimestamp' => wfTimestampNow() |
| 311 |
]; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
public function getRootJobParams() { |
| 321 |
return [ |
| 322 |
'rootJobSignature' => $this->params['rootJobSignature'] ?? null, |
| 323 |
'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null |
| 324 |
]; |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function hasRootJobParams() { |
| 334 |
return isset( $this->params['rootJobSignature'] ) |
| 335 |
&& isset( $this->params['rootJobTimestamp'] ); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public function isRootJob() { |
| 344 |
return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] ); |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
protected function addTeardownCallback( $callback ) { |
| 354 |
$this->teardownCallbacks[] = $callback; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public function teardown( $status ) { |
| 362 |
foreach ( $this->teardownCallbacks as $callback ) { |
| 363 |
call_user_func( $callback, $status ); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public function toString() { |
| 372 |
$paramString = ''; |
| 373 |
if ( $this->params ) { |
| 374 |
foreach ( $this->params as $key => $value ) { |
| 375 |
if ( $paramString != '' ) { |
| 376 |
$paramString .= ' '; |
| 377 |
} |
| 378 |
if ( is_array( $value ) ) { |
| 379 |
$filteredValue = []; |
| 380 |
foreach ( $value as $k => $v ) { |
| 381 |
$json = FormatJson::encode( $v ); |
| 382 |
if ( $json === false || mb_strlen( $json ) > 512 ) { |
| 383 |
$filteredValue[$k] = get_debug_type( $v ) . '(...)'; |
| 384 |
} else { |
| 385 |
$filteredValue[$k] = $v; |
| 386 |
} |
| 387 |
} |
| 388 |
if ( count( $filteredValue ) <= 10 ) { |
| 389 |
$value = FormatJson::encode( $filteredValue ); |
| 390 |
} else { |
| 391 |
$value = "array(" . count( $value ) . ")"; |
| 392 |
} |
| 393 |
} elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) { |
| 394 |
$value = get_debug_type( $value ); |
| 395 |
} |
| 396 |
|
| 397 |
$flatValue = (string)$value; |
| 398 |
if ( mb_strlen( $flatValue ) > 1024 ) { |
| 399 |
$flatValue = "string(" . mb_strlen( $value ) . ")"; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
$flatValue = preg_replace( '/\s+/', ' ', $flatValue ); |
| 405 |
|
| 406 |
$paramString .= "$key={$flatValue}"; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
$metaString = ''; |
| 411 |
foreach ( $this->metadata as $key => $value ) { |
| 412 |
if ( is_scalar( $value ) && mb_strlen( $value ) < 1024 ) { |
| 413 |
$metaString .= ( $metaString ? ",$key=$value" : "$key=$value" ); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
$s = $this->command; |
| 418 |
if ( is_object( $this->title ) ) { |
| 419 |
$s .= ' ' . $this->title->getPrefixedDBkey(); |
| 420 |
} |
| 421 |
if ( $paramString != '' ) { |
| 422 |
$s .= " $paramString"; |
| 423 |
} |
| 424 |
if ( $metaString != '' ) { |
| 425 |
$s .= " ($metaString)"; |
| 426 |
} |
| 427 |
|
| 428 |
return $s; |
| 429 |
} |
| 430 |
|
| 431 |
protected function setLastError( $error ) { |
| 432 |
$this->error = $error; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
public function getLastError() { |
| 440 |
return $this->error; |
| 441 |
} |
| 442 |
} |