Trying to clean up the mess with $wgCanonicalNamespaceNames and $wgExtraNamespaces.
authorNiklas Laxström <nikerabbit@users.mediawiki.org>
Fri, 20 Aug 2010 10:25:10 +0000 (10:25 +0000)
committerNiklas Laxström <nikerabbit@users.mediawiki.org>
Fri, 20 Aug 2010 10:25:10 +0000 (10:25 +0000)
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

includes/DefaultSettings.php
includes/Namespace.php
languages/Language.php

index 7b6b697..0913c7d 100644 (file)
@@ -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.
index a6e599d..f2f2f02 100644 (file)
@@ -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;
                                }
index 949ff71..2d05a43 100644 (file)
@@ -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.