Merge "(bug 37138) Fix broken Google sitesearch in DatabaseError"
[lhc/web/wiklou.git] / languages / Language.php
index 463ea19..5bb7762 100644 (file)
@@ -1,6 +1,21 @@
 <?php
 /**
- * Internationalisation code
+ * Internationalisation code.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
  *
  * @file
  * @ingroup Language
@@ -139,6 +154,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
@@ -229,6 +260,17 @@ class Language {
         * @return bool
         */
        public static function isValidBuiltInCode( $code ) {
+
+               if( !is_string($code) ) {
+                       $type = gettype( $code );
+                       if( $type === 'object' ) {
+                               $addmsg = " of class " . get_class( $code );
+                       } else {
+                               $addmsg = '';
+                       }
+                       throw new MWException( __METHOD__ . " must be passed a string, $type given$addmsg" );
+               }
+
                return preg_match( '/^[a-z0-9-]+$/i', $code );
        }
 
@@ -257,10 +299,6 @@ class Language {
                }
 
                if ( !defined( 'MW_COMPILED' ) ) {
-                       // Preload base classes to work around APC/PHP5 bug
-                       if ( file_exists( "$IP/languages/classes/$class.deps.php" ) ) {
-                               include_once( "$IP/languages/classes/$class.deps.php" );
-                       }
                        if ( file_exists( "$IP/languages/classes/$class.php" ) ) {
                                include_once( "$IP/languages/classes/$class.php" );
                        }
@@ -684,9 +722,9 @@ class Language {
         *              Use null for autonyms (native names)
         * @param $include string:
         *              'all' all available languages
-        *              'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames
+        *              'mw' only if the language is defined in MediaWiki or wgExtraLanguageNames (default)
         *              'mwfile' only if the language is in 'mw' *and* has a message file
-        * @return array|bool: language code => language name, false if $include is wrong
+        * @return array: language code => language name
         * @since 1.20
         */
        public static function fetchLanguageNames( $inLanguage = null, $include = 'mw' ) {
@@ -707,9 +745,8 @@ class Language {
                $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] ) ) {
+                       if ( $mwCode === $inLanguage || !isset( $names[$mwCode] ) ) {
                                $names[$mwCode] = $mwName;
                        }
                }
@@ -724,9 +761,7 @@ class Language {
                        $returnMw[$coreCode] = $names[$coreCode];
                }
 
-               if( $include === 'mw' ) {
-                       return $returnMw;
-               } elseif( $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.
@@ -737,7 +772,8 @@ class Language {
                        }
                        return $namesMwFile;
                }
-               return false;
+               # 'mw' option; default if it's not one of the other two options (all/mwfile)
+               return $returnMw;
        }
 
        /**
@@ -1908,6 +1944,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()
         *
@@ -2313,8 +2406,12 @@ class Language {
                        return $s;
                }
 
-               $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
-                               '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
+               if ( function_exists( 'mb_check_encoding' ) ) {
+                       $isutf8 = mb_check_encoding( $s, 'UTF-8' );
+               } else {
+                       $isutf8 = preg_match( '/^(?>[\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
+                                       '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
+               }
                if ( $isutf8 ) {
                        return $s;
                }
@@ -2635,12 +2732,26 @@ class Language {
        }
 
        /**
-        * An arrow, depending on the language direction
+        * An arrow, depending on the language direction.
         *
+        * @param $direction String: the direction of the arrow: forwards (default), backwards, left, right, up, down.
         * @return string
         */
-       function getArrow() {
-               return $this->isRTL() ? '←' : '→';
+       function getArrow( $direction = 'forwards' ) {
+               switch ( $direction ) {
+               case 'forwards':
+                       return $this->isRTL() ? '←' : '→';
+               case 'backwards':
+                       return $this->isRTL() ? '→' : '←';
+               case 'left':
+                       return '←';
+               case 'right':
+                       return '→';
+               case 'up':
+                       return '↑';
+               case 'down':
+                       return '↓';
+               }
        }
 
        /**