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 $r = $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 $r = Html::rawElement(
'div', $attribs, $r );
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 $e ) {
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->join(
'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 $r = $this->getCategoryBottom();
500 :
"<br style=\"clear:both;\"/>\n" . $r;
507 # Don't show subcategories section if there are none.
509 $rescnt = count( $this->children );
510 $dbcnt = $this->cat->getSubcatCount();
512 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'subcat' );
515 # Showing subcategories
516 $r .= Html::openElement(
'div', [
'id' =>
'mw-subcategories' ] ) .
"\n";
517 $r .= Html::rawElement(
'h2', [], $this->msg(
'subcategories' )->parse() ) .
"\n";
519 $r .= $this->getSectionPagingLinks(
'subcat' );
520 $r .= $this->formatList( $this->children, $this->children_start_char );
521 $r .= $this->getSectionPagingLinks(
'subcat' );
522 $r .=
"\n" . Html::closeElement(
'div' );
531 $name = $this->getOutput()->getUnprefixedDisplayTitle();
532 # Don't show articles section if there are none.
535 # @todo FIXME: Here and in the other two sections: we don't need to bother
536 # with this rigmarole if the entire category contents fit on one page
537 # and have already been retrieved. We can just use $rescnt in that
538 # case and save a query and some logic.
539 $dbcnt = $this->cat->getPageCount( Category::COUNT_CONTENT_PAGES );
540 $rescnt = count( $this->articles );
542 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'article' );
545 $r .= Html::openElement(
'div', [
'id' =>
'mw-pages' ] ) .
"\n";
546 $r .= Html::rawElement(
549 $this->msg(
'category_header' )->rawParams( $name )->parse()
552 $r .= $this->getSectionPagingLinks(
'page' );
553 $r .= $this->formatList( $this->articles, $this->articles_start_char );
554 $r .= $this->getSectionPagingLinks(
'page' );
555 $r .=
"\n" . Html::closeElement(
'div' );
564 $name = $this->getOutput()->getUnprefixedDisplayTitle();
566 $rescnt = $this->showGallery ?
567 $this->gallery->count() :
568 count( $this->imgsNoGallery ?? [] );
569 $dbcnt = $this->cat->getFileCount();
571 $countmsg = $this->getCountMessage( $rescnt, $dbcnt,
'file' );
574 $r .= Html::openElement(
'div', [
'id' =>
'mw-category-media' ] ) .
"\n";
575 $r .= Html::rawElement(
578 $this->msg(
'category-media-header' )->rawParams( $name )->parse()
581 $r .= $this->getSectionPagingLinks(
'file' );
582 if ( $this->showGallery ) {
583 $r .= $this->gallery->toHTML();
585 $r .= $this->formatList( $this->imgsNoGallery, $this->imgsNoGallery_start_char );
587 $r .= $this->getSectionPagingLinks(
'file' );
588 $r .=
"\n" . Html::closeElement(
'div' );
600 private function getSectionPagingLinks( $type ) {
601 if ( isset( $this->until[$type] ) ) {
606 if ( $this->nextPage[$type] !==
null ) {
607 return $this->pagingLinks(
608 $this->prevPage[$type] ??
'',
616 return $this->pagingLinks(
621 } elseif ( $this->nextPage[$type] !==
null || isset( $this->from[$type] ) ) {
622 return $this->pagingLinks(
623 $this->from[$type] ??
'',
624 $this->nextPage[$type],
649 private function formatList( $articles, $articles_start_char, $cutoff = 6 ) {
651 if ( count( $articles ) > $cutoff ) {
652 $list = self::columnList( $articles, $articles_start_char );
653 } elseif ( count( $articles ) > 0 ) {
655 $list = self::shortList( $articles, $articles_start_char );
658 $pageLang = MediaWikiServices::getInstance()->getTitleFactory()
659 ->newFromPageIdentity( $this->page )
661 $attribs = [
'lang' => $pageLang->getHtmlCode(),
'dir' => $pageLang->getDir(),
662 'class' =>
'mw-content-' . $pageLang->getDir() ];
663 $list = Html::rawElement(
'div', $attribs, $list );
680 $articles_start_char,
681 $cssClasses =
'mw-category mw-category-columns'
683 $columns = array_combine( $articles, $articles_start_char );
685 $ret = Html::openElement(
'div', [
'class' => $cssClasses ] );
689 # Kind of like array_flip() here, but we keep duplicates in an
690 # array instead of dropping them.
691 foreach ( $columns as $article => $char ) {
692 $colContents[$char][] = $article;
695 foreach ( $colContents as $char => $articles ) {
696 # Change space to non-breaking space to keep headers aligned
697 $h3char = $char ===
' ' ?
"\u{00A0}" : htmlspecialchars( $char );
699 $ret .= Html::openElement(
'div', [
'class' =>
'mw-category-group' ] );
700 $ret .= Html::rawElement(
'h3', [], $h3char ) .
"\n";
701 $ret .= Html::openElement(
'ul' );
705 static function ( $article ) {
706 return Html::rawElement(
'li', [], $article );
711 $ret .= Html::closeElement(
'ul' ) . Html::closeElement(
'div' );
715 $ret .= Html::closeElement(
'div' );
727 public static function shortList( $articles, $articles_start_char ) {
728 return self::columnList( $articles, $articles_start_char,
'mw-category' );
740 private function pagingLinks( $first, $last, $type =
'' ) {
741 $prevLink = $this->msg(
'prev-page' )->escaped();
743 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
744 if ( $first !=
'' ) {
745 $prevQuery = $this->query;
746 $prevQuery[
"{$type}until"] = $first;
747 unset( $prevQuery[
"{$type}from"] );
748 $prevLink = $linkRenderer->makeKnownLink(
749 $this->addFragmentToTitle( $this->page, $type ),
756 $nextLink = $this->msg(
'next-page' )->escaped();
759 $lastQuery = $this->query;
760 $lastQuery[
"{$type}from"] = $last;
761 unset( $lastQuery[
"{$type}until"] );
762 $nextLink = $linkRenderer->makeKnownLink(
763 $this->addFragmentToTitle( $this->page, $type ),
764 new HtmlArmor( $nextLink ),
770 return $this->msg(
'categoryviewer-pagedlinks' )->rawParams( $prevLink, $nextLink )->escaped();
781 private function addFragmentToTitle( PageReference $page,
string $section ): LinkTarget {
782 switch ( $section ) {
784 $fragment =
'mw-pages';
787 $fragment =
'mw-subcategories';
790 $fragment =
'mw-category-media';
793 throw new InvalidArgumentException( __METHOD__ .
794 " Invalid section $section." );
811 private function getCountMessage( $rescnt, $dbcnt, $type ) {
826 if ( $type ===
'article' ) {
827 $pagingType =
'page';
832 $fromOrUntil =
false;
833 if ( isset( $this->from[$pagingType] ) || isset( $this->until[$pagingType] ) ) {
837 if ( $dbcnt == $rescnt ||
838 ( ( $rescnt == $this->limit || $fromOrUntil ) && $dbcnt > $rescnt )
842 } elseif ( $rescnt < $this->limit && !$fromOrUntil ) {
849 return $this->msg(
"category-$type-count-limited" )->numParams( $rescnt )->parseAsBlock();
852 return $this->msg(
"category-$type-count" )->numParams( $rescnt, $totalcnt )->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.