264 bool $isRedirect =
false
267 ->newFromPageReference( $page );
268 if ( $this->showGallery ) {
269 $flip = $this->flip[
'file'];
271 $this->gallery->insert( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
273 $this->gallery->add( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
276 $this->imgsNoGallery[] = $this->generateLink(
'image', $page, $isRedirect );
278 $this->imgsNoGallery_start_char[] = $this->getStartKey( $sortkey );
316 $connProvider = MediaWikiServices::getInstance()->getConnectionProvider();
317 $dbr = $connProvider->getReplicaDatabase();
318 $categoryLinksDbr = $connProvider->getReplicaDatabase( CategoryLinksTable::VIRTUAL_DOMAIN );
331 $this->flip = [
'page' =>
false,
'subcat' =>
false,
'file' => false ];
336 $sortField = $this->sortByTimestamp ?
'cl_timestamp' :
'cl_sortkey';
338 foreach ( [
'page',
'subcat',
'file' ] as $type ) {
339 # Get the sort values for start/end, if applicable. Note that if
340 # the collation in the database differs from the one
341 # set in $wgCategoryCollation, pagination might go totally haywire.
342 $extraConds = [
'cl_type' => $type ];
343 if ( isset( $this->from[$type] ) ) {
344 $extraConds[] = $categoryLinksDbr->expr(
347 $this->sortByTimestamp
349 ? $categoryLinksDbr->timestamp( $this->from[$type] )
351 : $this->collation->getSortKey( $this->from[$type] )
353 } elseif ( isset( $this->until[$type] ) ) {
354 $extraConds[] = $categoryLinksDbr->expr(
357 $this->sortByTimestamp
359 ? $categoryLinksDbr->timestamp( $this->until[$type] )
361 : $this->collation->getSortKey( $this->until[$type] )
363 $this->flip[$type] =
true;
366 $queryBuilder = $categoryLinksDbr->newSelectQueryBuilder();
367 $queryBuilder->select( array_merge(
368 LinkCache::getSelectFields(),
377 ->andWhere( $extraConds );
379 if ( $this->flip[$type] ) {
380 $queryBuilder->orderBy( $sortField, SelectQueryBuilder::SORT_DESC );
382 $queryBuilder->orderBy( $sortField );
386 ->join(
'categorylinks',
null, [
'cl_from = page_id' ] )
387 ->join(
'linktarget',
null,
'cl_target_id = lt_id' )
388 ->straightJoin(
'collation',
null,
'cl_collation_id = collation_id' )
389 ->where( [
'lt_title' => $this->page->getDBkey(),
'lt_namespace' =>
NS_CATEGORY ] )
390 ->useIndex( [
'categorylinks' => $this->sortByTimestamp ?
'cl_timestamp_id' :
'cl_sortkey_id' ] )
391 ->limit( $this->limit + 1 );
393 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
395 $categoryTitles = [];
398 foreach ( $res as $row ) {
401 $categoryTitles[] = $row->page_title;
405 $categoryFields = [
'cat_id',
'cat_title',
'cat_subcats',
'cat_pages',
'cat_files' ];
408 if ( $categoryTitles !== [] ) {
409 $categoryRes = $dbr->newSelectQueryBuilder()
410 ->select( $categoryFields )
412 ->where( [
'cat_title' => $categoryTitles ] )
413 ->caller( __METHOD__ )
416 foreach ( $categoryRes as $catRow ) {
417 $categoryData[$catRow->cat_title] = $catRow;
421 foreach ( $pageRows as $row ) {
423 $catRow = $categoryData[$row->page_title] ??
null;
424 foreach ( $categoryFields as $field ) {
425 $row->$field = $catRow->$field ??
null;
432 $this->getHookRunner()->onCategoryViewer__doCategoryQuery( $type, $res );
433 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
436 foreach ( $pageRows as $row ) {
437 $title = Title::newFromRow( $row );
438 $linkCache->addGoodLinkObjFromRow( $title, $row );
440 if ( $this->sortByTimestamp ) {
442 $pageCursor = $row->cl_timestamp;
444 $groupLabel = $this->getLanguage()->userDate( $row->cl_timestamp, $this->getUser() );
446 $pageCursor = $title->getCategorySortkey( $row->cl_sortkey_prefix );
447 $groupLabel = $pageCursor;
450 if ( ++$count > $this->limit ) {
451 # We've reached the one extra which shows that there
452 # are additional pages to be had. Stop here...
453 $this->nextPage[$type] = $pageCursor;
456 if ( $count == $this->limit ) {
457 $this->prevPage[$type] = $pageCursor;
461 $cat = Category::newFromRow( $row, $title );
462 $this->addSubcategoryObject( $cat, $groupLabel, $row->page_len );
463 } elseif ( $title->getNamespace() ===
NS_FILE ) {
464 $this->addImage( $title, $groupLabel, $row->page_len, $row->page_is_redirect );
466 $this->addPage( $title, $groupLabel, $row->page_len, $row->page_is_redirect );
476 # Don't show subcategories section if there are none.
478 $localCount = count( $this->children );
479 $databaseCount = $this->cat->getSubcatCount();
481 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'subcat' );
483 if ( $localCount > 0 ) {
484 $html .= Html::openElement(
'div', [
'id' =>
'mw-subcategories' ] ) .
"\n";
485 $html .= Html::rawElement(
'h2', [], $this->msg(
'subcategories' )->parse() ) .
"\n";
486 $html .= $countMessage;
487 $html .= $this->getSectionPagingLinks(
'subcat' );
488 $html .= $this->formatList( $this->children, $this->children_start_char );
489 $html .= $this->getSectionPagingLinks(
'subcat' );
490 $html .=
"\n" . Html::closeElement(
'div' );
499 $name = $this->getOutput()->getUnprefixedDisplayTitle();
500 # Don't show articles section if there are none.
503 # @todo FIXME: Here and in the other two sections: we don't need to bother
504 # with this rigmarole if the entire category contents fit on one page
505 # and have already been retrieved. We can just use $rescnt in that
506 # case and save a query and some logic.
507 $databaseCount = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES );
508 $localCount = count( $this->articles );
510 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'page' );
512 if ( $localCount > 0 ) {
513 $html .= Html::openElement(
'div', [
'id' =>
'mw-pages' ] ) .
"\n";
514 $html .= Html::rawElement(
517 $this->msg(
'category_header' )->rawParams( $name )->parse()
519 $html .= $countMessage;
520 $html .= $this->getSectionPagingLinks(
'page' );
521 $html .= $this->formatList( $this->articles, $this->articles_start_char );
522 $html .= $this->getSectionPagingLinks(
'page' );
523 $html .=
"\n" . Html::closeElement(
'div' );
532 $name = $this->getOutput()->getUnprefixedDisplayTitle();
534 $localCount = $this->showGallery ?
535 $this->gallery->count() :
536 count( $this->imgsNoGallery );
537 $databaseCount = $this->cat->getFileCount();
539 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'file' );
541 if ( $localCount > 0 ) {
542 $html .= Html::openElement(
'div', [
'id' =>
'mw-category-media' ] ) .
"\n";
543 $html .= Html::rawElement(
546 $this->msg(
'category-media-header' )->rawParams( $name )->parse()
548 $html .= $countMessage;
549 $html .= $this->getSectionPagingLinks(
'file' );
550 if ( $this->showGallery ) {
551 $html .= $this->gallery->toHTML();
553 $html .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
555 $html .= $this->getSectionPagingLinks(
'file' );
556 $html .=
"\n" . Html::closeElement(
'div' );
643 $articles_start_char,
644 $cssClasses =
'mw-category mw-category-columns'
646 $columns = array_combine( $articles, $articles_start_char );
648 $ret = Html::openElement(
'div', [
'class' => $cssClasses ] );
652 # Kind of like array_flip() here, but we keep duplicates in an
653 # array instead of dropping them.
654 foreach ( $columns as $article => $char ) {
655 $colContents[$char][] = $article;
658 foreach ( $colContents as $char => $articles ) {
659 # Change space to non-breaking space to keep headers aligned
660 $h3char = $char ===
' ' ?
"\u{00A0}" : htmlspecialchars( $char );
662 $ret .= Html::openElement(
'div', [
'class' =>
'mw-category-group' ] );
663 $ret .= Html::rawElement(
'h3', [], $h3char ) .
"\n";
664 $ret .= Html::openElement(
'ul' );
668 static fn ( $article ) => Html::rawElement(
'li', [], $article ),
672 $ret .= Html::closeElement(
'ul' ) . Html::closeElement(
'div' );
676 $ret .= Html::closeElement(
'div' );