Combine getLanguageNames and getTranslatedLanguageNames into one function which is...
authorRobin Pepermans <robin@users.mediawiki.org>
Mon, 27 Feb 2012 11:59:24 +0000 (11:59 +0000)
committerRobin Pepermans <robin@users.mediawiki.org>
Mon, 27 Feb 2012 11:59:24 +0000 (11:59 +0000)
* Reduces the overly long code in r107002, and reduces code for {{#language:}}
* Fixes the language list in Special:Translate which contained languages that gave "invalid code" when selecting

includes/Preferences.php
includes/Xml.php
includes/parser/CoreParserFunctions.php
languages/Language.php

index 1baf453..a003c9f 100644 (file)
@@ -252,7 +252,7 @@ class Preferences {
                }
 
                // Language
-               $languages = Language::getLanguageNames( false );
+               $languages = Language::fetchLanguageNames( null, 'mw' );
                if ( !array_key_exists( $wgLanguageCode, $languages ) ) {
                        $languages[$wgLanguageCode] = $wgLanguageCode;
                }
index 8135a70..2e882ed 100644 (file)
@@ -190,29 +190,13 @@ class Xml {
         * 
         * @param string $selected The language code of the selected language
         * @param boolean $customisedOnly If true only languages which have some content are listed
-        * @param string $language The ISO code of the language to display the select list in (optional)
+        * @param string $inLanguage The ISO code of the language to display the select list in (optional)
         * @return array containing 2 items: label HTML and select list HTML
         */
-       public static function languageSelector( $selected, $customisedOnly = true, $language = null ) {
+       public static function languageSelector( $selected, $customisedOnly = true, $inLanguage = null ) {
                global $wgLanguageCode;
 
-               // TODO: This should be replaced with a hook or something, from r107002
-               // If a specific language was requested and CLDR is installed, use it
-               if ( $language && is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
-                       if ( $customisedOnly ) {
-                               $listType = LanguageNames::LIST_MW_SUPPORTED; // Only pull names that have localisation in MediaWiki
-                       } else {
-                               $listType = LanguageNames::LIST_MW; // Pull all languages that are in Names.php
-                       }
-                       // Retrieve the list of languages in the requested language (via CLDR)
-                       $languages = LanguageNames::getNames(
-                               $language, // Code of the requested language
-                               LanguageNames::FALLBACK_NORMAL, // Use fallback chain
-                               $listType
-                       );
-               } else {
-                       $languages = Language::getLanguageNames( $customisedOnly );
-               }
+               $languages = Language::fetchLanguageNames( $inLanguage, $customisedOnly ? 'mwfile' : 'mw' );
 
                // Make sure the site language is in the list; a custom language code might not have a
                // defined name...
index 9c39e8a..a070773 100644 (file)
@@ -622,21 +622,14 @@ class CoreParserFunctions {
        /**
         * Gives language names.
         * @param $parser Parser
-        * @param $code String  Language code
-        * @param $language String  Language code
+        * @param $code String  Language code (of which to get name)
+        * @param $inLnguage String  Language code (in which to get name)
         * @return String
         */
-       static function language( $parser, $code = '', $language = '' ) {
-               global $wgContLang;
+       static function language( $parser, $code = '', $inLanguage = '' ) {
                $code = strtolower( $code );
-               $language = strtolower( $language );
-
-               if ( $language !== '' ) {
-                       $names = Language::getTranslatedLanguageNames( $language );
-                       return isset( $names[$code] ) ? $names[$code] : wfBCP47( $code );
-               }
-
-               $lang = $wgContLang->getLanguageName( $code );
+               $inLanguage = strtolower( $inLanguage );
+               $lang = Language::fetchLanguageName( $code, $inLanguage );
                return $lang !== '' ? $lang : wfBCP47( $code );
        }
 
index 40dcda3..6e968b0 100644 (file)
@@ -659,29 +659,10 @@ class Language {
         * @param $customisedOnly bool
         *
         * @return array
+        * @deprecated in 1.20, use fetchLanguageNames()
         */
        public static function getLanguageNames( $customisedOnly = false ) {
-               global $wgExtraLanguageNames;
-               static $coreLanguageNames;
-
-               if ( $coreLanguageNames === null ) {
-                       include( MWInit::compiledPath( 'languages/Names.php' ) );
-               }
-
-               $allNames = $wgExtraLanguageNames + $coreLanguageNames;
-               if ( !$customisedOnly ) {
-                       return $allNames;
-               }
-
-               $names = array();
-               // We do this using a foreach over the codes instead of a directory
-               // loop so that messages files in extensions will work correctly.
-               foreach ( $allNames as $code => $value ) {
-                       if ( is_readable( self::getMessagesFileName( $code ) ) ) {
-                               $names[$code] = $allNames[$code];
-                       }
-               }
-               return $names;
+               return self::fetchLanguageNames( null, $customisedOnly ? 'mwfile' : 'mw' );
        }
 
        /**
@@ -691,16 +672,83 @@ class Language {
         * @param $code String Language code.
         * @return Array language code => language name
         * @since 1.18.0
+        * @deprecated in 1.20, use fetchLanguageNames()
         */
        public static function getTranslatedLanguageNames( $code ) {
+               return self::fetchLanguageNames( $code, 'all' );
+       }
+
+
+       /*
+        * Get an array of language names, indexed by code.
+        * @param $inLanguage null|string: Code of language in which to return the names
+        *                                                                      Use null for autonyms (native names)
+        * @param $include string:
+        *              'all' all available languages
+        *              'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames
+        *              'mwfile' only if the language is in 'mw' *and* has a message file
+        * @return array|false: language code => language name, false if $include is wrong
+        */
+       public static function fetchLanguageNames( $inLanguage = null, $include = 'all' ) {
+               global $wgExtraLanguageNames;
+               static $coreLanguageNames;
+
+               if ( $coreLanguageNames === null ) {
+                       include( MWInit::compiledPath( 'languages/Names.php' ) );
+               }
+
                $names = array();
-               wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $code ) );
 
-               foreach ( self::getLanguageNames() as $code => $name ) {
-                       if ( !isset( $names[$code] ) ) $names[$code] = $name;
+               if( $inLanguage ) {
+                       # TODO: also include when $inLanguage is null, when this code is more efficient
+                       wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
+               }
+
+               $mwNames = $wgExtraLanguageNames + $coreLanguageNames;
+               foreach ( $mwNames as $mwCode => $mwName ) {
+                       # - Prefer own MediaWiki native name when not using the hook
+                       #       TODO: prefer it always to make it consistent, but casing is different in CLDR
+                       # - For other names just add if not added through the hook
+                       if ( ( $mwCode === $inLanguage && !$inLanguage ) || !isset( $names[$mwCode] ) ) {
+                               $names[$mwCode] = $mwName;
+                       }
+               }
+
+               if ( $include === 'all' ) {
+                       return $names;
+               }
+
+               $returnMw = array();
+               $coreCodes = array_keys( $mwNames );
+               foreach( $coreCodes as $coreCode ) {
+                       $returnMw[$coreCode] = $names[$coreCode];
                }
 
-               return $names;
+               if( $include === 'mw' ) {
+                       return $returnMw;
+               } elseif( $include === 'mwfile' ) {
+                       $namesMwFile = array();
+                       # We do this using a foreach over the codes instead of a directory
+                       # loop so that messages files in extensions will work correctly.
+                       foreach ( $returnMw as $code => $value ) {
+                               if ( is_readable( self::getMessagesFileName( $code ) ) ) {
+                                       $namesMwFile[$code] = $names[$code];
+                               }
+                       }
+                       return $namesMwFile;
+               }
+               return false;
+       }
+
+       /**
+        * @param $code string: The code of the language for which to get the name
+        * @param $inLanguage null|string: Code of language in which to return the name (null for autonyms)
+        * @return string: Language name or empty
+        * @since 1.20
+        */
+       public static function fetchLanguageName( $code, $inLanguage = null ) {
+               $array = self::fetchLanguageNames( $inLanguage, 'all' );
+               return !array_key_exists( $code, $array ) ? '' : $array[$code];
        }
 
        /**
@@ -718,13 +766,10 @@ class Language {
         * Only if defined in MediaWiki, no other data like CLDR.
         * @param $code string
         * @return string
+        * @deprecated in 1.20, use fetchLanguageName()
         */
        function getLanguageName( $code ) {
-               $names = self::getLanguageNames();
-               if ( !array_key_exists( $code, $names ) ) {
-                       return '';
-               }
-               return $names[$code];
+               return self::fetchLanguageName( $code );
        }
 
        /**