| 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\MediaWikiServices; |
| 26 |
use MediaWiki\Title\MalformedTitleException; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use MediaWiki\Title\TitleValue; |
| 29 |
use stdClass; |
| 30 |
use Wikimedia\Rdbms\IDatabase; |
| 31 |
use Wikimedia\Rdbms\IExpression; |
| 32 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 33 |
use Wikimedia\Rdbms\IResultWrapper; |
| 34 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
abstract class ApiQueryBase extends ApiBase { |
| 46 |
use ApiQueryBlockInfoTrait; |
| 47 |
|
| 48 |
private ApiQuery $mQueryModule; |
| 49 |
private ?IReadableDatabase $mDb; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private $queryBuilder; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function __construct( ApiQuery $queryModule, string $moduleName, $paramPrefix = '' ) { |
| 63 |
parent::__construct( $queryModule->getMain(), $moduleName, $paramPrefix ); |
| 64 |
$this->mQueryModule = $queryModule; |
| 65 |
$this->mDb = null; |
| 66 |
$this->resetQueryParams(); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function getCacheMode( $params ) { |
| 86 |
return 'private'; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function requestExtraData( $pageSet ) { |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getQuery() { |
| 113 |
return $this->mQueryModule; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public function getParent() { |
| 118 |
return $this->getQuery(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
protected function getDB() { |
| 127 |
$this->mDb ??= $this->getQuery()->getDB(); |
| 128 |
|
| 129 |
return $this->mDb; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected function getPageSet() { |
| 138 |
return $this->getQuery()->getPageSet(); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
protected function resetQueryParams() { |
| 151 |
$this->queryBuilder = null; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
protected function getQueryBuilder() { |
| 163 |
$this->queryBuilder ??= $this->getDB()->newSelectQueryBuilder(); |
| 164 |
return $this->queryBuilder; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
protected function addTables( $tables, $alias = null ) { |
| 175 |
if ( is_array( $tables ) ) { |
| 176 |
if ( $alias !== null ) { |
| 177 |
ApiBase::dieDebug( __METHOD__, 'Multiple table aliases not supported' ); |
| 178 |
} |
| 179 |
$this->getQueryBuilder()->rawTables( $tables ); |
| 180 |
} else { |
| 181 |
$this->getQueryBuilder()->table( $tables, $alias ); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
protected function addJoinConds( $join_conds ) { |
| 194 |
if ( !is_array( $join_conds ) ) { |
| 195 |
ApiBase::dieDebug( __METHOD__, 'Join conditions have to be arrays' ); |
| 196 |
} |
| 197 |
$this->getQueryBuilder()->joinConds( $join_conds ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
protected function addFields( $value ) { |
| 205 |
$this->getQueryBuilder()->fields( $value ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
protected function addFieldsIf( $value, $condition ) { |
| 215 |
if ( $condition ) { |
| 216 |
$this->addFields( $value ); |
| 217 |
|
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
return false; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
protected function addWhere( $value ) { |
| 238 |
if ( is_array( $value ) ) { |
| 239 |
|
| 240 |
|
| 241 |
if ( count( $value ) ) { |
| 242 |
$this->getQueryBuilder()->where( $value ); |
| 243 |
} |
| 244 |
} else { |
| 245 |
$this->getQueryBuilder()->where( $value ); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
protected function addWhereIf( $value, $condition ) { |
| 256 |
if ( $condition ) { |
| 257 |
$this->addWhere( $value ); |
| 258 |
|
| 259 |
return true; |
| 260 |
} |
| 261 |
|
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
protected function addWhereFld( $field, $value ) { |
| 275 |
if ( $value !== null && !( is_array( $value ) && !$value ) ) { |
| 276 |
$this->getQueryBuilder()->where( [ $field => $value ] ); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
protected function addWhereIDsFld( $table, $field, $ids ) { |
| 302 |
|
| 303 |
|
| 304 |
if ( count( $ids ) ) { |
| 305 |
$ids = $this->filterIDs( [ [ $table, $field ] ], $ids ); |
| 306 |
|
| 307 |
if ( $ids === [] ) { |
| 308 |
|
| 309 |
$this->getQueryBuilder()->where( '0 = 1' ); |
| 310 |
} else { |
| 311 |
$this->getQueryBuilder()->where( [ $field => $ids ] ); |
| 312 |
} |
| 313 |
} |
| 314 |
return count( $ids ); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
protected function addWhereRange( $field, $dir, $start, $end, $sort = true ) { |
| 330 |
$isDirNewer = ( $dir === 'newer' ); |
| 331 |
$after = ( $isDirNewer ? '>=' : '<=' ); |
| 332 |
$before = ( $isDirNewer ? '<=' : '>=' ); |
| 333 |
$db = $this->getDB(); |
| 334 |
|
| 335 |
if ( $start !== null ) { |
| 336 |
$this->addWhere( $db->expr( $field, $after, $start ) ); |
| 337 |
} |
| 338 |
|
| 339 |
if ( $end !== null ) { |
| 340 |
$this->addWhere( $db->expr( $field, $before, $end ) ); |
| 341 |
} |
| 342 |
|
| 343 |
if ( $sort ) { |
| 344 |
$this->getQueryBuilder()->orderBy( $field, $isDirNewer ? null : 'DESC' ); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
protected function addTimestampWhereRange( $field, $dir, $start, $end, $sort = true ) { |
| 359 |
$db = $this->getDB(); |
| 360 |
$this->addWhereRange( $field, $dir, |
| 361 |
$db->timestampOrNull( $start ), $db->timestampOrNull( $end ), $sort ); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
protected function addOption( $name, $value = null ) { |
| 371 |
$this->getQueryBuilder()->option( $name, $value ); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
protected function select( $method, $extraQuery = [], ?array &$hookData = null ) { |
| 392 |
$queryBuilder = clone $this->getQueryBuilder(); |
| 393 |
if ( isset( $extraQuery['tables'] ) ) { |
| 394 |
$queryBuilder->rawTables( (array)$extraQuery['tables'] ); |
| 395 |
} |
| 396 |
if ( isset( $extraQuery['fields'] ) ) { |
| 397 |
$queryBuilder->fields( (array)$extraQuery['fields'] ); |
| 398 |
} |
| 399 |
if ( isset( $extraQuery['where'] ) ) { |
| 400 |
$queryBuilder->where( (array)$extraQuery['where'] ); |
| 401 |
} |
| 402 |
if ( isset( $extraQuery['options'] ) ) { |
| 403 |
$queryBuilder->options( (array)$extraQuery['options'] ); |
| 404 |
} |
| 405 |
if ( isset( $extraQuery['join_conds'] ) ) { |
| 406 |
$queryBuilder->joinConds( (array)$extraQuery['join_conds'] ); |
| 407 |
} |
| 408 |
|
| 409 |
if ( $hookData !== null && $this->getHookContainer()->isRegistered( 'ApiQueryBaseBeforeQuery' ) ) { |
| 410 |
$info = $queryBuilder->getQueryInfo(); |
| 411 |
$this->getHookRunner()->onApiQueryBaseBeforeQuery( |
| 412 |
$this, $info['tables'], $info['fields'], $info['conds'], |
| 413 |
$info['options'], $info['join_conds'], $hookData |
| 414 |
); |
| 415 |
$queryBuilder = $this->getDB()->newSelectQueryBuilder()->queryInfo( $info ); |
| 416 |
} |
| 417 |
|
| 418 |
$queryBuilder->caller( $method ); |
| 419 |
$res = $queryBuilder->fetchResultSet(); |
| 420 |
|
| 421 |
if ( $hookData !== null ) { |
| 422 |
$this->getHookRunner()->onApiQueryBaseAfterQuery( $this, $res, $hookData ); |
| 423 |
} |
| 424 |
|
| 425 |
return $res; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
protected function processRow( $row, array &$data, array &$hookData ) { |
| 442 |
return $this->getHookRunner()->onApiQueryBaseProcessRow( $this, $row, $data, $hookData ); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
t |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
public static function addTitleInfo( &$arr, $title, $prefix = '' ) { |
| 459 |
$arr[$prefix . 'ns'] = $title->getNamespace(); |
| 460 |
$arr[$prefix . 'title'] = $title->getPrefixedText(); |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
t |
| 467 |
|
| 468 |
|
| 469 |
protected function addPageSubItems( $pageId, $data ) { |
| 470 |
$result = $this->getResult(); |
| 471 |
ApiResult::setIndexedTagName( $data, $this->getModulePrefix() ); |
| 472 |
|
| 473 |
return $result->addValue( [ 'query', 'pages', (int)$pageId ], |
| 474 |
$this->getModuleName(), |
| 475 |
$data ); |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
t |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
protected function addPageSubItem( $pageId, $item, $elemname = null ) { |
| 487 |
$result = $this->getResult(); |
| 488 |
$fit = $result->addValue( [ 'query', 'pages', $pageId, |
| 489 |
$this->getModuleName() ], null, $item ); |
| 490 |
if ( !$fit ) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
$result->addIndexedTagName( |
| 494 |
[ 'query', 'pages', $pageId, $this->getModuleName() ], |
| 495 |
$elemname ?? $this->getModulePrefix() |
| 496 |
); |
| 497 |
|
| 498 |
return true; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
protected function setContinueEnumParameter( $paramName, $paramValue ) { |
| 507 |
$this->getContinuationManager()->addContinueParam( $this, $paramName, $paramValue ); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
public function titlePartToKey( $titlePart, $namespace = NS_MAIN ) { |
| 521 |
$t = Title::makeTitleSafe( $namespace, $titlePart . 'x' ); |
| 522 |
if ( !$t || $t->hasFragment() ) { |
| 523 |
|
| 524 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] ); |
| 525 |
} |
| 526 |
if ( $namespace != $t->getNamespace() || $t->isExternal() ) { |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] ); |
| 533 |
} |
| 534 |
|
| 535 |
return substr( $t->getDBkey(), 0, -1 ); |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
protected function parsePrefixedTitlePart( $titlePart, $defaultNamespace = NS_MAIN ) { |
| 547 |
try { |
| 548 |
$titleParser = MediaWikiServices::getInstance()->getTitleParser(); |
| 549 |
$t = $titleParser->parseTitle( $titlePart . 'X', $defaultNamespace ); |
| 550 |
} catch ( MalformedTitleException $e ) { |
| 551 |
$t = null; |
| 552 |
} |
| 553 |
|
| 554 |
if ( !$t || $t->hasFragment() || $t->isExternal() || $t->getDBkey() === 'X' ) { |
| 555 |
|
| 556 |
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $titlePart ) ] ); |
| 557 |
} |
| 558 |
|
| 559 |
return new TitleValue( $t->getNamespace(), substr( $t->getDBkey(), 0, -1 ) ); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
public function validateSha1Hash( $hash ) { |
| 567 |
return (bool)preg_match( '/^[a-f0-9]{40}$/', $hash ); |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
public function validateSha1Base36Hash( $hash ) { |
| 575 |
return (bool)preg_match( '/^[a-z0-9]{31}$/', $hash ); |
| 576 |
} |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public function userCanSeeRevDel() { |
| 584 |
return $this->getAuthority()->isAllowedAny( |
| 585 |
'deletedhistory', |
| 586 |
'deletedtext', |
| 587 |
'deleterevision', |
| 588 |
'suppressrevision', |
| 589 |
'viewsuppressed' |
| 590 |
); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
protected function executeGenderCacheFromResultWrapper( |
| 603 |
IResultWrapper $res, $fname = __METHOD__, $fieldPrefix = 'page' |
| 604 |
) { |
| 605 |
if ( !$res->numRows() ) { |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
$services = MediaWikiServices::getInstance(); |
| 610 |
if ( !$services->getContentLanguage()->needsGenderDistinction() ) { |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
$nsInfo = $services->getNamespaceInfo(); |
| 615 |
$namespaceField = $fieldPrefix . '_namespace'; |
| 616 |
$titleField = $fieldPrefix . '_title'; |
| 617 |
|
| 618 |
$usernames = []; |
| 619 |
foreach ( $res as $row ) { |
| 620 |
if ( $nsInfo->hasGenderDistinction( $row->$namespaceField ) ) { |
| 621 |
$usernames[] = $row->$titleField; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
if ( $usernames === [] ) { |
| 626 |
return; |
| 627 |
} |
| 628 |
|
| 629 |
$genderCache = $services->getGenderCache(); |
| 630 |
$genderCache->doQuery( $usernames, $fname ); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
class_alias( ApiQueryBase::class, 'ApiQueryBase' ); |