From: Niklas Laxström Date: Fri, 20 Aug 2010 10:25:10 +0000 (+0000) Subject: Trying to clean up the mess with $wgCanonicalNamespaceNames and $wgExtraNamespaces. X-Git-Tag: 1.31.0-rc.0~35434 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=b7824e03cf35591d2c934bd9c07a20baa8d2f5a8;p=lhc%2Fweb%2Fwiklou.git Trying to clean up the mess with $wgCanonicalNamespaceNames and $wgExtraNamespaces. Now those two together define the set of namespaces used in the wiki. $wgExtraNamespaces is for user configuration and overrides, while $wgCanonicalNamespaceNames is for extension adding their own namespaces. No code should access those two directly for reading. Instead they should use MWNamespace::getCanonicalNamespaces. Also fixed indentation with spaces to tabs in Language.php --- diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 7b6b697f0c..0913c7df7a 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -2295,7 +2295,10 @@ $wgMetaNamespaceTalk = false; /** * Additional namespaces. If the namespaces defined in Language.php and * Namespace.php are insufficient, you can create new ones here, for example, - * to import Help files in other languages. + * to import Help files in other languages. You can also override the namespace + * names of existing namespaces. Extensions developers should use + * $wgCanonicalNamespaceNames. + * * PLEASE NOTE: Once you delete a namespace, the pages in that namespace will * no longer be accessible. If you rename it, then you can access them through * the new namespace name. diff --git a/includes/Namespace.php b/includes/Namespace.php index a6e599d3c0..f2f2f02656 100644 --- a/includes/Namespace.php +++ b/includes/Namespace.php @@ -28,7 +28,8 @@ $wgCanonicalNamespaceNames = array( NS_CATEGORY_TALK => 'Category_talk', ); -if( isset( $wgExtraNamespaces ) && is_array( $wgExtraNamespaces ) ) { +/// @todo UGLY UGLY +if( is_array( $wgExtraNamespaces ) ) { $wgCanonicalNamespaceNames = $wgCanonicalNamespaceNames + $wgExtraNamespaces; } @@ -112,21 +113,41 @@ class MWNamespace { * Returns whether the specified namespace exists */ public static function exists( $index ) { - global $wgCanonicalNamespaceNames; - return isset( $wgCanonicalNamespaceNames[$index] ); + $nslist = self::getCanonicalNamespaces(); + return isset( $nslist[$index] ); } /** - * Returns the canonical (English Wikipedia) name for a given index + * Returns array of all defined namespaces with their canonical + * (English) names. + * + * @return \array + * @since 1.17 + */ + public static function getCanonicalNamespaces() { + static $namespaces = null; + if ( $namespaces === null ) { + global $wgExtraNamespaces, $wgCanonicalNamespaceNames; + if ( is_array( $wgExtraNamespaces ) ) { + $namespaces = $wgCanonicalNamespaceNames + $wgExtraNamespaces; + } + $namespaces[NS_MAIN] = ''; + var_dump( $namespaces ); + } + return $namespaces; + } + + /** + * Returns the canonical (English) name for a given index * * @param $index Int: namespace index * @return string or false if no canonical definition. */ public static function getCanonicalName( $index ) { - global $wgCanonicalNamespaceNames; - if( isset( $wgCanonicalNamespaceNames[$index] ) ) { - return $wgCanonicalNamespaceNames[$index]; + $nslist = self::getCanonicalNamespaces(); + if( isset( $nslist[$index] ) ) { + return $nslist[$index]; } else { return false; } @@ -140,11 +161,10 @@ class MWNamespace { * @return int */ public static function getCanonicalIndex( $name ) { - global $wgCanonicalNamespaceNames; static $xNamespaces = false; if ( $xNamespaces === false ) { $xNamespaces = array(); - foreach ( $wgCanonicalNamespaceNames as $i => $text ) { + foreach ( self::getCanonicalNamespaces() as $i => $text ) { $xNamespaces[strtolower($text)] = $i; } } @@ -164,9 +184,7 @@ class MWNamespace { static $mValidNamespaces = null; if ( is_null( $mValidNamespaces ) ) { - global $wgCanonicalNamespaceNames; - $mValidNamespaces = array( NS_MAIN ); // Doesn't appear in $wgCanonicalNamespaceNames for some reason - foreach ( array_keys( $wgCanonicalNamespaceNames ) as $ns ) { + foreach ( array_keys( self::getCanonicalNamespaces() ) as $ns ) { if ( $ns > 0 ) { $mValidNamespaces[] = $ns; } diff --git a/languages/Language.php b/languages/Language.php index 949ff71e82..2d05a43eff 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -241,12 +241,12 @@ class Language { */ function getNamespaces() { if ( is_null( $this->namespaceNames ) ) { - global $wgExtraNamespaces, $wgMetaNamespace, $wgMetaNamespaceTalk; + global $wgMetaNamespace, $wgMetaNamespaceTalk; $this->namespaceNames = self::$dataCache->getItem( $this->mCode, 'namespaceNames' ); - if ( $wgExtraNamespaces ) { - $this->namespaceNames = $wgExtraNamespaces + $this->namespaceNames; - } + $validNamespaces = MWNamespace::getCanonicalNamespaces(); + + $this->namespaceNames = $validNamespaces + $this->namespaceNames; $this->namespaceNames[NS_PROJECT] = $wgMetaNamespace; if ( $wgMetaNamespaceTalk ) { @@ -258,13 +258,10 @@ class Language { } # Sometimes a language will be localised but not actually exist on this wiki. - global $wgCanonicalNamespaceNames; - $validNamespaces = array_keys($wgCanonicalNamespaceNames); - $validNamespaces[] = NS_MAIN; foreach( $this->namespaceNames as $key => $text ) { - if ( ! in_array( $key, $validNamespaces ) ) { - unset( $this->namespaceNames[$key] ); - } + if ( !isset( $validNamespaces[$key] ) ) { + unset( $this->namespaceNames[$key] ); + } } # The above mixing may leave namespaces out of canonical order.