| 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 MediaWiki\Api\Validator\SubmoduleDef; |
| 26 |
use MediaWiki\Cache\GenderCache; |
| 27 |
use MediaWiki\Cache\LinkBatch; |
| 28 |
use MediaWiki\Cache\LinkBatchFactory; |
| 29 |
use MediaWiki\Cache\LinkCache; |
| 30 |
use MediaWiki\Context\DerivativeContext; |
| 31 |
use MediaWiki\Language\ILanguageConverter; |
| 32 |
use MediaWiki\Language\Language; |
| 33 |
use MediaWiki\Language\LanguageConverter; |
| 34 |
use MediaWiki\Linker\LinkTarget; |
| 35 |
use MediaWiki\MainConfigNames; |
| 36 |
use MediaWiki\MediaWikiServices; |
| 37 |
use MediaWiki\Message\Message; |
| 38 |
use MediaWiki\Page\PageIdentity; |
| 39 |
use MediaWiki\Page\PageReference; |
| 40 |
use MediaWiki\Request\FauxRequest; |
| 41 |
use MediaWiki\SpecialPage\RedirectSpecialArticle; |
| 42 |
use MediaWiki\SpecialPage\SpecialPageFactory; |
| 43 |
use MediaWiki\Title\MalformedTitleException; |
| 44 |
use MediaWiki\Title\NamespaceInfo; |
| 45 |
use MediaWiki\Title\Title; |
| 46 |
use MediaWiki\Title\TitleFactory; |
| 47 |
use stdClass; |
| 48 |
use Wikimedia\ParamValidator\ParamValidator; |
| 49 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 50 |
use Wikimedia\Rdbms\IResultWrapper; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
class ApiPageSet extends ApiBase { |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private const DISABLE_GENERATORS = 1; |
| 72 |
|
| 73 |
|
| 74 |
private $mDbSource; |
| 75 |
|
| 76 |
|
| 77 |
private $mParams; |
| 78 |
|
| 79 |
|
| 80 |
private $mResolveRedirects; |
| 81 |
|
| 82 |
|
| 83 |
private $mConvertTitles; |
| 84 |
|
| 85 |
|
| 86 |
private $mAllowGenerator; |
| 87 |
|
| 88 |
|
| 89 |
private $mAllPages = []; |
| 90 |
|
| 91 |
|
| 92 |
private $mTitles = []; |
| 93 |
|
| 94 |
|
| 95 |
private $mGoodAndMissingPages = []; |
| 96 |
|
| 97 |
|
| 98 |
private $mGoodPages = []; |
| 99 |
|
| 100 |
|
| 101 |
private $mGoodTitles = []; |
| 102 |
|
| 103 |
|
| 104 |
private $mMissingPages = []; |
| 105 |
|
| 106 |
|
| 107 |
private $mMissingTitles = []; |
| 108 |
|
| 109 |
|
| 110 |
private $mInvalidTitles = []; |
| 111 |
|
| 112 |
|
| 113 |
private $mMissingPageIDs = []; |
| 114 |
|
| 115 |
|
| 116 |
private $mRedirectTitles = []; |
| 117 |
|
| 118 |
|
| 119 |
private $mSpecialTitles = []; |
| 120 |
|
| 121 |
|
| 122 |
private $mAllSpecials = []; |
| 123 |
|
| 124 |
|
| 125 |
private $mNormalizedTitles = []; |
| 126 |
|
| 127 |
|
| 128 |
private $mInterwikiTitles = []; |
| 129 |
|
| 130 |
|
| 131 |
private $mPendingRedirectIDs = []; |
| 132 |
|
| 133 |
|
| 134 |
private $mPendingRedirectSpecialPages = []; |
| 135 |
|
| 136 |
|
| 137 |
private $mResolvedRedirectTitles = []; |
| 138 |
|
| 139 |
|
| 140 |
private $mConvertedTitles = []; |
| 141 |
|
| 142 |
|
| 143 |
private $mGoodRevIDs = []; |
| 144 |
|
| 145 |
|
| 146 |
private $mLiveRevIDs = []; |
| 147 |
|
| 148 |
|
| 149 |
private $mDeletedRevIDs = []; |
| 150 |
|
| 151 |
|
| 152 |
private $mMissingRevIDs = []; |
| 153 |
|
| 154 |
|
| 155 |
private $mGeneratorData = []; |
| 156 |
|
| 157 |
|
| 158 |
private $mFakePageId = -1; |
| 159 |
|
| 160 |
|
| 161 |
private $mCacheMode = 'public'; |
| 162 |
|
| 163 |
|
| 164 |
private $mRequestedPageFields = []; |
| 165 |
|
| 166 |
|
| 167 |
private $mDefaultNamespace; |
| 168 |
|
| 169 |
|
| 170 |
private $mRedirectMergePolicy; |
| 171 |
|
| 172 |
|
| 173 |
private static $generators = null; |
| 174 |
|
| 175 |
private Language $contentLanguage; |
| 176 |
private LinkCache $linkCache; |
| 177 |
private NamespaceInfo $namespaceInfo; |
| 178 |
private GenderCache $genderCache; |
| 179 |
private LinkBatchFactory $linkBatchFactory; |
| 180 |
private TitleFactory $titleFactory; |
| 181 |
private ILanguageConverter $languageConverter; |
| 182 |
private SpecialPageFactory $specialPageFactory; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
private static function addValues( array &$result, $values, $flags = [], $name = null ) { |
| 192 |
foreach ( $values as $val ) { |
| 193 |
if ( $val instanceof Title ) { |
| 194 |
$v = []; |
| 195 |
ApiQueryBase::addTitleInfo( $v, $val ); |
| 196 |
} elseif ( $name !== null ) { |
| 197 |
$v = [ $name => $val ]; |
| 198 |
} else { |
| 199 |
$v = $val; |
| 200 |
} |
| 201 |
foreach ( $flags as $flag ) { |
| 202 |
$v[$flag] = true; |
| 203 |
} |
| 204 |
$result[] = $v; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function __construct( ApiBase $dbSource, $flags = 0, $defaultNamespace = NS_MAIN ) { |
| 216 |
parent::__construct( $dbSource->getMain(), $dbSource->getModuleName() ); |
| 217 |
$this->mDbSource = $dbSource; |
| 218 |
$this->mAllowGenerator = ( $flags & self::DISABLE_GENERATORS ) == 0; |
| 219 |
$this->mDefaultNamespace = $defaultNamespace; |
| 220 |
|
| 221 |
$this->mParams = $this->extractRequestParams(); |
| 222 |
$this->mResolveRedirects = $this->mParams['redirects']; |
| 223 |
$this->mConvertTitles = $this->mParams['converttitles']; |
| 224 |
|
| 225 |
|
| 226 |
$services = MediaWikiServices::getInstance(); |
| 227 |
$this->contentLanguage = $services->getContentLanguage(); |
| 228 |
$this->linkCache = $services->getLinkCache(); |
| 229 |
$this->namespaceInfo = $services->getNamespaceInfo(); |
| 230 |
$this->genderCache = $services->getGenderCache(); |
| 231 |
$this->linkBatchFactory = $services->getLinkBatchFactory(); |
| 232 |
$this->titleFactory = $services->getTitleFactory(); |
| 233 |
$this->languageConverter = $services->getLanguageConverterFactory() |
| 234 |
->getLanguageConverter( $this->contentLanguage ); |
| 235 |
$this->specialPageFactory = $services->getSpecialPageFactory(); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function executeDryRun() { |
| 243 |
$this->executeInternal( true ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public function execute() { |
| 250 |
$this->executeInternal( false ); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
private function executeInternal( $isDryRun ) { |
| 259 |
$generatorName = $this->mAllowGenerator ? $this->mParams['generator'] : null; |
| 260 |
if ( isset( $generatorName ) ) { |
| 261 |
$dbSource = $this->mDbSource; |
| 262 |
if ( !$dbSource instanceof ApiQuery ) { |
| 263 |
|
| 264 |
$dbSource = $this->getMain()->getModuleManager()->getModule( 'query' ); |
| 265 |
} |
| 266 |
$generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true ); |
| 267 |
if ( $generator === null ) { |
| 268 |
$this->dieWithError( [ 'apierror-badgenerator-unknown', $generatorName ], 'badgenerator' ); |
| 269 |
} |
| 270 |
if ( !$generator instanceof ApiQueryGeneratorBase ) { |
| 271 |
$this->dieWithError( [ 'apierror-badgenerator-notgenerator', $generatorName ], 'badgenerator' ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
$tmpPageSet = new ApiPageSet( $dbSource, self::DISABLE_GENERATORS ); |
| 277 |
$generator->setGeneratorMode( $tmpPageSet ); |
| 278 |
$this->mCacheMode = $generator->getCacheMode( $generator->extractRequestParams() ); |
| 279 |
|
| 280 |
if ( !$isDryRun ) { |
| 281 |
$generator->requestExtraData( $tmpPageSet ); |
| 282 |
} |
| 283 |
$tmpPageSet->executeInternal( $isDryRun ); |
| 284 |
|
| 285 |
|
| 286 |
if ( !$isDryRun ) { |
| 287 |
$generator->executeGenerator( $this ); |
| 288 |
|
| 289 |
|
| 290 |
$this->getHookRunner()->onAPIQueryGeneratorAfterExecute( $generator, $this ); |
| 291 |
} else { |
| 292 |
|
| 293 |
$main = $this->getMain(); |
| 294 |
foreach ( $generator->extractRequestParams() as $paramName => $param ) { |
| 295 |
$main->markParamsUsed( $generator->encodeParamName( $paramName ) ); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if ( !$isDryRun ) { |
| 300 |
$this->resolvePendingRedirects(); |
| 301 |
} |
| 302 |
} else { |
| 303 |
|
| 304 |
$dataSource = null; |
| 305 |
if ( isset( $this->mParams['titles'] ) ) { |
| 306 |
$dataSource = 'titles'; |
| 307 |
} |
| 308 |
if ( isset( $this->mParams['pageids'] ) ) { |
| 309 |
if ( isset( $dataSource ) ) { |
| 310 |
$this->dieWithError( |
| 311 |
[ |
| 312 |
'apierror-invalidparammix-cannotusewith', |
| 313 |
$this->encodeParamName( 'pageids' ), |
| 314 |
$this->encodeParamName( $dataSource ) |
| 315 |
], |
| 316 |
'multisource' |
| 317 |
); |
| 318 |
} |
| 319 |
$dataSource = 'pageids'; |
| 320 |
} |
| 321 |
if ( isset( $this->mParams['revids'] ) ) { |
| 322 |
if ( isset( $dataSource ) ) { |
| 323 |
$this->dieWithError( |
| 324 |
[ |
| 325 |
'apierror-invalidparammix-cannotusewith', |
| 326 |
$this->encodeParamName( 'revids' ), |
| 327 |
$this->encodeParamName( $dataSource ) |
| 328 |
], |
| 329 |
'multisource' |
| 330 |
); |
| 331 |
} |
| 332 |
$dataSource = 'revids'; |
| 333 |
} |
| 334 |
|
| 335 |
if ( !$isDryRun ) { |
| 336 |
|
| 337 |
switch ( $dataSource ) { |
| 338 |
case 'titles': |
| 339 |
$this->initFromTitles( $this->mParams['titles'] ); |
| 340 |
break; |
| 341 |
case 'pageids': |
| 342 |
$this->initFromPageIds( $this->mParams['pageids'] ); |
| 343 |
break; |
| 344 |
case 'revids': |
| 345 |
if ( $this->mResolveRedirects ) { |
| 346 |
$this->addWarning( 'apiwarn-redirectsandrevids' ); |
| 347 |
} |
| 348 |
$this->mResolveRedirects = false; |
| 349 |
$this->initFromRevIDs( $this->mParams['revids'] ); |
| 350 |
break; |
| 351 |
default: |
| 352 |
|
| 353 |
break; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function isResolvingRedirects() { |
| 364 |
return $this->mResolveRedirects; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public function getDataSource() { |
| 376 |
if ( $this->mAllowGenerator && isset( $this->mParams['generator'] ) ) { |
| 377 |
return 'generator'; |
| 378 |
} |
| 379 |
if ( isset( $this->mParams['titles'] ) ) { |
| 380 |
return 'titles'; |
| 381 |
} |
| 382 |
if ( isset( $this->mParams['pageids'] ) ) { |
| 383 |
return 'pageids'; |
| 384 |
} |
| 385 |
if ( isset( $this->mParams['revids'] ) ) { |
| 386 |
return 'revids'; |
| 387 |
} |
| 388 |
|
| 389 |
return null; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
public function requestField( $fieldName ) { |
| 398 |
$this->mRequestedPageFields[$fieldName] = []; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function getCustomField( $fieldName ) { |
| 408 |
return $this->mRequestedPageFields[$fieldName]; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
public function getPageTableFields() { |
| 418 |
|
| 419 |
|
| 420 |
$pageFlds = [ |
| 421 |
'page_namespace' => null, |
| 422 |
'page_title' => null, |
| 423 |
'page_id' => null, |
| 424 |
]; |
| 425 |
|
| 426 |
if ( $this->mResolveRedirects ) { |
| 427 |
$pageFlds['page_is_redirect'] = null; |
| 428 |
} |
| 429 |
|
| 430 |
$pageFlds['page_content_model'] = null; |
| 431 |
|
| 432 |
if ( $this->getConfig()->get( MainConfigNames::PageLanguageUseDB ) ) { |
| 433 |
$pageFlds['page_lang'] = null; |
| 434 |
} |
| 435 |
|
| 436 |
foreach ( LinkCache::getSelectFields() as $field ) { |
| 437 |
$pageFlds[$field] = null; |
| 438 |
} |
| 439 |
|
| 440 |
$pageFlds = array_merge( $pageFlds, $this->mRequestedPageFields ); |
| 441 |
|
| 442 |
return array_keys( $pageFlds ); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
public function getAllTitlesByNamespace() { |
| 452 |
return $this->mAllPages; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public function getTitles() { |
| 464 |
wfDeprecated( __METHOD__, '1.37' ); |
| 465 |
return $this->mTitles; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public function getPages(): array { |
| 477 |
return $this->mTitles; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
public function getTitleCount() { |
| 485 |
return count( $this->mTitles ); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
public function getGoodTitlesByNamespace() { |
| 493 |
return $this->mGoodPages; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
public function getGoodTitles() { |
| 503 |
wfDeprecated( __METHOD__, '1.37' ); |
| 504 |
return $this->mGoodTitles; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
public function getGoodPages(): array { |
| 514 |
return $this->mGoodTitles; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
public function getGoodTitleCount() { |
| 522 |
return count( $this->mGoodTitles ); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
public function getMissingTitlesByNamespace() { |
| 531 |
return $this->mMissingPages; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public function getMissingTitles() { |
| 542 |
wfDeprecated( __METHOD__, '1.37' ); |
| 543 |
return $this->mMissingTitles; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
public function getMissingPages(): array { |
| 554 |
return $this->mMissingTitles; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
public function getGoodAndMissingTitlesByNamespace() { |
| 562 |
return $this->mGoodAndMissingPages; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
public function getGoodAndMissingTitles() { |
| 571 |
wfDeprecated( __METHOD__, '1.37' ); |
| 572 |
return $this->mGoodTitles + $this->mMissingTitles; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public function getGoodAndMissingPages(): array { |
| 581 |
return $this->mGoodTitles + $this->mMissingTitles; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
public function getInvalidTitlesAndReasons() { |
| 590 |
return $this->mInvalidTitles; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
public function getMissingPageIDs() { |
| 598 |
return $this->mMissingPageIDs; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
public function getRedirectTitles() { |
| 608 |
wfDeprecated( __METHOD__, '1.37' ); |
| 609 |
return $this->mRedirectTitles; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
public function getRedirectTargets(): array { |
| 619 |
return $this->mRedirectTitles; |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public function getRedirectTitlesAsResult( $result = null ) { |
| 630 |
$values = []; |
| 631 |
foreach ( $this->mRedirectTitles as $titleStrFrom => $titleTo ) { |
| 632 |
$r = [ |
| 633 |
'from' => strval( $titleStrFrom ), |
| 634 |
'to' => $titleTo->getPrefixedText(), |
| 635 |
]; |
| 636 |
if ( $titleTo->hasFragment() ) { |
| 637 |
$r['tofragment'] = $titleTo->getFragment(); |
| 638 |
} |
| 639 |
if ( $titleTo->isExternal() ) { |
| 640 |
$r['tointerwiki'] = $titleTo->getInterwiki(); |
| 641 |
} |
| 642 |
if ( isset( $this->mResolvedRedirectTitles[$titleStrFrom] ) ) { |
| 643 |
$titleFrom = $this->mResolvedRedirectTitles[$titleStrFrom]; |
| 644 |
$ns = $titleFrom->getNamespace(); |
| 645 |
$dbkey = $titleFrom->getDBkey(); |
| 646 |
if ( isset( $this->mGeneratorData[$ns][$dbkey] ) ) { |
| 647 |
$r = array_merge( $this->mGeneratorData[$ns][$dbkey], $r ); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
$values[] = $r; |
| 652 |
} |
| 653 |
if ( $values && $result ) { |
| 654 |
ApiResult::setIndexedTagName( $values, 'r' ); |
| 655 |
} |
| 656 |
|
| 657 |
return $values; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public function getNormalizedTitles() { |
| 666 |
return $this->mNormalizedTitles; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
public function getNormalizedTitlesAsResult( $result = null ) { |
| 677 |
$values = []; |
| 678 |
foreach ( $this->getNormalizedTitles() as $rawTitleStr => $titleStr ) { |
| 679 |
$encode = $this->contentLanguage->normalize( $rawTitleStr ) !== $rawTitleStr; |
| 680 |
$values[] = [ |
| 681 |
'fromencoded' => $encode, |
| 682 |
'from' => $encode ? rawurlencode( $rawTitleStr ) : $rawTitleStr, |
| 683 |
'to' => $titleStr |
| 684 |
]; |
| 685 |
} |
| 686 |
if ( $values && $result ) { |
| 687 |
ApiResult::setIndexedTagName( $values, 'n' ); |
| 688 |
} |
| 689 |
|
| 690 |
return $values; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
public function getConvertedTitles() { |
| 699 |
return $this->mConvertedTitles; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
public function getConvertedTitlesAsResult( $result = null ) { |
| 710 |
$values = []; |
| 711 |
foreach ( $this->getConvertedTitles() as $rawTitleStr => $titleStr ) { |
| 712 |
$values[] = [ |
| 713 |
'from' => $rawTitleStr, |
| 714 |
'to' => $titleStr |
| 715 |
]; |
| 716 |
} |
| 717 |
if ( $values && $result ) { |
| 718 |
ApiResult::setIndexedTagName( $values, 'c' ); |
| 719 |
} |
| 720 |
|
| 721 |
return $values; |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
public function getInterwikiTitles() { |
| 730 |
return $this->mInterwikiTitles; |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
public function getInterwikiTitlesAsResult( $result = null, $iwUrl = false ) { |
| 742 |
$values = []; |
| 743 |
foreach ( $this->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) { |
| 744 |
$item = [ |
| 745 |
'title' => $rawTitleStr, |
| 746 |
'iw' => $interwikiStr, |
| 747 |
]; |
| 748 |
if ( $iwUrl ) { |
| 749 |
$title = $this->titleFactory->newFromText( $rawTitleStr ); |
| 750 |
$item['url'] = $title->getFullURL( '', false, PROTO_CURRENT ); |
| 751 |
} |
| 752 |
$values[] = $item; |
| 753 |
} |
| 754 |
if ( $values && $result ) { |
| 755 |
ApiResult::setIndexedTagName( $values, 'i' ); |
| 756 |
} |
| 757 |
|
| 758 |
return $values; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
public function getInvalidTitlesAndRevisions( $invalidChecks = [ 'invalidTitles', |
| 776 |
'special', 'missingIds', 'missingRevIds', 'missingTitles', 'interwikiTitles' ] |
| 777 |
) { |
| 778 |
$result = []; |
| 779 |
if ( in_array( 'invalidTitles', $invalidChecks ) ) { |
| 780 |
self::addValues( $result, $this->getInvalidTitlesAndReasons(), [ 'invalid' ] ); |
| 781 |
} |
| 782 |
if ( in_array( 'special', $invalidChecks ) ) { |
| 783 |
$known = []; |
| 784 |
$unknown = []; |
| 785 |
foreach ( $this->mSpecialTitles as $title ) { |
| 786 |
if ( $title->isKnown() ) { |
| 787 |
$known[] = $title; |
| 788 |
} else { |
| 789 |
$unknown[] = $title; |
| 790 |
} |
| 791 |
} |
| 792 |
self::addValues( $result, $unknown, [ 'special', 'missing' ] ); |
| 793 |
self::addValues( $result, $known, [ 'special' ] ); |
| 794 |
} |
| 795 |
if ( in_array( 'missingIds', $invalidChecks ) ) { |
| 796 |
self::addValues( $result, $this->getMissingPageIDs(), [ 'missing' ], 'pageid' ); |
| 797 |
} |
| 798 |
if ( in_array( 'missingRevIds', $invalidChecks ) ) { |
| 799 |
self::addValues( $result, $this->getMissingRevisionIDs(), [ 'missing' ], 'revid' ); |
| 800 |
} |
| 801 |
if ( in_array( 'missingTitles', $invalidChecks ) ) { |
| 802 |
$known = []; |
| 803 |
$unknown = []; |
| 804 |
foreach ( $this->mMissingTitles as $title ) { |
| 805 |
if ( $title->isKnown() ) { |
| 806 |
$known[] = $title; |
| 807 |
} else { |
| 808 |
$unknown[] = $title; |
| 809 |
} |
| 810 |
} |
| 811 |
self::addValues( $result, $unknown, [ 'missing' ] ); |
| 812 |
self::addValues( $result, $known, [ 'missing', 'known' ] ); |
| 813 |
} |
| 814 |
if ( in_array( 'interwikiTitles', $invalidChecks ) ) { |
| 815 |
self::addValues( $result, $this->getInterwikiTitlesAsResult() ); |
| 816 |
} |
| 817 |
|
| 818 |
return $result; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
public function getRevisionIDs() { |
| 826 |
return $this->mGoodRevIDs; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
public function getLiveRevisionIDs() { |
| 834 |
return $this->mLiveRevIDs; |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
public function getDeletedRevisionIDs() { |
| 842 |
return $this->mDeletedRevIDs; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
public function getMissingRevisionIDs() { |
| 850 |
return $this->mMissingRevIDs; |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
public function getMissingRevisionIDsAsResult( $result = null ) { |
| 860 |
$values = []; |
| 861 |
foreach ( $this->getMissingRevisionIDs() as $revid ) { |
| 862 |
$values[$revid] = [ |
| 863 |
'revid' => $revid, |
| 864 |
'missing' => true, |
| 865 |
]; |
| 866 |
} |
| 867 |
if ( $values && $result ) { |
| 868 |
ApiResult::setIndexedTagName( $values, 'rev' ); |
| 869 |
} |
| 870 |
|
| 871 |
return $values; |
| 872 |
} |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
public function getSpecialTitles() { |
| 880 |
wfDeprecated( __METHOD__, '1.37' ); |
| 881 |
return $this->mSpecialTitles; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
public function getSpecialPages(): array { |
| 890 |
return $this->mSpecialTitles; |
| 891 |
} |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
public function getRevisionCount() { |
| 898 |
return count( $this->getRevisionIDs() ); |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
public function populateFromTitles( $titles ) { |
| 906 |
$this->initFromTitles( $titles ); |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
public function populateFromPageIDs( $pageIDs ) { |
| 914 |
$this->initFromPageIds( $pageIDs ); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
public function populateFromQueryResult( $db, $queryResult ) { |
| 927 |
$this->initFromQueryResult( $queryResult ); |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
public function populateFromRevisionIDs( $revIDs ) { |
| 935 |
$this->initFromRevIDs( $revIDs ); |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
public function processDbRow( $row ) { |
| 943 |
|
| 944 |
$title = $this->titleFactory->newFromRow( $row ); |
| 945 |
|
| 946 |
$this->linkCache->addGoodLinkObjFromRow( $title, $row ); |
| 947 |
|
| 948 |
$pageId = (int)$row->page_id; |
| 949 |
$this->mAllPages[$row->page_namespace][$row->page_title] = $pageId; |
| 950 |
$this->mTitles[] = $title; |
| 951 |
|
| 952 |
if ( $this->mResolveRedirects && $row->page_is_redirect == '1' ) { |
| 953 |
$this->mPendingRedirectIDs[$pageId] = $title; |
| 954 |
} else { |
| 955 |
$this->mGoodPages[$row->page_namespace][$row->page_title] = $pageId; |
| 956 |
$this->mGoodAndMissingPages[$row->page_namespace][$row->page_title] = $pageId; |
| 957 |
$this->mGoodTitles[$pageId] = $title; |
| 958 |
} |
| 959 |
|
| 960 |
foreach ( $this->mRequestedPageFields as $fieldName => &$fieldValues ) { |
| 961 |
$fieldValues[$pageId] = $row->$fieldName; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
private function initFromTitles( $titles ) { |
| 982 |
|
| 983 |
$linkBatch = $this->processTitlesArray( $titles ); |
| 984 |
if ( $linkBatch->isEmpty() ) { |
| 985 |
|
| 986 |
$this->resolvePendingRedirects(); |
| 987 |
return; |
| 988 |
} |
| 989 |
|
| 990 |
$db = $this->getDB(); |
| 991 |
|
| 992 |
|
| 993 |
$res = $db->newSelectQueryBuilder() |
| 994 |
->select( $this->getPageTableFields() ) |
| 995 |
->from( 'page' ) |
| 996 |
->where( $linkBatch->constructSet( 'page', $db ) ) |
| 997 |
->caller( __METHOD__ ) |
| 998 |
->fetchResultSet(); |
| 999 |
|
| 1000 |
|
| 1001 |
$this->initFromQueryResult( $res, $linkBatch->data, true ); |
| 1002 |
|
| 1003 |
|
| 1004 |
$this->resolvePendingRedirects(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
private function initFromPageIds( $pageids, $filterIds = true ) { |
| 1013 |
if ( !$pageids ) { |
| 1014 |
return; |
| 1015 |
} |
| 1016 |
|
| 1017 |
$pageids = array_map( 'intval', $pageids ); |
| 1018 |
$remaining = array_fill_keys( $pageids, true ); |
| 1019 |
|
| 1020 |
if ( $filterIds ) { |
| 1021 |
$pageids = $this->filterIDs( [ [ 'page', 'page_id' ] ], $pageids ); |
| 1022 |
} |
| 1023 |
|
| 1024 |
$res = null; |
| 1025 |
if ( $pageids ) { |
| 1026 |
$db = $this->getDB(); |
| 1027 |
|
| 1028 |
|
| 1029 |
$res = $db->newSelectQueryBuilder() |
| 1030 |
->select( $this->getPageTableFields() ) |
| 1031 |
->from( 'page' ) |
| 1032 |
->where( [ 'page_id' => $pageids ] ) |
| 1033 |
->caller( __METHOD__ ) |
| 1034 |
->fetchResultSet(); |
| 1035 |
} |
| 1036 |
|
| 1037 |
$this->initFromQueryResult( $res, $remaining, false ); |
| 1038 |
|
| 1039 |
|
| 1040 |
$this->resolvePendingRedirects(); |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) { |
| 1054 |
if ( $remaining !== null && $processTitles === null ) { |
| 1055 |
ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' ); |
| 1056 |
} |
| 1057 |
|
| 1058 |
$usernames = []; |
| 1059 |
if ( $res ) { |
| 1060 |
foreach ( $res as $row ) { |
| 1061 |
$pageId = (int)$row->page_id; |
| 1062 |
|
| 1063 |
|
| 1064 |
if ( $remaining ) { |
| 1065 |
if ( $processTitles ) { |
| 1066 |
unset( $remaining[$row->page_namespace][$row->page_title] ); |
| 1067 |
} else { |
| 1068 |
unset( $remaining[$pageId] ); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
$this->processDbRow( $row ); |
| 1074 |
|
| 1075 |
|
| 1076 |
if ( $this->namespaceInfo->hasGenderDistinction( $row->page_namespace ) ) { |
| 1077 |
$usernames[] = $row->page_title; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
if ( $remaining ) { |
| 1083 |
|
| 1084 |
if ( $processTitles ) { |
| 1085 |
|
| 1086 |
foreach ( $remaining as $ns => $dbkeys ) { |
| 1087 |
foreach ( $dbkeys as $dbkey => $_ ) { |
| 1088 |
$title = $this->titleFactory->makeTitle( $ns, $dbkey ); |
| 1089 |
$this->linkCache->addBadLinkObj( $title ); |
| 1090 |
$this->mAllPages[$ns][$dbkey] = $this->mFakePageId; |
| 1091 |
$this->mMissingPages[$ns][$dbkey] = $this->mFakePageId; |
| 1092 |
$this->mGoodAndMissingPages[$ns][$dbkey] = $this->mFakePageId; |
| 1093 |
$this->mMissingTitles[$this->mFakePageId] = $title; |
| 1094 |
$this->mFakePageId--; |
| 1095 |
$this->mTitles[] = $title; |
| 1096 |
|
| 1097 |
|
| 1098 |
if ( $this->namespaceInfo->hasGenderDistinction( $ns ) ) { |
| 1099 |
$usernames[] = $dbkey; |
| 1100 |
} |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} else { |
| 1104 |
|
| 1105 |
if ( !$this->mMissingPageIDs ) { |
| 1106 |
$this->mMissingPageIDs = array_keys( $remaining ); |
| 1107 |
} else { |
| 1108 |
$this->mMissingPageIDs = array_merge( $this->mMissingPageIDs, array_keys( $remaining ) ); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
|
| 1114 |
$this->genderCache->doQuery( $usernames, __METHOD__ ); |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
private function initFromRevIDs( $revids ) { |
| 1123 |
if ( !$revids ) { |
| 1124 |
return; |
| 1125 |
} |
| 1126 |
|
| 1127 |
$revids = array_map( 'intval', $revids ); |
| 1128 |
$db = $this->getDB(); |
| 1129 |
$pageids = []; |
| 1130 |
$remaining = array_fill_keys( $revids, true ); |
| 1131 |
|
| 1132 |
$revids = $this->filterIDs( [ [ 'revision', 'rev_id' ], [ 'archive', 'ar_rev_id' ] ], $revids ); |
| 1133 |
$goodRemaining = array_fill_keys( $revids, true ); |
| 1134 |
|
| 1135 |
if ( $revids ) { |
| 1136 |
$fields = [ 'rev_id', 'rev_page' ]; |
| 1137 |
|
| 1138 |
|
| 1139 |
$res = $db->newSelectQueryBuilder() |
| 1140 |
->select( $fields ) |
| 1141 |
->from( 'page' ) |
| 1142 |
->where( [ 'rev_id' => $revids ] ) |
| 1143 |
->join( 'revision', null, [ 'rev_page = page_id' ] ) |
| 1144 |
->caller( __METHOD__ ) |
| 1145 |
->fetchResultSet(); |
| 1146 |
foreach ( $res as $row ) { |
| 1147 |
$revid = (int)$row->rev_id; |
| 1148 |
$pageid = (int)$row->rev_page; |
| 1149 |
$this->mGoodRevIDs[$revid] = $pageid; |
| 1150 |
$this->mLiveRevIDs[$revid] = $pageid; |
| 1151 |
$pageids[$pageid] = ''; |
| 1152 |
unset( $remaining[$revid] ); |
| 1153 |
unset( $goodRemaining[$revid] ); |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
|
| 1158 |
$this->initFromPageIds( array_keys( $pageids ), false ); |
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
if ( $goodRemaining && |
| 1164 |
$this->getAuthority()->isAllowed( 'deletedhistory' ) ) { |
| 1165 |
|
| 1166 |
$res = $db->newSelectQueryBuilder() |
| 1167 |
->select( [ 'ar_rev_id', 'ar_namespace', 'ar_title' ] ) |
| 1168 |
->from( 'archive' ) |
| 1169 |
->where( [ 'ar_rev_id' => array_keys( $goodRemaining ) ] ) |
| 1170 |
->caller( __METHOD__ ) |
| 1171 |
->fetchResultSet(); |
| 1172 |
|
| 1173 |
$titles = []; |
| 1174 |
foreach ( $res as $row ) { |
| 1175 |
$revid = (int)$row->ar_rev_id; |
| 1176 |
$titles[$revid] = $this->titleFactory->makeTitle( $row->ar_namespace, $row->ar_title ); |
| 1177 |
unset( $remaining[$revid] ); |
| 1178 |
} |
| 1179 |
|
| 1180 |
$this->initFromTitles( $titles ); |
| 1181 |
|
| 1182 |
foreach ( $titles as $revid => $title ) { |
| 1183 |
$ns = $title->getNamespace(); |
| 1184 |
$dbkey = $title->getDBkey(); |
| 1185 |
|
| 1186 |
|
| 1187 |
if ( !isset( $this->mAllPages[$ns][$dbkey] ) && |
| 1188 |
isset( $this->mConvertedTitles[$title->getPrefixedText()] ) |
| 1189 |
) { |
| 1190 |
$title = $this->titleFactory->newFromText( $this->mConvertedTitles[$title->getPrefixedText()] ); |
| 1191 |
$ns = $title->getNamespace(); |
| 1192 |
$dbkey = $title->getDBkey(); |
| 1193 |
} |
| 1194 |
|
| 1195 |
if ( isset( $this->mAllPages[$ns][$dbkey] ) ) { |
| 1196 |
$this->mGoodRevIDs[$revid] = $this->mAllPages[$ns][$dbkey]; |
| 1197 |
$this->mDeletedRevIDs[$revid] = $this->mAllPages[$ns][$dbkey]; |
| 1198 |
} else { |
| 1199 |
$remaining[$revid] = true; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
} |
| 1203 |
|
| 1204 |
$this->mMissingRevIDs = array_keys( $remaining ); |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
private function resolvePendingRedirects() { |
| 1213 |
if ( $this->mResolveRedirects ) { |
| 1214 |
$db = $this->getDB(); |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
while ( $this->mPendingRedirectIDs || $this->mPendingRedirectSpecialPages ) { |
| 1219 |
|
| 1220 |
|
| 1221 |
$linkBatch = $this->loadRedirectTargets(); |
| 1222 |
|
| 1223 |
if ( $linkBatch->isEmpty() ) { |
| 1224 |
break; |
| 1225 |
} |
| 1226 |
|
| 1227 |
$set = $linkBatch->constructSet( 'page', $db ); |
| 1228 |
if ( $set === false ) { |
| 1229 |
break; |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
$res = $db->newSelectQueryBuilder() |
| 1234 |
->select( $this->getPageTableFields() ) |
| 1235 |
->from( 'page' ) |
| 1236 |
->where( $set ) |
| 1237 |
->caller( __METHOD__ ) |
| 1238 |
->fetchResultSet(); |
| 1239 |
|
| 1240 |
|
| 1241 |
$this->initFromQueryResult( $res, $linkBatch->data, true ); |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
private function loadRedirectTargets() { |
| 1254 |
$titlesToResolve = []; |
| 1255 |
$db = $this->getDB(); |
| 1256 |
|
| 1257 |
if ( $this->mPendingRedirectIDs ) { |
| 1258 |
$res = $db->newSelectQueryBuilder() |
| 1259 |
->select( [ |
| 1260 |
'rd_from', |
| 1261 |
'rd_namespace', |
| 1262 |
'rd_fragment', |
| 1263 |
'rd_interwiki', |
| 1264 |
'rd_title' |
| 1265 |
] ) |
| 1266 |
->from( 'redirect' ) |
| 1267 |
->where( [ 'rd_from' => array_keys( $this->mPendingRedirectIDs ) ] ) |
| 1268 |
->caller( __METHOD__ ) |
| 1269 |
->fetchResultSet(); |
| 1270 |
|
| 1271 |
foreach ( $res as $row ) { |
| 1272 |
$rdfrom = (int)$row->rd_from; |
| 1273 |
$from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText(); |
| 1274 |
$to = $this->titleFactory->makeTitle( |
| 1275 |
$row->rd_namespace, |
| 1276 |
$row->rd_title, |
| 1277 |
$row->rd_fragment, |
| 1278 |
$row->rd_interwiki |
| 1279 |
); |
| 1280 |
$this->mResolvedRedirectTitles[$from] = $this->mPendingRedirectIDs[$rdfrom]; |
| 1281 |
unset( $this->mPendingRedirectIDs[$rdfrom] ); |
| 1282 |
if ( $to->isExternal() ) { |
| 1283 |
$this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki(); |
| 1284 |
} elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] ) |
| 1285 |
&& !( $this->mConvertTitles && isset( $this->mConvertedTitles[$to->getPrefixedText()] ) ) |
| 1286 |
) { |
| 1287 |
$titlesToResolve[] = $to; |
| 1288 |
} |
| 1289 |
$this->mRedirectTitles[$from] = $to; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
|
| 1293 |
if ( $this->mPendingRedirectSpecialPages ) { |
| 1294 |
foreach ( $this->mPendingRedirectSpecialPages as [ $from, $to ] ) { |
| 1295 |
|
| 1296 |
$fromKey = $from->getPrefixedText(); |
| 1297 |
$this->mResolvedRedirectTitles[$fromKey] = $from; |
| 1298 |
$this->mRedirectTitles[$fromKey] = $to; |
| 1299 |
if ( $to->isExternal() ) { |
| 1300 |
$this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki(); |
| 1301 |
} elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] ) ) { |
| 1302 |
$titlesToResolve[] = $to; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
$this->mPendingRedirectSpecialPages = []; |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
$this->mCacheMode = 'private'; |
| 1310 |
} |
| 1311 |
|
| 1312 |
return $this->processTitlesArray( $titlesToResolve ); |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
public function getCacheMode( $params = null ) { |
| 1329 |
return $this->mCacheMode; |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
private function processTitlesArray( $titles ) { |
| 1342 |
$linkBatch = $this->linkBatchFactory->newLinkBatch(); |
| 1343 |
|
| 1344 |
|
| 1345 |
$titleObjects = []; |
| 1346 |
foreach ( $titles as $index => $title ) { |
| 1347 |
if ( is_string( $title ) ) { |
| 1348 |
try { |
| 1349 |
|
| 1350 |
$titleObj = $this->titleFactory->newFromTextThrow( $title, $this->mDefaultNamespace ); |
| 1351 |
} catch ( MalformedTitleException $ex ) { |
| 1352 |
|
| 1353 |
if ( !isset( $this->mAllPages[0][$title] ) ) { |
| 1354 |
$this->mAllPages[0][$title] = $this->mFakePageId; |
| 1355 |
$this->mInvalidTitles[$this->mFakePageId] = [ |
| 1356 |
'title' => $title, |
| 1357 |
'invalidreason' => $this->getErrorFormatter()->formatException( $ex, [ 'bc' => true ] ), |
| 1358 |
]; |
| 1359 |
$this->mFakePageId--; |
| 1360 |
} |
| 1361 |
continue; |
| 1362 |
} |
| 1363 |
} elseif ( $title instanceof LinkTarget ) { |
| 1364 |
$titleObj = $this->titleFactory->newFromLinkTarget( $title ); |
| 1365 |
} else { |
| 1366 |
$titleObj = $this->titleFactory->newFromPageReference( $title ); |
| 1367 |
} |
| 1368 |
|
| 1369 |
$titleObjects[$index] = $titleObj; |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
$this->genderCache->doTitlesArray( $titleObjects, __METHOD__ ); |
| 1374 |
|
| 1375 |
foreach ( $titleObjects as $index => $titleObj ) { |
| 1376 |
$title = is_string( $titles[$index] ) ? $titles[$index] : false; |
| 1377 |
$unconvertedTitle = $titleObj->getPrefixedText(); |
| 1378 |
$titleWasConverted = false; |
| 1379 |
if ( $titleObj->isExternal() ) { |
| 1380 |
|
| 1381 |
$this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki(); |
| 1382 |
} else { |
| 1383 |
|
| 1384 |
if ( |
| 1385 |
$this->mConvertTitles |
| 1386 |
&& $this->languageConverter->hasVariants() |
| 1387 |
&& !$titleObj->exists() |
| 1388 |
) { |
| 1389 |
|
| 1390 |
|
| 1391 |
$titleText = $title !== false ? $title : $titleObj->getPrefixedText(); |
| 1392 |
$this->languageConverter->findVariantLink( $titleText, $titleObj ); |
| 1393 |
$titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText(); |
| 1394 |
} |
| 1395 |
|
| 1396 |
if ( $titleObj->getNamespace() < 0 ) { |
| 1397 |
|
| 1398 |
$titleObj = $titleObj->fixSpecialName(); |
| 1399 |
$ns = $titleObj->getNamespace(); |
| 1400 |
$dbkey = $titleObj->getDBkey(); |
| 1401 |
if ( !isset( $this->mAllSpecials[$ns][$dbkey] ) ) { |
| 1402 |
$this->mAllSpecials[$ns][$dbkey] = $this->mFakePageId; |
| 1403 |
$target = null; |
| 1404 |
if ( $ns === NS_SPECIAL && $this->mResolveRedirects ) { |
| 1405 |
$special = $this->specialPageFactory->getPage( $dbkey ); |
| 1406 |
if ( $special instanceof RedirectSpecialArticle ) { |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
$context = new DerivativeContext( $this ); |
| 1411 |
$context->setTitle( $titleObj ); |
| 1412 |
$context->setRequest( new FauxRequest ); |
| 1413 |
$special->setContext( $context ); |
| 1414 |
[ , $subpage ] = $this->specialPageFactory->resolveAlias( $dbkey ); |
| 1415 |
$target = $special->getRedirect( $subpage ); |
| 1416 |
} |
| 1417 |
} |
| 1418 |
if ( $target ) { |
| 1419 |
$this->mPendingRedirectSpecialPages[$dbkey] = [ $titleObj, $target ]; |
| 1420 |
} else { |
| 1421 |
$this->mSpecialTitles[$this->mFakePageId] = $titleObj; |
| 1422 |
$this->mFakePageId--; |
| 1423 |
} |
| 1424 |
} |
| 1425 |
} else { |
| 1426 |
|
| 1427 |
$linkBatch->addObj( $titleObj ); |
| 1428 |
} |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
if ( $titleWasConverted ) { |
| 1437 |
$this->mConvertedTitles[$unconvertedTitle] = $titleObj->getPrefixedText(); |
| 1438 |
|
| 1439 |
if ( $title !== false && $title !== $unconvertedTitle ) { |
| 1440 |
$this->mNormalizedTitles[$title] = $unconvertedTitle; |
| 1441 |
} |
| 1442 |
} elseif ( $title !== false && $title !== $titleObj->getPrefixedText() ) { |
| 1443 |
$this->mNormalizedTitles[$title] = $titleObj->getPrefixedText(); |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
return $linkBatch; |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
public function setGeneratorData( $title, array $data ) { |
| 1466 |
$ns = $title->getNamespace(); |
| 1467 |
$dbkey = $title->getDBkey(); |
| 1468 |
$this->mGeneratorData[$ns][$dbkey] = $data; |
| 1469 |
} |
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
public function setRedirectMergePolicy( $callable ) { |
| 1491 |
$this->mRedirectMergePolicy = $callable; |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
private function resolveRedirectTitleDest( Title $titleFrom ): Title { |
| 1506 |
$seen = []; |
| 1507 |
$dest = $titleFrom; |
| 1508 |
while ( isset( $this->mRedirectTitles[$dest->getPrefixedText()] ) ) { |
| 1509 |
$dest = $this->mRedirectTitles[$dest->getPrefixedText()]; |
| 1510 |
if ( isset( $seen[$dest->getPrefixedText()] ) ) { |
| 1511 |
return $titleFrom; |
| 1512 |
} |
| 1513 |
$seen[$dest->getPrefixedText()] = true; |
| 1514 |
} |
| 1515 |
return $dest; |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
public function populateGeneratorData( &$result, array $path = [] ) { |
| 1539 |
if ( $result instanceof ApiResult ) { |
| 1540 |
$data = $result->getResultData( $path ); |
| 1541 |
if ( $data === null ) { |
| 1542 |
return true; |
| 1543 |
} |
| 1544 |
} else { |
| 1545 |
$data = &$result; |
| 1546 |
foreach ( $path as $key ) { |
| 1547 |
if ( !isset( $data[$key] ) ) { |
| 1548 |
|
| 1549 |
|
| 1550 |
return true; |
| 1551 |
} |
| 1552 |
$data = &$data[$key]; |
| 1553 |
} |
| 1554 |
} |
| 1555 |
foreach ( $this->mGeneratorData as $ns => $dbkeys ) { |
| 1556 |
if ( $ns === NS_SPECIAL ) { |
| 1557 |
$pages = []; |
| 1558 |
foreach ( $this->mSpecialTitles as $id => $title ) { |
| 1559 |
$pages[$title->getDBkey()] = $id; |
| 1560 |
} |
| 1561 |
} else { |
| 1562 |
if ( !isset( $this->mAllPages[$ns] ) ) { |
| 1563 |
|
| 1564 |
continue; |
| 1565 |
} |
| 1566 |
$pages = $this->mAllPages[$ns]; |
| 1567 |
} |
| 1568 |
foreach ( $dbkeys as $dbkey => $genData ) { |
| 1569 |
if ( !isset( $pages[$dbkey] ) ) { |
| 1570 |
|
| 1571 |
continue; |
| 1572 |
} |
| 1573 |
$pageId = $pages[$dbkey]; |
| 1574 |
if ( !isset( $data[$pageId] ) ) { |
| 1575 |
|
| 1576 |
continue; |
| 1577 |
} |
| 1578 |
|
| 1579 |
if ( $result instanceof ApiResult ) { |
| 1580 |
$path2 = array_merge( $path, [ $pageId ] ); |
| 1581 |
foreach ( $genData as $key => $value ) { |
| 1582 |
if ( !$result->addValue( $path2, $key, $value ) ) { |
| 1583 |
return false; |
| 1584 |
} |
| 1585 |
} |
| 1586 |
} else { |
| 1587 |
$data[$pageId] = array_merge( $data[$pageId], $genData ); |
| 1588 |
} |
| 1589 |
} |
| 1590 |
} |
| 1591 |
|
| 1592 |
|
| 1593 |
if ( $this->mRedirectMergePolicy ) { |
| 1594 |
foreach ( $this->mResolvedRedirectTitles as $titleFrom ) { |
| 1595 |
$dest = $this->resolveRedirectTitleDest( $titleFrom ); |
| 1596 |
$fromNs = $titleFrom->getNamespace(); |
| 1597 |
$fromDBkey = $titleFrom->getDBkey(); |
| 1598 |
$toPageId = $dest->getArticleID(); |
| 1599 |
if ( isset( $data[$toPageId] ) && |
| 1600 |
isset( $this->mGeneratorData[$fromNs][$fromDBkey] ) |
| 1601 |
) { |
| 1602 |
|
| 1603 |
|
| 1604 |
$data[$toPageId] = call_user_func( |
| 1605 |
$this->mRedirectMergePolicy, |
| 1606 |
$data[$toPageId], |
| 1607 |
$this->mGeneratorData[$fromNs][$fromDBkey] |
| 1608 |
); |
| 1609 |
if ( $result instanceof ApiResult && |
| 1610 |
!$result->addValue( $path, $toPageId, $data[$toPageId], ApiResult::OVERRIDE ) |
| 1611 |
) { |
| 1612 |
return false; |
| 1613 |
} |
| 1614 |
} |
| 1615 |
} |
| 1616 |
} |
| 1617 |
|
| 1618 |
return true; |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
protected function getDB() { |
| 1626 |
return $this->mDbSource->getDB(); |
| 1627 |
} |
| 1628 |
|
| 1629 |
public function getAllowedParams( $flags = 0 ) { |
| 1630 |
$result = [ |
| 1631 |
'titles' => [ |
| 1632 |
ParamValidator::PARAM_ISMULTI => true, |
| 1633 |
ApiBase::PARAM_HELP_MSG => 'api-pageset-param-titles', |
| 1634 |
], |
| 1635 |
'pageids' => [ |
| 1636 |
ParamValidator::PARAM_TYPE => 'integer', |
| 1637 |
ParamValidator::PARAM_ISMULTI => true, |
| 1638 |
ApiBase::PARAM_HELP_MSG => 'api-pageset-param-pageids', |
| 1639 |
], |
| 1640 |
'revids' => [ |
| 1641 |
ParamValidator::PARAM_TYPE => 'integer', |
| 1642 |
ParamValidator::PARAM_ISMULTI => true, |
| 1643 |
ApiBase::PARAM_HELP_MSG => 'api-pageset-param-revids', |
| 1644 |
], |
| 1645 |
'generator' => [ |
| 1646 |
ParamValidator::PARAM_TYPE => null, |
| 1647 |
ApiBase::PARAM_HELP_MSG => 'api-pageset-param-generator', |
| 1648 |
SubmoduleDef::PARAM_SUBMODULE_PARAM_PREFIX => 'g', |
| 1649 |
], |
| 1650 |
'redirects' => [ |
| 1651 |
ParamValidator::PARAM_DEFAULT => false, |
| 1652 |
ApiBase::PARAM_HELP_MSG => $this->mAllowGenerator |
| 1653 |
? 'api-pageset-param-redirects-generator' |
| 1654 |
: 'api-pageset-param-redirects-nogenerator', |
| 1655 |
], |
| 1656 |
'converttitles' => [ |
| 1657 |
ParamValidator::PARAM_DEFAULT => false, |
| 1658 |
ApiBase::PARAM_HELP_MSG => [ |
| 1659 |
'api-pageset-param-converttitles', |
| 1660 |
Message::listParam( LanguageConverter::$languagesWithVariants, 'text' ), |
| 1661 |
], |
| 1662 |
], |
| 1663 |
]; |
| 1664 |
|
| 1665 |
if ( !$this->mAllowGenerator ) { |
| 1666 |
unset( $result['generator'] ); |
| 1667 |
} elseif ( $flags & ApiBase::GET_VALUES_FOR_HELP ) { |
| 1668 |
$result['generator'][ParamValidator::PARAM_TYPE] = 'submodule'; |
| 1669 |
$result['generator'][SubmoduleDef::PARAM_SUBMODULE_MAP] = $this->getGenerators(); |
| 1670 |
} |
| 1671 |
|
| 1672 |
return $result; |
| 1673 |
} |
| 1674 |
|
| 1675 |
public function handleParamNormalization( $paramName, $value, $rawValue ) { |
| 1676 |
parent::handleParamNormalization( $paramName, $value, $rawValue ); |
| 1677 |
|
| 1678 |
if ( $paramName === 'titles' ) { |
| 1679 |
|
| 1680 |
|
| 1681 |
$value = ParamValidator::explodeMultiValue( $value, self::LIMIT_SML2 + 1 ); |
| 1682 |
$l = count( $value ); |
| 1683 |
$rawValue = ParamValidator::explodeMultiValue( $rawValue, $l ); |
| 1684 |
for ( $i = 0; $i < $l; $i++ ) { |
| 1685 |
if ( $value[$i] !== $rawValue[$i] ) { |
| 1686 |
$this->mNormalizedTitles[$rawValue[$i]] = $value[$i]; |
| 1687 |
} |
| 1688 |
} |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
private function getGenerators() { |
| 1697 |
if ( self::$generators === null ) { |
| 1698 |
$query = $this->mDbSource; |
| 1699 |
if ( !( $query instanceof ApiQuery ) ) { |
| 1700 |
|
| 1701 |
|
| 1702 |
$query = $this->getMain()->getModuleManager()->getModule( 'query' ); |
| 1703 |
} |
| 1704 |
$gens = []; |
| 1705 |
$prefix = $query->getModulePath() . '+'; |
| 1706 |
$mgr = $query->getModuleManager(); |
| 1707 |
foreach ( $mgr->getNamesWithClasses() as $name => $class ) { |
| 1708 |
if ( is_subclass_of( $class, ApiQueryGeneratorBase::class ) ) { |
| 1709 |
$gens[$name] = $prefix . $name; |
| 1710 |
} |
| 1711 |
} |
| 1712 |
ksort( $gens ); |
| 1713 |
self::$generators = $gens; |
| 1714 |
} |
| 1715 |
|
| 1716 |
return self::$generators; |
| 1717 |
} |
| 1718 |
} |
| 1719 |
|
| 1720 |
|
| 1721 |
class_alias( ApiPageSet::class, 'ApiPageSet' ); |