*
* @file
*/
+use MediaWiki\MediaWikiServices;
class CategoryViewer extends ContextSource {
/** @var int */
* @param array $until An array with 3 keys for until of each section (since 1.17)
* @param array $query
*/
- function __construct( $title, IContextSource $context, $from = array(),
- $until = array(), $query = array()
+ function __construct( $title, IContextSource $context, $from = [],
+ $until = [], $query = []
) {
$this->title = $title;
$this->setContext( $context );
- $this->getOutput()->addModuleStyles( array(
+ $this->getOutput()->addModuleStyles( [
'mediawiki.action.view.categoryPage.styles'
- ) );
+ ] );
$this->from = $from;
$this->until = $until;
$this->limit = $context->getConfig()->get( 'CategoryPagingLimit' );
}
$lang = $this->getLanguage();
- $langAttribs = array( 'lang' => $lang->getHtmlCode(), 'dir' => $lang->getDir() );
+ $attribs = [
+ 'class' => 'mw-category-generated',
+ 'lang' => $lang->getHtmlCode(),
+ 'dir' => $lang->getDir()
+ ];
# put a div around the headings which are in the user language
- $r = Html::openElement( 'div', $langAttribs ) . $r . '</div>';
+ $r = Html::openElement( 'div', $attribs ) . $r . '</div>';
return $r;
}
function clearCategoryState() {
- $this->articles = array();
- $this->articles_start_char = array();
- $this->children = array();
- $this->children_start_char = array();
+ $this->articles = [];
+ $this->articles_start_char = [];
+ $this->children = [];
+ $this->children_start_char = [];
if ( $this->showGallery ) {
// Note that null for mode is taken to mean use default.
$mode = $this->getRequest()->getVal( 'gallerymode', null );
$this->gallery->setHideBadImages();
} else {
- $this->imgsNoGallery = array();
- $this->imgsNoGallery_start_char = array();
+ $this->imgsNoGallery = [];
+ $this->imgsNoGallery_start_char = [];
}
}
function generateLink( $type, Title $title, $isRedirect, $html = null ) {
$link = null;
- Hooks::run( 'CategoryViewer::generateLink', array( $type, $title, $html, &$link ) );
+ Hooks::run( 'CategoryViewer::generateLink', [ $type, $title, $html, &$link ] );
if ( $link === null ) {
$link = Linker::link( $title, $html );
}
}
function doCategoryQuery() {
- $dbr = wfGetDB( DB_SLAVE, 'category' );
+ $dbr = wfGetDB( DB_REPLICA, 'category' );
- $this->nextPage = array(
+ $this->nextPage = [
'page' => null,
'subcat' => null,
'file' => null,
- );
- $this->prevPage = array(
+ ];
+ $this->prevPage = [
'page' => null,
'subcat' => null,
'file' => null,
- );
+ ];
- $this->flip = array( 'page' => false, 'subcat' => false, 'file' => false );
+ $this->flip = [ 'page' => false, 'subcat' => false, 'file' => false ];
- foreach ( array( 'page', 'subcat', 'file' ) as $type ) {
+ foreach ( [ 'page', 'subcat', 'file' ] as $type ) {
# Get the sortkeys for start/end, if applicable. Note that if
# the collation in the database differs from the one
# set in $wgCategoryCollation, pagination might go totally haywire.
- $extraConds = array( 'cl_type' => $type );
+ $extraConds = [ 'cl_type' => $type ];
if ( isset( $this->from[$type] ) && $this->from[$type] !== null ) {
$extraConds[] = 'cl_sortkey >= '
. $dbr->addQuotes( $this->collation->getSortKey( $this->from[$type] ) );
}
$res = $dbr->select(
- array( 'page', 'categorylinks', 'category' ),
- array( 'page_id', 'page_title', 'page_namespace', 'page_len',
- 'page_is_redirect', 'cl_sortkey', 'cat_id', 'cat_title',
- 'cat_subcats', 'cat_pages', 'cat_files',
- 'cl_sortkey_prefix', 'cl_collation' ),
- array_merge( array( 'cl_to' => $this->title->getDBkey() ), $extraConds ),
+ [ 'page', 'categorylinks', 'category' ],
+ array_merge(
+ LinkCache::getSelectFields(),
+ [
+ 'page_namespace',
+ 'page_title',
+ 'cl_sortkey',
+ 'cat_id',
+ 'cat_title',
+ 'cat_subcats',
+ 'cat_pages',
+ 'cat_files',
+ 'cl_sortkey_prefix',
+ 'cl_collation'
+ ]
+ ),
+ array_merge( [ 'cl_to' => $this->title->getDBkey() ], $extraConds ),
__METHOD__,
- array(
- 'USE INDEX' => array( 'categorylinks' => 'cl_sortkey' ),
+ [
+ 'USE INDEX' => [ 'categorylinks' => 'cl_sortkey' ],
'LIMIT' => $this->limit + 1,
'ORDER BY' => $this->flip[$type] ? 'cl_sortkey DESC' : 'cl_sortkey',
- ),
- array(
- 'categorylinks' => array( 'INNER JOIN', 'cl_from = page_id' ),
- 'category' => array( 'LEFT JOIN', array(
+ ],
+ [
+ 'categorylinks' => [ 'INNER JOIN', 'cl_from = page_id' ],
+ 'category' => [ 'LEFT JOIN', [
'cat_title = page_title',
'page_namespace' => NS_CATEGORY
- ) )
- )
+ ] ]
+ ]
);
- Hooks::run( 'CategoryViewer::doCategoryQuery', array( $type, $res ) );
+ Hooks::run( 'CategoryViewer::doCategoryQuery', [ $type, $res ] );
+ $linkCache = MediaWikiServices::getInstance()->getLinkCache();
$count = 0;
foreach ( $res as $row ) {
$title = Title::newFromRow( $row );
+ $linkCache->addGoodLinkObjFromRow( $title, $row );
+
if ( $row->cl_collation === '' ) {
// Hack to make sure that while updating from 1.16 schema
// and db is inconsistent, that the sky doesn't fall.
$r = '';
$rescnt = count( $this->children );
$dbcnt = $this->cat->getSubcatCount();
+ // This function should be called even if the result isn't used, it has side-effects
$countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'subcat' );
if ( $rescnt > 0 ) {
return $r;
}
+ /**
+ * Return pretty name which is display name if given and different from prefix text or
+ * the unprefixed page name.
+ *
+ * @return string HTML safe name.
+ */
+ function getPrettyPageNameHtml() {
+ $displayTitle = $this->getOutput()->getPageTitle();
+ if ( $displayTitle === $this->getTitle()->getPrefixedText() ) {
+ return htmlspecialchars( $this->getTitle()->getText() );
+ } else {
+ return $displayTitle;
+ }
+ }
+
/**
* @return string
*/
function getPagesSection() {
- $ti = wfEscapeWikiText( $this->title->getText() );
+ $name = $this->getPrettyPageNameHtml();
# Don't show articles section if there are none.
$r = '';
$dbcnt = $this->cat->getPageCount() - $this->cat->getSubcatCount()
- $this->cat->getFileCount();
$rescnt = count( $this->articles );
+ // This function should be called even if the result isn't used, it has side-effects
$countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'article' );
if ( $rescnt > 0 ) {
$r = "<div id=\"mw-pages\">\n";
- $r .= '<h2>' . $this->msg( 'category_header', $ti )->parse() . "</h2>\n";
+ $r .= '<h2>' . $this->msg( 'category_header' )->rawParams( $name )->parse() . "</h2>\n";
$r .= $countmsg;
$r .= $this->getSectionPagingLinks( 'page' );
$r .= $this->formatList( $this->articles, $this->articles_start_char );
* @return string
*/
function getImageSection() {
+ $name = $this->getPrettyPageNameHtml();
$r = '';
$rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
- if ( $rescnt > 0 ) {
- $dbcnt = $this->cat->getFileCount();
- $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
+ $dbcnt = $this->cat->getFileCount();
+ // This function should be called even if the result isn't used, it has side-effects
+ $countmsg = $this->getCountMessage( $rescnt, $dbcnt, 'file' );
+ if ( $rescnt > 0 ) {
$r .= "<div id=\"mw-category-media\">\n";
$r .= '<h2>' .
- $this->msg(
- 'category-media-header',
- wfEscapeWikiText( $this->title->getText() )
- )->text() .
+ $this->msg( 'category-media-header' )->rawParams( $name )->parse() .
"</h2>\n";
$r .= $countmsg;
$r .= $this->getSectionPagingLinks( 'file' );
}
$pageLang = $this->title->getPageLanguage();
- $attribs = array( 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(),
- 'class' => 'mw-content-' . $pageLang->getDir() );
+ $attribs = [ 'lang' => $pageLang->getHtmlCode(), 'dir' => $pageLang->getDir(),
+ 'class' => 'mw-content-' . $pageLang->getDir() ];
$list = Html::rawElement( 'div', $attribs, $list );
return $list;
}
/**
- * Format a list of articles chunked by letter in a three-column
- * list, ordered vertically.
+ * Format a list of articles chunked by letter in a three-column list, ordered
+ * vertically. This is used for categories with a significant number of pages.
*
* TODO: Take the headers into account when creating columns, so they're
* more visually equal.
*
* TODO: shortList and columnList are similar, need merging
*
- * @param array $articles
- * @param string[] $articles_start_char
- * @return string
+ * @param string[] $articles HTML links to each article
+ * @param string[] $articles_start_char The header characters for each article
+ * @return string HTML to output
* @private
*/
static function columnList( $articles, $articles_start_char ) {
$columns = array_combine( $articles, $articles_start_char );
- $ret = Html::openElement( 'div', array( 'class' => 'mw-category' ) );
+ $ret = Html::openElement( 'div', [ 'class' => 'mw-category' ] );
- $colContents = array();
+ $colContents = [];
# Kind of like array_flip() here, but we keep duplicates in an
# array instead of dropping them.
foreach ( $columns as $article => $char ) {
if ( !isset( $colContents[$char] ) ) {
- $colContents[$char] = array();
+ $colContents[$char] = [];
}
$colContents[$char][] = $article;
}
}
/**
- * Format a list of articles chunked by letter in a bullet list.
- * @param array $articles
- * @param string[] $articles_start_char
- * @return string
+ * Format a list of articles chunked by letter in a bullet list. This is used
+ * for categories with a small number of pages (when columns aren't needed).
+ * @param string[] $articles HTML links to each article
+ * @param string[] $articles_start_char The header characters for each article
+ * @return string HTML to output
* @private
*/
static function shortList( $articles, $articles_start_char ) {
$prevLink = Linker::linkKnown(
$this->addFragmentToTitle( $this->title, $type ),
$prevLink,
- array(),
+ [],
$prevQuery
);
}
$nextLink = Linker::linkKnown(
$this->addFragmentToTitle( $this->title, $type ),
$nextLink,
- array(),
+ [],
$lastQuery
);
}