Merge "Add namespace restrictions to `meta=siteinfo&siprop=namespaces` API result"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 16 Sep 2019 22:47:52 +0000 (22:47 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 16 Sep 2019 22:47:52 +0000 (22:47 +0000)
includes/api/ApiQuerySiteinfo.php
tests/phpunit/includes/api/ApiQuerySiteinfoTest.php

index 7e4a891..47212b3 100644 (file)
@@ -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;
index 0a2558a..dce1a5f 100644 (file)
@@ -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;