Show HTTP error (if any) when scary transclusion fails
[lhc/web/wiklou.git] / languages / Language.php
index 6fd5e1b..69a72eb 100644 (file)
@@ -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 = '';
@@ -742,7 +742,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 +762,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.
@@ -800,7 +800,7 @@ class Language {
         * @return string
         */
        function getMessageFromDB( $msg ) {
-               return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
+               return wfMessage( $msg )->inLanguage( $this )->text();
        }
 
        /**
@@ -2706,6 +2706,7 @@ class Language {
         *
         * @param $opposite Boolean Get the direction mark opposite to your language
         * @return string
+        * @since 1.20
         */
        function getDirMarkEntity( $opposite = false ) {
                if ( $opposite ) { return $this->isRTL() ? '‎' : '‏'; }
@@ -2864,7 +2865,7 @@ class Language {
          *
          * An example of this function being called:
          * <code>
-         * wfMsg( 'message', $wgLang->formatNum( $num ) )
+         * wfMessage( 'message' )->numParams( $num )->text()
          * </code>
          *
          * See LanguageGu.php for the Gujarati implementation and
@@ -2996,6 +2997,7 @@ class Language {
         * Take a list of strings and build a locale-friendly comma-separated
         * list, using the local comma-separator message.
         * The last two strings are chained with an "and".
+        * NOTE: This function will only work with standard numeric array keys (0, 1, 2…)
         *
         * @param $l Array
         * @return string
@@ -3003,7 +3005,10 @@ class Language {
        function listToText( array $l ) {
                $s = '';
                $m = count( $l ) - 1;
-               if ( $m == 1 ) {
+               
+               if ( $m === 0 ) {
+                       return $l[0];
+               } elseif ( $m === 1 ) {
                        return $l[0] . $this->getMessageFromDB( 'and' ) . $this->getMessageFromDB( 'word-separator' ) . $l[1];
                } else {
                        for ( $i = $m; $i >= 0; $i-- ) {
@@ -3027,10 +3032,7 @@ class Language {
         */
        function commaList( array $list ) {
                return implode(
-                       wfMsgExt(
-                               'comma-separator',
-                               array( 'parsemag', 'escapenoentities', 'language' => $this )
-                       ),
+                       wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
                        $list
                );
        }
@@ -3043,10 +3045,7 @@ class Language {
         */
        function semicolonList( array $list ) {
                return implode(
-                       wfMsgExt(
-                               'semicolon-separator',
-                               array( 'parsemag', 'escapenoentities', 'language' => $this )
-                       ),
+                       wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
                        $list
                );
        }
@@ -3058,10 +3057,7 @@ class Language {
         */
        function pipeList( array $list ) {
                return implode(
-                       wfMsgExt(
-                               'pipe-separator',
-                               array( 'escapenoentities', 'language' => $this )
-                       ),
+                       wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
                        $list
                );
        }
@@ -3086,7 +3082,7 @@ class Language {
        function truncate( $string, $length, $ellipsis = '...', $adjustLength = true ) {
                # Use the localized ellipsis character
                if ( $ellipsis == '...' ) {
-                       $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
+                       $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
                }
                # Check if there is no need to truncate
                if ( $length == 0 ) {
@@ -3184,7 +3180,7 @@ class Language {
        function truncateHtml( $text, $length, $ellipsis = '...' ) {
                # Use the localized ellipsis character
                if ( $ellipsis == '...' ) {
-                       $ellipsis = wfMsgExt( 'ellipsis', array( 'escapenoentities', 'language' => $this ) );
+                       $ellipsis = wfMessage( 'ellipsis' )->inLanguage( $this )->escaped();
                }
                # Check if there is clearly no need to truncate
                if ( $length <= 0 ) {
@@ -3413,9 +3409,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];
        }
 
        /**
@@ -4111,7 +4107,7 @@ class Language {
                $dirmark = ( $oppositedm ? $this->getDirMark( true ) : '' ) .
                        $this->getDirMark();
                $details = $details ? $dirmark . $this->getMessageFromDB( 'word-separator' ) .
-                       wfMsgExt( 'parentheses', array( 'escape', 'replaceafter', 'language' => $this ), $details ) : '';
+                       wfMessage( 'parentheses' )->rawParams( $details )->inLanguage( $this )->escaped() : '';
                return $page . $details;
        }
 
@@ -4184,4 +4180,34 @@ 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() {
+               return self::$dataCache->getItem( strtolower( $this->mCode ), 'compiledPluralRules' );
+       }
+
+       /**
+        * 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() {
+               return self::$dataCache->getItem( strtolower( $this->mCode ), '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;
+       }
+
 }