[FileBackend] Renamed getOperations() to reflect that it is "internal".
[lhc/web/wiklou.git] / languages / Language.php
index 4cfee5b..e6feb45 100644 (file)
@@ -139,6 +139,22 @@ class Language {
                'hijri-calendar-m10', 'hijri-calendar-m11', 'hijri-calendar-m12'
        );
 
+       /**
+        * @since 1.20
+        * @var array
+        */
+       static public $durationIntervals = array(
+               'millennia' => 31557600000,
+               'centuries' => 3155760000,
+               'decades' => 315576000,
+               'years' => 31557600, // 86400 * 365.25
+               'weeks' => 604800,
+               'days' => 86400,
+               'hours' => 3600,
+               'minutes' => 60,
+               'seconds' => 1,
+       );
+
        /**
         * Get a cached language object for a given language code
         * @param $code String
@@ -158,6 +174,7 @@ class Language {
        /**
         * Create a language object for a given language code
         * @param $code String
+        * @throws MWException
         * @return Language
         */
        protected static function newFromCode( $code ) {
@@ -555,11 +572,10 @@ class Language {
         */
        function getVariantname( $code, $usemsg = true ) {
                $msg = "variantname-$code";
-               list( $rootCode ) = explode( '-', $code );
                if ( $usemsg && wfMessage( $msg )->exists() ) {
                        return $this->getMessageFromDB( $msg );
                }
-               $name = self::getLanguageName( $code );
+               $name = self::fetchLanguageName( $code );
                if ( $name ) {
                        return $name; # if it's defined as a language name, show that
                } else {
@@ -678,18 +694,18 @@ class Language {
                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)
+        *              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
+        * @return array|bool: language code => language name, false if $include is wrong
+        * @since 1.20
         */
-       public static function fetchLanguageNames( $inLanguage = null, $include = 'all' ) {
+       public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
                global $wgExtraLanguageNames;
                static $coreLanguageNames;
 
@@ -743,11 +759,12 @@ class Language {
        /**
         * @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)
+        * @param $include string: 'all', 'mw' or 'mwfile'; see fetchLanguageNames()
         * @return string: Language name or empty
         * @since 1.20
         */
-       public static function fetchLanguageName( $code, $inLanguage = null ) {
-               $array = self::fetchLanguageNames( $inLanguage, 'all' );
+       public static function fetchLanguageName( $code, $inLanguage = null, $include = 'all' ) {
+               $array = self::fetchLanguageNames( $inLanguage, $include );
                return !array_key_exists( $code, $array ) ? '' : $array[$code];
        }
 
@@ -1907,6 +1924,63 @@ class Language {
                return $this->sprintfDate( $df, $ts );
        }
 
+       /**
+        * Takes a number of seconds and turns it into a text using values such as hours and minutes.
+        *
+        * @since 1.20
+        *
+        * @param integer $seconds The amount of seconds.
+        * @param array $chosenIntervals The intervals to enable.
+        *
+        * @return string
+        */
+       public function formatDuration( $seconds, array $chosenIntervals = array() ) {
+               $intervals = $this->getDurationIntervals( $seconds, $chosenIntervals );
+
+               $segments = array();
+
+               foreach ( $intervals as $intervalName => $intervalValue ) {
+                       $message = new Message( 'duration-' . $intervalName, array( $intervalValue ) );
+                       $segments[] = $message->inLanguage( $this )->escaped();
+               }
+
+               return $this->listToText( $segments );
+       }
+
+       /**
+        * Takes a number of seconds and returns an array with a set of corresponding intervals.
+        * For example 65 will be turned into array( minutes => 1, seconds => 5 ).
+        *
+        * @since 1.20
+        *
+        * @param integer $seconds The amount of seconds.
+        * @param array $chosenIntervals The intervals to enable.
+        *
+        * @return array
+        */
+       public function getDurationIntervals( $seconds, array $chosenIntervals = array() ) {
+               if ( empty( $chosenIntervals ) ) {
+                       $chosenIntervals = array( 'millennia', 'centuries', 'decades', 'years', 'days', 'hours', 'minutes', 'seconds' );
+               }
+
+               $intervals = array_intersect_key( self::$durationIntervals, array_flip( $chosenIntervals ) );
+               $sortedNames = array_keys( $intervals );
+               $smallestInterval = array_pop( $sortedNames );
+
+               $segments = array();
+
+               foreach ( $intervals as $name => $length ) {
+                       $value = floor( $seconds / $length );
+
+                       if ( $value > 0 || ( $name == $smallestInterval && empty( $segments ) ) ) {
+                               $seconds -= $value * $length;
+                               $segments[$name] = $value;
+                       }
+               }
+
+               return $segments;
+       }
+
        /**
         * Internal helper function for userDate(), userTime() and userTimeAndDate()
         *
@@ -2539,6 +2613,7 @@ class Language {
         * @param $file string
         * @param $string string
         *
+        * @throws MWException
         * @return string
         */
        function transformUsingPairFile( $file, $string ) {
@@ -2594,16 +2669,35 @@ class Language {
        }
 
        /**
-        * A hidden direction mark (LRM or RLM), depending on the language direction
+        * A hidden direction mark (LRM or RLM), depending on the language direction.
+        * Unlike getDirMark(), this function returns the character as an HTML entity.
+        * This function should be used when the output is guaranteed to be HTML,
+        * because it makes the output HTML source code more readable. When
+        * the output is plain text or can be escaped, getDirMark() should be used.
+        *
+        * @param $opposite Boolean Get the direction mark opposite to your language
+        * @return string
+        */
+       function getDirMarkEntity( $opposite = false ) {
+               if ( $opposite ) { return $this->isRTL() ? '‎' : '‏'; }
+               return $this->isRTL() ? '‏' : '‎';
+       }
+
+       /**
+        * A hidden direction mark (LRM or RLM), depending on the language direction.
+        * This function produces them as invisible Unicode characters and
+        * the output may be hard to read and debug, so it should only be used
+        * when the output is plain text or can be escaped. When the output is
+        * HTML, use getDirMarkEntity() instead.
         *
         * @param $opposite Boolean Get the direction mark opposite to your language
         * @return string
         */
        function getDirMark( $opposite = false ) {
-               $rtl = "\xE2\x80\x8F";
-               $ltr = "\xE2\x80\x8E";
-               if ( $opposite ) { return $this->isRTL() ? $ltr : $rtl; }
-               return $this->isRTL() ? $rtl : $ltr;
+               $lrm = "\xE2\x80\x8E"; # LEFT-TO-RIGHT MARK, commonly abbreviated LRM
+               $rlm = "\xE2\x80\x8F"; # RIGHT-TO-LEFT MARK, commonly abbreviated RLM
+               if ( $opposite ) { return $this->isRTL() ? $lrm : $rlm; }
+               return $this->isRTL() ? $rlm : $lrm;
        }
 
        /**
@@ -3212,7 +3306,18 @@ class Language {
                }
                return $word;
        }
-
+       /**
+        * Get the grammar forms for the content language
+        * @return array of grammar forms
+        * @since 1.20
+        */
+       function getGrammarForms() {
+               global $wgGrammarForms;
+               if ( isset( $wgGrammarForms[$this->getCode()] ) && is_array( $wgGrammarForms[$this->getCode()] ) ) {
+                        return $wgGrammarForms[$this->getCode()];
+               }
+               return array();
+       }
        /**
         * Provides an alternative text depending on specified gender.
         * Usage {{gender:username|masculine|feminine|neutral}}.
@@ -3577,6 +3682,7 @@ class Language {
         * @param $prefix string Prepend this to the filename
         * @param $code string Language code
         * @param $suffix string Append this to the filename
+        * @throws MWException
         * @return string $prefix . $mangledCode . $suffix
         */
        public static function getFileName( $prefix = 'Language', $code, $suffix = '.php' ) {
@@ -3754,7 +3860,7 @@ class Language {
 
        /**
         * Decode an expiry (block, protection, etc) which has come from the DB
-        * 
+        *
         * @FIXME: why are we returnings DBMS-dependent strings???
         *
         * @param $expiry String: Database expiry String