| 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\Category; |
| 24 |
|
| 25 |
use Collation; |
| 26 |
use HtmlArmor; |
| 27 |
use ImageGalleryBase; |
| 28 |
use ImageGalleryClassNotFoundException; |
| 29 |
use InvalidArgumentException; |
| 30 |
use MediaWiki\Cache\LinkCache; |
| 31 |
use MediaWiki\Context\ContextSource; |
| 32 |
use MediaWiki\Context\IContextSource; |
| 33 |
use MediaWiki\Debug\DeprecationHelper; |
| 34 |
use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 35 |
use MediaWiki\Html\Html; |
| 36 |
use MediaWiki\Language\ILanguageConverter; |
| 37 |
use MediaWiki\Linker\LinkTarget; |
| 38 |
use MediaWiki\MainConfigNames; |
| 39 |
use MediaWiki\MediaWikiServices; |
| 40 |
use MediaWiki\Page\PageIdentity; |
| 41 |
use MediaWiki\Page\PageReference; |
| 42 |
use MediaWiki\Title\Title; |
| 43 |
use MediaWiki\Title\TitleValue; |
| 44 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 45 |
|
| 46 |
class CategoryViewer extends ContextSource { |
| 47 |
use ProtectedHookAccessorTrait; |
| 48 |
use DeprecationHelper; |
| 49 |
|
| 50 |
|
| 51 |
public $limit; |
| 52 |
|
| 53 |
|
| 54 |
public $from; |
| 55 |
|
| 56 |
|
| 57 |
public $until; |
| 58 |
|
| 59 |
|
| 60 |
public $articles; |
| 61 |
|
| 62 |
|
| 63 |
public $articles_start_char; |
| 64 |
|
| 65 |
|
| 66 |
public $children; |
| 67 |
|
| 68 |
|
| 69 |
public $children_start_char; |
| 70 |
|
| 71 |
|
| 72 |
public $showGallery; |
| 73 |
|
| 74 |
|
| 75 |
public $imgsNoGallery_start_char; |
| 76 |
|
| 77 |
|
| 78 |
public $imgsNoGallery; |
| 79 |
|
| 80 |
|
| 81 |
public $nextPage; |
| 82 |
|
| 83 |
|
| 84 |
protected $prevPage; |
| 85 |
|
| 86 |
|
| 87 |
public $flip; |
| 88 |
|
| 89 |
|
| 90 |
protected $page; |
| 91 |
|
| 92 |
|
| 93 |
public $collation; |
| 94 |
|
| 95 |
|
| 96 |
public $gallery; |
| 97 |
|
| 98 |
|
| 99 |
private $cat; |
| 100 |
|
| 101 |
|
| 102 |
private $query; |
| 103 |
|
| 104 |
|
| 105 |
private $languageConverter; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function __construct( PageIdentity $page, IContextSource $context, array $from = [], |
| 117 |
array $until = [], array $query = [] |
| 118 |
) { |
| 119 |
$this->page = $page; |
| 120 |
|
| 121 |
$this->deprecatePublicPropertyFallback( |
| 122 |
'title', |
| 123 |
'1.37', |
| 124 |
function (): Title { |
| 125 |
return Title::newFromPageIdentity( $this->page ); |
| 126 |
}, |
| 127 |
function ( PageIdentity $page ) { |
| 128 |
$this->page = $page; |
| 129 |
} |
| 130 |
); |
| 131 |
|
| 132 |
$this->setContext( $context ); |
| 133 |
$this->getOutput()->addModuleStyles( [ |
| 134 |
'mediawiki.action.styles', |
| 135 |
] ); |
| 136 |
$this->from = $from; |
| 137 |
$this->until = $until; |
| 138 |
$this->limit = $context->getConfig()->get( MainConfigNames::CategoryPagingLimit ); |
| 139 |
$this->cat = Category::newFromTitle( $page ); |
| 140 |
$this->query = $query; |
| 141 |
$this->collation = MediaWikiServices::getInstance()->getCollationFactory()->getCategoryCollation(); |
| 142 |
$this->languageConverter = MediaWikiServices::getInstance() |
| 143 |
->getLanguageConverterFactory()->getLanguageConverter(); |
| 144 |
unset( $this->query['title'] ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function getHTML() { |
| 153 |
$this->showGallery = $this->getConfig()->get( MainConfigNames::CategoryMagicGallery ) |
| 154 |
&& !$this->getOutput()->getNoGallery(); |
| 155 |
|
| 156 |
$this->clearCategoryState(); |
| 157 |
$this->doCategoryQuery(); |
| 158 |
$this->finaliseCategoryState(); |
| 159 |
|
| 160 |
$r = $this->getSubcategorySection() . |
| 161 |
$this->getPagesSection() . |
| 162 |
$this->getImageSection(); |
| 163 |
|
| 164 |
if ( $r == '' ) { |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
$r = $this->getCategoryTop(); |
| 171 |
} else { |
| 172 |
$r = $this->getCategoryTop() . |
| 173 |
$r . |
| 174 |
$this->getCategoryBottom(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
if ( $r == '' ) { |
| 179 |
$r = $this->msg( 'category-empty' )->parseAsBlock(); |
| 180 |
} |
| 181 |
|
| 182 |
$lang = $this->getLanguage(); |
| 183 |
$attribs = [ |
| 184 |
'class' => 'mw-category-generated', |
| 185 |
'lang' => $lang->getHtmlCode(), |
| 186 |
'dir' => $lang->getDir() |
| 187 |
]; |
| 188 |
|
| 189 |
$r = Html::rawElement( 'div', $attribs, $r ); |
| 190 |
|
| 191 |
return $r; |
| 192 |
} |
| 193 |
|
| 194 |
protected function clearCategoryState() { |
| 195 |
$this->articles = []; |
| 196 |
$this->articles_start_char = []; |
| 197 |
$this->children = []; |
| 198 |
$this->children_start_char = []; |
| 199 |
if ( $this->showGallery ) { |
| 200 |
|
| 201 |
$mode = $this->getRequest()->getVal( 'gallerymode', null ); |
| 202 |
try { |
| 203 |
$this->gallery = ImageGalleryBase::factory( $mode, $this->getContext() ); |
| 204 |
} catch ( ImageGalleryClassNotFoundException $e ) { |
| 205 |
|
| 206 |
$this->gallery = ImageGalleryBase::factory( false, $this->getContext() ); |
| 207 |
} |
| 208 |
|
| 209 |
$this->gallery->setHideBadImages(); |
| 210 |
} else { |
| 211 |
$this->imgsNoGallery = []; |
| 212 |
$this->imgsNoGallery_start_char = []; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public function addSubcategoryObject( Category $cat, $sortkey, $pageLength ) { |
| 223 |
$page = $cat->getPage(); |
| 224 |
if ( !$page ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
$pageRecord = MediaWikiServices::getInstance()->getPageStore() |
| 230 |
->getPageByReference( $page ); |
| 231 |
if ( !$pageRecord ) { |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
$this->children[] = $this->generateLink( |
| 236 |
'subcat', |
| 237 |
$pageRecord, |
| 238 |
$pageRecord->isRedirect(), |
| 239 |
htmlspecialchars( str_replace( '_', ' ', $pageRecord->getDBkey() ) ) |
| 240 |
); |
| 241 |
|
| 242 |
$this->children_start_char[] = |
| 243 |
$this->getSubcategorySortChar( $page, $sortkey ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
private function generateLink( |
| 258 |
string $type, PageReference $page, bool $isRedirect, ?string $html = null |
| 259 |
): string { |
| 260 |
$link = null; |
| 261 |
$legacyTitle = MediaWikiServices::getInstance()->getTitleFactory() |
| 262 |
->newFromPageReference( $page ); |
| 263 |
|
| 264 |
$this->getHookRunner()->onCategoryViewer__generateLink( $type, $legacyTitle, $html, $link ); |
| 265 |
if ( $link === null ) { |
| 266 |
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); |
| 267 |
if ( $html !== null ) { |
| 268 |
$html = new HtmlArmor( $html ); |
| 269 |
} |
| 270 |
$link = $linkRenderer->makeLink( $page, $html ); |
| 271 |
} |
| 272 |
if ( $isRedirect ) { |
| 273 |
$link = Html::rawElement( |
| 274 |
'span', |
| 275 |
[ 'class' => 'redirect-in-category' ], |
| 276 |
$link |
| 277 |
); |
| 278 |
} |
| 279 |
|
| 280 |
return $link; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function getSubcategorySortChar( PageIdentity $page, string $sortkey ): string { |
| 295 |
$titleText = MediaWikiServices::getInstance()->getTitleFormatter() |
| 296 |
->getPrefixedText( $page ); |
| 297 |
if ( $titleText === $sortkey ) { |
| 298 |
$word = $page->getDBkey(); |
| 299 |
} else { |
| 300 |
$word = $sortkey; |
| 301 |
} |
| 302 |
|
| 303 |
$firstChar = $this->collation->getFirstLetter( $word ); |
| 304 |
|
| 305 |
return $this->languageConverter->convert( $firstChar ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
public function addImage( |
| 316 |
PageReference $page, string $sortkey, int $pageLength, bool $isRedirect = false |
| 317 |
): void { |
| 318 |
$title = MediaWikiServices::getInstance()->getTitleFactory() |
| 319 |
->newFromPageReference( $page ); |
| 320 |
if ( $this->showGallery ) { |
| 321 |
$flip = $this->flip['file']; |
| 322 |
if ( $flip ) { |
| 323 |
$this->gallery->insert( $title, '', '', '', [], ImageGalleryBase::LOADING_LAZY ); |
| 324 |
} else { |
| 325 |
$this->gallery->add( $title, '', '', '', [], ImageGalleryBase::LOADING_LAZY ); |
| 326 |
} |
| 327 |
} else { |
| 328 |
$this->imgsNoGallery[] = $this->generateLink( 'image', $page, $isRedirect ); |
| 329 |
|
| 330 |
$this->imgsNoGallery_start_char[] = |
| 331 |
$this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
public function addPage( |
| 343 |
PageReference $page, |
| 344 |
string $sortkey, |
| 345 |
int $pageLength, |
| 346 |
bool $isRedirect = false |
| 347 |
): void { |
| 348 |
$this->articles[] = $this->generateLink( 'page', $page, $isRedirect ); |
| 349 |
|
| 350 |
$this->articles_start_char[] = |
| 351 |
$this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) ); |
| 352 |
} |
| 353 |
|
| 354 |
protected function finaliseCategoryState() { |
| 355 |
if ( $this->flip['subcat'] ) { |
| 356 |
$this->children = array_reverse( $this->children ); |
| 357 |
$this->children_start_char = array_reverse( $this->children_start_char ); |
| 358 |
} |
| 359 |
if ( $this->flip['page'] ) { |
| 360 |
$this->articles = array_reverse( $this->articles ); |
| 361 |
$this->articles_start_char = array_reverse( $this->articles_start_char ); |
| 362 |
} |
| 363 |
if ( !$this->showGallery && $this->flip['file'] ) { |
| 364 |
$this->imgsNoGallery = array_reverse( $this->imgsNoGallery ); |
| 365 |
$this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char ); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
protected function doCategoryQuery() { |
| 370 |
$dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 371 |
|
| 372 |
$this->nextPage = [ |
| 373 |
'page' => null, |
| 374 |
'subcat' => null, |
| 375 |
'file' => null, |
| 376 |
]; |
| 377 |
$this->prevPage = [ |
| 378 |
'page' => null, |
| 379 |
'subcat' => null, |
| 380 |
'file' => null, |
| 381 |
]; |
| 382 |
|
| 383 |
$this->flip = [ 'page' => false, 'subcat' => false, 'file' => false ]; |
| 384 |
|
| 385 |
foreach ( [ 'page', 'subcat', 'file' ] as $type ) { |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
$extraConds = [ 'cl_type' => $type ]; |
| 390 |
if ( isset( $this->from[$type] ) ) { |
| 391 |
$extraConds[] = $dbr->expr( |
| 392 |
'cl_sortkey', |
| 393 |
'>=', |
| 394 |
$this->collation->getSortKey( $this->from[$type] ) |
| 395 |
); |
| 396 |
} elseif ( isset( $this->until[$type] ) ) { |
| 397 |
$extraConds[] = $dbr->expr( |
| 398 |
'cl_sortkey', |
| 399 |
'<', |
| 400 |
$this->collation->getSortKey( $this->until[$type] ) |
| 401 |
); |
| 402 |
$this->flip[$type] = true; |
| 403 |
} |
| 404 |
|
| 405 |
$queryBuilder = $dbr->newSelectQueryBuilder(); |
| 406 |
$queryBuilder->select( array_merge( |
| 407 |
LinkCache::getSelectFields(), |
| 408 |
[ |
| 409 |
'cl_sortkey', |
| 410 |
'cat_id', |
| 411 |
'cat_title', |
| 412 |
'cat_subcats', |
| 413 |
'cat_pages', |
| 414 |
'cat_files', |
| 415 |
'cl_sortkey_prefix', |
| 416 |
'cl_collation' |
| 417 |
] |
| 418 |
) ) |
| 419 |
->from( 'page' ) |
| 420 |
->where( [ 'cl_to' => $this->page->getDBkey() ] ) |
| 421 |
->andWhere( $extraConds ) |
| 422 |
->useIndex( [ 'categorylinks' => 'cl_sortkey' ] ); |
| 423 |
|
| 424 |
if ( $this->flip[$type] ) { |
| 425 |
$queryBuilder->orderBy( 'cl_sortkey', SelectQueryBuilder::SORT_DESC ); |
| 426 |
} else { |
| 427 |
$queryBuilder->orderBy( 'cl_sortkey' ); |
| 428 |
} |
| 429 |
|
| 430 |
$queryBuilder |
| 431 |
->join( 'categorylinks', null, [ 'cl_from = page_id' ] ) |
| 432 |
->leftJoin( 'category', null, [ |
| 433 |
'cat_title = page_title', |
| 434 |
'page_namespace' => NS_CATEGORY |
| 435 |
] ) |
| 436 |
->limit( $this->limit + 1 ) |
| 437 |
->caller( __METHOD__ ); |
| 438 |
|
| 439 |
$res = $queryBuilder->fetchResultSet(); |
| 440 |
|
| 441 |
$this->getHookRunner()->onCategoryViewer__doCategoryQuery( $type, $res ); |
| 442 |
$linkCache = MediaWikiServices::getInstance()->getLinkCache(); |
| 443 |
|
| 444 |
$count = 0; |
| 445 |
foreach ( $res as $row ) { |
| 446 |
$title = Title::newFromRow( $row ); |
| 447 |
$linkCache->addGoodLinkObjFromRow( $title, $row ); |
| 448 |
|
| 449 |
if ( $row->cl_collation === '' ) { |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
$humanSortkey = $row->cl_sortkey; |
| 454 |
} else { |
| 455 |
$humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix ); |
| 456 |
} |
| 457 |
|
| 458 |
if ( ++$count > $this->limit ) { |
| 459 |
|
| 460 |
|
| 461 |
$this->nextPage[$type] = $humanSortkey; |
| 462 |
break; |
| 463 |
} |
| 464 |
if ( $count == $this->limit ) { |
| 465 |
$this->prevPage[$type] = $humanSortkey; |
| 466 |
} |
| 467 |
|
| 468 |
if ( $title->getNamespace() === NS_CATEGORY ) { |
| 469 |
$cat = Category::newFromRow( $row, $title ); |
| 470 |
$this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len ); |
| 471 |
} elseif ( $title->getNamespace() === NS_FILE ) { |
| 472 |
$this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect ); |
| 473 |
} else { |
| 474 |
$this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect ); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
protected function getCategoryTop() { |
| 484 |
$r = $this->getCategoryBottom(); |
| 485 |
return $r === '' |
| 486 |
? $r |
| 487 |
: "<br style=\"clear:both;\"/>\n" . $r; |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
protected function getSubcategorySection() { |
| 494 |
|
| 495 |
$r = ''; |
| 496 |
$rescnt = count( $this->children ); |
| 497 |
$dbcnt = $this->cat->getSubcatCount(); |
| 498 |
|
| 499 |
$countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' ); |
| 500 |
|
| 501 |
if ( $rescnt > 0 ) { |
| 502 |
|
| 503 |
$r .= Html::openElement( 'div', [ 'id' => 'mw-subcategories' ] ) . "\n"; |
| 504 |
$r .= Html::rawElement( 'h2', [], $this->msg( 'subcategories' )->parse() ) . "\n"; |
| 505 |
$r .= $countmsg; |
| 506 |
$r .= $this->getSectionPagingLinks( 'subcat' ); |
| 507 |
$r .= $this->formatList( $this->children, $this->children_start_char ); |
| 508 |
$r .= $this->getSectionPagingLinks( 'subcat' ); |
| 509 |
$r .= "\n" . Html::closeElement( 'div' ); |
| 510 |
} |
| 511 |
return $r; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
protected function getPagesSection() { |
| 518 |
$name = $this->getOutput()->getUnprefixedDisplayTitle(); |
| 519 |
|
| 520 |
$r = ''; |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
$dbcnt = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES ); |
| 527 |
$rescnt = count( $this->articles ); |
| 528 |
|
| 529 |
$countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' ); |
| 530 |
|
| 531 |
if ( $rescnt > 0 ) { |
| 532 |
$r .= Html::openElement( 'div', [ 'id' => 'mw-pages' ] ) . "\n"; |
| 533 |
$r .= Html::rawElement( |
| 534 |
'h2', |
| 535 |
[], |
| 536 |
$this->msg( 'category_header' )->rawParams( $name )->parse() |
| 537 |
) . "\n"; |
| 538 |
$r .= $countmsg; |
| 539 |
$r .= $this->getSectionPagingLinks( 'page' ); |
| 540 |
$r .= $this->formatList( $this->articles, $this->articles_start_char ); |
| 541 |
$r .= $this->getSectionPagingLinks( 'page' ); |
| 542 |
$r .= "\n" . Html::closeElement( 'div' ); |
| 543 |
} |
| 544 |
return $r; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
protected function getImageSection() { |
| 551 |
$name = $this->getOutput()->getUnprefixedDisplayTitle(); |
| 552 |
$r = ''; |
| 553 |
$rescnt = $this->showGallery ? |
| 554 |
$this->gallery->count() : |
| 555 |
count( $this->imgsNoGallery ?? [] ); |
| 556 |
$dbcnt = $this->cat->getFileCount(); |
| 557 |
|
| 558 |
$countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' ); |
| 559 |
|
| 560 |
if ( $rescnt > 0 ) { |
| 561 |
$r .= Html::openElement( 'div', [ 'id' => 'mw-category-media' ] ) . "\n"; |
| 562 |
$r .= Html::rawElement( |
| 563 |
'h2', |
| 564 |
[], |
| 565 |
$this->msg( 'category-media-header' )->rawParams( $name )->parse() |
| 566 |
) . "\n"; |
| 567 |
$r .= $countmsg; |
| 568 |
$r .= $this->getSectionPagingLinks( 'file' ); |
| 569 |
if ( $this->showGallery ) { |
| 570 |
$r .= $this->gallery->toHTML(); |
| 571 |
} else { |
| 572 |
$r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char ); |
| 573 |
} |
| 574 |
$r .= $this->getSectionPagingLinks( 'file' ); |
| 575 |
$r .= "\n" . Html::closeElement( 'div' ); |
| 576 |
} |
| 577 |
return $r; |
| 578 |
} |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
private function getSectionPagingLinks( $type ) { |
| 588 |
if ( isset( $this->until[$type] ) ) { |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
if ( $this->nextPage[$type] !== null ) { |
| 594 |
return $this->pagingLinks( |
| 595 |
$this->prevPage[$type] ?? '', |
| 596 |
$this->until[$type], |
| 597 |
$type |
| 598 |
); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
return $this->pagingLinks( |
| 604 |
'', |
| 605 |
$this->until[$type], |
| 606 |
$type |
| 607 |
); |
| 608 |
} elseif ( $this->nextPage[$type] !== null || isset( $this->from[$type] ) ) { |
| 609 |
return $this->pagingLinks( |
| 610 |
$this->from[$type] ?? '', |
| 611 |
$this->nextPage[$type], |
| 612 |
$type |
| 613 |
); |
| 614 |
} |
| 615 |
|
| 616 |
return ''; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
protected function getCategoryBottom() { |
| 623 |
return ''; |
| 624 |
} |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
private function formatList( $articles, $articles_start_char, $cutoff = 6 ) { |
| 637 |
$list = ''; |
| 638 |
if ( count( $articles ) > $cutoff ) { |
| 639 |
$list = self::columnList( $articles, $articles_start_char ); |
| 640 |
} elseif ( count( $articles ) > 0 ) { |
| 641 |
|
| 642 |
$list = self::shortList( $articles, $articles_start_char ); |
| 643 |
} |
| 644 |
|
| 645 |
$pageLang = MediaWikiServices::getInstance()->getTitleFactory() |
| 646 |
->newFromPageIdentity( $this->page ) |
| 647 |
->getPageLanguage(); |
| 648 |
$attribs = [ 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(), |
| 649 |
'class' => 'mw-content-' . $pageLang->getDir() ]; |
| 650 |
$list = Html::rawElement( 'div', $attribs, $list ); |
| 651 |
|
| 652 |
return $list; |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public static function columnList( |
| 666 |
$articles, |
| 667 |
$articles_start_char, |
| 668 |
$cssClasses = 'mw-category mw-category-columns' |
| 669 |
) { |
| 670 |
$columns = array_combine( $articles, $articles_start_char ); |
| 671 |
|
| 672 |
$ret = Html::openElement( 'div', [ 'class' => $cssClasses ] ); |
| 673 |
|
| 674 |
$colContents = []; |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
foreach ( $columns as $article => $char ) { |
| 679 |
$colContents[$char][] = $article; |
| 680 |
} |
| 681 |
|
| 682 |
foreach ( $colContents as $char => $articles ) { |
| 683 |
|
| 684 |
$h3char = $char === ' ' ? "\u{00A0}" : htmlspecialchars( $char ); |
| 685 |
|
| 686 |
$ret .= Html::openElement( 'div', [ 'class' => 'mw-category-group' ] ); |
| 687 |
$ret .= Html::rawElement( 'h3', [], $h3char ) . "\n"; |
| 688 |
$ret .= Html::openElement( 'ul' ); |
| 689 |
$ret .= implode( |
| 690 |
"\n", |
| 691 |
array_map( |
| 692 |
static function ( $article ) { |
| 693 |
return Html::rawElement( 'li', [], $article ); |
| 694 |
}, |
| 695 |
$articles |
| 696 |
) |
| 697 |
); |
| 698 |
$ret .= Html::closeElement( 'ul' ) . Html::closeElement( 'div' ); |
| 699 |
|
| 700 |
} |
| 701 |
|
| 702 |
$ret .= Html::closeElement( 'div' ); |
| 703 |
return $ret; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
public static function shortList( $articles, $articles_start_char ) { |
| 715 |
return self::columnList( $articles, $articles_start_char, 'mw-category' ); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
private function pagingLinks( $first, $last, $type = '' ) { |
| 728 |
$prevLink = $this->msg( 'prev-page' )->escaped(); |
| 729 |
|
| 730 |
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); |
| 731 |
if ( $first != '' ) { |
| 732 |
$prevQuery = $this->query; |
| 733 |
$prevQuery["{$type}until"] = $first; |
| 734 |
unset( $prevQuery["{$type}from"] ); |
| 735 |
$prevLink = $linkRenderer->makeKnownLink( |
| 736 |
$this->addFragmentToTitle( $this->page, $type ), |
| 737 |
new HtmlArmor( $prevLink ), |
| 738 |
[], |
| 739 |
$prevQuery |
| 740 |
); |
| 741 |
} |
| 742 |
|
| 743 |
$nextLink = $this->msg( 'next-page' )->escaped(); |
| 744 |
|
| 745 |
if ( $last != '' ) { |
| 746 |
$lastQuery = $this->query; |
| 747 |
$lastQuery["{$type}from"] = $last; |
| 748 |
unset( $lastQuery["{$type}until"] ); |
| 749 |
$nextLink = $linkRenderer->makeKnownLink( |
| 750 |
$this->addFragmentToTitle( $this->page, $type ), |
| 751 |
new HtmlArmor( $nextLink ), |
| 752 |
[], |
| 753 |
$lastQuery |
| 754 |
); |
| 755 |
} |
| 756 |
|
| 757 |
return $this->msg( 'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped(); |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
private function addFragmentToTitle( PageReference $page, string $section ): LinkTarget { |
| 769 |
switch ( $section ) { |
| 770 |
case 'page': |
| 771 |
$fragment = 'mw-pages'; |
| 772 |
break; |
| 773 |
case 'subcat': |
| 774 |
$fragment = 'mw-subcategories'; |
| 775 |
break; |
| 776 |
case 'file': |
| 777 |
$fragment = 'mw-category-media'; |
| 778 |
break; |
| 779 |
default: |
| 780 |
throw new InvalidArgumentException( __METHOD__ . |
| 781 |
" Invalid section $section." ); |
| 782 |
} |
| 783 |
|
| 784 |
return new TitleValue( $page->getNamespace(), |
| 785 |
$page->getDBkey(), $fragment ); |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
private function getCountMessage( $rescnt, $dbcnt, $type ) { |
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
if ( $type === 'article' ) { |
| 814 |
$pagingType = 'page'; |
| 815 |
} else { |
| 816 |
$pagingType = $type; |
| 817 |
} |
| 818 |
|
| 819 |
$fromOrUntil = false; |
| 820 |
if ( isset( $this->from[$pagingType] ) || isset( $this->until[$pagingType] ) ) { |
| 821 |
$fromOrUntil = true; |
| 822 |
} |
| 823 |
|
| 824 |
if ( $dbcnt == $rescnt || |
| 825 |
( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt ) |
| 826 |
) { |
| 827 |
|
| 828 |
$totalcnt = $dbcnt; |
| 829 |
} elseif ( $rescnt < $this->limit && !$fromOrUntil ) { |
| 830 |
|
| 831 |
$totalcnt = $rescnt; |
| 832 |
} else { |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
return $this->msg( "category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock(); |
| 837 |
} |
| 838 |
|
| 839 |
return $this->msg( "category-$type-count" )->numParams( $rescnt, $totalcnt )->parseAsBlock(); |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
class_alias( CategoryViewer::class, 'CategoryViewer' ); |