* Replaced $wgMessageCache by MessageCache::singleton(); since we only use one instan...
[lhc/web/wiklou.git] / languages / LanguageConverter.php
index 7a2d66f..5528c15 100644 (file)
@@ -134,20 +134,18 @@ class LanguageConverter {
 
        /**
         * Get preferred language variant.
-        * @param $fromUser Boolean: get it from $wgUser's preferences
-        * @param $fromHeader Boolean: get it from Accept-Language
         * @return String: the preferred language code
         */
-       public function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
-               global $wgDefaultLanguageVariant;
+       public function getPreferredVariant() {
+               global $wgDefaultLanguageVariant, $wgUser;
 
                $req = $this->getURLVariant();
 
-               if ( $fromUser && !$req ) {
+               if ( $wgUser->isLoggedIn() && !$req ) {
                        $req = $this->getUserVariant();
                }
 
-               if ( $fromHeader && !$req ) {
+               elseif ( !$req ) {
                        $req = $this->getHeaderVariant();
                }
 
@@ -165,6 +163,26 @@ class LanguageConverter {
                return $this->mMainLanguageCode;
        }
 
+       /**
+        * Get default variant.
+        * This function would not be affected by user's settings or headers
+        * @return String: the default variant code
+        */
+       public function getDefaultVariant() {
+               global $wgDefaultLanguageVariant;
+
+               $req = $this->getURLVariant();
+
+               if ( $wgDefaultLanguageVariant && !$req ) {
+                       $req = $this->validateVariant( $wgDefaultLanguageVariant );
+               }
+
+               if ( $req ) {
+                       return $req;
+               }
+               return $this->mMainLanguageCode;
+       }
+
        /**
         * Validate the variant
         * @param $variant String: the variant to validate
@@ -183,9 +201,8 @@ class LanguageConverter {
         *
         * @return Mixed: variant if one found, false otherwise.
         */
-       protected function getURLVariant() {
+       public function getURLVariant() {
                global $wgRequest;
-               $ret = null;
 
                if ( $this->mURLVariant ) {
                        return $this->mURLVariant;
@@ -208,7 +225,6 @@ class LanguageConverter {
         */
        protected function getUserVariant() {
                global $wgUser;
-               $ret = null;
 
                // memoizing this function wreaks havoc on parserTest.php
                /* if ( $this->mUserVariant ) { */
@@ -230,7 +246,6 @@ class LanguageConverter {
                return $this->mUserVariant = $this->validateVariant( $ret );
        }
 
-
        /**
         * Determine the language variant from the Accept-Language header.
         *
@@ -293,14 +308,23 @@ class LanguageConverter {
         * @return String like ' alt="yyyy"' or ' title="yyyy"'
         */
        protected function captionConvert( $matches ) {
+         // TODO: cache the preferred variant in every autoConvert() process,
+         // this helps improve performance in a way.
                $toVariant = $this->getPreferredVariant();
                $title = $matches[1];
-               $text  = $matches[2];
+               $text = $matches[2];
+               
                // we convert captions except URL
                if ( !strpos( $text, '://' ) ) {
                        $text = $this->translate( $text, $toVariant );
                }
-               return " $title=\"$text\"";
+               
+               // remove HTML tags to prevent disrupting the layout
+               $text = preg_replace( '/<[^>]+>/', '', $text );
+               // escape HTML special chars to prevent disrupting the layout
+               $text = htmlspecialchars( $text );
+               
+               return " {$title}=\"{$text}\"";
        }
 
        /**
@@ -561,7 +585,6 @@ class LanguageConverter {
                        if ( $pos === false ) {
                                // No more markup, append final segment
                                $out .= $this->autoConvert( substr( $text, $startPos ), $variant );
-                               $startPos = $length;
                                return $out;
                        }
 
@@ -819,13 +842,8 @@ class LanguageConverter {
         *
         */
        function parseCachedTable( $code, $subpage = '', $recursive = true ) {
-               global $wgMessageCache;
                static $parsed = array();
 
-               if ( !is_object( $wgMessageCache ) ) {
-                       return array();
-               }
-
                $key = 'Conversiontable/' . $code;
                if ( $subpage ) {
                        $key .= '/' . $subpage;
@@ -835,7 +853,7 @@ class LanguageConverter {
                }
 
                if ( strpos( $code, '/' ) === false ) {
-                       $txt = $wgMessageCache->get( 'Conversiontable', true, $code );
+                       $txt = MessageCache::singleton()->get( 'Conversiontable', true, $code );
                        if ( $txt === false ) {
                                # FIXME: this method doesn't seem to be expecting
                                # this possible outcome...
@@ -966,14 +984,13 @@ class LanguageConverter {
 
        /**
         * Armour rendered math against conversion.
-        * Wrap math into rawoutput -{R| math }- syntax.
+        * Escape special chars in parsed math text.(in most cases are img elements)
         */
        public function armourMath( $text ) {
-               // we need to convert '-{' and '}-' to '-&#123;' and '&#125;-'
-               // to avoid a unwanted '}-' appeared after the math-image.
+               // convert '-{' and '}-' to '-&#123;' and '&#125;-' to prevent
+               // any unwanted markup appearing in the math image tag.
                $text = strtr( $text, array( '-{' => '-&#123;', '}-' => '&#125;-' ) );
-               $ret = "-{R|$text}-";
-               return $ret;
+               return $text;
        }
 
        /**