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';
+ }
+ }
}
[ '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;
$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
*/
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 );
$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(
}
# 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 ) {
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" );
+ }
}