merged master
[lhc/web/wiklou.git] / languages / Language.php
index ae66d7c..472871f 100644 (file)
@@ -32,7 +32,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 
 # Read language names
 global $wgLanguageNames;
-require_once( dirname( __FILE__ ) . '/Names.php' );
+require_once( __DIR__ . '/Names.php' );
 
 if ( function_exists( 'mb_strtoupper' ) ) {
        mb_internal_encoding( 'UTF-8' );
@@ -266,9 +266,9 @@ class Language {
         */
        public static function isValidBuiltInCode( $code ) {
 
-               if( !is_string($code) ) {
+               if ( !is_string( $code ) ) {
                        $type = gettype( $code );
-                       if( $type === 'object' ) {
+                       if ( $type === 'object' ) {
                                $addmsg = " of class " . get_class( $code );
                        } else {
                                $addmsg = '';
@@ -419,6 +419,16 @@ class Language {
         */
        public function setNamespaces( array $namespaces ) {
                $this->namespaceNames = $namespaces;
+               $this->mNamespaceIds = null;
+       }
+
+       /**
+        * Resets all of the namespace caches. Mainly used for testing
+        */
+       public function resetNamespaces( ) {
+               $this->namespaceNames = null;
+               $this->mNamespaceIds = null;
+               $this->namespaceAliases = null;
        }
 
        /**
@@ -742,7 +752,7 @@ class Language {
 
                $names = array();
 
-               if( $inLanguage ) {
+               if ( $inLanguage ) {
                        # TODO: also include when $inLanguage is null, when this code is more efficient
                        wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
                }
@@ -762,11 +772,11 @@ class Language {
 
                $returnMw = array();
                $coreCodes = array_keys( $mwNames );
-               foreach( $coreCodes as $coreCode ) {
+               foreach ( $coreCodes as $coreCode ) {
                        $returnMw[$coreCode] = $names[$coreCode];
                }
 
-               if( $include === 'mwfile' ) {
+               if ( $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.
@@ -3005,7 +3015,7 @@ class Language {
        function listToText( array $l ) {
                $s = '';
                $m = count( $l ) - 1;
-               
+
                if ( $m === 0 ) {
                        return $l[0];
                } elseif ( $m === 1 ) {
@@ -3409,9 +3419,9 @@ class Language {
                if ( !count( $forms ) ) {
                        return '';
                }
-               $forms = $this->preConvertPlural( $forms, 2 );
-
-               return ( $count == 1 ) ? $forms[0] : $forms[1];
+               $pluralForm = $this->getPluralForm( $count );
+               $pluralForm = min( $pluralForm, count( $forms ) - 1 );
+               return $forms[$pluralForm];
        }
 
        /**
@@ -4180,4 +4190,54 @@ class Language {
        public function getConvRuleTitle() {
                return $this->mConverter->getConvRuleTitle();
        }
+
+       /**
+        * Get the compiled plural rules for the language
+        * @since 1.20
+        * @return array Associative array with plural form, and plural rule as key-value pairs
+        */
+       public function getCompiledPluralRules() {
+               $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
+               $fallbacks = Language::getFallbacksFor( $this->mCode );
+               if ( !$pluralRules ) {
+                       foreach ( $fallbacks as $fallbackCode ) {
+                               $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'compiledPluralRules' );
+                               if ( $pluralRules ) {
+                                       break;
+                               }
+                       }
+               }
+               return $pluralRules;
+       }
+
+       /**
+        * Get the plural rules for the language
+        * @since 1.20
+        * @return array Associative array with plural form, and plural rule as key-value pairs
+        */
+       public function getPluralRules() {
+               $pluralRules = self::$dataCache->getItem( strtolower( $this->mCode ), 'pluralRules' );
+               $fallbacks = Language::getFallbacksFor( $this->mCode );
+               if ( !$pluralRules ) {
+                       foreach ( $fallbacks as $fallbackCode ) {
+                               $pluralRules = self::$dataCache->getItem( strtolower( $fallbackCode ), 'pluralRules' );
+                               if ( $pluralRules ) {
+                                       break;
+                               }
+                       }
+               }
+               return $pluralRules;
+       }
+
+       /**
+        * Find the plural form matching to the given number
+        * It return the form index.
+        * @return int The index of the plural form
+        */
+       private function getPluralForm( $number ) {
+               $pluralRules = $this->getCompiledPluralRules();
+               $form = CLDRPluralRuleEvaluator::evaluateCompiled( $number, $pluralRules );
+               return $form;
+       }
+
 }