28use InvalidArgumentException;
32use MediaWiki\Debug\DeprecationHelper;
33use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
48 use ProtectedHookAccessorTrait;
49 use DeprecationHelper;
106 private $languageConverter;
108 private int $migrationStage;
120 array
$until = [], array $query = []
124 $this->deprecatePublicPropertyFallback(
128 return Title::newFromPageIdentity( $this->page );
137 'mediawiki.action.styles',
144 $this->query = $query;
147 ->getLanguageConverterFactory()->getLanguageConverter();
148 unset( $this->query[
'title'] );
158 && !$this->
getOutput()->getOutputFlag( ParserOutputFlags::NO_GALLERY );
183 $html = $this->
msg(
'category-empty' )->parseAsBlock();
188 'class' =>
'mw-category-generated',
189 'lang' => $lang->getHtmlCode(),
190 'dir' => $lang->getDir()
192 # put a div around the headings which are in the user language
193 $html = Html::rawElement(
'div', $attribs, $html );
199 $this->articles = [];
200 $this->articles_start_char = [];
201 $this->children = [];
202 $this->children_start_char = [];
203 if ( $this->showGallery ) {
205 $mode = $this->
getRequest()->getVal(
'gallerymode',
null );
208 }
catch ( ImageGalleryClassNotFoundException ) {
213 $this->gallery->setHideBadImages();
215 $this->imgsNoGallery = [];
216 $this->imgsNoGallery_start_char = [];
234 ->getPageByReference(
$page );
235 if ( !$pageRecord ) {
239 $this->children[] = $this->generateLink(
242 $pageRecord->isRedirect(),
243 htmlspecialchars( str_replace(
'_',
' ', $pageRecord->getDBkey() ) )
246 $this->children_start_char[] =
261 private function generateLink(
266 ->newFromPageReference(
$page );
268 $this->getHookRunner()->onCategoryViewer__generateLink( $type, $legacyTitle, $html, $link );
269 if ( $link ===
null ) {
271 if ( $html !==
null ) {
274 $link = $linkRenderer->makeLink(
$page, $html );
277 $link = Html::rawElement(
279 [
'class' =>
'redirect-in-category' ],
300 ->getPrefixedText( $page );
301 if ( $titleText === $sortkey ) {
307 $firstChar = $this->collation->getFirstLetter( $word );
309 return $this->languageConverter->convert( $firstChar );
320 PageReference $page,
string $sortkey,
int $pageLength,
bool $isRedirect =
false
323 ->newFromPageReference( $page );
324 if ( $this->showGallery ) {
325 $flip = $this->flip[
'file'];
327 $this->gallery->insert( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
329 $this->gallery->add( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
332 $this->imgsNoGallery[] = $this->generateLink(
'image', $page, $isRedirect );
334 $this->imgsNoGallery_start_char[] =
335 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
350 bool $isRedirect =
false
352 $this->articles[] = $this->generateLink(
'page', $page, $isRedirect );
354 $this->articles_start_char[] =
355 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
359 if ( $this->flip[
'subcat'] ) {
360 $this->children = array_reverse( $this->children );
361 $this->children_start_char = array_reverse( $this->children_start_char );
363 if ( $this->flip[
'page'] ) {
364 $this->articles = array_reverse( $this->articles );
365 $this->articles_start_char = array_reverse( $this->articles_start_char );
367 if ( !$this->showGallery && $this->flip[
'file'] ) {
368 $this->imgsNoGallery = array_reverse( $this->imgsNoGallery );
369 $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char );
374 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
387 $this->flip = [
'page' =>
false,
'subcat' =>
false,
'file' => false ];
389 foreach ( [
'page',
'subcat',
'file' ] as $type ) {
390 # Get the sortkeys for start/end, if applicable. Note that if
391 # the collation in the database differs from the one
392 # set in $wgCategoryCollation, pagination might go totally haywire.
393 $extraConds = [
'cl_type' => $type ];
394 if ( isset( $this->from[$type] ) ) {
395 $extraConds[] = $dbr->expr(
398 $this->collation->getSortKey( $this->from[$type] )
400 } elseif ( isset( $this->until[$type] ) ) {
401 $extraConds[] = $dbr->expr(
404 $this->collation->getSortKey( $this->until[$type] )
406 $this->flip[$type] =
true;
409 $queryBuilder = $dbr->newSelectQueryBuilder();
410 $queryBuilder->select( array_merge(
411 LinkCache::getSelectFields(),
423 ->andWhere( $extraConds );
425 if ( $this->flip[$type] ) {
426 $queryBuilder->orderBy(
'cl_sortkey', SelectQueryBuilder::SORT_DESC );
428 $queryBuilder->orderBy(
'cl_sortkey' );
432 ->join(
'categorylinks',
null, [
'cl_from = page_id' ] )
433 ->leftJoin(
'category',
null, [
434 'cat_title = page_title',
437 ->limit( $this->limit + 1 );
439 $queryBuilder->where( [
'cl_to' => $this->page->getDBkey() ] )
440 ->field(
'cl_collation' )
441 ->useIndex( [
'categorylinks' =>
'cl_sortkey' ] );
443 $queryBuilder->join(
'linktarget',
null,
'cl_target_id = lt_id' )
444 ->where( [
'lt_title' => $this->page->getDBkey(),
'lt_namespace' =>
NS_CATEGORY ] );
446 $queryBuilder->straightJoin(
'collation',
null,
'cl_collation_id = collation_id' )
447 ->field(
'collation_name',
'cl_collation' );
449 $queryBuilder->useIndex( [
'categorylinks' =>
'cl_sortkey_id' ] );
452 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
454 $this->getHookRunner()->onCategoryViewer__doCategoryQuery( $type, $res );
455 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
458 foreach ( $res as $row ) {
459 $title = Title::newFromRow( $row );
460 $linkCache->addGoodLinkObjFromRow( $title, $row );
462 if ( $row->cl_collation ===
'' ) {
466 $humanSortkey = $row->cl_sortkey;
468 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
471 if ( ++$count > $this->limit ) {
472 # We've reached the one extra which shows that there
473 # are additional pages to be had. Stop here...
474 $this->nextPage[$type] = $humanSortkey;
477 if ( $count == $this->limit ) {
478 $this->prevPage[$type] = $humanSortkey;
482 $cat = Category::newFromRow( $row, $title );
483 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
484 } elseif ( $title->getNamespace() ===
NS_FILE ) {
485 $this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
487 $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
497 $html = $this->getCategoryBottom();
500 :
"<br style=\"clear:both;\"/>\n" . $html;
507 # Don't show subcategories section if there are none.
509 $localCount = count( $this->children );
510 $databaseCount = $this->cat->getSubcatCount();
512 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'subcat' );
514 if ( $localCount > 0 ) {
515 $html .= Html::openElement(
'div', [
'id' =>
'mw-subcategories' ] ) .
"\n";
516 $html .= Html::rawElement(
'h2', [], $this->msg(
'subcategories' )->parse() ) .
"\n";
517 $html .= $countMessage;
518 $html .= $this->getSectionPagingLinks(
'subcat' );
519 $html .= $this->formatList( $this->children, $this->children_start_char );
520 $html .= $this->getSectionPagingLinks(
'subcat' );
521 $html .=
"\n" . Html::closeElement(
'div' );
530 $name = $this->getOutput()->getUnprefixedDisplayTitle();
531 # Don't show articles section if there are none.
534 # @todo FIXME: Here and in the other two sections: we don't need to bother
535 # with this rigmarole if the entire category contents fit on one page
536 # and have already been retrieved. We can just use $rescnt in that
537 # case and save a query and some logic.
538 $databaseCount = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES );
539 $localCount = count( $this->articles );
541 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'article' );
543 if ( $localCount > 0 ) {
544 $html .= Html::openElement(
'div', [
'id' =>
'mw-pages' ] ) .
"\n";
545 $html .= Html::rawElement(
548 $this->msg(
'category_header' )->rawParams( $name )->parse()
550 $html .= $countMessage;
551 $html .= $this->getSectionPagingLinks(
'page' );
552 $html .= $this->formatList( $this->articles, $this->articles_start_char );
553 $html .= $this->getSectionPagingLinks(
'page' );
554 $html .=
"\n" . Html::closeElement(
'div' );
563 $name = $this->getOutput()->getUnprefixedDisplayTitle();
565 $localCount = $this->showGallery ?
566 $this->gallery->count() :
567 count( $this->imgsNoGallery ?? [] );
568 $databaseCount = $this->cat->getFileCount();
570 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'file' );
572 if ( $localCount > 0 ) {
573 $html .= Html::openElement(
'div', [
'id' =>
'mw-category-media' ] ) .
"\n";
574 $html .= Html::rawElement(
577 $this->msg(
'category-media-header' )->rawParams( $name )->parse()
579 $html .= $countMessage;
580 $html .= $this->getSectionPagingLinks(
'file' );
581 if ( $this->showGallery ) {
582 $html .= $this->gallery->toHTML();
584 $html .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
586 $html .= $this->getSectionPagingLinks(
'file' );
587 $html .=
"\n" . Html::closeElement(
'div' );
599 private function getSectionPagingLinks( $type ) {
600 if ( isset( $this->until[$type] ) ) {
605 if ( $this->nextPage[$type] !==
null ) {
606 return $this->pagingLinks(
607 $this->prevPage[$type] ??
'',
615 return $this->pagingLinks(
620 } elseif ( $this->nextPage[$type] !==
null || isset( $this->from[$type] ) ) {
621 return $this->pagingLinks(
622 $this->from[$type] ??
'',
623 $this->nextPage[$type],
648 private function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
650 if ( count( $articles ) > $cutoff ) {
651 $list = self::columnList( $articles, $articles_start_char );
652 } elseif ( count( $articles ) > 0 ) {
654 $list = self::shortList( $articles, $articles_start_char );
657 $pageLang = MediaWikiServices::getInstance()->getTitleFactory()
658 ->newFromPageIdentity( $this->page )
660 $attribs = [
'lang' => $pageLang->getHtmlCode(),
'dir' => $pageLang->getDir(),
661 'class' =>
'mw-content-' . $pageLang->getDir() ];
662 $list = Html::rawElement(
'div', $attribs, $list );
679 $articles_start_char,
680 $cssClasses =
'mw-category mw-category-columns'
682 $columns = array_combine( $articles, $articles_start_char );
684 $ret = Html::openElement(
'div', [
'class' => $cssClasses ] );
688 # Kind of like array_flip() here, but we keep duplicates in an
689 # array instead of dropping them.
690 foreach ( $columns as $article => $char ) {
691 $colContents[$char][] = $article;
694 foreach ( $colContents as $char => $articles ) {
695 # Change space to non-breaking space to keep headers aligned
696 $h3char = $char ===
' ' ?
"\u{00A0}" : htmlspecialchars( $char );
698 $ret .= Html::openElement(
'div', [
'class' =>
'mw-category-group' ] );
699 $ret .= Html::rawElement(
'h3', [], $h3char ) .
"\n";
700 $ret .= Html::openElement(
'ul' );
704 static function ( $article ) {
705 return Html::rawElement(
'li', [], $article );
710 $ret .= Html::closeElement(
'ul' ) . Html::closeElement(
'div' );
714 $ret .= Html::closeElement(
'div' );
726 public static function shortList( $articles, $articles_start_char ) {
727 return self::columnList( $articles, $articles_start_char,
'mw-category' );
739 private function pagingLinks( $first, $last, $type =
'' ) {
740 $prevLink = $this->msg(
'prev-page' )->escaped();
742 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
743 if ( $first !=
'' ) {
744 $prevQuery = $this->query;
745 $prevQuery[
"{$type}until"] = $first;
746 unset( $prevQuery[
"{$type}from"] );
747 $prevLink = $linkRenderer->makeKnownLink(
748 $this->addFragmentToTitle( $this->page, $type ),
755 $nextLink = $this->msg(
'next-page' )->escaped();
758 $lastQuery = $this->query;
759 $lastQuery[
"{$type}from"] = $last;
760 unset( $lastQuery[
"{$type}until"] );
761 $nextLink = $linkRenderer->makeKnownLink(
762 $this->addFragmentToTitle( $this->page, $type ),
763 new HtmlArmor( $nextLink ),
769 return $this->msg(
'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped();
780 private function addFragmentToTitle( PageReference $page,
string $section ): LinkTarget {
781 switch ( $section ) {
783 $fragment =
'mw-pages';
786 $fragment =
'mw-subcategories';
789 $fragment =
'mw-category-media';
792 throw new InvalidArgumentException( __METHOD__ .
793 " Invalid section $section." );
810 private function getCountMessage( $localCount, $databaseCount, $type ) {
825 if ( $type ===
'article' ) {
826 $pagingType =
'page';
831 $fromOrUntil =
false;
832 if ( isset( $this->from[$pagingType] ) || isset( $this->until[$pagingType] ) ) {
836 if ( $databaseCount == $localCount ||
837 ( ( $localCount == $this->limit || $fromOrUntil ) && $databaseCount > $localCount )
840 $totalCount = $databaseCount;
841 } elseif ( $localCount < $this->limit && !$fromOrUntil ) {
843 $totalCount = $localCount;
848 return $this->msg(
"category-$type-count-limited" )->numParams( $localCount )->parseAsBlock();
851 return $this->msg(
"category-$type-count" )->numParams( $localCount, $totalCount )->parseAsBlock();
const SCHEMA_COMPAT_READ_OLD
static factory( $mode=false, ?IContextSource $context=null)
Get a new image gallery.
Class for exceptions thrown by ImageGalleryBase::factory().
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
setContext(IContextSource $context)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
getContext()
Get the base IContextSource object.
A class containing constants representing the names of configuration variables.
const CategoryLinksSchemaMigrationStage
Name constant for the CategoryLinksSchemaMigrationStage setting, for use with Config::get()
const CategoryPagingLimit
Name constant for the CategoryPagingLimit setting, for use with Config::get()
const CategoryMagicGallery
Name constant for the CategoryMagicGallery setting, for use with Config::get()
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.
Interface for objects (potentially) representing an editable wiki page.