From: Kevin Israel Date: Thu, 28 Jun 2018 09:58:28 +0000 (-0400) Subject: MWNamespace: Add getCategoryLinkType() method X-Git-Tag: 1.34.0-rc.0~4914 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=a50f61009df774890ae33cc98d990139d7501d65;p=lhc%2Fweb%2Fwiklou.git 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 --- diff --git a/includes/MWNamespace.php b/includes/MWNamespace.php index 73fdd826eb..1df5d519a4 100644 --- a/includes/MWNamespace.php +++ b/includes/MWNamespace.php @@ -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'; + } + } } diff --git a/includes/MovePage.php b/includes/MovePage.php index 614ea7daa5..ec44b6eb69 100644 --- a/includes/MovePage.php +++ b/includes/MovePage.php @@ -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; diff --git a/includes/deferred/LinksUpdate.php b/includes/deferred/LinksUpdate.php index 398df01b9a..5c82f09209 100644 --- a/includes/deferred/LinksUpdate.php +++ b/includes/deferred/LinksUpdate.php @@ -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 diff --git a/includes/page/WikiPage.php b/includes/page/WikiPage.php index ecb113bbf8..ad3c8c592b 100644 --- a/includes/page/WikiPage.php +++ b/includes/page/WikiPage.php @@ -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( diff --git a/maintenance/updateCollation.php b/maintenance/updateCollation.php index d88d5e968e..0670454947 100644 --- a/maintenance/updateCollation.php +++ b/maintenance/updateCollation.php @@ -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 ) { diff --git a/tests/phpunit/includes/MWNamespaceTest.php b/tests/phpunit/includes/MWNamespaceTest.php index 15e2defcb5..1b91a87fb7 100644 --- a/tests/phpunit/includes/MWNamespaceTest.php +++ b/tests/phpunit/includes/MWNamespaceTest.php @@ -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" ); + } }