12use InvalidArgumentException;
13use MediaWiki\Cache\LinkCache;
16use MediaWiki\Debug\DeprecationHelper;
19use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
34 use ProtectedHookAccessorTrait;
35 use DeprecationHelper;
92 private $languageConverter;
104 array
$until = [], array $query = []
108 $this->deprecatePublicPropertyFallback(
112 return Title::newFromPageIdentity( $this->page );
121 'mediawiki.action.styles',
127 $this->query = $query;
130 ->getLanguageConverterFactory()->getLanguageConverter();
131 unset( $this->query[
'title'] );
141 && !$this->
getOutput()->getOutputFlag( ParserOutputFlags::NO_GALLERY );
166 $html = $this->
msg(
'category-empty' )->parseAsBlock();
171 'class' =>
'mw-category-generated',
172 'lang' => $lang->getHtmlCode(),
173 'dir' => $lang->getDir()
175 # put a div around the headings which are in the user language
176 $html = Html::rawElement(
'div', $attribs, $html );
182 $this->articles = [];
183 $this->articles_start_char = [];
184 $this->children = [];
185 $this->children_start_char = [];
186 if ( $this->showGallery ) {
188 $mode = $this->
getRequest()->getVal(
'gallerymode',
null );
196 $this->gallery->setHideBadImages();
198 $this->imgsNoGallery = [];
199 $this->imgsNoGallery_start_char = [];
217 ->getPageByReference(
$page );
218 if ( !$pageRecord ) {
222 $this->children[] = $this->generateLink(
225 $pageRecord->isRedirect(),
226 htmlspecialchars( str_replace(
'_',
' ', $pageRecord->getDBkey() ) )
229 $this->children_start_char[] =
230 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
244 private function generateLink(
249 ->newFromPageReference(
$page );
251 $this->getHookRunner()->onCategoryViewer__generateLink( $type, $legacyTitle, $html, $link );
252 if ( $link ===
null ) {
254 if ( $html !==
null ) {
257 $link = $linkRenderer->makeLink(
$page, $html );
260 $link = Html::rawElement(
262 [
'class' =>
'redirect-in-category' ],
280 $firstChar = $this->collation->getFirstLetter( $sortkey );
282 return $this->languageConverter->convert( $firstChar );
293 PageReference $page,
string $sortkey,
int $pageLength,
bool $isRedirect =
false
296 ->newFromPageReference( $page );
297 if ( $this->showGallery ) {
298 $flip = $this->flip[
'file'];
300 $this->gallery->insert( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
302 $this->gallery->add( $title,
'',
'',
'', [], ImageGalleryBase::LOADING_LAZY );
305 $this->imgsNoGallery[] = $this->generateLink(
'image', $page, $isRedirect );
307 $this->imgsNoGallery_start_char[] =
308 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
323 bool $isRedirect =
false
325 $this->articles[] = $this->generateLink(
'page', $page, $isRedirect );
327 $this->articles_start_char[] =
328 $this->languageConverter->convert( $this->collation->getFirstLetter( $sortkey ) );
332 if ( $this->flip[
'subcat'] ) {
333 $this->children = array_reverse( $this->children );
334 $this->children_start_char = array_reverse( $this->children_start_char );
336 if ( $this->flip[
'page'] ) {
337 $this->articles = array_reverse( $this->articles );
338 $this->articles_start_char = array_reverse( $this->articles_start_char );
340 if ( !$this->showGallery && $this->flip[
'file'] ) {
341 $this->imgsNoGallery = array_reverse( $this->imgsNoGallery );
342 $this->imgsNoGallery_start_char = array_reverse( $this->imgsNoGallery_start_char );
347 $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase();
360 $this->flip = [
'page' =>
false,
'subcat' =>
false,
'file' => false ];
362 foreach ( [
'page',
'subcat',
'file' ] as $type ) {
363 # Get the sortkeys for start/end, if applicable. Note that if
364 # the collation in the database differs from the one
365 # set in $wgCategoryCollation, pagination might go totally haywire.
366 $extraConds = [
'cl_type' => $type ];
367 if ( isset( $this->from[$type] ) ) {
368 $extraConds[] = $dbr->expr(
371 $this->collation->getSortKey( $this->from[$type] )
373 } elseif ( isset( $this->until[$type] ) ) {
374 $extraConds[] = $dbr->expr(
377 $this->collation->getSortKey( $this->until[$type] )
379 $this->flip[$type] =
true;
382 $queryBuilder = $dbr->newSelectQueryBuilder();
383 $queryBuilder->select( array_merge(
384 LinkCache::getSelectFields(),
397 ->andWhere( $extraConds );
399 if ( $this->flip[$type] ) {
400 $queryBuilder->orderBy(
'cl_sortkey', SelectQueryBuilder::SORT_DESC );
402 $queryBuilder->orderBy(
'cl_sortkey' );
406 ->join(
'categorylinks',
null, [
'cl_from = page_id' ] )
407 ->join(
'linktarget',
null,
'cl_target_id = lt_id' )
408 ->straightJoin(
'collation',
null,
'cl_collation_id = collation_id' )
409 ->where( [
'lt_title' => $this->page->getDBkey(),
'lt_namespace' =>
NS_CATEGORY ] )
410 ->leftJoin(
'category',
null, [
411 'cat_title = page_title',
414 ->useIndex( [
'categorylinks' =>
'cl_sortkey_id' ] )
415 ->limit( $this->limit + 1 );
417 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
419 $this->getHookRunner()->onCategoryViewer__doCategoryQuery( $type, $res );
420 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
423 foreach ( $res as $row ) {
424 $title = Title::newFromRow( $row );
425 $linkCache->addGoodLinkObjFromRow( $title, $row );
426 $humanSortkey = $title->getCategorySortkey( $row->cl_sortkey_prefix );
428 if ( ++$count > $this->limit ) {
429 # We've reached the one extra which shows that there
430 # are additional pages to be had. Stop here...
431 $this->nextPage[$type] = $humanSortkey;
434 if ( $count == $this->limit ) {
435 $this->prevPage[$type] = $humanSortkey;
439 $cat = Category::newFromRow( $row, $title );
440 $this->addSubcategoryObject( $cat, $humanSortkey, $row->page_len );
441 } elseif ( $title->getNamespace() ===
NS_FILE ) {
442 $this->addImage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
444 $this->addPage( $title, $humanSortkey, $row->page_len, $row->page_is_redirect );
454 $html = $this->getCategoryBottom();
457 :
"<br style=\"clear:both;\"/>\n" . $html;
464 # Don't show subcategories section if there are none.
466 $localCount = count( $this->children );
467 $databaseCount = $this->cat->getSubcatCount();
469 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'subcat' );
471 if ( $localCount > 0 ) {
472 $html .= Html::openElement(
'div', [
'id' =>
'mw-subcategories' ] ) .
"\n";
473 $html .= Html::rawElement(
'h2', [], $this->msg(
'subcategories' )->parse() ) .
"\n";
474 $html .= $countMessage;
475 $html .= $this->getSectionPagingLinks(
'subcat' );
476 $html .= $this->formatList( $this->children, $this->children_start_char );
477 $html .= $this->getSectionPagingLinks(
'subcat' );
478 $html .=
"\n" . Html::closeElement(
'div' );
487 $name = $this->getOutput()->getUnprefixedDisplayTitle();
488 # Don't show articles section if there are none.
491 # @todo FIXME: Here and in the other two sections: we don't need to bother
492 # with this rigmarole if the entire category contents fit on one page
493 # and have already been retrieved. We can just use $rescnt in that
494 # case and save a query and some logic.
495 $databaseCount = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES );
496 $localCount = count( $this->articles );
498 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'article' );
500 if ( $localCount > 0 ) {
501 $html .= Html::openElement(
'div', [
'id' =>
'mw-pages' ] ) .
"\n";
502 $html .= Html::rawElement(
505 $this->msg(
'category_header' )->rawParams( $name )->parse()
507 $html .= $countMessage;
508 $html .= $this->getSectionPagingLinks(
'page' );
509 $html .= $this->formatList( $this->articles, $this->articles_start_char );
510 $html .= $this->getSectionPagingLinks(
'page' );
511 $html .=
"\n" . Html::closeElement(
'div' );
520 $name = $this->getOutput()->getUnprefixedDisplayTitle();
522 $localCount = $this->showGallery ?
523 $this->gallery->count() :
524 count( $this->imgsNoGallery ?? [] );
525 $databaseCount = $this->cat->getFileCount();
527 $countMessage = $this->getCountMessage( $localCount, $databaseCount,
'file' );
529 if ( $localCount > 0 ) {
530 $html .= Html::openElement(
'div', [
'id' =>
'mw-category-media' ] ) .
"\n";
531 $html .= Html::rawElement(
534 $this->msg(
'category-media-header' )->rawParams( $name )->parse()
536 $html .= $countMessage;
537 $html .= $this->getSectionPagingLinks(
'file' );
538 if ( $this->showGallery ) {
539 $html .= $this->gallery->toHTML();
541 $html .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
543 $html .= $this->getSectionPagingLinks(
'file' );
544 $html .=
"\n" . Html::closeElement(
'div' );
556 private function getSectionPagingLinks( $type ) {
557 if ( isset( $this->until[$type] ) ) {
562 if ( $this->nextPage[$type] !==
null ) {
563 return $this->pagingLinks(
564 $this->prevPage[$type] ??
'',
572 return $this->pagingLinks(
577 } elseif ( $this->nextPage[$type] !==
null || isset( $this->from[$type] ) ) {
578 return $this->pagingLinks(
579 $this->from[$type] ??
'',
580 $this->nextPage[$type],
605 private function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
607 if ( count( $articles ) > $cutoff ) {
608 $list = self::columnList( $articles, $articles_start_char );
609 } elseif ( count( $articles ) > 0 ) {
611 $list = self::shortList( $articles, $articles_start_char );
614 $pageLang = MediaWikiServices::getInstance()->getTitleFactory()
615 ->newFromPageIdentity( $this->page )
617 $attribs = [
'lang' => $pageLang->getHtmlCode(),
'dir' => $pageLang->getDir(),
618 'class' =>
'mw-content-' . $pageLang->getDir() ];
619 $list = Html::rawElement(
'div', $attribs, $list );
636 $articles_start_char,
637 $cssClasses =
'mw-category mw-category-columns'
639 $columns = array_combine( $articles, $articles_start_char );
641 $ret = Html::openElement(
'div', [
'class' => $cssClasses ] );
645 # Kind of like array_flip() here, but we keep duplicates in an
646 # array instead of dropping them.
647 foreach ( $columns as $article => $char ) {
648 $colContents[$char][] = $article;
651 foreach ( $colContents as $char => $articles ) {
652 # Change space to non-breaking space to keep headers aligned
653 $h3char = $char ===
' ' ?
"\u{00A0}" : htmlspecialchars( $char );
655 $ret .= Html::openElement(
'div', [
'class' =>
'mw-category-group' ] );
656 $ret .= Html::rawElement(
'h3', [], $h3char ) .
"\n";
657 $ret .= Html::openElement(
'ul' );
661 static function ( $article ) {
662 return Html::rawElement(
'li', [], $article );
667 $ret .= Html::closeElement(
'ul' ) . Html::closeElement(
'div' );
671 $ret .= Html::closeElement(
'div' );
683 public static function shortList( $articles, $articles_start_char ) {
684 return self::columnList( $articles, $articles_start_char,
'mw-category' );
696 private function pagingLinks( $first, $last, $type =
'' ) {
697 $prevLink = $this->msg(
'prev-page' )->escaped();
699 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
700 if ( $first !=
'' ) {
701 $prevQuery = $this->query;
702 $prevQuery[
"{$type}until"] = $first;
703 unset( $prevQuery[
"{$type}from"] );
704 $prevLink = $linkRenderer->makeKnownLink(
705 $this->addFragmentToTitle( $this->page, $type ),
712 $nextLink = $this->msg(
'next-page' )->escaped();
715 $lastQuery = $this->query;
716 $lastQuery[
"{$type}from"] = $last;
717 unset( $lastQuery[
"{$type}until"] );
718 $nextLink = $linkRenderer->makeKnownLink(
719 $this->addFragmentToTitle( $this->page, $type ),
720 new HtmlArmor( $nextLink ),
726 return $this->msg(
'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped();
737 private function addFragmentToTitle( PageReference $page,
string $section ): LinkTarget {
738 $fragment = match ( $section ) {
739 'page' =>
'mw-pages',
740 'subcat' =>
'mw-subcategories',
741 'file' =>
'mw-category-media',
742 default =>
throw new InvalidArgumentException( __METHOD__ .
" Invalid section $section." )
757 private function getCountMessage( $localCount, $databaseCount, $type ) {
772 if ( $type ===
'article' ) {
773 $pagingType =
'page';
778 $fromOrUntil =
false;
779 if ( isset( $this->from[$pagingType] ) || isset( $this->until[$pagingType] ) ) {
783 if ( $databaseCount == $localCount ||
784 ( ( $localCount == $this->limit || $fromOrUntil ) && $databaseCount > $localCount )
787 $totalCount = $databaseCount;
788 } elseif ( $localCount < $this->limit && !$fromOrUntil ) {
790 $totalCount = $localCount;
795 return $this->
msg(
"category-$type-count-limited" )->numParams( $localCount )->parseAsBlock();
798 return $this->
msg(
"category-$type-count" )->numParams( $localCount, $totalCount )->parseAsBlock();
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
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.