| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use LogicException; |
| 27 |
use MediaWiki\Api\Validator\SubmoduleDef; |
| 28 |
use MediaWiki\Block\Block; |
| 29 |
use MediaWiki\Context\ContextSource; |
| 30 |
use MediaWiki\Context\IContextSource; |
| 31 |
use MediaWiki\HookContainer\HookContainer; |
| 32 |
use MediaWiki\Language\RawMessage; |
| 33 |
use MediaWiki\MainConfigNames; |
| 34 |
use MediaWiki\MediaWikiServices; |
| 35 |
use MediaWiki\Message\Message; |
| 36 |
use MediaWiki\Page\PageIdentity; |
| 37 |
use MediaWiki\ParamValidator\TypeDef\NamespaceDef; |
| 38 |
use MediaWiki\Permissions\Authority; |
| 39 |
use MediaWiki\Permissions\PermissionManager; |
| 40 |
use MediaWiki\Permissions\PermissionStatus; |
| 41 |
use MediaWiki\Registration\ExtensionRegistry; |
| 42 |
use MediaWiki\Specials\SpecialVersion; |
| 43 |
use MediaWiki\Status\Status; |
| 44 |
use MediaWiki\Title\Title; |
| 45 |
use MediaWiki\User\User; |
| 46 |
use MediaWiki\User\UserRigorOptions; |
| 47 |
use MWException; |
| 48 |
use ReflectionClass; |
| 49 |
use StatusValue; |
| 50 |
use stdClass; |
| 51 |
use Throwable; |
| 52 |
use Wikimedia\Message\MessageSpecifier; |
| 53 |
use Wikimedia\ParamValidator\ParamValidator; |
| 54 |
use Wikimedia\ParamValidator\TypeDef\EnumDef; |
| 55 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 56 |
use Wikimedia\ParamValidator\TypeDef\StringDef; |
| 57 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 58 |
use Wikimedia\Timestamp\TimestampException; |
| 59 |
use WikiPage; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
abstract class ApiBase extends ContextSource { |
| 77 |
|
| 78 |
use ApiBlockInfoTrait; |
| 79 |
|
| 80 |
|
| 81 |
private $hookContainer; |
| 82 |
|
| 83 |
|
| 84 |
private $hookRunner; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public const PARAM_DFLT = ParamValidator::PARAM_DEFAULT; |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public const PARAM_ISMULTI = ParamValidator::PARAM_ISMULTI; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public const PARAM_TYPE = ParamValidator::PARAM_TYPE; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public const PARAM_MAX = IntegerDef::PARAM_MAX; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public const PARAM_MAX2 = IntegerDef::PARAM_MAX2; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public const PARAM_MIN = IntegerDef::PARAM_MIN; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public const PARAM_ALLOW_DUPLICATES = ParamValidator::PARAM_ALLOW_DUPLICATES; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public const PARAM_DEPRECATED = ParamValidator::PARAM_DEPRECATED; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public const PARAM_REQUIRED = ParamValidator::PARAM_REQUIRED; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public const PARAM_SUBMODULE_MAP = SubmoduleDef::PARAM_SUBMODULE_MAP; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public const PARAM_SUBMODULE_PARAM_PREFIX = SubmoduleDef::PARAM_SUBMODULE_PARAM_PREFIX; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public const PARAM_ALL = ParamValidator::PARAM_ALL; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public const PARAM_EXTRA_NAMESPACES = NamespaceDef::PARAM_EXTRA_NAMESPACES; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public const PARAM_SENSITIVE = ParamValidator::PARAM_SENSITIVE; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public const PARAM_DEPRECATED_VALUES = EnumDef::PARAM_DEPRECATED_VALUES; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public const PARAM_ISMULTI_LIMIT1 = ParamValidator::PARAM_ISMULTI_LIMIT1; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public const PARAM_ISMULTI_LIMIT2 = ParamValidator::PARAM_ISMULTI_LIMIT2; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public const PARAM_MAX_BYTES = StringDef::PARAM_MAX_BYTES; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public const PARAM_MAX_CHARS = StringDef::PARAM_MAX_CHARS; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public const PARAM_RANGE_ENFORCE = 'api-param-range-enforce'; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public const PARAM_HELP_MSG = 'api-param-help-msg'; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public const PARAM_HELP_MSG_APPEND = 'api-param-help-msg-append'; |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public const PARAM_HELP_MSG_INFO = 'api-param-help-msg-info'; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public const PARAM_VALUE_LINKS = 'api-param-value-links'; |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public const PARAM_HELP_MSG_PER_VALUE = 'api-param-help-msg-per-value'; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public const PARAM_TEMPLATE_VARS = 'param-template-vars'; |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public const ALL_DEFAULT_STRING = '*'; |
| 247 |
|
| 248 |
|
| 249 |
public const LIMIT_BIG1 = 500; |
| 250 |
|
| 251 |
public const LIMIT_BIG2 = 5000; |
| 252 |
|
| 253 |
public const LIMIT_SML1 = 50; |
| 254 |
|
| 255 |
public const LIMIT_SML2 = 500; |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public const GET_VALUES_FOR_HELP = 1; |
| 263 |
|
| 264 |
|
| 265 |
private static $extensionInfo = null; |
| 266 |
|
| 267 |
|
| 268 |
private static $filterIDsCache = []; |
| 269 |
|
| 270 |
|
| 271 |
private const BLOCK_CODE_MAP = [ |
| 272 |
'blockedtext' => true, |
| 273 |
'blockedtext-partial' => true, |
| 274 |
'autoblockedtext' => true, |
| 275 |
'systemblockedtext' => true, |
| 276 |
'blockedtext-composite' => true, |
| 277 |
'blockedtext-tempuser' => true, |
| 278 |
'autoblockedtext-tempuser' => true, |
| 279 |
]; |
| 280 |
|
| 281 |
|
| 282 |
private const MESSAGE_CODE_MAP = [ |
| 283 |
'actionthrottled' => [ 'apierror-ratelimited', 'ratelimited' ], |
| 284 |
'actionthrottledtext' => [ 'apierror-ratelimited', 'ratelimited' ], |
| 285 |
]; |
| 286 |
|
| 287 |
|
| 288 |
private $mMainModule; |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
private $mModuleName; |
| 297 |
|
| 298 |
private $mModulePrefix; |
| 299 |
|
| 300 |
|
| 301 |
private $mReplicaDB = null; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
private $mParamCache = []; |
| 306 |
|
| 307 |
private $mModuleSource = false; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function __construct( ApiMain $mainModule, string $moduleName, string $modulePrefix = '' ) { |
| 316 |
$this->mMainModule = $mainModule; |
| 317 |
$this->mModuleName = $moduleName; |
| 318 |
$this->mModulePrefix = $modulePrefix; |
| 319 |
|
| 320 |
if ( !$this->isMain() ) { |
| 321 |
$this->setContext( $mainModule->getContext() ); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
abstract public function execute(); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function getModuleManager() { |
| 355 |
return null; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public function getCustomPrinter() { |
| 371 |
return null; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
protected function getExamplesMessages() { |
| 387 |
return []; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public function getHelpUrls() { |
| 398 |
return []; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
protected function getAllowedParams( ) { |
| 415 |
|
| 416 |
|
| 417 |
return []; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public function shouldCheckMaxlag() { |
| 427 |
return true; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
public function isReadMode() { |
| 437 |
return true; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
public function isWriteMode() { |
| 466 |
return false; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
public function mustBePosted() { |
| 479 |
return $this->needsToken() !== false; |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public function isDeprecated() { |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public function isInternal() { |
| 503 |
return false; |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public function needsToken() { |
| 526 |
return false; |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
protected function getWebUITokenSalt( array $params ) { |
| 540 |
return null; |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
. |
| 552 |
|
| 553 |
. |
| 554 |
|
| 555 |
|
| 556 |
public function getConditionalRequestData( $condition ) { |
| 557 |
return null; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public function getModuleName() { |
| 572 |
return $this->mModuleName; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public function getModulePrefix() { |
| 581 |
return $this->mModulePrefix; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public function getMain() { |
| 590 |
return $this->mMainModule; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
public function isMain() { |
| 600 |
return $this === $this->mMainModule; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
public function getParent() { |
| 611 |
return $this->isMain() ? null : $this->getMain(); |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
private function dieIfMain( string $methodName ) { |
| 622 |
if ( $this->isMain() ) { |
| 623 |
self::dieDebug( $methodName, 'base method was called on main module.' ); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
public function lacksSameOriginSecurity() { |
| 638 |
|
| 639 |
$this->dieIfMain( __METHOD__ ); |
| 640 |
|
| 641 |
return $this->getMain()->lacksSameOriginSecurity(); |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
public function getModulePath() { |
| 651 |
if ( $this->isMain() ) { |
| 652 |
return 'main'; |
| 653 |
} |
| 654 |
|
| 655 |
if ( $this->getParent()->isMain() ) { |
| 656 |
return $this->getModuleName(); |
| 657 |
} |
| 658 |
|
| 659 |
return $this->getParent()->getModulePath() . '+' . $this->getModuleName(); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
public function getModuleFromPath( $path ) { |
| 671 |
$module = $this->getMain(); |
| 672 |
if ( $path === 'main' ) { |
| 673 |
return $module; |
| 674 |
} |
| 675 |
|
| 676 |
$parts = explode( '+', $path ); |
| 677 |
if ( count( $parts ) === 1 ) { |
| 678 |
|
| 679 |
$parts = explode( ' ', $path ); |
| 680 |
} |
| 681 |
|
| 682 |
foreach ( $parts as $i => $v ) { |
| 683 |
$parent = $module; |
| 684 |
$manager = $parent->getModuleManager(); |
| 685 |
if ( $manager === null ) { |
| 686 |
$errorPath = implode( '+', array_slice( $parts, 0, $i ) ); |
| 687 |
$this->dieWithError( [ 'apierror-badmodule-nosubmodules', $errorPath ], 'badmodule' ); |
| 688 |
} |
| 689 |
$module = $manager->getModule( $v ); |
| 690 |
|
| 691 |
if ( $module === null ) { |
| 692 |
$errorPath = $i |
| 693 |
? implode( '+', array_slice( $parts, 0, $i ) ) |
| 694 |
: $parent->getModuleName(); |
| 695 |
$this->dieWithError( |
| 696 |
[ 'apierror-badmodule-badsubmodule', $errorPath, wfEscapeWikiText( $v ) ], |
| 697 |
'badmodule' |
| 698 |
); |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
return $module; |
| 703 |
} |
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
public function getResult() { |
| 711 |
|
| 712 |
$this->dieIfMain( __METHOD__ ); |
| 713 |
|
| 714 |
return $this->getMain()->getResult(); |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
public function getErrorFormatter() { |
| 722 |
|
| 723 |
$this->dieIfMain( __METHOD__ ); |
| 724 |
|
| 725 |
return $this->getMain()->getErrorFormatter(); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
protected function getDB() { |
| 735 |
if ( !isset( $this->mReplicaDB ) ) { |
| 736 |
$this->mReplicaDB = MediaWikiServices::getInstance() |
| 737 |
->getConnectionProvider() |
| 738 |
->getReplicaDatabase( false, 'api' ); |
| 739 |
} |
| 740 |
|
| 741 |
return $this->mReplicaDB; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
public function getContinuationManager() { |
| 748 |
|
| 749 |
$this->dieIfMain( __METHOD__ ); |
| 750 |
|
| 751 |
return $this->getMain()->getContinuationManager(); |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
public function setContinuationManager( ?ApiContinuationManager $manager = null ) { |
| 758 |
|
| 759 |
$this->dieIfMain( __METHOD__ ); |
| 760 |
|
| 761 |
$this->getMain()->setContinuationManager( $manager ); |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
protected function getPermissionManager(): PermissionManager { |
| 771 |
return MediaWikiServices::getInstance()->getPermissionManager(); |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
protected function getHookContainer() { |
| 781 |
if ( !$this->hookContainer ) { |
| 782 |
$this->hookContainer = MediaWikiServices::getInstance()->getHookContainer(); |
| 783 |
} |
| 784 |
return $this->hookContainer; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
protected function getHookRunner() { |
| 796 |
if ( !$this->hookRunner ) { |
| 797 |
$this->hookRunner = new ApiHookRunner( $this->getHookContainer() ); |
| 798 |
} |
| 799 |
return $this->hookRunner; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
public function dynamicParameterDocumentation() { |
| 818 |
return null; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
public function encodeParamName( $paramName ) { |
| 830 |
if ( is_array( $paramName ) ) { |
| 831 |
return array_map( function ( $name ) { |
| 832 |
return $this->mModulePrefix . $name; |
| 833 |
}, $paramName ); |
| 834 |
} |
| 835 |
|
| 836 |
return $this->mModulePrefix . $paramName; |
| 837 |
} |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
public function extractRequestParams( $options = [] ) { |
| 852 |
if ( is_bool( $options ) ) { |
| 853 |
$options = [ 'parseLimit' => $options ]; |
| 854 |
} |
| 855 |
$options += [ |
| 856 |
'parseLimit' => true, |
| 857 |
'safeMode' => false, |
| 858 |
]; |
| 859 |
|
| 860 |
|
| 861 |
$parseLimit = (bool)$options['parseLimit']; |
| 862 |
$cacheKey = (int)$parseLimit; |
| 863 |
|
| 864 |
|
| 865 |
if ( !isset( $this->mParamCache[$cacheKey] ) ) { |
| 866 |
$params = $this->getFinalParams() ?: []; |
| 867 |
$results = []; |
| 868 |
$warned = []; |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
$toProcess = []; |
| 873 |
foreach ( $params as $paramName => $paramSettings ) { |
| 874 |
if ( isset( $paramSettings[self::PARAM_TEMPLATE_VARS] ) ) { |
| 875 |
$toProcess[] = [ $paramName, $paramSettings[self::PARAM_TEMPLATE_VARS], $paramSettings ]; |
| 876 |
} else { |
| 877 |
try { |
| 878 |
$results[$paramName] = $this->getParameterFromSettings( |
| 879 |
$paramName, $paramSettings, $parseLimit |
| 880 |
); |
| 881 |
} catch ( ApiUsageException $ex ) { |
| 882 |
$results[$paramName] = $ex; |
| 883 |
} |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
while ( $toProcess ) { |
| 893 |
[ $name, $targets, $settings ] = array_shift( $toProcess ); |
| 894 |
|
| 895 |
foreach ( $targets as $placeholder => $target ) { |
| 896 |
if ( !array_key_exists( $target, $results ) ) { |
| 897 |
|
| 898 |
|
| 899 |
continue; |
| 900 |
} |
| 901 |
if ( !is_array( $results[$target] ) || !$results[$target] ) { |
| 902 |
|
| 903 |
|
| 904 |
break; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
unset( $targets[$placeholder] ); |
| 911 |
$placeholder = '{' . $placeholder . '}'; |
| 912 |
|
| 913 |
foreach ( $results[$target] as $value ) { |
| 914 |
if ( !preg_match( '/^[^{}]*$/', $value ) ) { |
| 915 |
|
| 916 |
$encTargetName = $this->encodeParamName( $target ); |
| 917 |
if ( !isset( $warned[$encTargetName][$value] ) ) { |
| 918 |
$warned[$encTargetName][$value] = true; |
| 919 |
$this->addWarning( [ |
| 920 |
'apiwarn-ignoring-invalid-templated-value', |
| 921 |
wfEscapeWikiText( $encTargetName ), |
| 922 |
wfEscapeWikiText( $value ), |
| 923 |
] ); |
| 924 |
} |
| 925 |
continue; |
| 926 |
} |
| 927 |
|
| 928 |
$newName = str_replace( $placeholder, $value, $name ); |
| 929 |
if ( !$targets ) { |
| 930 |
try { |
| 931 |
$results[$newName] = $this->getParameterFromSettings( |
| 932 |
$newName, |
| 933 |
$settings, |
| 934 |
$parseLimit |
| 935 |
); |
| 936 |
} catch ( ApiUsageException $ex ) { |
| 937 |
$results[$newName] = $ex; |
| 938 |
} |
| 939 |
} else { |
| 940 |
$newTargets = []; |
| 941 |
foreach ( $targets as $k => $v ) { |
| 942 |
$newTargets[$k] = str_replace( $placeholder, $value, $v ); |
| 943 |
} |
| 944 |
$toProcess[] = [ $newName, $newTargets, $settings ]; |
| 945 |
} |
| 946 |
} |
| 947 |
break; |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
$this->mParamCache[$cacheKey] = $results; |
| 952 |
} |
| 953 |
|
| 954 |
$ret = $this->mParamCache[$cacheKey]; |
| 955 |
if ( !$options['safeMode'] ) { |
| 956 |
foreach ( $ret as $v ) { |
| 957 |
if ( $v instanceof ApiUsageException ) { |
| 958 |
throw $v; |
| 959 |
} |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
return $this->mParamCache[$cacheKey]; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
protected function getParameter( $paramName, $parseLimit = true ) { |
| 974 |
$ret = $this->extractRequestParams( [ |
| 975 |
'parseLimit' => $parseLimit, |
| 976 |
'safeMode' => true, |
| 977 |
] )[$paramName]; |
| 978 |
if ( $ret instanceof ApiUsageException ) { |
| 979 |
throw $ret; |
| 980 |
} |
| 981 |
return $ret; |
| 982 |
} |
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
public function requireOnlyOneParameter( $params, ...$required ) { |
| 991 |
$intersection = array_intersect( array_keys( array_filter( $params, |
| 992 |
[ $this, 'parameterNotEmpty' ] ) ), $required ); |
| 993 |
|
| 994 |
if ( count( $intersection ) > 1 ) { |
| 995 |
$this->dieWithError( [ |
| 996 |
'apierror-invalidparammix', |
| 997 |
Message::listParam( array_map( |
| 998 |
function ( $p ) { |
| 999 |
return '<var>' . $this->encodeParamName( $p ) . '</var>'; |
| 1000 |
}, |
| 1001 |
array_values( $intersection ) |
| 1002 |
) ), |
| 1003 |
count( $intersection ), |
| 1004 |
] ); |
| 1005 |
} elseif ( count( $intersection ) == 0 ) { |
| 1006 |
$this->dieWithError( [ |
| 1007 |
'apierror-missingparam-one-of', |
| 1008 |
Message::listParam( array_map( |
| 1009 |
function ( $p ) { |
| 1010 |
return '<var>' . $this->encodeParamName( $p ) . '</var>'; |
| 1011 |
}, |
| 1012 |
$required |
| 1013 |
) ), |
| 1014 |
count( $required ), |
| 1015 |
], 'missingparam' ); |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
public function requireMaxOneParameter( $params, ...$required ) { |
| 1026 |
$intersection = array_intersect( array_keys( array_filter( $params, |
| 1027 |
[ $this, 'parameterNotEmpty' ] ) ), $required ); |
| 1028 |
|
| 1029 |
if ( count( $intersection ) > 1 ) { |
| 1030 |
$this->dieWithError( [ |
| 1031 |
'apierror-invalidparammix', |
| 1032 |
Message::listParam( array_map( |
| 1033 |
function ( $p ) { |
| 1034 |
return '<var>' . $this->encodeParamName( $p ) . '</var>'; |
| 1035 |
}, |
| 1036 |
array_values( $intersection ) |
| 1037 |
) ), |
| 1038 |
count( $intersection ), |
| 1039 |
] ); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
public function requireAtLeastOneParameter( $params, ...$required ) { |
| 1051 |
$intersection = array_intersect( |
| 1052 |
array_keys( array_filter( $params, [ $this, 'parameterNotEmpty' ] ) ), |
| 1053 |
$required |
| 1054 |
); |
| 1055 |
|
| 1056 |
if ( count( $intersection ) == 0 ) { |
| 1057 |
$this->dieWithError( [ |
| 1058 |
'apierror-missingparam-at-least-one-of', |
| 1059 |
Message::listParam( array_map( |
| 1060 |
function ( $p ) { |
| 1061 |
return '<var>' . $this->encodeParamName( $p ) . '</var>'; |
| 1062 |
}, |
| 1063 |
$required |
| 1064 |
) ), |
| 1065 |
count( $required ), |
| 1066 |
], 'missingparam' ); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
public function requirePostedParameters( $params, $prefix = 'prefix' ) { |
| 1079 |
if ( !$this->mustBePosted() ) { |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
wfDeprecatedMsg( 'mustBePosted() must be true when using requirePostedParameters()', |
| 1090 |
'1.42' ); |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
if ( $this->getConfig()->get( MainConfigNames::DebugAPI ) || |
| 1095 |
$this->getMain()->isInternalMode() ) { |
| 1096 |
return; |
| 1097 |
} |
| 1098 |
|
| 1099 |
$queryValues = $this->getRequest()->getQueryValuesOnly(); |
| 1100 |
$badParams = []; |
| 1101 |
foreach ( $params as $param ) { |
| 1102 |
if ( $prefix !== 'noprefix' ) { |
| 1103 |
$param = $this->encodeParamName( $param ); |
| 1104 |
} |
| 1105 |
if ( array_key_exists( $param, $queryValues ) ) { |
| 1106 |
$badParams[] = $param; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
if ( $badParams ) { |
| 1111 |
$this->dieWithError( |
| 1112 |
[ 'apierror-mustpostparams', implode( ', ', $badParams ), count( $badParams ) ] |
| 1113 |
); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
private function parameterNotEmpty( $x ) { |
| 1124 |
return $x !== null && $x !== false; |
| 1125 |
} |
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
public function getTitleOrPageId( $params, $load = false ) { |
| 1139 |
$this->requireOnlyOneParameter( $params, 'title', 'pageid' ); |
| 1140 |
|
| 1141 |
$pageObj = null; |
| 1142 |
if ( isset( $params['title'] ) ) { |
| 1143 |
$titleObj = Title::newFromText( $params['title'] ); |
| 1144 |
if ( !$titleObj || $titleObj->isExternal() ) { |
| 1145 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] ); |
| 1146 |
} |
| 1147 |
if ( !$titleObj->canExist() ) { |
| 1148 |
$this->dieWithError( 'apierror-pagecannotexist' ); |
| 1149 |
} |
| 1150 |
|
| 1151 |
$pageObj = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $titleObj ); |
| 1152 |
if ( $load !== false ) { |
| 1153 |
$pageObj->loadPageData( $load ); |
| 1154 |
} |
| 1155 |
} elseif ( isset( $params['pageid'] ) ) { |
| 1156 |
if ( $load === false ) { |
| 1157 |
$load = 'fromdb'; |
| 1158 |
} |
| 1159 |
$pageObj = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromID( $params['pageid'], $load ); |
| 1160 |
if ( !$pageObj ) { |
| 1161 |
$this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] ); |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
|
| 1166 |
return $pageObj; |
| 1167 |
} |
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
public function getTitleFromTitleOrPageId( $params ) { |
| 1178 |
$this->requireOnlyOneParameter( $params, 'title', 'pageid' ); |
| 1179 |
|
| 1180 |
$titleObj = null; |
| 1181 |
if ( isset( $params['title'] ) ) { |
| 1182 |
$titleObj = Title::newFromText( $params['title'] ); |
| 1183 |
if ( !$titleObj || $titleObj->isExternal() ) { |
| 1184 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] ); |
| 1185 |
} |
| 1186 |
|
| 1187 |
return $titleObj; |
| 1188 |
} |
| 1189 |
|
| 1190 |
if ( isset( $params['pageid'] ) ) { |
| 1191 |
$titleObj = Title::newFromID( $params['pageid'] ); |
| 1192 |
if ( !$titleObj ) { |
| 1193 |
$this->dieWithError( [ 'apierror-nosuchpageid', $params['pageid'] ] ); |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
|
| 1198 |
return $titleObj; |
| 1199 |
} |
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
protected function getParameterFromSettings( $name, $settings, $parseLimit ) { |
| 1211 |
$validator = $this->getMain()->getParamValidator(); |
| 1212 |
$value = $validator->getValue( $this, $name, $settings, [ |
| 1213 |
'parse-limit' => $parseLimit, |
| 1214 |
'raw' => ( $settings[ParamValidator::PARAM_TYPE] ?? '' ) === 'raw', |
| 1215 |
] ); |
| 1216 |
|
| 1217 |
|
| 1218 |
if ( $parseLimit && isset( $settings[ParamValidator::PARAM_TYPE] ) && |
| 1219 |
$settings[ParamValidator::PARAM_TYPE] === 'limit' && |
| 1220 |
$this->getMain()->getVal( $this->encodeParamName( $name ) ) === 'max' |
| 1221 |
) { |
| 1222 |
$this->getResult()->addParsedLimit( $this->getModuleName(), $value ); |
| 1223 |
} |
| 1224 |
|
| 1225 |
return $value; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
public function handleParamNormalization( $paramName, $value, $rawValue ) { |
| 1239 |
$this->addWarning( [ 'apiwarn-badutf8', $paramName ] ); |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
final public function validateToken( $token, array $params ) { |
| 1251 |
$tokenType = $this->needsToken(); |
| 1252 |
$salts = ApiQueryTokens::getTokenTypeSalts(); |
| 1253 |
if ( !isset( $salts[$tokenType] ) ) { |
| 1254 |
throw new LogicException( |
| 1255 |
"Module '{$this->getModuleName()}' tried to use token type '$tokenType' " . |
| 1256 |
'without registering it' |
| 1257 |
); |
| 1258 |
} |
| 1259 |
|
| 1260 |
$tokenObj = ApiQueryTokens::getToken( |
| 1261 |
$this->getUser(), $this->getRequest()->getSession(), $salts[$tokenType] |
| 1262 |
); |
| 1263 |
if ( $tokenObj->match( $token ) ) { |
| 1264 |
return true; |
| 1265 |
} |
| 1266 |
|
| 1267 |
$webUiSalt = $this->getWebUITokenSalt( $params ); |
| 1268 |
|
| 1269 |
return $webUiSalt !== null && $this->getUser()->matchEditToken( |
| 1270 |
$token, $webUiSalt, $this->getRequest() |
| 1271 |
); |
| 1272 |
} |
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
public function getWatchlistUser( $params ) { |
| 1287 |
if ( $params['owner'] !== null && $params['token'] !== null ) { |
| 1288 |
$services = MediaWikiServices::getInstance(); |
| 1289 |
$user = $services->getUserFactory()->newFromName( $params['owner'], UserRigorOptions::RIGOR_NONE ); |
| 1290 |
if ( !$user || !$user->isRegistered() ) { |
| 1291 |
$this->dieWithError( |
| 1292 |
[ 'nosuchusershort', wfEscapeWikiText( $params['owner'] ) ], 'bad_wlowner' |
| 1293 |
); |
| 1294 |
} |
| 1295 |
|
| 1296 |
$token = $services->getUserOptionsLookup()->getOption( $user, 'watchlisttoken' ); |
| 1297 |
if ( $token == '' || !hash_equals( $token, $params['token'] ) ) { |
| 1298 |
$this->dieWithError( 'apierror-bad-watchlist-token', 'bad_wltoken' ); |
| 1299 |
} |
| 1300 |
} else { |
| 1301 |
$user = $this->getUser(); |
| 1302 |
if ( !$user->isRegistered() ) { |
| 1303 |
$this->dieWithError( 'watchlistanontext', 'notloggedin' ); |
| 1304 |
} |
| 1305 |
$this->checkUserRightsAny( 'viewmywatchlist' ); |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
return $user; |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
public static function makeMessage( $msg, IContextSource $context, ?array $params = null ) { |
| 1327 |
wfDeprecated( __METHOD__, '1.43' ); |
| 1328 |
if ( is_string( $msg ) ) { |
| 1329 |
$msg = wfMessage( $msg ); |
| 1330 |
} elseif ( is_array( $msg ) ) { |
| 1331 |
$msg = wfMessage( ...$msg ); |
| 1332 |
} |
| 1333 |
if ( !$msg instanceof Message ) { |
| 1334 |
return null; |
| 1335 |
} |
| 1336 |
|
| 1337 |
$msg->setContext( $context ); |
| 1338 |
if ( $params ) { |
| 1339 |
$msg->params( $params ); |
| 1340 |
} |
| 1341 |
|
| 1342 |
return $msg; |
| 1343 |
} |
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
public function errorArrayToStatus( array $errors, ?Authority $performer = null ) { |
| 1360 |
wfDeprecated( __METHOD__, '1.43' ); |
| 1361 |
|
| 1362 |
$performer ??= $this->getAuthority(); |
| 1363 |
$block = $performer->getBlock(); |
| 1364 |
|
| 1365 |
$status = Status::newGood(); |
| 1366 |
foreach ( $errors as $error ) { |
| 1367 |
if ( !is_array( $error ) ) { |
| 1368 |
$error = [ $error ]; |
| 1369 |
} |
| 1370 |
|
| 1371 |
$head = reset( $error ); |
| 1372 |
$key = ( $head instanceof MessageSpecifier ) ? $head->getKey() : (string)$head; |
| 1373 |
|
| 1374 |
if ( isset( self::BLOCK_CODE_MAP[$key] ) && $block ) { |
| 1375 |
$status->fatal( ApiMessage::create( |
| 1376 |
$error, |
| 1377 |
$this->getBlockCode( $block ), |
| 1378 |
[ 'blockinfo' => $this->getBlockDetails( $block ) ] |
| 1379 |
) ); |
| 1380 |
} elseif ( isset( self::MESSAGE_CODE_MAP[$key] ) ) { |
| 1381 |
[ $msg, $code ] = self::MESSAGE_CODE_MAP[$key]; |
| 1382 |
$status->fatal( ApiMessage::create( $msg, $code ) ); |
| 1383 |
} else { |
| 1384 |
|
| 1385 |
$status->fatal( ...$error ); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
return $status; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
public function addBlockInfoToStatus( StatusValue $status, ?Authority $user = null ) { |
| 1399 |
if ( $status instanceof PermissionStatus ) { |
| 1400 |
$block = $status->getBlock(); |
| 1401 |
} else { |
| 1402 |
$user = $user ?: $this->getAuthority(); |
| 1403 |
$block = $user->getBlock(); |
| 1404 |
} |
| 1405 |
|
| 1406 |
if ( !$block ) { |
| 1407 |
return; |
| 1408 |
} |
| 1409 |
foreach ( $status->getMessages() as $msg ) { |
| 1410 |
if ( isset( self::BLOCK_CODE_MAP[$msg->getKey()] ) ) { |
| 1411 |
$status->replaceMessage( $msg->getKey(), ApiMessage::create( |
| 1412 |
Message::newFromSpecifier( $msg ), |
| 1413 |
$this->getBlockCode( $block ), |
| 1414 |
[ 'blockinfo' => $this->getBlockDetails( $block ) ] |
| 1415 |
) ); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
} |
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
protected function useTransactionalTimeLimit() { |
| 1426 |
if ( $this->getRequest()->wasPosted() ) { |
| 1427 |
wfTransactionalTimeLimit(); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
public static function clearCacheForTest(): void { |
| 1437 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 1438 |
throw new LogicException( 'Not allowed outside tests' ); |
| 1439 |
} |
| 1440 |
self::$filterIDsCache = []; |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
protected function filterIDs( $fields, array $ids ) { |
| 1453 |
$min = INF; |
| 1454 |
$max = 0; |
| 1455 |
foreach ( $fields as [ $table, $field ] ) { |
| 1456 |
if ( isset( self::$filterIDsCache[$table][$field] ) ) { |
| 1457 |
$row = self::$filterIDsCache[$table][$field]; |
| 1458 |
} else { |
| 1459 |
$row = $this->getDB()->newSelectQueryBuilder() |
| 1460 |
->select( [ 'min_id' => "MIN($field)", 'max_id' => "MAX($field)" ] ) |
| 1461 |
->from( $table ) |
| 1462 |
->caller( __METHOD__ )->fetchRow(); |
| 1463 |
self::$filterIDsCache[$table][$field] = $row; |
| 1464 |
} |
| 1465 |
$min = min( $min, $row->min_id ); |
| 1466 |
$max = max( $max, $row->max_id ); |
| 1467 |
} |
| 1468 |
return array_filter( $ids, static function ( $id ) use ( $min, $max ) { |
| 1469 |
return ( ( is_int( $id ) && $id >= 0 ) || ctype_digit( (string)$id ) ) |
| 1470 |
&& $id >= $min && $id <= $max; |
| 1471 |
} ); |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
public function addWarning( $msg, $code = null, $data = null ) { |
| 1496 |
$this->getErrorFormatter()->addWarning( $this->getModulePath(), $msg, $code, $data ); |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
public function addDeprecation( $msg, $feature, $data = [] ) { |
| 1510 |
$data = (array)$data; |
| 1511 |
if ( $feature !== null ) { |
| 1512 |
$data['feature'] = $feature; |
| 1513 |
$this->logFeatureUsage( $feature ); |
| 1514 |
} |
| 1515 |
$this->addWarning( $msg, 'deprecation', $data ); |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
$msgs = [ $this->msg( 'api-usage-mailinglist-ref' ) ]; |
| 1520 |
$this->getHookRunner()->onApiDeprecationHelp( $msgs ); |
| 1521 |
if ( count( $msgs ) > 1 ) { |
| 1522 |
$key = '$' . implode( ' $', range( 1, count( $msgs ) ) ); |
| 1523 |
$msg = ( new RawMessage( $key ) )->params( $msgs ); |
| 1524 |
} else { |
| 1525 |
$msg = reset( $msgs ); |
| 1526 |
} |
| 1527 |
$this->getMain()->addWarning( $msg, 'deprecation-help' ); |
| 1528 |
} |
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
public function addError( $msg, $code = null, $data = null ) { |
| 1543 |
$this->getErrorFormatter()->addError( $this->getModulePath(), $msg, $code, $data ); |
| 1544 |
} |
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
public function addMessagesFromStatus( |
| 1556 |
StatusValue $status, $types = [ 'warning', 'error' ], array $filter = [] |
| 1557 |
) { |
| 1558 |
$this->getErrorFormatter()->addMessagesFromStatus( |
| 1559 |
$this->getModulePath(), $status, $types, $filter |
| 1560 |
); |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
public function dieWithError( $msg, $code = null, $data = null, $httpCode = 0 ) { |
| 1578 |
throw ApiUsageException::newWithMessage( $this, $msg, $code, $data, $httpCode ); |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
public function dieWithException( Throwable $exception, array $options = [] ) { |
| 1591 |
$this->dieWithError( |
| 1592 |
$this->getErrorFormatter()->getMessageFromException( $exception, $options ) |
| 1593 |
); |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
public function dieBlocked( Block $block ) { |
| 1606 |
$blockErrorFormatter = MediaWikiServices::getInstance()->getFormatterFactory() |
| 1607 |
->getBlockErrorFormatter( $this->getContext() ); |
| 1608 |
|
| 1609 |
$msg = $blockErrorFormatter->getMessage( |
| 1610 |
$block, |
| 1611 |
$this->getUser(), |
| 1612 |
null, |
| 1613 |
$this->getRequest()->getIP() |
| 1614 |
); |
| 1615 |
|
| 1616 |
$this->dieWithError( |
| 1617 |
$msg, |
| 1618 |
$this->getBlockCode( $block ), |
| 1619 |
[ 'blockinfo' => $this->getBlockDetails( $block ) ] |
| 1620 |
); |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
public function dieStatus( StatusValue $status ) { |
| 1633 |
if ( $status->isGood() ) { |
| 1634 |
throw new InvalidArgumentException( 'Successful status passed to ApiBase::dieStatus' ); |
| 1635 |
} |
| 1636 |
|
| 1637 |
foreach ( self::MESSAGE_CODE_MAP as $msg => [ $apiMsg, $code ] ) { |
| 1638 |
if ( $status->hasMessage( $msg ) ) { |
| 1639 |
$status->replaceMessage( $msg, ApiMessage::create( $apiMsg, $code ) ); |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
if ( |
| 1644 |
$status instanceof PermissionStatus |
| 1645 |
&& $status->isRateLimitExceeded() |
| 1646 |
&& !$status->hasMessage( 'apierror-ratelimited' ) |
| 1647 |
) { |
| 1648 |
$status->fatal( ApiMessage::create( 'apierror-ratelimited', 'ratelimited' ) ); |
| 1649 |
} |
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
$status->setOK( false ); |
| 1654 |
if ( !$status->getMessages( 'error' ) ) { |
| 1655 |
$newStatus = Status::newGood(); |
| 1656 |
foreach ( $status->getMessages( 'warning' ) as $err ) { |
| 1657 |
$newStatus->fatal( $err ); |
| 1658 |
} |
| 1659 |
if ( !$newStatus->getMessages( 'error' ) ) { |
| 1660 |
$newStatus->fatal( 'unknownerror-nocode' ); |
| 1661 |
} |
| 1662 |
$status = $newStatus; |
| 1663 |
} |
| 1664 |
|
| 1665 |
$this->addBlockInfoToStatus( $status ); |
| 1666 |
|
| 1667 |
throw new ApiUsageException( $this, $status ); |
| 1668 |
} |
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
public function dieReadOnly() { |
| 1677 |
$this->dieWithError( |
| 1678 |
'apierror-readonly', |
| 1679 |
'readonly', |
| 1680 |
[ 'readonlyreason' => MediaWikiServices::getInstance()->getReadOnlyMode()->getReason() ] |
| 1681 |
); |
| 1682 |
} |
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
public function checkUserRightsAny( $rights ) { |
| 1693 |
$rights = (array)$rights; |
| 1694 |
if ( !$this->getAuthority()->isAllowedAny( ...$rights ) ) { |
| 1695 |
$this->dieWithError( [ 'apierror-permissiondenied', $this->msg( "action-{$rights[0]}" ) ] ); |
| 1696 |
} |
| 1697 |
} |
| 1698 |
|
| 1699 |
|
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
public function checkTitleUserPermissions( |
| 1716 |
PageIdentity $pageIdentity, |
| 1717 |
$actions, |
| 1718 |
array $options = [] |
| 1719 |
) { |
| 1720 |
$authority = $options['user'] ?? $this->getAuthority(); |
| 1721 |
$status = new PermissionStatus(); |
| 1722 |
foreach ( (array)$actions as $action ) { |
| 1723 |
if ( $this->isWriteMode() ) { |
| 1724 |
$authority->authorizeWrite( $action, $pageIdentity, $status ); |
| 1725 |
} else { |
| 1726 |
$authority->authorizeRead( $action, $pageIdentity, $status ); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
if ( !$status->isGood() ) { |
| 1730 |
if ( !empty( $options['autoblock'] ) ) { |
| 1731 |
$this->getUser()->spreadAnyEditBlock(); |
| 1732 |
} |
| 1733 |
$this->dieStatus( $status ); |
| 1734 |
} |
| 1735 |
} |
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
public function dieWithErrorOrDebug( $msg, $code = null, $data = null, $httpCode = null ) { |
| 1751 |
if ( $this->getConfig()->get( MainConfigNames::DebugAPI ) !== true ) { |
| 1752 |
$this->dieWithError( $msg, $code, $data, $httpCode ?? 0 ); |
| 1753 |
} else { |
| 1754 |
$this->addWarning( $msg, $code, $data ); |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
protected function parseContinueParamOrDie( string $continue, array $types ): array { |
| 1769 |
$cont = explode( '|', $continue ); |
| 1770 |
$this->dieContinueUsageIf( count( $cont ) != count( $types ) ); |
| 1771 |
|
| 1772 |
foreach ( $cont as $i => &$value ) { |
| 1773 |
switch ( $types[$i] ) { |
| 1774 |
case 'string': |
| 1775 |
|
| 1776 |
break; |
| 1777 |
case 'int': |
| 1778 |
$this->dieContinueUsageIf( $value !== (string)(int)$value ); |
| 1779 |
$value = (int)$value; |
| 1780 |
break; |
| 1781 |
case 'timestamp': |
| 1782 |
try { |
| 1783 |
$dbTs = $this->getDB()->timestamp( $value ); |
| 1784 |
} catch ( TimestampException $ex ) { |
| 1785 |
$dbTs = false; |
| 1786 |
} |
| 1787 |
$this->dieContinueUsageIf( $value !== $dbTs ); |
| 1788 |
break; |
| 1789 |
default: |
| 1790 |
throw new InvalidArgumentException( "Unknown type '{$types[$i]}'" ); |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
return $cont; |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
protected function dieContinueUsageIf( $condition ) { |
| 1808 |
if ( $condition ) { |
| 1809 |
$this->dieWithError( 'apierror-badcontinue' ); |
| 1810 |
} |
| 1811 |
} |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
protected static function dieDebug( $method, $message ) { |
| 1821 |
throw new MWException( "Internal error in $method: $message" ); |
| 1822 |
} |
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
public function logFeatureUsage( $feature ) { |
| 1832 |
static $loggedFeatures = []; |
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
if ( isset( $loggedFeatures[$feature] ) ) { |
| 1837 |
return; |
| 1838 |
} |
| 1839 |
$loggedFeatures[$feature] = true; |
| 1840 |
|
| 1841 |
$request = $this->getRequest(); |
| 1842 |
$ctx = [ |
| 1843 |
'feature' => $feature, |
| 1844 |
|
| 1845 |
'username' => str_replace( ' ', '_', $this->getUser()->getName() ), |
| 1846 |
'clientip' => $request->getIP(), |
| 1847 |
'referer' => (string)$request->getHeader( 'Referer' ), |
| 1848 |
'agent' => $this->getMain()->getUserAgent(), |
| 1849 |
]; |
| 1850 |
|
| 1851 |
|
| 1852 |
$s = '"' . addslashes( $ctx['feature'] ) . '"' . |
| 1853 |
' "' . wfUrlencode( $ctx['username'] ) . '"' . |
| 1854 |
' "' . $ctx['clientip'] . '"' . |
| 1855 |
' "' . addslashes( $ctx['referer'] ) . '"' . |
| 1856 |
' "' . addslashes( $ctx['agent'] ) . '"'; |
| 1857 |
|
| 1858 |
wfDebugLog( 'api-feature-usage', $s, 'private', $ctx ); |
| 1859 |
|
| 1860 |
$this->getHookRunner()->onApiLogFeatureUsage( |
| 1861 |
$feature, |
| 1862 |
[ |
| 1863 |
'userName' => $this->getUser()->getName(), |
| 1864 |
'userAgent' => $this->getMain()->getUserAgent(), |
| 1865 |
'ipAddress' => $request->getIP() |
| 1866 |
] |
| 1867 |
); |
| 1868 |
} |
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
|
| 1883 |
|
| 1884 |
|
| 1885 |
|
| 1886 |
protected function getSummaryMessage() { |
| 1887 |
return "apihelp-{$this->getModulePath()}-summary"; |
| 1888 |
} |
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
protected function getExtendedDescription() { |
| 1902 |
return [ [ |
| 1903 |
"apihelp-{$this->getModulePath()}-extended-description", |
| 1904 |
'api-help-no-extended-description', |
| 1905 |
] ]; |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
public function getFinalSummary() { |
| 1916 |
return $this->msg( |
| 1917 |
Message::newFromSpecifier( $this->getSummaryMessage() ), |
| 1918 |
$this->getModulePrefix(), |
| 1919 |
$this->getModuleName(), |
| 1920 |
$this->getModulePath(), |
| 1921 |
); |
| 1922 |
} |
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 1930 |
|
| 1931 |
public function getFinalDescription() { |
| 1932 |
$summary = $this->msg( |
| 1933 |
Message::newFromSpecifier( $this->getSummaryMessage() ), |
| 1934 |
$this->getModulePrefix(), |
| 1935 |
$this->getModuleName(), |
| 1936 |
$this->getModulePath(), |
| 1937 |
); |
| 1938 |
$extendedDesc = $this->getExtendedDescription(); |
| 1939 |
if ( is_array( $extendedDesc ) && is_array( $extendedDesc[0] ) ) { |
| 1940 |
|
| 1941 |
|
| 1942 |
$extendedDesc = Message::newFallbackSequence( $extendedDesc[0] ) |
| 1943 |
->params( array_slice( $extendedDesc, 1 ) ); |
| 1944 |
} |
| 1945 |
$extendedDesc = $this->msg( |
| 1946 |
Message::newFromSpecifier( $extendedDesc ), |
| 1947 |
$this->getModulePrefix(), |
| 1948 |
$this->getModuleName(), |
| 1949 |
$this->getModulePath(), |
| 1950 |
); |
| 1951 |
|
| 1952 |
$msgs = [ $summary, $extendedDesc ]; |
| 1953 |
|
| 1954 |
$this->getHookRunner()->onAPIGetDescriptionMessages( $this, $msgs ); |
| 1955 |
|
| 1956 |
return $msgs; |
| 1957 |
} |
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
public function getFinalParams( $flags = 0 ) { |
| 1968 |
|
| 1969 |
$params = $this->getAllowedParams( $flags ); |
| 1970 |
if ( !$params ) { |
| 1971 |
$params = []; |
| 1972 |
} |
| 1973 |
|
| 1974 |
if ( $this->needsToken() ) { |
| 1975 |
$params['token'] = [ |
| 1976 |
ParamValidator::PARAM_TYPE => 'string', |
| 1977 |
ParamValidator::PARAM_REQUIRED => true, |
| 1978 |
ParamValidator::PARAM_SENSITIVE => true, |
| 1979 |
self::PARAM_HELP_MSG => [ |
| 1980 |
'api-help-param-token', |
| 1981 |
$this->needsToken(), |
| 1982 |
], |
| 1983 |
] + ( $params['token'] ?? [] ); |
| 1984 |
} |
| 1985 |
|
| 1986 |
$this->getHookRunner()->onAPIGetAllowedParams( $this, $params, $flags ); |
| 1987 |
|
| 1988 |
return $params; |
| 1989 |
} |
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
public function getFinalParamDescription() { |
| 1999 |
$prefix = $this->getModulePrefix(); |
| 2000 |
$name = $this->getModuleName(); |
| 2001 |
$path = $this->getModulePath(); |
| 2002 |
|
| 2003 |
$params = $this->getFinalParams( self::GET_VALUES_FOR_HELP ); |
| 2004 |
$msgs = []; |
| 2005 |
foreach ( $params as $param => $settings ) { |
| 2006 |
if ( !is_array( $settings ) ) { |
| 2007 |
$settings = []; |
| 2008 |
} |
| 2009 |
|
| 2010 |
$msg = isset( $settings[self::PARAM_HELP_MSG] ) |
| 2011 |
? Message::newFromSpecifier( $settings[self::PARAM_HELP_MSG] ) |
| 2012 |
: Message::newFallbackSequence( [ "apihelp-$path-param-$param", 'api-help-param-no-description' ] ); |
| 2013 |
$msg = $this->msg( $msg, $prefix, $param, $name, $path ); |
| 2014 |
$msgs[$param] = [ $msg ]; |
| 2015 |
|
| 2016 |
if ( isset( $settings[ParamValidator::PARAM_TYPE] ) && |
| 2017 |
$settings[ParamValidator::PARAM_TYPE] === 'submodule' |
| 2018 |
) { |
| 2019 |
if ( isset( $settings[SubmoduleDef::PARAM_SUBMODULE_MAP] ) ) { |
| 2020 |
$map = $settings[SubmoduleDef::PARAM_SUBMODULE_MAP]; |
| 2021 |
} else { |
| 2022 |
$prefix = $this->isMain() ? '' : ( $this->getModulePath() . '+' ); |
| 2023 |
$map = []; |
| 2024 |
foreach ( $this->getModuleManager()->getNames( $param ) as $submoduleName ) { |
| 2025 |
$map[$submoduleName] = $prefix . $submoduleName; |
| 2026 |
} |
| 2027 |
} |
| 2028 |
|
| 2029 |
$submodules = []; |
| 2030 |
$submoduleFlags = []; |
| 2031 |
$submoduleNames = []; |
| 2032 |
foreach ( $map as $v => $m ) { |
| 2033 |
$isDeprecated = false; |
| 2034 |
$isInternal = false; |
| 2035 |
$summary = null; |
| 2036 |
try { |
| 2037 |
$submod = $this->getModuleFromPath( $m ); |
| 2038 |
if ( $submod ) { |
| 2039 |
$summary = $submod->getFinalSummary(); |
| 2040 |
$isDeprecated = $submod->isDeprecated(); |
| 2041 |
$isInternal = $submod->isInternal(); |
| 2042 |
} |
| 2043 |
} catch ( ApiUsageException $ex ) { |
| 2044 |
|
| 2045 |
} |
| 2046 |
if ( $summary ) { |
| 2047 |
$key = $summary->getKey(); |
| 2048 |
$params = $summary->getParams(); |
| 2049 |
} else { |
| 2050 |
$key = 'api-help-undocumented-module'; |
| 2051 |
$params = [ $m ]; |
| 2052 |
} |
| 2053 |
$m = new ApiHelpParamValueMessage( |
| 2054 |
"[[Special:ApiHelp/$m|$v]]", |
| 2055 |
$key, |
| 2056 |
$params, |
| 2057 |
$isDeprecated, |
| 2058 |
$isInternal |
| 2059 |
); |
| 2060 |
$submodules[] = $m->setContext( $this->getContext() ); |
| 2061 |
$submoduleFlags[] = ( $isDeprecated ? 1 : 0 ) | ( $isInternal ? 2 : 0 ); |
| 2062 |
$submoduleNames[] = $v; |
| 2063 |
} |
| 2064 |
|
| 2065 |
array_multisort( $submoduleFlags, $submoduleNames, $submodules ); |
| 2066 |
$msgs[$param] = array_merge( $msgs[$param], $submodules ); |
| 2067 |
} elseif ( isset( $settings[self::PARAM_HELP_MSG_PER_VALUE] ) ) { |
| 2068 |
if ( !is_array( $settings[self::PARAM_HELP_MSG_PER_VALUE] ) ) { |
| 2069 |
self::dieDebug( __METHOD__, |
| 2070 |
'ApiBase::PARAM_HELP_MSG_PER_VALUE is not valid' ); |
| 2071 |
} |
| 2072 |
$isArrayOfStrings = is_array( $settings[ParamValidator::PARAM_TYPE] ) |
| 2073 |
|| ( |
| 2074 |
$settings[ParamValidator::PARAM_TYPE] === 'string' |
| 2075 |
&& ( $settings[ParamValidator::PARAM_ISMULTI] ?? false ) |
| 2076 |
); |
| 2077 |
if ( !$isArrayOfStrings ) { |
| 2078 |
self::dieDebug( __METHOD__, |
| 2079 |
'ApiBase::PARAM_HELP_MSG_PER_VALUE may only be used when ' . |
| 2080 |
'ParamValidator::PARAM_TYPE is an array or it is \'string\' and ' . |
| 2081 |
'ParamValidator::PARAM_ISMULTI is true' ); |
| 2082 |
} |
| 2083 |
|
| 2084 |
$values = is_array( $settings[ParamValidator::PARAM_TYPE] ) ? |
| 2085 |
$settings[ParamValidator::PARAM_TYPE] : |
| 2086 |
array_keys( $settings[self::PARAM_HELP_MSG_PER_VALUE] ); |
| 2087 |
$valueMsgs = $settings[self::PARAM_HELP_MSG_PER_VALUE]; |
| 2088 |
$deprecatedValues = $settings[EnumDef::PARAM_DEPRECATED_VALUES] ?? []; |
| 2089 |
|
| 2090 |
foreach ( $values as $value ) { |
| 2091 |
$msg = Message::newFromSpecifier( $valueMsgs[$value] ?? "apihelp-$path-paramvalue-$param-$value" ); |
| 2092 |
$m = $this->msg( $msg, [ $prefix, $param, $name, $path, $value ] ); |
| 2093 |
$m = new ApiHelpParamValueMessage( |
| 2094 |
$value, |
| 2095 |
|
| 2096 |
[ $m->getKey(), 'api-help-param-no-description' ], |
| 2097 |
$m->getParams(), |
| 2098 |
isset( $deprecatedValues[$value] ) |
| 2099 |
); |
| 2100 |
$msgs[$param][] = $m->setContext( $this->getContext() ); |
| 2101 |
} |
| 2102 |
} |
| 2103 |
|
| 2104 |
if ( isset( $settings[self::PARAM_HELP_MSG_APPEND] ) ) { |
| 2105 |
if ( !is_array( $settings[self::PARAM_HELP_MSG_APPEND] ) ) { |
| 2106 |
self::dieDebug( __METHOD__, |
| 2107 |
'Value for ApiBase::PARAM_HELP_MSG_APPEND is not an array' ); |
| 2108 |
} |
| 2109 |
foreach ( $settings[self::PARAM_HELP_MSG_APPEND] as $m ) { |
| 2110 |
$m = $this->msg( Message::newFromSpecifier( $m ), [ $prefix, $param, $name, $path ] ); |
| 2111 |
$msgs[$param][] = $m; |
| 2112 |
} |
| 2113 |
} |
| 2114 |
} |
| 2115 |
|
| 2116 |
$this->getHookRunner()->onAPIGetParamDescriptionMessages( $this, $msgs ); |
| 2117 |
|
| 2118 |
return $msgs; |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
protected function getHelpFlags() { |
| 2131 |
$flags = []; |
| 2132 |
|
| 2133 |
if ( $this->isDeprecated() ) { |
| 2134 |
$flags[] = 'deprecated'; |
| 2135 |
} |
| 2136 |
if ( $this->isInternal() ) { |
| 2137 |
$flags[] = 'internal'; |
| 2138 |
} |
| 2139 |
if ( $this->isReadMode() ) { |
| 2140 |
$flags[] = 'readrights'; |
| 2141 |
} |
| 2142 |
if ( $this->isWriteMode() ) { |
| 2143 |
$flags[] = 'writerights'; |
| 2144 |
} |
| 2145 |
if ( $this->mustBePosted() ) { |
| 2146 |
$flags[] = 'mustbeposted'; |
| 2147 |
} |
| 2148 |
|
| 2149 |
return $flags; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
protected function getModuleSourceInfo() { |
| 2164 |
if ( $this->mModuleSource !== false ) { |
| 2165 |
return $this->mModuleSource; |
| 2166 |
} |
| 2167 |
|
| 2168 |
|
| 2169 |
$rClass = new ReflectionClass( $this ); |
| 2170 |
$path = $rClass->getFileName(); |
| 2171 |
if ( !$path ) { |
| 2172 |
|
| 2173 |
$this->mModuleSource = null; |
| 2174 |
return null; |
| 2175 |
} |
| 2176 |
$path = realpath( $path ) ?: $path; |
| 2177 |
|
| 2178 |
|
| 2179 |
if ( self::$extensionInfo === null ) { |
| 2180 |
$extDir = $this->getConfig()->get( MainConfigNames::ExtensionDirectory ); |
| 2181 |
$baseDir = MW_INSTALL_PATH; |
| 2182 |
self::$extensionInfo = [ |
| 2183 |
realpath( __DIR__ ) ?: __DIR__ => [ |
| 2184 |
'path' => $baseDir, |
| 2185 |
'name' => 'MediaWiki', |
| 2186 |
'license-name' => 'GPL-2.0-or-later', |
| 2187 |
], |
| 2188 |
realpath( "$baseDir/extensions" ) ?: "$baseDir/extensions" => null, |
| 2189 |
realpath( $extDir ) ?: $extDir => null, |
| 2190 |
]; |
| 2191 |
$keep = [ |
| 2192 |
'path' => null, |
| 2193 |
'name' => null, |
| 2194 |
'namemsg' => null, |
| 2195 |
'license-name' => null, |
| 2196 |
]; |
| 2197 |
$credits = SpecialVersion::getCredits( ExtensionRegistry::getInstance(), $this->getConfig() ); |
| 2198 |
foreach ( $credits as $group ) { |
| 2199 |
foreach ( $group as $ext ) { |
| 2200 |
if ( !isset( $ext['path'] ) || !isset( $ext['name'] ) ) { |
| 2201 |
|
| 2202 |
continue; |
| 2203 |
} |
| 2204 |
|
| 2205 |
$extpath = $ext['path']; |
| 2206 |
if ( !is_dir( $extpath ) ) { |
| 2207 |
$extpath = dirname( $extpath ); |
| 2208 |
} |
| 2209 |
self::$extensionInfo[realpath( $extpath ) ?: $extpath] = |
| 2210 |
array_intersect_key( $ext, $keep ); |
| 2211 |
} |
| 2212 |
} |
| 2213 |
} |
| 2214 |
|
| 2215 |
|
| 2216 |
do { |
| 2217 |
if ( array_key_exists( $path, self::$extensionInfo ) ) { |
| 2218 |
|
| 2219 |
$this->mModuleSource = self::$extensionInfo[$path]; |
| 2220 |
return $this->mModuleSource; |
| 2221 |
} |
| 2222 |
|
| 2223 |
$oldpath = $path; |
| 2224 |
$path = dirname( $path ); |
| 2225 |
} while ( $path !== $oldpath ); |
| 2226 |
|
| 2227 |
|
| 2228 |
$this->mModuleSource = null; |
| 2229 |
return null; |
| 2230 |
} |
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
public function modifyHelp( array &$help, array $options, array &$tocData ) { |
| 2245 |
} |
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
} |
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
class_alias( ApiBase::class, 'ApiBase' ); |