MWNamespace: Add getCategoryLinkType() method

This method returns the value used as cl_type for category links that
are "from" pages within the namespace, and is added to avoid duplication
of code across a few classes.

Change-Id: I4e55932a5a27858cfedb12009b455fcd02f9b5df
This commit is contained in:
Kevin Israel 2018-06-28 05:58:28 -04:00 committed by Aaron Schulz
parent bad624a2bf
commit a50f61009d
6 changed files with 61 additions and 30 deletions

View file

@ -540,4 +540,26 @@ class MWNamespace {
return $usableLevels;
}
/**
* Returns the link type to be used for categories.
*
* This determines which section of a category page titles
* in the namespace will appear within.
*
* @since 1.32
* @param int $index Namespace index
* @return string One of 'subcat', 'file', 'page'
*/
public static function getCategoryLinkType( $index ) {
self::isMethodValidFor( $index, __METHOD__ );
if ( $index == NS_CATEGORY ) {
return 'subcat';
} elseif ( $index == NS_FILE ) {
return 'file';
} else {
return 'page';
}
}
}

View file

@ -280,13 +280,7 @@ class MovePage {
[ 'cl_from' => $pageid ],
__METHOD__
);
if ( $this->newTitle->getNamespace() == NS_CATEGORY ) {
$type = 'subcat';
} elseif ( $this->newTitle->getNamespace() == NS_FILE ) {
$type = 'file';
} else {
$type = 'page';
}
$type = MWNamespace::getCategoryLinkType( $this->newTitle->getNamespace() );
foreach ( $prefixes as $prefixRow ) {
$prefix = $prefixRow->cl_sortkey_prefix;
$catTo = $prefixRow->cl_to;

View file

@ -593,13 +593,7 @@ class LinksUpdate extends DataUpdate implements EnqueueableDataUpdate {
$nt = Title::makeTitleSafe( NS_CATEGORY, $name );
$wgContLang->findVariantLink( $name, $nt, true );
if ( $this->mTitle->getNamespace() == NS_CATEGORY ) {
$type = 'subcat';
} elseif ( $this->mTitle->getNamespace() == NS_FILE ) {
$type = 'file';
} else {
$type = 'page';
}
$type = MWNamespace::getCategoryLinkType( $this->mTitle->getNamespace() );
# Treat custom sortkeys as a prefix, so that if multiple
# things are forced to sort as '*' or something, they'll

View file

@ -3258,16 +3258,13 @@ class WikiPage implements Page, IDBAccessObject {
*/
public function updateCategoryCounts( array $added, array $deleted, $id = 0 ) {
$id = $id ?: $this->getId();
$ns = $this->getTitle()->getNamespace();
$type = MWNamespace::getCategoryLinkType( $this->getTitle()->getNamespace() );
$addFields = [ 'cat_pages = cat_pages + 1' ];
$removeFields = [ 'cat_pages = cat_pages - 1' ];
if ( $ns == NS_CATEGORY ) {
$addFields[] = 'cat_subcats = cat_subcats + 1';
$removeFields[] = 'cat_subcats = cat_subcats - 1';
} elseif ( $ns == NS_FILE ) {
$addFields[] = 'cat_files = cat_files + 1';
$removeFields[] = 'cat_files = cat_files - 1';
if ( $type !== 'page' ) {
$addFields[] = "cat_{$type}s = cat_{$type}s + 1";
$removeFields[] = "cat_{$type}s = cat_{$type}s - 1";
}
$dbw = wfGetDB( DB_MASTER );
@ -3299,8 +3296,8 @@ class WikiPage implements Page, IDBAccessObject {
$insertRows[] = [
'cat_title' => $cat,
'cat_pages' => 1,
'cat_subcats' => ( $ns == NS_CATEGORY ) ? 1 : 0,
'cat_files' => ( $ns == NS_FILE ) ? 1 : 0,
'cat_subcats' => ( $type === 'subcat' ) ? 1 : 0,
'cat_files' => ( $type === 'file' ) ? 1 : 0,
];
}
$dbw->upsert(

View file

@ -187,13 +187,7 @@ TEXT
}
# cl_type will be wrong for lots of pages if cl_collation is 0,
# so let's update it while we're here.
if ( $title->getNamespace() == NS_CATEGORY ) {
$type = 'subcat';
} elseif ( $title->getNamespace() == NS_FILE ) {
$type = 'file';
} else {
$type = 'page';
}
$type = MWNamespace::getCategoryLinkType( $title->getNamespace() );
$newSortKey = $collation->getSortKey(
$title->getCategorySortkey( $prefix ) );
if ( $verboseStats ) {

View file

@ -575,4 +575,34 @@ class MWNamespaceTest extends MediaWikiTestCase {
private function assertDifferentSubject( $ns1, $ns2, $msg = '' ) {
$this->assertFalse( MWNamespace::subjectEquals( $ns1, $ns2 ), $msg );
}
public function provideGetCategoryLinkType() {
return [
[ NS_MAIN, 'page' ],
[ NS_TALK, 'page' ],
[ NS_USER, 'page' ],
[ NS_USER_TALK, 'page' ],
[ NS_FILE, 'file' ],
[ NS_FILE_TALK, 'page' ],
[ NS_CATEGORY, 'subcat' ],
[ NS_CATEGORY_TALK, 'page' ],
[ 100, 'page' ],
[ 101, 'page' ],
];
}
/**
* @dataProvider provideGetCategoryLinkType
* @covers MWNamespace::getCategoryLinkType
*
* @param int $index
* @param string $expected
*/
public function testGetCategoryLinkType( $index, $expected ) {
$actual = MWNamespace::getCategoryLinkType( $index );
$this->assertSame( $expected, $actual, "NS $index" );
}
}