Merge changes I1aa3b081,I65ee788c,I58a9cdfe
authorNikerabbit <niklas.laxstrom@gmail.com>
Thu, 23 Aug 2012 18:40:31 +0000 (18:40 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 23 Aug 2012 18:40:31 +0000 (18:40 +0000)
* changes:
  Adding JavaScript CLDR plural parser.
  CLDR plural parser in PHP
  CLDR Plural rules based plural form calculation

1  2 
includes/AutoLoader.php
languages/Language.php

diff --combined includes/AutoLoader.php
@@@ -892,7 -892,6 +892,7 @@@ $wgAutoloadLocalClasses = array
        'MIMEsearchPage' => 'includes/specials/SpecialMIMEsearch.php',
        'MostcategoriesPage' => 'includes/specials/SpecialMostcategories.php',
        'MostimagesPage' => 'includes/specials/SpecialMostimages.php',
 +      'MostinterwikisPage' => 'includes/specials/SpecialMostinterwikis.php',
        'MostlinkedCategoriesPage' => 'includes/specials/SpecialMostlinkedcategories.php',
        'MostlinkedPage' => 'includes/specials/SpecialMostlinked.php',
        'MostlinkedTemplatesPage' => 'includes/specials/SpecialMostlinkedtemplates.php',
        'FakeConverter' => 'languages/Language.php',
        'Language' => 'languages/Language.php',
        'LanguageConverter' => 'languages/LanguageConverter.php',
+       'CLDRPluralRuleEvaluator' => 'languages/utils/CLDRPluralRuleEvaluator.php',
  
        # maintenance
        'ConvertLinks' => 'maintenance/convertLinks.php',
diff --combined languages/Language.php
@@@ -266,9 -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 = '';
  
                $names = array();
  
-               if( $inLanguage ) {
+               if ( $inLanguage ) {
                        # TODO: also include when $inLanguage is null, when this code is more efficient
                        wfRunHooks( 'LanguageGetTranslatedLanguageNames', array( &$names, $inLanguage ) );
                }
  
                $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.
         * @return string
         */
        function getMessageFromDB( $msg ) {
 -              return wfMsgExt( $msg, array( 'parsemag', 'language' => $this ) );
 +              return wfMessage( $msg )->inLanguage( $this )->text();
        }
  
        /**
          *
          * 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
         */
        function commaList( array $list ) {
                return implode(
 -                      wfMsgExt(
 -                              'comma-separator',
 -                              array( 'parsemag', 'escapenoentities', 'language' => $this )
 -                      ),
 +                      wfMessage( 'comma-separator' )->inLanguage( $this )->escaped(),
                        $list
                );
        }
         */
        function semicolonList( array $list ) {
                return implode(
 -                      wfMsgExt(
 -                              'semicolon-separator',
 -                              array( 'parsemag', 'escapenoentities', 'language' => $this )
 -                      ),
 +                      wfMessage( 'semicolon-separator' )->inLanguage( $this )->escaped(),
                        $list
                );
        }
         */
        function pipeList( array $list ) {
                return implode(
 -                      wfMsgExt(
 -                              'pipe-separator',
 -                              array( 'escapenoentities', 'language' => $this )
 -                      ),
 +                      wfMessage( 'pipe-separator' )->inLanguage( $this )->escaped(),
                        $list
                );
        }
        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 ) {
        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 ) {
                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];
        }
  
        /**
                $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;
        }
  
        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;
+       }
  }