| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Api; |
| 22 |
|
| 23 |
use Exception; |
| 24 |
use InvalidArgumentException; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
use MediaWiki\Message\Message; |
| 27 |
use RuntimeException; |
| 28 |
use stdClass; |
| 29 |
use UnexpectedValueException; |
| 30 |
use Wikimedia\Message\ListParam; |
| 31 |
use Wikimedia\Message\ScalarParam; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class ApiResult implements ApiSerializable { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public const OVERRIDE = 1; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public const ADD_ON_TOP = 2; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public const NO_SIZE_CHECK = 4; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public const NO_VALIDATE = self::NO_SIZE_CHECK | 8; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public const IGNORE_CONFLICT_KEYS = 16; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public const META_INDEXED_TAG_NAME = '_element'; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public const META_SUBELEMENTS = '_subelements'; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public const META_PRESERVE_KEYS = '_preservekeys'; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public const META_CONTENT = '_content'; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public const META_TYPE = '_type'; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public const META_KVP_KEY_NAME = '_kvpkeyname'; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public const META_KVP_MERGE = '_kvpmerge'; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public const META_BC_BOOLS = '_BC_bools'; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public const META_BC_SUBELEMENTS = '_BC_subelements'; |
| 161 |
|
| 162 |
|
| 163 |
private $data; |
| 164 |
private int $size; |
| 165 |
|
| 166 |
private $maxSize; |
| 167 |
private ApiErrorFormatter $errorFormatter; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public function __construct( $maxSize ) { |
| 173 |
$this->maxSize = $maxSize; |
| 174 |
$this->reset(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function setErrorFormatter( ApiErrorFormatter $formatter ) { |
| 182 |
$this->errorFormatter = $formatter; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function serializeForApiResult() { |
| 191 |
return $this->data; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function reset() { |
| 202 |
$this->data = [ |
| 203 |
self::META_TYPE => 'assoc', |
| 204 |
]; |
| 205 |
$this->size = 0; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function getResultData( $path = [], $transforms = [] ) { |
| 262 |
$path = (array)$path; |
| 263 |
if ( !$path ) { |
| 264 |
return self::applyTransformations( $this->data, $transforms ); |
| 265 |
} |
| 266 |
|
| 267 |
$last = array_pop( $path ); |
| 268 |
$ret = &$this->path( $path, 'dummy' ); |
| 269 |
if ( !isset( $ret[$last] ) ) { |
| 270 |
return null; |
| 271 |
} elseif ( is_array( $ret[$last] ) ) { |
| 272 |
return self::applyTransformations( $ret[$last], $transforms ); |
| 273 |
} else { |
| 274 |
return $ret[$last]; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function getSize() { |
| 283 |
return $this->size; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public static function setValue( array &$arr, $name, $value, $flags = 0 ) { |
| 299 |
if ( ( $flags & self::NO_VALIDATE ) !== self::NO_VALIDATE ) { |
| 300 |
$value = self::validateValue( $value ); |
| 301 |
} |
| 302 |
|
| 303 |
if ( $name === null ) { |
| 304 |
if ( $flags & self::ADD_ON_TOP ) { |
| 305 |
array_unshift( $arr, $value ); |
| 306 |
} else { |
| 307 |
$arr[] = $value; |
| 308 |
} |
| 309 |
return; |
| 310 |
} |
| 311 |
|
| 312 |
$exists = isset( $arr[$name] ); |
| 313 |
if ( !$exists || ( $flags & self::OVERRIDE ) ) { |
| 314 |
if ( !$exists && ( $flags & self::ADD_ON_TOP ) ) { |
| 315 |
$arr = [ $name => $value ] + $arr; |
| 316 |
} else { |
| 317 |
$arr[$name] = $value; |
| 318 |
} |
| 319 |
} elseif ( is_array( $arr[$name] ) && is_array( $value ) ) { |
| 320 |
$conflicts = array_intersect_key( $arr[$name], $value ); |
| 321 |
if ( !$conflicts || ( $flags & self::IGNORE_CONFLICT_KEYS ) ) { |
| 322 |
$arr[$name] += $value; |
| 323 |
} else { |
| 324 |
$keys = implode( ', ', array_keys( $conflicts ) ); |
| 325 |
throw new RuntimeException( |
| 326 |
"Conflicting keys ($keys) when attempting to merge element $name" |
| 327 |
); |
| 328 |
} |
| 329 |
} elseif ( $value !== $arr[$name] ) { |
| 330 |
throw new RuntimeException( |
| 331 |
"Attempting to add element $name=$value, existing value is {$arr[$name]}" |
| 332 |
); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
private static function validateValue( $value ) { |
| 342 |
if ( is_object( $value ) ) { |
| 343 |
|
| 344 |
|
| 345 |
if ( is_callable( [ $value, 'serializeForApiResult' ] ) ) { |
| 346 |
$oldValue = $value; |
| 347 |
$value = $value->serializeForApiResult(); |
| 348 |
if ( is_object( $value ) ) { |
| 349 |
throw new UnexpectedValueException( |
| 350 |
get_class( $oldValue ) . '::serializeForApiResult() returned an object of class ' . |
| 351 |
get_class( $value ) |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
try { |
| 358 |
return self::validateValue( $value ); |
| 359 |
} catch ( Exception $ex ) { |
| 360 |
throw new UnexpectedValueException( |
| 361 |
get_class( $oldValue ) . '::serializeForApiResult() returned an invalid value: ' . |
| 362 |
$ex->getMessage(), |
| 363 |
0, |
| 364 |
$ex |
| 365 |
); |
| 366 |
} |
| 367 |
} elseif ( $value instanceof ScalarParam || $value instanceof ListParam ) { |
| 368 |
|
| 369 |
|
| 370 |
$value = $value->getType() === 'text' ? $value->getValue() : $value->toJsonArray(); |
| 371 |
} elseif ( is_callable( [ $value, '__toString' ] ) ) { |
| 372 |
$value = (string)$value; |
| 373 |
} else { |
| 374 |
$value = (array)$value + [ self::META_TYPE => 'assoc' ]; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
if ( is_string( $value ) ) { |
| 379 |
|
| 380 |
static $contentLanguage = null; |
| 381 |
if ( !$contentLanguage ) { |
| 382 |
$contentLanguage = MediaWikiServices::getInstance()->getContentLanguage(); |
| 383 |
} |
| 384 |
$value = $contentLanguage->normalize( $value ); |
| 385 |
} elseif ( is_array( $value ) ) { |
| 386 |
foreach ( $value as $k => $v ) { |
| 387 |
$value[$k] = self::validateValue( $v ); |
| 388 |
} |
| 389 |
} elseif ( $value !== null && !is_scalar( $value ) ) { |
| 390 |
$type = get_debug_type( $value ); |
| 391 |
throw new InvalidArgumentException( "Cannot add $type to ApiResult" ); |
| 392 |
} elseif ( is_float( $value ) && !is_finite( $value ) ) { |
| 393 |
throw new InvalidArgumentException( 'Cannot add non-finite floats to ApiResult' ); |
| 394 |
} |
| 395 |
|
| 396 |
return $value; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
public function addValue( $path, $name, $value, $flags = 0 ) { |
| 416 |
$arr = &$this->path( $path, ( $flags & self::ADD_ON_TOP ) ? 'prepend' : 'append' ); |
| 417 |
|
| 418 |
if ( !( $flags & self::NO_SIZE_CHECK ) ) { |
| 419 |
|
| 420 |
|
| 421 |
$value = self::validateValue( $value ); |
| 422 |
$flags |= self::NO_VALIDATE; |
| 423 |
|
| 424 |
$newsize = $this->size + self::size( $value ); |
| 425 |
if ( $this->maxSize !== false && $newsize > $this->maxSize ) { |
| 426 |
$this->errorFormatter->addWarning( |
| 427 |
'result', [ 'apiwarn-truncatedresult', Message::numParam( $this->maxSize ) ] |
| 428 |
); |
| 429 |
return false; |
| 430 |
} |
| 431 |
$this->size = $newsize; |
| 432 |
} |
| 433 |
|
| 434 |
self::setValue( $arr, $name, $value, $flags ); |
| 435 |
return true; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
public static function unsetValue( array &$arr, $name ) { |
| 445 |
$ret = null; |
| 446 |
if ( isset( $arr[$name] ) ) { |
| 447 |
$ret = $arr[$name]; |
| 448 |
unset( $arr[$name] ); |
| 449 |
} |
| 450 |
return $ret; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public function removeValue( $path, $name, $flags = 0 ) { |
| 464 |
$path = (array)$path; |
| 465 |
if ( $name === null ) { |
| 466 |
if ( !$path ) { |
| 467 |
throw new InvalidArgumentException( 'Cannot remove the data root' ); |
| 468 |
} |
| 469 |
$name = array_pop( $path ); |
| 470 |
} |
| 471 |
$ret = self::unsetValue( $this->path( $path, 'dummy' ), $name ); |
| 472 |
if ( !( $flags & self::NO_SIZE_CHECK ) ) { |
| 473 |
$newsize = $this->size - self::size( $ret ); |
| 474 |
$this->size = max( $newsize, 0 ); |
| 475 |
} |
| 476 |
return $ret; |
| 477 |
} |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
public static function setContentValue( array &$arr, $name, $value, $flags = 0 ) { |
| 489 |
if ( $name === null ) { |
| 490 |
throw new InvalidArgumentException( 'Content value must be named' ); |
| 491 |
} |
| 492 |
self::setContentField( $arr, $name, $flags ); |
| 493 |
self::setValue( $arr, $name, $value, $flags ); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
public function addContentValue( $path, $name, $value, $flags = 0 ) { |
| 507 |
if ( $name === null ) { |
| 508 |
throw new InvalidArgumentException( 'Content value must be named' ); |
| 509 |
} |
| 510 |
$this->addContentField( $path, $name, $flags ); |
| 511 |
return $this->addValue( $path, $name, $value, $flags ); |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public function addParsedLimit( $moduleName, $limit ) { |
| 522 |
|
| 523 |
$this->addValue( 'limits', $moduleName, $limit, |
| 524 |
self::OVERRIDE | self::NO_SIZE_CHECK ); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public static function setContentField( array &$arr, $name, $flags = 0 ) { |
| 542 |
if ( isset( $arr[self::META_CONTENT] ) && |
| 543 |
isset( $arr[$arr[self::META_CONTENT]] ) && |
| 544 |
!( $flags & self::OVERRIDE ) |
| 545 |
) { |
| 546 |
throw new RuntimeException( |
| 547 |
"Attempting to set content element as $name when " . $arr[self::META_CONTENT] . |
| 548 |
' is already set as the content element' |
| 549 |
); |
| 550 |
} |
| 551 |
$arr[self::META_CONTENT] = $name; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
public function addContentField( $path, $name, $flags = 0 ) { |
| 563 |
$arr = &$this->path( $path, ( $flags & self::ADD_ON_TOP ) ? 'prepend' : 'append' ); |
| 564 |
self::setContentField( $arr, $name, $flags ); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public static function setSubelementsList( array &$arr, $names ) { |
| 575 |
if ( !isset( $arr[self::META_SUBELEMENTS] ) ) { |
| 576 |
$arr[self::META_SUBELEMENTS] = (array)$names; |
| 577 |
} else { |
| 578 |
$arr[self::META_SUBELEMENTS] = array_merge( $arr[self::META_SUBELEMENTS], (array)$names ); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public function addSubelementsList( $path, $names ) { |
| 590 |
$arr = &$this->path( $path ); |
| 591 |
self::setSubelementsList( $arr, $names ); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public static function unsetSubelementsList( array &$arr, $names ) { |
| 602 |
if ( isset( $arr[self::META_SUBELEMENTS] ) ) { |
| 603 |
$arr[self::META_SUBELEMENTS] = array_diff( $arr[self::META_SUBELEMENTS], (array)$names ); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
public function removeSubelementsList( $path, $names ) { |
| 615 |
$arr = &$this->path( $path ); |
| 616 |
self::unsetSubelementsList( $arr, $names ); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
public static function setIndexedTagName( array &$arr, $tag ) { |
| 626 |
if ( !is_string( $tag ) ) { |
| 627 |
throw new InvalidArgumentException( 'Bad tag name' ); |
| 628 |
} |
| 629 |
$arr[self::META_INDEXED_TAG_NAME] = $tag; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
public function addIndexedTagName( $path, $tag ) { |
| 639 |
$arr = &$this->path( $path ); |
| 640 |
self::setIndexedTagName( $arr, $tag ); |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
public static function setIndexedTagNameRecursive( array &$arr, $tag ) { |
| 651 |
if ( !is_string( $tag ) ) { |
| 652 |
throw new InvalidArgumentException( 'Bad tag name' ); |
| 653 |
} |
| 654 |
$arr[self::META_INDEXED_TAG_NAME] = $tag; |
| 655 |
foreach ( $arr as $k => &$v ) { |
| 656 |
if ( is_array( $v ) && !self::isMetadataKey( $k ) ) { |
| 657 |
self::setIndexedTagNameRecursive( $v, $tag ); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
public function addIndexedTagNameRecursive( $path, $tag ) { |
| 670 |
$arr = &$this->path( $path ); |
| 671 |
self::setIndexedTagNameRecursive( $arr, $tag ); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public static function setPreserveKeysList( array &$arr, $names ) { |
| 685 |
if ( !isset( $arr[self::META_PRESERVE_KEYS] ) ) { |
| 686 |
$arr[self::META_PRESERVE_KEYS] = (array)$names; |
| 687 |
} else { |
| 688 |
$arr[self::META_PRESERVE_KEYS] = array_merge( $arr[self::META_PRESERVE_KEYS], (array)$names ); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
public function addPreserveKeysList( $path, $names ) { |
| 700 |
$arr = &$this->path( $path ); |
| 701 |
self::setPreserveKeysList( $arr, $names ); |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
public static function unsetPreserveKeysList( array &$arr, $names ) { |
| 712 |
if ( isset( $arr[self::META_PRESERVE_KEYS] ) ) { |
| 713 |
$arr[self::META_PRESERVE_KEYS] = array_diff( $arr[self::META_PRESERVE_KEYS], (array)$names ); |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
public function removePreserveKeysList( $path, $names ) { |
| 725 |
$arr = &$this->path( $path ); |
| 726 |
self::unsetPreserveKeysList( $arr, $names ); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
public static function setArrayType( array &$arr, $type, $kvpKeyName = null ) { |
| 738 |
if ( !in_array( $type, [ |
| 739 |
'default', 'array', 'assoc', 'kvp', 'BCarray', 'BCassoc', 'BCkvp' |
| 740 |
], true ) ) { |
| 741 |
throw new InvalidArgumentException( 'Bad type' ); |
| 742 |
} |
| 743 |
$arr[self::META_TYPE] = $type; |
| 744 |
if ( is_string( $kvpKeyName ) ) { |
| 745 |
$arr[self::META_KVP_KEY_NAME] = $kvpKeyName; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
public function addArrayType( $path, $tag, $kvpKeyName = null ) { |
| 757 |
$arr = &$this->path( $path ); |
| 758 |
self::setArrayType( $arr, $tag, $kvpKeyName ); |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
public static function setArrayTypeRecursive( array &$arr, $type, $kvpKeyName = null ) { |
| 769 |
self::setArrayType( $arr, $type, $kvpKeyName ); |
| 770 |
foreach ( $arr as $k => &$v ) { |
| 771 |
if ( is_array( $v ) && !self::isMetadataKey( $k ) ) { |
| 772 |
self::setArrayTypeRecursive( $v, $type, $kvpKeyName ); |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
public function addArrayTypeRecursive( $path, $tag, $kvpKeyName = null ) { |
| 785 |
$arr = &$this->path( $path ); |
| 786 |
self::setArrayTypeRecursive( $arr, $tag, $kvpKeyName ); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
public static function isMetadataKey( $key ) { |
| 802 |
return str_starts_with( $key, '_' ); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
protected static function applyTransformations( array $dataIn, array $transforms ) { |
| 815 |
$strip = $transforms['Strip'] ?? 'none'; |
| 816 |
if ( $strip === 'base' ) { |
| 817 |
$transforms['Strip'] = 'none'; |
| 818 |
} |
| 819 |
$transformTypes = $transforms['Types'] ?? null; |
| 820 |
if ( $transformTypes !== null && !is_array( $transformTypes ) ) { |
| 821 |
throw new InvalidArgumentException( __METHOD__ . ':Value for "Types" must be an array' ); |
| 822 |
} |
| 823 |
|
| 824 |
$metadata = []; |
| 825 |
$data = self::stripMetadataNonRecursive( $dataIn, $metadata ); |
| 826 |
|
| 827 |
if ( isset( $transforms['Custom'] ) ) { |
| 828 |
if ( !is_callable( $transforms['Custom'] ) ) { |
| 829 |
throw new InvalidArgumentException( __METHOD__ . ': Value for "Custom" must be callable' ); |
| 830 |
} |
| 831 |
call_user_func_array( $transforms['Custom'], [ &$data, &$metadata ] ); |
| 832 |
} |
| 833 |
|
| 834 |
if ( ( isset( $transforms['BC'] ) || $transformTypes !== null ) && |
| 835 |
isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] === 'BCkvp' && |
| 836 |
!isset( $metadata[self::META_KVP_KEY_NAME] ) |
| 837 |
) { |
| 838 |
throw new UnexpectedValueException( 'Type "BCkvp" used without setting ' . |
| 839 |
'ApiResult::META_KVP_KEY_NAME metadata item' ); |
| 840 |
} |
| 841 |
|
| 842 |
|
| 843 |
$boolKeys = null; |
| 844 |
if ( isset( $transforms['BC'] ) ) { |
| 845 |
if ( !is_array( $transforms['BC'] ) ) { |
| 846 |
throw new InvalidArgumentException( __METHOD__ . ':Value for "BC" must be an array' ); |
| 847 |
} |
| 848 |
if ( !in_array( 'nobool', $transforms['BC'], true ) ) { |
| 849 |
$boolKeys = isset( $metadata[self::META_BC_BOOLS] ) |
| 850 |
? array_fill_keys( $metadata[self::META_BC_BOOLS], true ) |
| 851 |
: []; |
| 852 |
} |
| 853 |
|
| 854 |
if ( !in_array( 'no*', $transforms['BC'], true ) && |
| 855 |
isset( $metadata[self::META_CONTENT] ) && $metadata[self::META_CONTENT] !== '*' |
| 856 |
) { |
| 857 |
$k = $metadata[self::META_CONTENT]; |
| 858 |
$data['*'] = $data[$k]; |
| 859 |
unset( $data[$k] ); |
| 860 |
$metadata[self::META_CONTENT] = '*'; |
| 861 |
} |
| 862 |
|
| 863 |
if ( !in_array( 'nosub', $transforms['BC'], true ) && |
| 864 |
isset( $metadata[self::META_BC_SUBELEMENTS] ) |
| 865 |
) { |
| 866 |
foreach ( $metadata[self::META_BC_SUBELEMENTS] as $k ) { |
| 867 |
if ( isset( $data[$k] ) ) { |
| 868 |
$data[$k] = [ |
| 869 |
'*' => $data[$k], |
| 870 |
self::META_CONTENT => '*', |
| 871 |
self::META_TYPE => 'assoc', |
| 872 |
]; |
| 873 |
} |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
if ( isset( $metadata[self::META_TYPE] ) ) { |
| 878 |
switch ( $metadata[self::META_TYPE] ) { |
| 879 |
case 'BCarray': |
| 880 |
case 'BCassoc': |
| 881 |
$metadata[self::META_TYPE] = 'default'; |
| 882 |
break; |
| 883 |
case 'BCkvp': |
| 884 |
$transformTypes['ArmorKVP'] = $metadata[self::META_KVP_KEY_NAME]; |
| 885 |
break; |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
$defaultType = 'array'; |
| 892 |
$maxKey = -1; |
| 893 |
foreach ( $data as $k => &$v ) { |
| 894 |
$v = is_array( $v ) ? self::applyTransformations( $v, $transforms ) : $v; |
| 895 |
if ( $boolKeys !== null && is_bool( $v ) && !isset( $boolKeys[$k] ) ) { |
| 896 |
if ( !$v ) { |
| 897 |
unset( $data[$k] ); |
| 898 |
continue; |
| 899 |
} |
| 900 |
$v = ''; |
| 901 |
} |
| 902 |
if ( is_string( $k ) ) { |
| 903 |
$defaultType = 'assoc'; |
| 904 |
} elseif ( $k > $maxKey ) { |
| 905 |
$maxKey = $k; |
| 906 |
} |
| 907 |
} |
| 908 |
unset( $v ); |
| 909 |
|
| 910 |
|
| 911 |
switch ( $strip ) { |
| 912 |
case 'all': |
| 913 |
case 'base': |
| 914 |
$keepMetadata = []; |
| 915 |
break; |
| 916 |
case 'none': |
| 917 |
$keepMetadata = &$metadata; |
| 918 |
break; |
| 919 |
case 'bc': |
| 920 |
|
| 921 |
$keepMetadata = array_intersect_key( $metadata, [ |
| 922 |
self::META_INDEXED_TAG_NAME => 1, |
| 923 |
self::META_SUBELEMENTS => 1, |
| 924 |
] ); |
| 925 |
break; |
| 926 |
default: |
| 927 |
throw new InvalidArgumentException( __METHOD__ . ': Unknown value for "Strip"' ); |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
if ( $transformTypes === null ) { |
| 932 |
return $data + $keepMetadata; |
| 933 |
} |
| 934 |
|
| 935 |
if ( $defaultType === 'array' && $maxKey !== count( $data ) - 1 ) { |
| 936 |
$defaultType = 'assoc'; |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
$type = $defaultType; |
| 941 |
if ( isset( $metadata[self::META_TYPE] ) && $metadata[self::META_TYPE] !== 'default' ) { |
| 942 |
$type = $metadata[self::META_TYPE]; |
| 943 |
} |
| 944 |
if ( ( $type === 'kvp' || $type === 'BCkvp' ) && |
| 945 |
empty( $transformTypes['ArmorKVP'] ) |
| 946 |
) { |
| 947 |
$type = 'assoc'; |
| 948 |
} elseif ( $type === 'BCarray' ) { |
| 949 |
$type = 'array'; |
| 950 |
} elseif ( $type === 'BCassoc' ) { |
| 951 |
$type = 'assoc'; |
| 952 |
} |
| 953 |
|
| 954 |
|
| 955 |
switch ( $type ) { |
| 956 |
case 'assoc': |
| 957 |
$metadata[self::META_TYPE] = 'assoc'; |
| 958 |
$data += $keepMetadata; |
| 959 |
return empty( $transformTypes['AssocAsObject'] ) ? $data : (object)$data; |
| 960 |
|
| 961 |
case 'array': |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
uksort( $data, static function ( $a, $b ): int { |
| 972 |
|
| 973 |
|
| 974 |
if ( is_numeric( $a ) xor is_numeric( $b ) ) { |
| 975 |
return (string)$a <=> (string)$b; |
| 976 |
} |
| 977 |
|
| 978 |
return $a <=> $b; |
| 979 |
} ); |
| 980 |
|
| 981 |
$data = array_values( $data ); |
| 982 |
$metadata[self::META_TYPE] = 'array'; |
| 983 |
|
| 984 |
return $data + $keepMetadata; |
| 985 |
|
| 986 |
case 'kvp': |
| 987 |
case 'BCkvp': |
| 988 |
$key = $metadata[self::META_KVP_KEY_NAME] ?? $transformTypes['ArmorKVP']; |
| 989 |
$valKey = isset( $transforms['BC'] ) ? '*' : 'value'; |
| 990 |
$assocAsObject = !empty( $transformTypes['AssocAsObject'] ); |
| 991 |
$merge = !empty( $metadata[self::META_KVP_MERGE] ); |
| 992 |
|
| 993 |
$ret = []; |
| 994 |
foreach ( $data as $k => $v ) { |
| 995 |
if ( $merge && ( is_array( $v ) || is_object( $v ) ) ) { |
| 996 |
$vArr = (array)$v; |
| 997 |
if ( isset( $vArr[self::META_TYPE] ) ) { |
| 998 |
$mergeType = $vArr[self::META_TYPE]; |
| 999 |
} elseif ( is_object( $v ) ) { |
| 1000 |
$mergeType = 'assoc'; |
| 1001 |
} else { |
| 1002 |
$keys = array_keys( $vArr ); |
| 1003 |
sort( $keys, SORT_NUMERIC ); |
| 1004 |
$mergeType = ( $keys === array_keys( $keys ) ) ? 'array' : 'assoc'; |
| 1005 |
} |
| 1006 |
} else { |
| 1007 |
$mergeType = 'n/a'; |
| 1008 |
} |
| 1009 |
if ( $mergeType === 'assoc' ) { |
| 1010 |
|
| 1011 |
$item = $vArr + [ |
| 1012 |
$key => $k, |
| 1013 |
]; |
| 1014 |
if ( $strip === 'none' ) { |
| 1015 |
self::setPreserveKeysList( $item, [ $key ] ); |
| 1016 |
} |
| 1017 |
} else { |
| 1018 |
$item = [ |
| 1019 |
$key => $k, |
| 1020 |
$valKey => $v, |
| 1021 |
]; |
| 1022 |
if ( $strip === 'none' ) { |
| 1023 |
$item += [ |
| 1024 |
self::META_PRESERVE_KEYS => [ $key ], |
| 1025 |
self::META_CONTENT => $valKey, |
| 1026 |
self::META_TYPE => 'assoc', |
| 1027 |
]; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
$ret[] = $assocAsObject ? (object)$item : $item; |
| 1031 |
} |
| 1032 |
$metadata[self::META_TYPE] = 'array'; |
| 1033 |
|
| 1034 |
|
| 1035 |
return $ret + $keepMetadata; |
| 1036 |
|
| 1037 |
default: |
| 1038 |
throw new UnexpectedValueException( "Unknown type '$type'" ); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
public static function stripMetadata( $data ) { |
| 1053 |
if ( is_array( $data ) || is_object( $data ) ) { |
| 1054 |
$isObj = is_object( $data ); |
| 1055 |
if ( $isObj ) { |
| 1056 |
$data = (array)$data; |
| 1057 |
} |
| 1058 |
$preserveKeys = isset( $data[self::META_PRESERVE_KEYS] ) |
| 1059 |
? (array)$data[self::META_PRESERVE_KEYS] |
| 1060 |
: []; |
| 1061 |
foreach ( $data as $k => $v ) { |
| 1062 |
if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys, true ) ) { |
| 1063 |
unset( $data[$k] ); |
| 1064 |
} elseif ( is_array( $v ) || is_object( $v ) ) { |
| 1065 |
$data[$k] = self::stripMetadata( $v ); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
if ( $isObj ) { |
| 1069 |
$data = (object)$data; |
| 1070 |
} |
| 1071 |
} |
| 1072 |
return $data; |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
public static function stripMetadataNonRecursive( $data, &$metadata = null ) { |
| 1087 |
if ( !is_array( $metadata ) ) { |
| 1088 |
$metadata = []; |
| 1089 |
} |
| 1090 |
if ( is_array( $data ) || is_object( $data ) ) { |
| 1091 |
$isObj = is_object( $data ); |
| 1092 |
if ( $isObj ) { |
| 1093 |
$data = (array)$data; |
| 1094 |
} |
| 1095 |
$preserveKeys = isset( $data[self::META_PRESERVE_KEYS] ) |
| 1096 |
? (array)$data[self::META_PRESERVE_KEYS] |
| 1097 |
: []; |
| 1098 |
foreach ( $data as $k => $v ) { |
| 1099 |
if ( self::isMetadataKey( $k ) && !in_array( $k, $preserveKeys, true ) ) { |
| 1100 |
$metadata[$k] = $v; |
| 1101 |
unset( $data[$k] ); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
if ( $isObj ) { |
| 1105 |
$data = (object)$data; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
return $data; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
private static function size( $value ) { |
| 1118 |
$s = 0; |
| 1119 |
if ( is_array( $value ) ) { |
| 1120 |
foreach ( $value as $k => $v ) { |
| 1121 |
if ( !self::isMetadataKey( $k ) ) { |
| 1122 |
$s += self::size( $v ); |
| 1123 |
} |
| 1124 |
} |
| 1125 |
} elseif ( is_scalar( $value ) ) { |
| 1126 |
$s = strlen( $value ); |
| 1127 |
} |
| 1128 |
|
| 1129 |
return $s; |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
private function &path( $path, $create = 'append' ) { |
| 1144 |
$path = (array)$path; |
| 1145 |
$ret = &$this->data; |
| 1146 |
foreach ( $path as $i => $k ) { |
| 1147 |
if ( !isset( $ret[$k] ) ) { |
| 1148 |
switch ( $create ) { |
| 1149 |
case 'append': |
| 1150 |
$ret[$k] = []; |
| 1151 |
break; |
| 1152 |
case 'prepend': |
| 1153 |
$ret = [ $k => [] ] + $ret; |
| 1154 |
break; |
| 1155 |
case 'dummy': |
| 1156 |
$tmp = []; |
| 1157 |
return $tmp; |
| 1158 |
default: |
| 1159 |
$fail = implode( '.', array_slice( $path, 0, $i + 1 ) ); |
| 1160 |
throw new InvalidArgumentException( "Path $fail does not exist" ); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
if ( !is_array( $ret[$k] ) ) { |
| 1164 |
$fail = implode( '.', array_slice( $path, 0, $i + 1 ) ); |
| 1165 |
throw new InvalidArgumentException( "Path $fail is not an array" ); |
| 1166 |
} |
| 1167 |
$ret = &$ret[$k]; |
| 1168 |
} |
| 1169 |
return $ret; |
| 1170 |
} |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
public static function addMetadataToResultVars( $vars, $forceHash = true ) { |
| 1181 |
|
| 1182 |
$hash = $forceHash; |
| 1183 |
$maxKey = -1; |
| 1184 |
$bools = []; |
| 1185 |
foreach ( $vars as $k => $v ) { |
| 1186 |
if ( is_array( $v ) || is_object( $v ) ) { |
| 1187 |
$vars[$k] = self::addMetadataToResultVars( (array)$v, is_object( $v ) ); |
| 1188 |
} elseif ( is_bool( $v ) ) { |
| 1189 |
|
| 1190 |
$bools[] = $k; |
| 1191 |
} |
| 1192 |
if ( is_string( $k ) ) { |
| 1193 |
$hash = true; |
| 1194 |
} elseif ( $k > $maxKey ) { |
| 1195 |
$maxKey = $k; |
| 1196 |
} |
| 1197 |
} |
| 1198 |
if ( !$hash && $maxKey !== count( $vars ) - 1 ) { |
| 1199 |
$hash = true; |
| 1200 |
} |
| 1201 |
|
| 1202 |
|
| 1203 |
if ( $hash ) { |
| 1204 |
|
| 1205 |
|
| 1206 |
$keys = array_diff( array_keys( $vars ), [ |
| 1207 |
self::META_TYPE, self::META_PRESERVE_KEYS, self::META_KVP_KEY_NAME, |
| 1208 |
self::META_INDEXED_TAG_NAME, self::META_BC_BOOLS |
| 1209 |
] ); |
| 1210 |
|
| 1211 |
return [ |
| 1212 |
self::META_TYPE => 'kvp', |
| 1213 |
self::META_KVP_KEY_NAME => 'key', |
| 1214 |
self::META_PRESERVE_KEYS => $keys, |
| 1215 |
self::META_BC_BOOLS => $bools, |
| 1216 |
self::META_INDEXED_TAG_NAME => 'var', |
| 1217 |
] + $vars; |
| 1218 |
} else { |
| 1219 |
return [ |
| 1220 |
self::META_TYPE => 'array', |
| 1221 |
self::META_BC_BOOLS => $bools, |
| 1222 |
self::META_INDEXED_TAG_NAME => 'value', |
| 1223 |
] + $vars; |
| 1224 |
} |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
public static function formatExpiry( $expiry, $infinity = 'infinity' ) { |
| 1236 |
static $dbInfinity; |
| 1237 |
$dbInfinity ??= MediaWikiServices::getInstance()->getConnectionProvider() |
| 1238 |
->getReplicaDatabase() |
| 1239 |
->getInfinity(); |
| 1240 |
|
| 1241 |
if ( $expiry === '' || $expiry === null || $expiry === false || |
| 1242 |
wfIsInfinity( $expiry ) || $expiry === $dbInfinity |
| 1243 |
) { |
| 1244 |
return $infinity; |
| 1245 |
} else { |
| 1246 |
return wfTimestamp( TS_ISO_8601, $expiry ); |
| 1247 |
} |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
class_alias( ApiResult::class, 'ApiResult' ); |