From c711cb207f199b7089800743a31667439c01f7a7 Mon Sep 17 00:00:00 2001 From: DannyS712 Date: Thu, 29 Aug 2019 21:34:00 +0000 Subject: [PATCH] Add namespace restrictions to `meta=siteinfo&siprop=namespaces` API result Bug: T73952 Change-Id: I6b4484fa9170ce39953a37eae2ffb859118fd1e5 --- includes/api/ApiQuerySiteinfo.php | 13 +++++++ .../includes/api/ApiQuerySiteinfoTest.php | 35 +++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/includes/api/ApiQuerySiteinfo.php b/includes/api/ApiQuerySiteinfo.php index 7e4a891adf..47212b321a 100644 --- a/includes/api/ApiQuerySiteinfo.php +++ b/includes/api/ApiQuerySiteinfo.php @@ -279,6 +279,8 @@ class ApiQuerySiteinfo extends ApiQueryBase { } protected function appendNamespaces( $property ) { + $nsProtection = $this->getConfig()->get( 'NamespaceProtection' ); + $data = [ ApiResult::META_TYPE => 'assoc', ]; @@ -303,6 +305,17 @@ class ApiQuerySiteinfo extends ApiQueryBase { $data[$ns]['content'] = $nsInfo->isContent( $ns ); $data[$ns]['nonincludable'] = $nsInfo->isNonincludable( $ns ); + if ( isset( $nsProtection[$ns] ) ) { + if ( is_array( $nsProtection[$ns] ) ) { + $specificNs = implode( "|", array_filter( $nsProtection[$ns] ) ); + } elseif ( $nsProtection[$ns] !== '' ) { + $specificNs = $nsProtection[$ns]; + } + if ( isset( $specificNs ) && $specificNs !== '' ) { + $data[$ns]['namespaceprotection'] = $specificNs; + } + } + $contentmodel = $nsInfo->getNamespaceContentModel( $ns ); if ( $contentmodel ) { $data[$ns]['defaultcontentmodel'] = $contentmodel; diff --git a/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php b/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php index 0a2558a227..dce1a5feef 100644 --- a/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php +++ b/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php @@ -79,15 +79,46 @@ class ApiQuerySiteinfoTest extends ApiTestCase { $this->assertSame( 'Need more donations', $data['readonlyreason'] ); } - public function testNamespaces() { - $this->setMwGlobals( 'wgExtraNamespaces', [ '138' => 'Testing' ] ); + public function testNamespacesBasic() { + $this->assertSame( + array_keys( MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces() ), + array_keys( $this->doQuery( 'namespaces' ) ) + ); + } + public function testNamespacesExtraNS() { + $this->setMwGlobals( 'wgExtraNamespaces', [ '138' => 'Testing' ] ); $this->assertSame( array_keys( MediaWikiServices::getInstance()->getContentLanguage()->getFormattedNamespaces() ), array_keys( $this->doQuery( 'namespaces' ) ) ); } + public function testNamespacesProtection() { + $this->setMwGlobals( + 'wgNamespaceProtection', + [ + '0' => '', + '2' => [ '' ], + '4' => 'editsemiprotected', + '8' => [ + 'editinterface', + 'noratelimit' + ], + '14' => [ + 'move-categorypages', + '' + ] + ] + ); + $data = $this->doQuery( 'namespaces' ); + $this->assertArrayNotHasKey( 'namespaceprotection', $data['0'] ); + $this->assertArrayNotHasKey( 'namespaceprotection', $data['2'] ); + $this->assertSame( 'editsemiprotected', $data['4']['namespaceprotection'] ); + $this->assertSame( 'editinterface|noratelimit', $data['8']['namespaceprotection'] ); + $this->assertSame( 'move-categorypages', $data['14']['namespaceprotection'] ); + } + public function testNamespaceAliases() { global $wgNamespaceAliases; -- 2.20.1