| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\SpecialPage; |
| 25 |
|
| 26 |
use Exception; |
| 27 |
use MediaWiki\Cache\LinkBatchFactory; |
| 28 |
use MediaWiki\Config\Config; |
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MediaWiki\Linker\LinkTarget; |
| 31 |
use MediaWiki\MainConfigNames; |
| 32 |
use MediaWiki\MediaWikiServices; |
| 33 |
use MediaWiki\Output\OutputPage; |
| 34 |
use MediaWiki\Specials\SpecialAncientPages; |
| 35 |
use MediaWiki\Specials\SpecialBrokenRedirects; |
| 36 |
use MediaWiki\Specials\SpecialDeadendPages; |
| 37 |
use MediaWiki\Specials\SpecialDoubleRedirects; |
| 38 |
use MediaWiki\Specials\SpecialFewestRevisions; |
| 39 |
use MediaWiki\Specials\SpecialLinkSearch; |
| 40 |
use MediaWiki\Specials\SpecialListDuplicatedFiles; |
| 41 |
use MediaWiki\Specials\SpecialListRedirects; |
| 42 |
use MediaWiki\Specials\SpecialLonelyPages; |
| 43 |
use MediaWiki\Specials\SpecialLongPages; |
| 44 |
use MediaWiki\Specials\SpecialMediaStatistics; |
| 45 |
use MediaWiki\Specials\SpecialMIMESearch; |
| 46 |
use MediaWiki\Specials\SpecialMostCategories; |
| 47 |
use MediaWiki\Specials\SpecialMostImages; |
| 48 |
use MediaWiki\Specials\SpecialMostInterwikis; |
| 49 |
use MediaWiki\Specials\SpecialMostLinked; |
| 50 |
use MediaWiki\Specials\SpecialMostLinkedCategories; |
| 51 |
use MediaWiki\Specials\SpecialMostLinkedTemplates; |
| 52 |
use MediaWiki\Specials\SpecialMostRevisions; |
| 53 |
use MediaWiki\Specials\SpecialShortPages; |
| 54 |
use MediaWiki\Specials\SpecialUncategorizedCategories; |
| 55 |
use MediaWiki\Specials\SpecialUncategorizedImages; |
| 56 |
use MediaWiki\Specials\SpecialUncategorizedPages; |
| 57 |
use MediaWiki\Specials\SpecialUncategorizedTemplates; |
| 58 |
use MediaWiki\Specials\SpecialUnusedCategories; |
| 59 |
use MediaWiki\Specials\SpecialUnusedImages; |
| 60 |
use MediaWiki\Specials\SpecialUnusedTemplates; |
| 61 |
use MediaWiki\Specials\SpecialUnwatchedPages; |
| 62 |
use MediaWiki\Specials\SpecialWantedCategories; |
| 63 |
use MediaWiki\Specials\SpecialWantedFiles; |
| 64 |
use MediaWiki\Specials\SpecialWantedPages; |
| 65 |
use MediaWiki\Specials\SpecialWantedTemplates; |
| 66 |
use MediaWiki\Specials\SpecialWithoutInterwiki; |
| 67 |
use MediaWiki\Xml\Xml; |
| 68 |
use Skin; |
| 69 |
use stdClass; |
| 70 |
use Wikimedia\Rdbms\DBError; |
| 71 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 72 |
use Wikimedia\Rdbms\IDatabase; |
| 73 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 74 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 75 |
use Wikimedia\Rdbms\IResultWrapper; |
| 76 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
abstract class QueryPage extends SpecialPage { |
| 88 |
|
| 89 |
protected $listoutput = false; |
| 90 |
|
| 91 |
|
| 92 |
protected $offset = 0; |
| 93 |
|
| 94 |
|
| 95 |
protected $limit = 0; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
protected $numRows; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
protected $cachedTimestamp = null; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
protected $shownavigation = true; |
| 115 |
|
| 116 |
|
| 117 |
private $loadBalancer = null; |
| 118 |
|
| 119 |
|
| 120 |
private $databaseProvider = null; |
| 121 |
|
| 122 |
|
| 123 |
private $linkBatchFactory = null; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public static function getPages() { |
| 136 |
static $qp = null; |
| 137 |
|
| 138 |
if ( $qp === null ) { |
| 139 |
$qp = [ |
| 140 |
[ SpecialAncientPages::class, 'Ancientpages' ], |
| 141 |
[ SpecialBrokenRedirects::class, 'BrokenRedirects' ], |
| 142 |
[ SpecialDeadendPages::class, 'Deadendpages' ], |
| 143 |
[ SpecialDoubleRedirects::class, 'DoubleRedirects' ], |
| 144 |
[ SpecialListDuplicatedFiles::class, 'ListDuplicatedFiles' ], |
| 145 |
[ SpecialLinkSearch::class, 'LinkSearch' ], |
| 146 |
[ SpecialListRedirects::class, 'Listredirects' ], |
| 147 |
[ SpecialLonelyPages::class, 'Lonelypages' ], |
| 148 |
[ SpecialLongPages::class, 'Longpages' ], |
| 149 |
[ SpecialMediaStatistics::class, 'MediaStatistics', SpecialMediaStatistics::MAX_LIMIT ], |
| 150 |
[ SpecialMIMESearch::class, 'MIMEsearch' ], |
| 151 |
[ SpecialMostCategories::class, 'Mostcategories' ], |
| 152 |
[ SpecialMostImages::class, 'Mostimages' ], |
| 153 |
[ SpecialMostInterwikis::class, 'Mostinterwikis' ], |
| 154 |
[ SpecialMostLinkedCategories::class, 'Mostlinkedcategories' ], |
| 155 |
[ SpecialMostLinkedTemplates::class, 'Mostlinkedtemplates' ], |
| 156 |
[ SpecialMostLinked::class, 'Mostlinked' ], |
| 157 |
[ SpecialMostRevisions::class, 'Mostrevisions' ], |
| 158 |
[ SpecialFewestRevisions::class, 'Fewestrevisions' ], |
| 159 |
[ SpecialShortPages::class, 'Shortpages' ], |
| 160 |
[ SpecialUncategorizedCategories::class, 'Uncategorizedcategories' ], |
| 161 |
[ SpecialUncategorizedPages::class, 'Uncategorizedpages' ], |
| 162 |
[ SpecialUncategorizedImages::class, 'Uncategorizedimages' ], |
| 163 |
[ SpecialUncategorizedTemplates::class, 'Uncategorizedtemplates' ], |
| 164 |
[ SpecialUnusedCategories::class, 'Unusedcategories' ], |
| 165 |
[ SpecialUnusedImages::class, 'Unusedimages' ], |
| 166 |
[ SpecialWantedCategories::class, 'Wantedcategories' ], |
| 167 |
[ SpecialWantedFiles::class, 'Wantedfiles' ], |
| 168 |
[ SpecialWantedPages::class, 'Wantedpages' ], |
| 169 |
[ SpecialWantedTemplates::class, 'Wantedtemplates' ], |
| 170 |
[ SpecialUnwatchedPages::class, 'Unwatchedpages' ], |
| 171 |
[ SpecialUnusedTemplates::class, 'Unusedtemplates' ], |
| 172 |
[ SpecialWithoutInterwiki::class, 'Withoutinterwiki' ], |
| 173 |
]; |
| 174 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onWgQueryPages( $qp ); |
| 175 |
} |
| 176 |
|
| 177 |
return $qp; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
final protected function setLinkBatchFactory( LinkBatchFactory $linkBatchFactory ) { |
| 185 |
$this->linkBatchFactory = $linkBatchFactory; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
final protected function getLinkBatchFactory(): LinkBatchFactory { |
| 193 |
if ( $this->linkBatchFactory === null ) { |
| 194 |
|
| 195 |
|
| 196 |
$this->linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory(); |
| 197 |
} |
| 198 |
return $this->linkBatchFactory; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public static function getDisabledQueryPages( Config $config ) { |
| 207 |
$disableQueryPageUpdate = $config->get( MainConfigNames::DisableQueryPageUpdate ); |
| 208 |
|
| 209 |
if ( !is_array( $disableQueryPageUpdate ) ) { |
| 210 |
return []; |
| 211 |
} |
| 212 |
|
| 213 |
$pages = []; |
| 214 |
foreach ( $disableQueryPageUpdate as $name => $runMode ) { |
| 215 |
if ( is_int( $name ) ) { |
| 216 |
|
| 217 |
$pages[$runMode] = 'disabled'; |
| 218 |
} else { |
| 219 |
$pages[$name] = $runMode; |
| 220 |
} |
| 221 |
} |
| 222 |
return $pages; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
protected function setListoutput( $bool ) { |
| 231 |
$this->listoutput = $bool; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
abstract public function getQueryInfo(); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
protected function getOrderFields() { |
| 268 |
return [ 'value' ]; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public function usesTimestamps() { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
protected function sortDescending() { |
| 293 |
return true; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function isExpensive() { |
| 317 |
return $this->getConfig()->get( MainConfigNames::DisableQueryPages ); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public function isCacheable() { |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function isCached() { |
| 340 |
return $this->isExpensive() && $this->getConfig()->get( MainConfigNames::MiserMode ); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
public function isSyndicated() { |
| 350 |
return true; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
abstract protected function formatResult( $skin, $result ); |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
protected function getPageHeader() { |
| 371 |
return ''; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
protected function showEmptyText() { |
| 382 |
$this->getOutput()->addWikiMsg( 'specialpage-empty' ); |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
protected function linkParameters() { |
| 394 |
return []; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
public function recache( $limit, $unused = true ) { |
| 408 |
if ( !$this->isCacheable() ) { |
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
$fname = static::class . '::recache'; |
| 413 |
$dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
| 414 |
|
| 415 |
|
| 416 |
$res = $this->reallyDoQuery( $limit, false ); |
| 417 |
$num = false; |
| 418 |
if ( $res ) { |
| 419 |
$num = $res->numRows(); |
| 420 |
|
| 421 |
$vals = []; |
| 422 |
foreach ( $res as $i => $row ) { |
| 423 |
if ( isset( $row->value ) ) { |
| 424 |
if ( $this->usesTimestamps() ) { |
| 425 |
$value = (int)wfTimestamp( TS_UNIX, $row->value ); |
| 426 |
} else { |
| 427 |
$value = intval( $row->value ); |
| 428 |
} |
| 429 |
} else { |
| 430 |
$value = $i; |
| 431 |
} |
| 432 |
|
| 433 |
$vals[] = [ |
| 434 |
'qc_type' => $this->getName(), |
| 435 |
'qc_namespace' => $row->namespace, |
| 436 |
'qc_title' => $row->title, |
| 437 |
'qc_value' => $value |
| 438 |
]; |
| 439 |
} |
| 440 |
|
| 441 |
$dbw->doAtomicSection( |
| 442 |
$fname, |
| 443 |
function ( IDatabase $dbw, $fname ) use ( $vals ) { |
| 444 |
|
| 445 |
$dbw->newDeleteQueryBuilder() |
| 446 |
->deleteFrom( 'querycache' ) |
| 447 |
->where( [ 'qc_type' => $this->getName() ] ) |
| 448 |
->caller( $fname )->execute(); |
| 449 |
|
| 450 |
$dbw->newInsertQueryBuilder() |
| 451 |
->insertInto( 'querycache_info' ) |
| 452 |
->row( [ 'qci_type' => $this->getName(), 'qci_timestamp' => $dbw->timestamp() ] ) |
| 453 |
->onDuplicateKeyUpdate() |
| 454 |
->uniqueIndexFields( [ 'qci_type' ] ) |
| 455 |
->set( [ 'qci_timestamp' => $dbw->timestamp() ] ) |
| 456 |
->caller( $fname )->execute(); |
| 457 |
} |
| 458 |
); |
| 459 |
|
| 460 |
if ( count( $vals ) ) { |
| 461 |
foreach ( array_chunk( $vals, 500 ) as $chunk ) { |
| 462 |
$dbw->newInsertQueryBuilder() |
| 463 |
->insertInto( 'querycache' ) |
| 464 |
->rows( $chunk ) |
| 465 |
->caller( $fname )->execute(); |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return $num; |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
protected function getRecacheDB() { |
| 479 |
return $this->getDBLoadBalancer() |
| 480 |
->getConnection( ILoadBalancer::DB_REPLICA, 'vslow' ); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
public function delete( LinkTarget $title ) { |
| 490 |
if ( $this->isCached() ) { |
| 491 |
$dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
| 492 |
$dbw->newDeleteQueryBuilder() |
| 493 |
->deleteFrom( 'querycache' ) |
| 494 |
->where( [ |
| 495 |
'qc_type' => $this->getName(), |
| 496 |
'qc_namespace' => $title->getNamespace(), |
| 497 |
'qc_title' => $title->getDBkey(), |
| 498 |
] ) |
| 499 |
->caller( __METHOD__ )->execute(); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
public function deleteAllCachedData() { |
| 509 |
$fname = static::class . '::' . __FUNCTION__; |
| 510 |
$dbw = $this->getDatabaseProvider()->getPrimaryDatabase(); |
| 511 |
$dbw->newDeleteQueryBuilder() |
| 512 |
->deleteFrom( 'querycache' ) |
| 513 |
->where( [ 'qc_type' => $this->getName() ] ) |
| 514 |
->caller( $fname )->execute(); |
| 515 |
$dbw->newDeleteQueryBuilder() |
| 516 |
->deleteFrom( 'querycachetwo' ) |
| 517 |
->where( [ 'qcc_type' => $this->getName() ] ) |
| 518 |
->caller( $fname )->execute(); |
| 519 |
$dbw->newDeleteQueryBuilder() |
| 520 |
->deleteFrom( 'querycache_info' ) |
| 521 |
->where( [ 'qci_type' => $this->getName() ] ) |
| 522 |
->caller( $fname )->execute(); |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
public function reallyDoQuery( $limit, $offset = false ) { |
| 534 |
$fname = static::class . '::reallyDoQuery'; |
| 535 |
$dbr = $this->getRecacheDB(); |
| 536 |
$query = $this->getQueryInfo(); |
| 537 |
$order = $this->getOrderFields(); |
| 538 |
|
| 539 |
if ( $this->sortDescending() ) { |
| 540 |
foreach ( $order as &$field ) { |
| 541 |
$field .= ' DESC'; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
$tables = isset( $query['tables'] ) ? (array)$query['tables'] : []; |
| 546 |
$fields = isset( $query['fields'] ) ? (array)$query['fields'] : []; |
| 547 |
$conds = isset( $query['conds'] ) ? (array)$query['conds'] : []; |
| 548 |
$options = isset( $query['options'] ) ? (array)$query['options'] : []; |
| 549 |
$join_conds = isset( $query['join_conds'] ) ? (array)$query['join_conds'] : []; |
| 550 |
|
| 551 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 552 |
->tables( $tables ) |
| 553 |
->fields( $fields ) |
| 554 |
->conds( $conds ) |
| 555 |
->caller( $fname ) |
| 556 |
->options( $options ) |
| 557 |
->joinConds( $join_conds ); |
| 558 |
if ( $order ) { |
| 559 |
$queryBuilder->orderBy( $order ); |
| 560 |
} |
| 561 |
|
| 562 |
if ( $limit !== false ) { |
| 563 |
$queryBuilder->limit( intval( $limit ) ); |
| 564 |
} |
| 565 |
|
| 566 |
if ( $offset !== false ) { |
| 567 |
$queryBuilder->offset( intval( $offset ) ); |
| 568 |
} |
| 569 |
|
| 570 |
return $queryBuilder->fetchResultSet(); |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
public function doQuery( $offset = false, $limit = false ) { |
| 580 |
if ( $this->isCached() && $this->isCacheable() ) { |
| 581 |
return $this->fetchFromCache( $limit, $offset ); |
| 582 |
} else { |
| 583 |
return $this->reallyDoQuery( $limit, $offset ); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public function fetchFromCache( $limit, $offset = false ) { |
| 597 |
$dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
| 598 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 599 |
->select( [ 'qc_type', 'namespace' => 'qc_namespace', 'title' => 'qc_title', 'value' => 'qc_value' ] ) |
| 600 |
->from( 'querycache' ) |
| 601 |
->where( [ 'qc_type' => $this->getName() ] ); |
| 602 |
|
| 603 |
if ( $limit !== false ) { |
| 604 |
$queryBuilder->limit( intval( $limit ) ); |
| 605 |
} |
| 606 |
|
| 607 |
if ( $offset !== false ) { |
| 608 |
$queryBuilder->offset( intval( $offset ) ); |
| 609 |
} |
| 610 |
|
| 611 |
$order = $this->getCacheOrderFields(); |
| 612 |
if ( $this->sortDescending() ) { |
| 613 |
$queryBuilder->orderBy( $order, SelectQueryBuilder::SORT_DESC ); |
| 614 |
} else { |
| 615 |
$queryBuilder->orderBy( $order ); |
| 616 |
} |
| 617 |
|
| 618 |
return $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
protected function getCacheOrderFields() { |
| 629 |
return [ 'value' ]; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
public function getCachedTimestamp() { |
| 636 |
if ( $this->cachedTimestamp === null ) { |
| 637 |
$dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
| 638 |
$fname = static::class . '::getCachedTimestamp'; |
| 639 |
$this->cachedTimestamp = $dbr->newSelectQueryBuilder() |
| 640 |
->select( 'qci_timestamp' ) |
| 641 |
->from( 'querycache_info' ) |
| 642 |
->where( [ 'qci_type' => $this->getName() ] ) |
| 643 |
->caller( $fname )->fetchField(); |
| 644 |
} |
| 645 |
return $this->cachedTimestamp; |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
protected function getLimitOffset() { |
| 661 |
[ $limit, $offset ] = $this->getRequest() |
| 662 |
->getLimitOffsetForUser( $this->getUser() ); |
| 663 |
if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 664 |
$maxResults = $this->getMaxResults(); |
| 665 |
|
| 666 |
$limit = min( $limit, $maxResults ); |
| 667 |
|
| 668 |
$offset = min( $offset, $maxResults + 1 ); |
| 669 |
} |
| 670 |
return [ $limit, $offset ]; |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
protected function getDBLimit( $uiLimit, $uiOffset ) { |
| 682 |
$maxResults = $this->getMaxResults(); |
| 683 |
if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) { |
| 684 |
$limit = min( $uiLimit + 1, $maxResults - $uiOffset ); |
| 685 |
return max( $limit, 0 ); |
| 686 |
} else { |
| 687 |
return $uiLimit + 1; |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
protected function getMaxResults() { |
| 702 |
|
| 703 |
return max( $this->getConfig()->get( MainConfigNames::QueryCacheLimit ), 10000 ); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
public function execute( $par ) { |
| 713 |
$this->checkPermissions(); |
| 714 |
|
| 715 |
$this->setHeaders(); |
| 716 |
$this->outputHeader(); |
| 717 |
|
| 718 |
$out = $this->getOutput(); |
| 719 |
|
| 720 |
if ( $this->isCached() && !$this->isCacheable() ) { |
| 721 |
$out->addWikiMsg( 'querypage-disabled' ); |
| 722 |
return; |
| 723 |
} |
| 724 |
|
| 725 |
$out->setSyndicated( $this->isSyndicated() ); |
| 726 |
|
| 727 |
if ( $this->limit == 0 && $this->offset == 0 ) { |
| 728 |
[ $this->limit, $this->offset ] = $this->getLimitOffset(); |
| 729 |
} |
| 730 |
$dbLimit = $this->getDBLimit( $this->limit, $this->offset ); |
| 731 |
|
| 732 |
if ( !$this->isCached() ) { |
| 733 |
|
| 734 |
$res = $this->reallyDoQuery( $dbLimit, $this->offset ); |
| 735 |
} else { |
| 736 |
|
| 737 |
$res = $this->fetchFromCache( $dbLimit, $this->offset ); |
| 738 |
if ( !$this->listoutput ) { |
| 739 |
|
| 740 |
$ts = $this->getCachedTimestamp(); |
| 741 |
$lang = $this->getLanguage(); |
| 742 |
$maxResults = $lang->formatNum( $this->getConfig()->get( |
| 743 |
MainConfigNames::QueryCacheLimit ) ); |
| 744 |
|
| 745 |
if ( $ts ) { |
| 746 |
$user = $this->getUser(); |
| 747 |
$updated = $lang->userTimeAndDate( $ts, $user ); |
| 748 |
$updateddate = $lang->userDate( $ts, $user ); |
| 749 |
$updatedtime = $lang->userTime( $ts, $user ); |
| 750 |
$out->addMeta( 'Data-Cache-Time', $ts ); |
| 751 |
$out->addJsConfigVars( 'dataCacheTime', $ts ); |
| 752 |
$out->addWikiMsg( 'perfcachedts', $updated, $updateddate, $updatedtime, $maxResults ); |
| 753 |
} else { |
| 754 |
$out->addWikiMsg( 'perfcached', $maxResults ); |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
$disabledQueryPages = self::getDisabledQueryPages( $this->getConfig() ); |
| 760 |
if ( isset( $disabledQueryPages[$this->getName()] ) ) { |
| 761 |
$runMode = $disabledQueryPages[$this->getName()]; |
| 762 |
if ( $runMode === 'disabled' ) { |
| 763 |
$out->wrapWikiMsg( |
| 764 |
"<div class=\"mw-querypage-no-updates\">\n$1\n</div>", |
| 765 |
'querypage-no-updates' |
| 766 |
); |
| 767 |
} else { |
| 768 |
|
| 769 |
$out->wrapWikiMsg( |
| 770 |
"<div class=\"mw-querypage-updates-" . $runMode . "\">\n$1\n</div>", |
| 771 |
'querypage-updates-' . $runMode |
| 772 |
); |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
$this->numRows = $res->numRows(); |
| 779 |
|
| 780 |
$dbr = $this->getRecacheDB(); |
| 781 |
$this->preprocessResults( $dbr, $res ); |
| 782 |
|
| 783 |
$out->addHTML( Xml::openElement( 'div', [ 'class' => 'mw-spcontent' ] ) ); |
| 784 |
|
| 785 |
|
| 786 |
if ( $this->shownavigation ) { |
| 787 |
$out->addHTML( $this->getPageHeader() ); |
| 788 |
if ( $this->numRows > 0 ) { |
| 789 |
$out->addHTML( $this->msg( 'showingresultsinrange' )->numParams( |
| 790 |
min( $this->numRows, $this->limit ), |
| 791 |
$this->offset + 1, ( min( $this->numRows, $this->limit ) + $this->offset ) )->parseAsBlock() ); |
| 792 |
|
| 793 |
$miserMaxResults = $this->getConfig()->get( MainConfigNames::MiserMode ) |
| 794 |
&& ( $this->offset + $this->limit >= $this->getMaxResults() ); |
| 795 |
$atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults; |
| 796 |
$paging = $this->buildPrevNextNavigation( $this->offset, |
| 797 |
$this->limit, $this->linkParameters(), $atEnd, $par ); |
| 798 |
$out->addHTML( '<p>' . $paging . '</p>' ); |
| 799 |
} else { |
| 800 |
|
| 801 |
|
| 802 |
$this->showEmptyText(); |
| 803 |
$out->addHTML( Xml::closeElement( 'div' ) ); |
| 804 |
return; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
$this->outputResults( $out, |
| 812 |
$this->getSkin(), |
| 813 |
$dbr, |
| 814 |
$res, |
| 815 |
min( $this->numRows, $this->limit ), |
| 816 |
$this->offset ); |
| 817 |
|
| 818 |
|
| 819 |
if ( $this->shownavigation ) { |
| 820 |
|
| 821 |
$out->addHTML( '<p>' . $paging . '</p>' ); |
| 822 |
} |
| 823 |
|
| 824 |
$out->addHTML( Xml::closeElement( 'div' ) ); |
| 825 |
} |
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) { |
| 841 |
if ( $num > 0 ) { |
| 842 |
$html = []; |
| 843 |
if ( !$this->listoutput ) { |
| 844 |
$html[] = $this->openList( $offset ); |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
for ( $i = 0; $i < $num && $row = $res->fetchObject(); $i++ ) { |
| 851 |
$line = $this->formatResult( $skin, $row ); |
| 852 |
if ( $line ) { |
| 853 |
$html[] = $this->listoutput |
| 854 |
? $line |
| 855 |
: "<li>{$line}</li>\n"; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
if ( !$this->listoutput ) { |
| 860 |
$html[] = $this->closeList(); |
| 861 |
} |
| 862 |
|
| 863 |
$html = $this->listoutput |
| 864 |
? $this->getContentLanguage()->listToText( $html ) |
| 865 |
: implode( '', $html ); |
| 866 |
|
| 867 |
$out->addHTML( $html ); |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
protected function openList( $offset ) { |
| 876 |
return "\n<ol start='" . ( $offset + 1 ) . "' class='special'>\n"; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
protected function closeList() { |
| 883 |
return "</ol>\n"; |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
protected function preprocessResults( $db, $res ) { |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
protected function executeLBFromResultWrapper( IResultWrapper $res, $ns = null ) { |
| 908 |
if ( !$res->numRows() ) { |
| 909 |
return; |
| 910 |
} |
| 911 |
|
| 912 |
$batch = $this->getLinkBatchFactory()->newLinkBatch(); |
| 913 |
foreach ( $res as $row ) { |
| 914 |
$batch->add( $ns ?? (int)$row->namespace, $row->title ); |
| 915 |
} |
| 916 |
$batch->execute(); |
| 917 |
|
| 918 |
$res->seek( 0 ); |
| 919 |
} |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
final protected function setDBLoadBalancer( ILoadBalancer $loadBalancer ) { |
| 927 |
$this->loadBalancer = $loadBalancer; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
final protected function getDBLoadBalancer(): ILoadBalancer { |
| 936 |
if ( $this->loadBalancer === null ) { |
| 937 |
|
| 938 |
|
| 939 |
$this->loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer(); |
| 940 |
} |
| 941 |
return $this->loadBalancer; |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
final protected function setDatabaseProvider( IConnectionProvider $databaseProvider ) { |
| 949 |
$this->databaseProvider = $databaseProvider; |
| 950 |
} |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
final protected function getDatabaseProvider(): IConnectionProvider { |
| 957 |
if ( $this->databaseProvider === null ) { |
| 958 |
$this->databaseProvider = MediaWikiServices::getInstance()->getConnectionProvider(); |
| 959 |
} |
| 960 |
return $this->databaseProvider; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
class_alias( QueryPage::class, 'QueryPage' ); |