* @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com>
*/
class LanguageConverter {
+
+ /**
+ * languages supporting variants
+ * @since 1.20
+ * @var array
+ */
+ static public $languagesWithVariants = array(
+ 'gan',
+ 'iu',
+ 'kk',
+ 'ku',
+ 'shi',
+ 'sr',
+ 'tg',
+ 'uz',
+ 'zh',
+ );
+
var $mMainLanguageCode;
var $mVariants, $mVariantFallbacks, $mVariantNames;
var $mTablesLoaded = false;
$this->mMainLanguageCode = $maincode;
$this->mVariants = array_diff( $variants, $wgDisabledVariants );
$this->mVariantFallbacks = $variantfallbacks;
- $this->mVariantNames = Language::getLanguageNames();
+ $this->mVariantNames = Language::fetchLanguageNames();
$this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
$defaultflags = array(
// 'S' show converted text
* @param $variant String: the variant to validate
* @return Mixed: returns the variant if it is valid, null otherwise
*/
- protected function validateVariant( $variant = null ) {
+ public function validateVariant( $variant = null ) {
if ( $variant !== null && in_array( $variant, $this->mVariants ) ) {
return $variant;
}
}
}
+ if( $this->guessVariant( $text, $toVariant ) ) {
+ wfProfileOut( __METHOD__ );
+ return $text;
+ }
+
/* we convert everything except:
1. HTML markups (anything between < and >)
2. HTML entities
$startPos = 0;
$out = '';
$length = strlen( $text );
+ $shouldConvert = !$this->guessVariant( $text, $variant );
+
while ( $startPos < $length ) {
$pos = strpos( $text, '-{', $startPos );
if ( $pos === false ) {
// No more markup, append final segment
- $out .= $this->autoConvert( substr( $text, $startPos ), $variant );
+ $fragment = substr( $text, $startPos );
+ $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
return $out;
}
// Markup found
// Append initial segment
- $out .= $this->autoConvert( substr( $text, $startPos, $pos - $startPos ), $variant );
+ $fragment = substr( $text, $startPos, $pos - $startPos );
+ $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
// Advance position
$startPos = $pos;
* @param $startPos int
* @param $depth Integer: depth of recursion
*
+ * @throws MWException
* @return String: converted text
*/
protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
$inner .= '-{';
if ( !$warningDone ) {
$inner .= '<span class="error">' .
- wfMsgForContent( 'language-converter-depth-warning',
- $this->mMaxDepth ) .
+ wfMessage( 'language-converter-depth-warning' )
+ ->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
'</span>';
$warningDone = true;
}
return '!' . $variant;
}
+ /**
+ * Guess if a text is written in a variant. This should be implemented in subclasses.
+ *
+ * @param string $text the text to be checked
+ * @param string $variant language code of the variant to be checked for
+ * @return bool true if $text appears to be written in $variant, false if not
+ *
+ * @author Nikola Smolenski <smolensk@eunet.rs>
+ * @since 1.19
+ */
+ public function guessVariant($text, $variant) {
+ return false;
+ }
+
/**
* Load default conversion tables.
* This method must be implemented in derived class.
*
* @private
+ * @throws MWException
*/
function loadDefaultTables() {
$name = get_class( $this );
* @param $fromCache Boolean: load from memcached? Defaults to true.
*/
function loadTables( $fromCache = true ) {
+ global $wgLangConvMemc;
+
if ( $this->mTablesLoaded ) {
return;
}
- global $wgMemc;
+
wfProfileIn( __METHOD__ );
$this->mTablesLoaded = true;
$this->mTables = false;
if ( $fromCache ) {
wfProfileIn( __METHOD__ . '-cache' );
- $this->mTables = $wgMemc->get( $this->mCacheKey );
+ $this->mTables = $wgLangConvMemc->get( $this->mCacheKey );
wfProfileOut( __METHOD__ . '-cache' );
}
if ( !$this->mTables
$this->postLoadTables();
$this->mTables[self::CACHE_VERSION_KEY] = true;
- $wgMemc->set( $this->mCacheKey, $this->mTables, 43200 );
+ $wgLangConvMemc->set( $this->mCacheKey, $this->mTables, 43200 );
wfProfileOut( __METHOD__ . '-recache' );
}
wfProfileOut( __METHOD__ );
return array();
}
- if ( strpos( $code, '/' ) === false ) {
- $txt = MessageCache::singleton()->get( 'Conversiontable', true, $code );
- if ( $txt === false ) {
- # @todo FIXME: This method doesn't seem to be expecting
- # this possible outcome...
- $txt = '<Conversiontable>';
- }
+ $parsed[$key] = true;
+
+ if ( $subpage === '' ) {
+ $txt = MessageCache::singleton()->get( 'conversiontable', true, $code );
} else {
- $title = Title::makeTitleSafe(
- NS_MEDIAWIKI,
- "Conversiontable/$code"
- );
+ $txt = false;
+ $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
if ( $title && $title->exists() ) {
- $article = new Article( $title );
- $txt = $article->getContents();
- } else {
- $txt = '';
+ $revision = Revision::newFromTitle( $title );
+ if ( $revision ) {
+ $txt = $revision->getRawText();
+ }
}
}
+ # Nothing to parse if there's no text
+ if ( $txt === false || $txt === null || $txt === '' ) {
+ return array();
+ }
+
// get all subpage links of the form
// [[MediaWiki:Conversiontable/zh-xx/...|...]]
$linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
$ret[trim( $m[0] )] = trim( $tt[0] );
}
}
- $parsed[$key] = true;
// recursively parse the subpages
if ( $recursive ) {
* @param $summary String: edit summary of the edit
* @param $isMinor Boolean: was the edit marked as minor?
* @param $isWatch Boolean: did the user watch this page or not?
- * @param $section Unused
- * @param $flags Bitfield
+ * @param $section
+ * @param $flags int Bitfield
* @param $revision Object: new Revision object or null
* @return Boolean: true
*/
class ConverterRule {
var $mText; // original text in -{text}-
var $mConverter; // LanguageConverter object
- var $mManualCodeError = '<strong class="error">code error!</strong>';
var $mRuleDisplay = '';
var $mRuleTitle = false;
var $mRules = '';// string : the text of the rules
}
}
if ( $this->mRuleDisplay === false ) {
- $this->mRuleDisplay = $this->mManualCodeError;
+ $this->mRuleDisplay = '<span class="error">'
+ . wfMessage( 'converter-manual-rule-error' )->inContentLanguage()->escaped()
+ . '</span>';
}
$this->generateConvTable();