/**
* 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();
}
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
*
* @return Mixed: variant if one found, false otherwise.
*/
- protected function getURLVariant() {
+ public function getURLVariant() {
global $wgRequest;
- $ret = null;
if ( $this->mURLVariant ) {
return $this->mURLVariant;
*/
protected function getUserVariant() {
global $wgUser;
- $ret = null;
// memoizing this function wreaks havoc on parserTest.php
/* if ( $this->mUserVariant ) { */
return $this->mUserVariant = $this->validateVariant( $ret );
}
-
/**
* Determine the language variant from the Accept-Language header.
*
* @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}\"";
}
/**
if ( $pos === false ) {
// No more markup, append final segment
$out .= $this->autoConvert( substr( $text, $startPos ), $variant );
- $startPos = $length;
return $out;
}
*
*/
function parseCachedTable( $code, $subpage = '', $recursive = true ) {
- global $wgMessageCache;
static $parsed = array();
- if ( !is_object( $wgMessageCache ) ) {
- return array();
- }
-
$key = 'Conversiontable/' . $code;
if ( $subpage ) {
$key .= '/' . $subpage;
}
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...
/**
* 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 '-{' and '}-'
- // to avoid a unwanted '}-' appeared after the math-image.
+ // convert '-{' and '}-' to '-{' and '}-' to prevent
+ // any unwanted markup appearing in the math image tag.
$text = strtr( $text, array( '-{' => '-{', '}-' => '}-' ) );
- $ret = "-{R|$text}-";
- return $ret;
+ return $text;
}
/**