Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / languages / Language.php
index 240b0d3..0dca39d 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
@@ -250,6 +265,8 @@ class Language {
                        $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" );
                }
@@ -282,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" );
                        }
@@ -401,6 +414,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;
        }
 
        /**
@@ -732,9 +755,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;
                        }
                }
@@ -2394,8 +2416,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;
                }
@@ -2716,12 +2742,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 '↓';
+               }
        }
 
        /**