23use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
32 use ProtectedHookAccessorTrait;
33 use DeprecationHelper;
90 private $languageConverter;
102 array
$until = [], array $query = []
111 return Title::castFromPageIdentity( $this->page );
120 'mediawiki.action.styles',
124 $this->limit = $context->
getConfig()->get( MainConfigNames::CategoryPagingLimit );
125 $this->cat = Category::newFromTitle(
$page );
126 $this->query = $query;
127 $this->collation = MediaWikiServices::getInstance()->getCollationFactory()->getCategoryCollation();
128 $this->languageConverter = MediaWikiServices::getInstance()
129 ->getLanguageConverterFactory()->getLanguageConverter();
130 unset( $this->query[
'title'] );
139 $this->showGallery = $this->
getConfig()->get( MainConfigNames::CategoryMagicGallery )
165 $r = $this->
msg(
'category-empty' )->parseAsBlock();
170 'class' =>
'mw-category-generated',
171 'lang' =>
$lang->getHtmlCode(),
172 'dir' =>
$lang->getDir()
174 # put a div around the headings which are in the user language
175 $r = Html::rawElement(
'div', $attribs, $r );
181 $this->articles = [];
182 $this->articles_start_char = [];
183 $this->children = [];
184 $this->children_start_char = [];
185 if ( $this->showGallery ) {
187 $mode = $this->
getRequest()->getVal(
'gallerymode',
null );
189 $this->gallery = ImageGalleryBase::factory( $mode, $this->
getContext() );
192 $this->gallery = ImageGalleryBase::factory(
false, $this->
getContext() );
195 $this->gallery->setHideBadImages();
197 $this->imgsNoGallery = [];
198 $this->imgsNoGallery_start_char = [];
215 $pageRecord = MediaWikiServices::getInstance()->getPageStore()
216 ->getPageByReference(
$page );
217 if ( !$pageRecord ) {
221 $this->children[] = $this->generateLink(
224 $pageRecord->isRedirect(),
225 htmlspecialchars( str_replace(
'_',
' ', $pageRecord->getDBkey() ) )
228 $this->children_start_char[] =
243 private function generateLink(
247 $legacyTitle = MediaWikiServices::getInstance()->getTitleFactory()
248 ->castFromPageReference(
$page );
250 $this->getHookRunner()->onCategoryViewer__generateLink(
$type, $legacyTitle, $html, $link );
251 if ( $link ===
null ) {
252 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
253 if ( $html !==
null ) {
256 $link = $linkRenderer->makeLink(
$page, $html );
261 [
'class' =>
'redirect-in-category' ],
282 ->getPrefixedText( $page );
283 if ( $titleText === $sortkey ) {
289 $firstChar = $this->collation->getFirstLetter( $word );
291 return $this->languageConverter->convert( $firstChar );
302 PageReference $page,
string $sortkey,
int $pageLength,
bool $isRedirect =
false
305 ->castFromPageReference( $page );
306 if ( $this->showGallery ) {
307 $flip = $this->flip[
'file'];
310 $this->gallery->insert(
$title );
313 $this->gallery->add(
$title );
316 $this->imgsNoGallery[] = $this->generateLink(
'image', $page, $isRedirect );
318 $this->imgsNoGallery_start_char[] =
319 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
331 PageReference $page,
string $sortkey,
int $pageLength,
bool $isRedirect =
false
333 $this->articles[] = $this->generateLink(
'page', $page, $isRedirect );
335 $this->articles_start_char[] =
336 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
340 if ( $this->flip[
'subcat'] ) {
341 $this->children = array_reverse( $this->children );
342 $this->children_start_char = array_reverse( $this->children_start_char );
344 if ( $this->flip[
'page'] ) {
345 $this->articles = array_reverse( $this->articles );
346 $this->articles_start_char = array_reverse( $this->articles_start_char );
348 if ( !$this->showGallery && $this->flip[
'file'] ) {
349 $this->imgsNoGallery = array_reverse( $this->imgsNoGallery );
350 $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char );
368 $this->flip = [
'page' =>
false,
'subcat' =>
false,
'file' => false ];
370 foreach ( [
'page',
'subcat',
'file' ] as
$type ) {
371 # Get the sortkeys for start/end, if applicable. Note that if
372 # the collation in the database differs from the one
373 # set in $wgCategoryCollation, pagination might go totally haywire.
374 $extraConds = [
'cl_type' =>
$type ];
375 if ( isset( $this->from[
$type] ) ) {
376 $extraConds[] =
'cl_sortkey >= '
377 .
$dbr->addQuotes( $this->collation->getSortKey( $this->from[
$type] ) );
378 } elseif ( isset( $this->until[
$type] ) ) {
379 $extraConds[] =
'cl_sortkey < '
380 .
$dbr->addQuotes( $this->collation->getSortKey( $this->until[
$type] ) );
381 $this->flip[
$type] =
true;
384 $queryBuilder =
$dbr->newSelectQueryBuilder();
385 $queryBuilder->select( array_merge(
386 LinkCache::getSelectFields(),
399 ->where( [
'cl_to' => $this->page->getDBkey() ] )
400 ->andWhere( $extraConds )
401 ->useIndex( [
'categorylinks' =>
'cl_sortkey' ] );
403 if ( $this->flip[
$type] ) {
404 $queryBuilder->orderBy(
'cl_sortkey', SelectQueryBuilder::SORT_DESC );
406 $queryBuilder->orderBy(
'cl_sortkey' );
410 ->join(
'categorylinks',
null, [
'cl_from = page_id' ] )
411 ->leftJoin(
'category',
null, [
412 'cat_title = page_title',
415 ->limit( $this->limit + 1 )
416 ->caller( __METHOD__ );
418 $res = $queryBuilder->fetchResultSet();
420 $this->getHookRunner()->onCategoryViewer__doCategoryQuery(
$type,
$res );
421 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
424 foreach (
$res as $row ) {
425 $title = Title::newFromRow( $row );
426 $linkCache->addGoodLinkObjFromRow(
$title, $row );
428 if ( $row->cl_collation ===
'' ) {
432 $humanSortkey = $row->cl_sortkey;
434 $humanSortkey =
$title->getCategorySortkey( $row->cl_sortkey_prefix );
437 if ( ++$count > $this->limit ) {
438 # We've reached the one extra which shows that there
439 # are additional pages to be had. Stop here...
440 $this->nextPage[
$type] = $humanSortkey;
443 if ( $count == $this->limit ) {
444 $this->prevPage[
$type] = $humanSortkey;
448 $cat = Category::newFromRow( $row,
$title );
449 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
451 $this->addImage(
$title, $humanSortkey, $row->page_len, $row->page_is_redirect );
453 $this->addPage(
$title, $humanSortkey, $row->page_len, $row->page_is_redirect );
463 $r = $this->getCategoryBottom();
466 :
"<br style=\"clear:both;\"/>\n" . $r;
473 # Don't show subcategories section if there are none.
475 $rescnt = count( $this->children );
476 $dbcnt = $this->cat->getSubcatCount();
478 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'subcat' );
481 # Showing subcategories
482 $r .= Html::openElement(
'div', [
'id' =>
'mw-subcategories' ] ) .
"\n";
483 $r .= Html::rawElement(
'h2', [], $this->msg(
'subcategories' )->parse() ) .
"\n";
485 $r .= $this->getSectionPagingLinks(
'subcat' );
486 $r .= $this->formatList( $this->children, $this->children_start_char );
487 $r .= $this->getSectionPagingLinks(
'subcat' );
488 $r .=
"\n" . Html::closeElement(
'div' );
497 $name = $this->getOutput()->getUnprefixedDisplayTitle();
498 # Don't show articles section if there are none.
501 # @todo FIXME: Here and in the other two sections: we don't need to bother
502 # with this rigmarole if the entire category contents fit on one page
503 # and have already been retrieved. We can just use $rescnt in that
504 # case and save a query and some logic.
505 $dbcnt = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES );
506 $rescnt = count( $this->articles );
508 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'article' );
511 $r .= Html::openElement(
'div', [
'id' =>
'mw-pages' ] ) .
"\n";
512 $r .= Html::rawElement(
515 $this->msg(
'category_header' )->rawParams( $name )->parse()
518 $r .= $this->getSectionPagingLinks(
'page' );
519 $r .= $this->formatList( $this->articles, $this->articles_start_char );
520 $r .= $this->getSectionPagingLinks(
'page' );
521 $r .=
"\n" . Html::closeElement(
'div' );
530 $name = $this->getOutput()->getUnprefixedDisplayTitle();
532 $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
533 $dbcnt = $this->cat->getFileCount();
535 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'file' );
538 $r .= Html::openElement(
'div', [
'id' =>
'mw-category-media' ] ) .
"\n";
539 $r .= Html::rawElement(
542 $this->msg(
'category-media-header' )->rawParams( $name )->parse()
545 $r .= $this->getSectionPagingLinks(
'file' );
546 if ( $this->showGallery ) {
547 $r .= $this->gallery->toHTML();
549 $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
551 $r .= $this->getSectionPagingLinks(
'file' );
552 $r .=
"\n" . Html::closeElement(
'div' );
564 private function getSectionPagingLinks(
$type ) {
565 if ( isset( $this->until[
$type] ) ) {
570 if ( $this->nextPage[
$type] !==
null ) {
571 return $this->pagingLinks( $this->prevPage[
$type], $this->until[
$type],
$type );
575 return $this->pagingLinks(
'', $this->until[
$type],
$type );
577 } elseif ( $this->nextPage[
$type] !==
null || isset( $this->from[
$type] ) ) {
578 return $this->pagingLinks( $this->from[
$type], $this->nextPage[
$type],
$type );
601 private function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
603 if ( count( $articles ) > $cutoff ) {
604 $list = self::columnList( $articles, $articles_start_char );
605 } elseif ( count( $articles ) > 0 ) {
607 $list = self::shortList( $articles, $articles_start_char );
610 $pageLang = MediaWikiServices::getInstance()->getTitleFactory()
611 ->castFromPageIdentity( $this->page )
613 $attribs = [
'lang' => $pageLang->getHtmlCode(),
'dir' => $pageLang->getDir(),
614 'class' =>
'mw-content-' . $pageLang->getDir() ];
632 $articles_start_char,
633 $cssClasses =
'mw-category mw-category-columns'
635 $columns = array_combine( $articles, $articles_start_char );
637 $ret = Html::openElement(
'div', [
'class' => $cssClasses ] );
641 # Kind of like array_flip() here, but we keep duplicates in an
642 # array instead of dropping them.
643 foreach ( $columns as $article => $char ) {
644 if ( !isset( $colContents[$char] ) ) {
645 $colContents[$char] = [];
647 $colContents[$char][] = $article;
650 foreach ( $colContents as $char => $articles ) {
651 # Change space to non-breaking space to keep headers aligned
652 $h3char = $char ===
' ' ?
"\u{00A0}" : htmlspecialchars( $char );
654 $ret .= Html::openElement(
'div', [
'class' =>
'mw-category-group' ] );
655 $ret .= Html::rawElement(
'h3', [], $h3char ) .
"\n";
656 $ret .= Html::openElement(
'ul' );
660 static function ( $article ) {
661 return Html::rawElement(
'li', [], $article );
666 $ret .= Html::closeElement(
'ul' ) . Html::closeElement(
'div' );
670 $ret .= Html::closeElement(
'div' );
682 public static function shortList( $articles, $articles_start_char ) {
683 return self::columnList( $articles, $articles_start_char,
'mw-category' );
695 private function pagingLinks( $first, $last,
$type =
'' ) {
696 $prevLink = $this->msg(
'prev-page' )->escaped();
698 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
699 if ( $first !=
'' ) {
700 $prevQuery = $this->query;
701 $prevQuery[
"{$type}until"] = $first;
702 unset( $prevQuery[
"{$type}from"] );
703 $prevLink = $linkRenderer->makeKnownLink(
704 $this->addFragmentToTitle( $this->page,
$type ),
711 $nextLink = $this->msg(
'next-page' )->escaped();
714 $lastQuery = $this->query;
715 $lastQuery[
"{$type}from"] = $last;
716 unset( $lastQuery[
"{$type}until"] );
717 $nextLink = $linkRenderer->makeKnownLink(
718 $this->addFragmentToTitle( $this->page,
$type ),
725 return $this->msg(
'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped();
738 switch ( $section ) {
740 $fragment =
'mw-pages';
743 $fragment =
'mw-subcategories';
746 $fragment =
'mw-category-media';
750 " Invalid section $section." );
767 private function getCountMessage( $rescnt, $dbcnt,
$type ) {
782 if (
$type ===
'article' ) {
783 $pagingType =
'page';
788 $fromOrUntil =
false;
789 if ( isset( $this->from[$pagingType] ) || isset( $this->until[$pagingType] ) ) {
793 if ( $dbcnt == $rescnt ||
794 ( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt )
798 } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
805 return $this->msg(
"category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock();
808 return $this->msg(
"category-$type-count" )->numParams( $rescnt, $totalcnt )->parseAsBlock();
deprecatePublicPropertyFallback(string $property, string $version, $getter, $setter=null, $class=null, $component=null)
Mark a removed public property as deprecated and provide fallback getter and setter callables.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
array $articles_start_char
array $children_start_char
__construct(PageIdentity $page, IContextSource $context, array $from=[], array $until=[], array $query=[])
getSubcategorySortChar(PageIdentity $page, string $sortkey)
Get the character to be used for sorting subcategories.
ImageGalleryBase $gallery
addImage(PageReference $page, string $sortkey, int $pageLength, bool $isRedirect=false)
Add a page in the image namespace.
addSubcategoryObject(Category $cat, $sortkey, $pageLength)
Add a subcategory to the internal lists, using a Category object.
addPage(PageReference $page, string $sortkey, int $pageLength, bool $isRedirect=false)
Add a miscellaneous page.
getHTML()
Format the category data list.
array $imgsNoGallery_start_char
static shortList( $articles, $articles_start_char)
Format a list of articles chunked by letter in a bullet list.
static columnList( $articles, $articles_start_char, $cssClasses='mw-category mw-category-columns')
Format a list of articles chunked by letter in a three-column list, ordered vertically.
Category objects are immutable, strictly speaking.
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
getContext()
Get the base IContextSource object.
setContext(IContextSource $context)
Marks HTML that shouldn't be escaped.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Class for exceptions thrown by ImageGalleryBase::factory().
A class containing constants representing the names of configuration variables.
Represents a page (or page fragment) title within MediaWiki.
Represents a title within MediaWiki.
Interface for objects which can provide a MediaWiki context on request.
getConfig()
Get the site configuration.
The shared interface for all language converters.
Interface for objects (potentially) representing an editable wiki page.
if(!isset( $args[0])) $lang