29use InvalidArgumentException;
33use MediaWiki\Debug\DeprecationHelper;
34use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
47 use ProtectedHookAccessorTrait;
48 use DeprecationHelper;
105 private $languageConverter;
117 array
$until = [], array $query = []
121 $this->deprecatePublicPropertyFallback(
125 return Title::newFromPageIdentity( $this->page );
134 'mediawiki.action.styles',
140 $this->query = $query;
143 ->getLanguageConverterFactory()->getLanguageConverter();
144 unset( $this->query[
'title'] );
179 $r = $this->
msg(
'category-empty' )->parseAsBlock();
184 'class' =>
'mw-category-generated',
185 'lang' => $lang->getHtmlCode(),
186 'dir' => $lang->getDir()
188 # put a div around the headings which are in the user language
189 $r = Html::rawElement(
'div', $attribs, $r );
195 $this->articles = [];
196 $this->articles_start_char = [];
197 $this->children = [];
198 $this->children_start_char = [];
199 if ( $this->showGallery ) {
201 $mode = $this->
getRequest()->getVal(
'gallerymode',
null );
204 }
catch ( ImageGalleryClassNotFoundException $e ) {
209 $this->gallery->setHideBadImages();
211 $this->imgsNoGallery = [];
212 $this->imgsNoGallery_start_char = [];
230 ->getPageByReference(
$page );
231 if ( !$pageRecord ) {
235 $this->children[] = $this->generateLink(
238 $pageRecord->isRedirect(),
239 htmlspecialchars( str_replace(
'_',
' ', $pageRecord->getDBkey() ) )
242 $this->children_start_char[] =
257 private function generateLink(
262 ->newFromPageReference(
$page );
264 $this->getHookRunner()->onCategoryViewer__generateLink( $type, $legacyTitle, $html, $link );
265 if ( $link ===
null ) {
267 if ( $html !==
null ) {
270 $link = $linkRenderer->makeLink(
$page, $html );
273 $link = Html::rawElement(
275 [
'class' =>
'redirect-in-category' ],
296 ->getPrefixedText( $page );
297 if ( $titleText === $sortkey ) {
303 $firstChar = $this->collation->getFirstLetter( $word );
305 return $this->languageConverter->convert( $firstChar );
316 PageReference $page,
string $sortkey,
int $pageLength,
bool $isRedirect =
false
319 ->newFromPageReference( $page );
320 if ( $this->showGallery ) {
321 $flip = $this->flip[
'file'];
323 $this->gallery->insert( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
325 $this->gallery->add( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
328 $this->imgsNoGallery[] = $this->generateLink(
'image', $page, $isRedirect );
330 $this->imgsNoGallery_start_char[] =
331 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
346 bool $isRedirect =
false
348 $this->articles[] = $this->generateLink(
'page', $page, $isRedirect );
350 $this->articles_start_char[] =
351 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
355 if ( $this->flip[
'subcat'] ) {
356 $this->children = array_reverse( $this->children );
357 $this->children_start_char = array_reverse( $this->children_start_char );
359 if ( $this->flip[
'page'] ) {
360 $this->articles = array_reverse( $this->articles );
361 $this->articles_start_char = array_reverse( $this->articles_start_char );
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 );
370 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
383 $this->flip = [
'page' =>
false,
'subcat' =>
false,
'file' => false ];
385 foreach ( [
'page',
'subcat',
'file' ] as $type ) {
386 # Get the sortkeys for start/end, if applicable. Note that if
387 # the collation in the database differs from the one
388 # set in $wgCategoryCollation, pagination might go totally haywire.
389 $extraConds = [
'cl_type' => $type ];
390 if ( isset( $this->from[$type] ) ) {
391 $extraConds[] = $dbr->expr(
394 $this->collation->getSortKey( $this->from[$type] )
396 } elseif ( isset( $this->until[$type] ) ) {
397 $extraConds[] = $dbr->expr(
400 $this->collation->getSortKey( $this->until[$type] )
402 $this->flip[$type] =
true;
405 $queryBuilder = $dbr->newSelectQueryBuilder();
406 $queryBuilder->select( array_merge(
407 LinkCache::getSelectFields(),
420 ->where( [
'cl_to' => $this->page->getDBkey() ] )
421 ->andWhere( $extraConds )
422 ->useIndex( [
'categorylinks' =>
'cl_sortkey' ] );
424 if ( $this->flip[$type] ) {
425 $queryBuilder->orderBy(
'cl_sortkey', SelectQueryBuilder::SORT_DESC );
427 $queryBuilder->orderBy(
'cl_sortkey' );
431 ->join(
'categorylinks',
null, [
'cl_from = page_id' ] )
432 ->leftJoin(
'category',
null, [
433 'cat_title = page_title',
436 ->limit( $this->limit + 1 )
437 ->caller( __METHOD__ );
439 $res = $queryBuilder->fetchResultSet();
441 $this->getHookRunner()->onCategoryViewer__doCategoryQuery( $type, $res );
442 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
445 foreach ( $res as $row ) {
446 $title = Title::newFromRow( $row );
447 $linkCache->addGoodLinkObjFromRow( $title, $row );
449 if ( $row->cl_collation ===
'' ) {
453 $humanSortkey = $row->cl_sortkey;
455 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
458 if ( ++$count > $this->limit ) {
459 # We've reached the one extra which shows that there
460 # are additional pages to be had. Stop here...
461 $this->nextPage[$type] = $humanSortkey;
464 if ( $count == $this->limit ) {
465 $this->prevPage[$type] = $humanSortkey;
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 );
474 $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
484 $r = $this->getCategoryBottom();
487 :
"<br style=\"clear:both;\"/>\n" . $r;
494 # Don't show subcategories section if there are none.
496 $rescnt = count( $this->children );
497 $dbcnt = $this->cat->getSubcatCount();
499 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'subcat' );
502 # Showing subcategories
503 $r .= Html::openElement(
'div', [
'id' =>
'mw-subcategories' ] ) .
"\n";
504 $r .= Html::rawElement(
'h2', [], $this->msg(
'subcategories' )->parse() ) .
"\n";
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' );
518 $name = $this->getOutput()->getUnprefixedDisplayTitle();
519 # Don't show articles section if there are none.
522 # @todo FIXME: Here and in the other two sections: we don't need to bother
523 # with this rigmarole if the entire category contents fit on one page
524 # and have already been retrieved. We can just use $rescnt in that
525 # case and save a query and some logic.
526 $dbcnt = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES );
527 $rescnt = count( $this->articles );
529 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'article' );
532 $r .= Html::openElement(
'div', [
'id' =>
'mw-pages' ] ) .
"\n";
533 $r .= Html::rawElement(
536 $this->msg(
'category_header' )->rawParams( $name )->parse()
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' );
551 $name = $this->getOutput()->getUnprefixedDisplayTitle();
553 $rescnt = $this->showGallery ?
554 $this->gallery->count() :
555 count( $this->imgsNoGallery ?? [] );
556 $dbcnt = $this->cat->getFileCount();
558 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'file' );
561 $r .= Html::openElement(
'div', [
'id' =>
'mw-category-media' ] ) .
"\n";
562 $r .= Html::rawElement(
565 $this->msg(
'category-media-header' )->rawParams( $name )->parse()
568 $r .= $this->getSectionPagingLinks(
'file' );
569 if ( $this->showGallery ) {
570 $r .= $this->gallery->toHTML();
572 $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
574 $r .= $this->getSectionPagingLinks(
'file' );
575 $r .=
"\n" . Html::closeElement(
'div' );
587 private function getSectionPagingLinks( $type ) {
588 if ( isset( $this->until[$type] ) ) {
593 if ( $this->nextPage[$type] !==
null ) {
594 return $this->pagingLinks(
595 $this->prevPage[$type] ??
'',
603 return $this->pagingLinks(
608 } elseif ( $this->nextPage[$type] !==
null || isset( $this->from[$type] ) ) {
609 return $this->pagingLinks(
610 $this->from[$type] ??
'',
611 $this->nextPage[$type],
636 private function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
638 if ( count( $articles ) > $cutoff ) {
639 $list = self::columnList( $articles, $articles_start_char );
640 } elseif ( count( $articles ) > 0 ) {
642 $list = self::shortList( $articles, $articles_start_char );
645 $pageLang = MediaWikiServices::getInstance()->getTitleFactory()
646 ->newFromPageIdentity( $this->page )
648 $attribs = [
'lang' => $pageLang->getHtmlCode(),
'dir' => $pageLang->getDir(),
649 'class' =>
'mw-content-' . $pageLang->getDir() ];
650 $list = Html::rawElement(
'div', $attribs, $list );
667 $articles_start_char,
668 $cssClasses =
'mw-category mw-category-columns'
670 $columns = array_combine( $articles, $articles_start_char );
672 $ret = Html::openElement(
'div', [
'class' => $cssClasses ] );
676 # Kind of like array_flip() here, but we keep duplicates in an
677 # array instead of dropping them.
678 foreach ( $columns as $article => $char ) {
679 $colContents[$char][] = $article;
682 foreach ( $colContents as $char => $articles ) {
683 # Change space to non-breaking space to keep headers aligned
684 $h3char = $char ===
' ' ?
"\u{00A0}" : htmlspecialchars( $char );
686 $ret .= Html::openElement(
'div', [
'class' =>
'mw-category-group' ] );
687 $ret .= Html::rawElement(
'h3', [], $h3char ) .
"\n";
688 $ret .= Html::openElement(
'ul' );
692 static function ( $article ) {
693 return Html::rawElement(
'li', [], $article );
698 $ret .= Html::closeElement(
'ul' ) . Html::closeElement(
'div' );
702 $ret .= Html::closeElement(
'div' );
714 public static function shortList( $articles, $articles_start_char ) {
715 return self::columnList( $articles, $articles_start_char,
'mw-category' );
727 private function pagingLinks( $first, $last, $type =
'' ) {
728 $prevLink = $this->msg(
'prev-page' )->escaped();
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 ),
743 $nextLink = $this->msg(
'next-page' )->escaped();
746 $lastQuery = $this->query;
747 $lastQuery[
"{$type}from"] = $last;
748 unset( $lastQuery[
"{$type}until"] );
749 $nextLink = $linkRenderer->makeKnownLink(
750 $this->addFragmentToTitle( $this->page, $type ),
757 return $this->msg(
'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped();
768 private function addFragmentToTitle( PageReference $page,
string $section ): LinkTarget {
769 switch ( $section ) {
771 $fragment =
'mw-pages';
774 $fragment =
'mw-subcategories';
777 $fragment =
'mw-category-media';
780 throw new InvalidArgumentException( __METHOD__ .
781 " Invalid section $section." );
798 private function getCountMessage( $rescnt, $dbcnt, $type ) {
813 if ( $type ===
'article' ) {
814 $pagingType =
'page';
819 $fromOrUntil =
false;
820 if ( isset( $this->from[$pagingType] ) || isset( $this->until[$pagingType] ) ) {
824 if ( $dbcnt == $rescnt ||
825 ( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt )
829 } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
836 return $this->msg(
"category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock();
839 return $this->msg(
"category-$type-count" )->numParams( $rescnt, $totalcnt )->parseAsBlock();
844class_alias( CategoryViewer::class,
'CategoryViewer' );
Marks HTML that shouldn't be escaped.
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 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.