88 public function __construct( $langobj, $maincode, $variants = [],
89 $variantfallbacks = [], $flags = [],
92 $this->mLangObj = $langobj;
93 $this->mMainLanguageCode = $maincode;
95 $this->mVariantFallbacks = $variantfallbacks;
96 $this->mVariantNames = Language::fetchLanguageNames();
110 $this->mFlags = array_merge( $defaultflags, $flags );
111 foreach ( $this->mVariants as $v ) {
112 if ( array_key_exists( $v, $manualLevel ) ) {
113 $this->mManualLevel[$v] = $manualLevel[$v];
115 $this->mManualLevel[$v] =
'bidirectional';
117 $this->mFlags[$v] = $v;
128 return $this->mVariants;
143 if ( isset( $this->mVariantFallbacks[$variant] ) ) {
144 return $this->mVariantFallbacks[$variant];
146 return $this->mMainLanguageCode;
154 return $this->mConvRuleTitle;
166 Hooks::run(
'GetLangPreferredVariant', [ &
$req ] );
185 return $this->mMainLanguageCode;
209 return $this->mMainLanguageCode;
218 if ( $variant !==
null && in_array( $variant, $this->mVariants ) ) {
232 if ( $this->mURLVariant ) {
233 return $this->mURLVariant;
244 return $this->mURLVariant;
265 if ( !
$wgUser->isSafeToLoad() ) {
269 if ( $this->mMainLanguageCode ==
$wgContLang->getCode() ) {
272 $ret =
$wgUser->getOption(
'variant-' . $this->mMainLanguageCode );
281 return $this->mUserVariant;
292 if ( $this->mHeaderVariant ) {
293 return $this->mHeaderVariant;
303 $fallbackLanguages = [];
306 if ( $this->mHeaderVariant ) {
314 if ( is_string( $fallbacks ) && $fallbacks !== $this->mMainLanguageCode ) {
315 $fallbackLanguages[] = $fallbacks;
316 } elseif ( is_array( $fallbacks ) ) {
318 array_merge( $fallbackLanguages, $fallbacks );
322 if ( !$this->mHeaderVariant ) {
324 $fallback_languages = array_unique( $fallbackLanguages );
325 foreach ( $fallback_languages as $language ) {
327 if ( $this->mHeaderVariant ) {
333 return $this->mHeaderVariant;
366 $marker =
'|' . Parser::MARKER_PREFIX .
'[^\x7f]++\x7f';
369 $htmlfix =
'|<[^>\004]++(?=\004$)|^[^<>]*+>';
376 $codefix =
'<code>[^<]*+(?:(?:(?!<\/code>).)[^<]*+)*+<\/code>|';
378 $scriptfix =
'<script[^>]*+>[^<]*+(?:(?:(?!<\/script>).)[^<]*+)*+<\/script>|';
380 $prefix =
'<pre[^>]*+>[^<]*+(?:(?:(?!<\/pre>).)[^<]*+)*+<\/pre>|';
383 $htmlFullTag =
'<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)|';
385 $reg =
'/' . $codefix . $scriptfix . $prefix . $htmlFullTag .
386 '&[a-zA-Z#][a-z0-9]++;' . $marker . $htmlfix .
'|\004$/s';
393 $text = str_replace(
"\000",
'', $text );
394 $text = str_replace(
"\004",
'', $text );
396 $markupMatches =
null;
397 $elementMatches =
null;
401 while ( $startPos < strlen( $text ) ) {
402 if ( preg_match( $reg, $text .
"\004", $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
403 $elementPos = $markupMatches[0][1];
404 $element = $markupMatches[0][0];
405 if ( $element ===
"\004" ) {
407 $elementPos = strlen( $text );
409 } elseif ( substr( $element, -1 ) ===
"\004" ) {
415 $element = substr( $element, 0, -1 );
422 $log = LoggerFactory::getInstance(
'languageconverter' );
423 $log->error(
"Hit pcre.backtrack_limit in " . __METHOD__
424 .
". Disabling language conversion for this page.",
426 "method" => __METHOD__,
427 "variant" => $toVariant,
428 "startOfText" => substr( $text, 0, 500 )
434 $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) .
"\000";
437 $startPos = $elementPos + strlen( $element );
441 && preg_match(
'/^(<[^>\s]*+)\s([^>]*+)(.*+)$/', $element, $elementMatches )
448 $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
450 $close = substr( $elementMatches[2], -1 ) ===
'/' ?
' /' :
'';
452 foreach ( [
'title',
'alt' ] as $attrName ) {
453 if ( !isset( $attrs[$attrName] ) ) {
456 $attr = $attrs[$attrName];
458 if ( !strpos( $attr,
'://' ) ) {
462 if ( $attr !== $attrs[$attrName] ) {
463 $attrs[$attrName] = $attr;
468 $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
469 $close . $elementMatches[3];
472 $literalBlob .= $element .
"\000";
476 $translatedBlob = $this->
translate( $sourceBlob, $toVariant );
482 while ( $translatedIter->valid() && $literalIter->valid() ) {
483 $output .= $translatedIter->current();
484 $output .= $literalIter->current();
485 $translatedIter->next();
486 $literalIter->next();
504 if ( trim( $text ) ) {
506 $text = $this->mTables[$variant]->replace( $text );
521 foreach ( $this->mVariants as $variant ) {
538 $newConvRuleTitle = $convRule->getTitle();
539 if ( $newConvRuleTitle ) {
541 $this->mConvRuleTitle = $newConvRuleTitle;
545 $convTable = $convRule->getConvTable();
546 $action = $convRule->getRulesAction();
547 foreach ( $convTable as $variant => $pair ) {
552 if ( $action ==
'add' ) {
554 foreach ( $pair as $from => $to ) {
555 $this->mTables[$variant]->setPair( $from, $to );
557 } elseif ( $action ==
'remove' ) {
558 $this->mTables[$variant]->removeArray( $pair );
572 $index = $title->getNamespace();
578 $text .= $this->
translate( $title->getText(), $variant );
594 if ( $variant ===
null ) {
598 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
599 $key =
$cache->makeKey(
'languageconverter',
'namespace-text', $index, $variant );
600 $nsVariantText =
$cache->get( $key );
601 if ( $nsVariantText !==
false ) {
602 return $nsVariantText;
606 $nsConvMsg =
wfMessage(
'conversion-ns' . $index )->inLanguage( $variant );
607 if ( $nsConvMsg->exists() ) {
608 $nsVariantText = $nsConvMsg->plain();
613 if ( $nsVariantText ===
false ) {
614 $nsConvMsg =
wfMessage(
'conversion-ns' . $index )->inContentLanguage();
615 if ( $nsConvMsg->exists() ) {
616 $nsVariantText = $this->
translate( $nsConvMsg->plain(), $variant );
620 if ( $nsVariantText ===
false ) {
622 $langObj = $this->mLangObj->factory( $variant );
623 $nsVariantText = $langObj->getFormattedNsText( $index );
626 $cache->set( $key, $nsVariantText, 60 );
628 return $nsVariantText;
647 return $this->
convertTo( $text, $variant );
663 $this->mConvRuleTitle =
false;
679 $length = strlen( $text );
680 $shouldConvert = !$this->
guessVariant( $text, $variant );
683 $noScript =
'<script.*?>.*?<\/script>(*SKIP)(*FAIL)';
684 $noStyle =
'<style.*?>.*?<\/style>(*SKIP)(*FAIL)';
686 $noHtml =
'<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)(*SKIP)(*FAIL)';
687 while ( $startPos < $length && $continue ) {
688 $continue = preg_match(
690 "/$noScript|$noStyle|$noHtml|-\{/",
699 $fragment = substr( $text, $startPos );
700 $out .= $shouldConvert ? $this->
autoConvert( $fragment, $variant ) : $fragment;
708 $fragment = substr( $text, $startPos, $pos - $startPos );
709 $out .= $shouldConvert ? $this->
autoConvert( $fragment, $variant ) : $fragment;
733 if ( $text[$startPos] !==
'-' || $text[$startPos + 1] !==
'{' ) {
734 throw new MWException( __METHOD__ .
': invalid input string' );
739 $warningDone =
false;
740 $length = strlen( $text );
742 while ( $startPos < $length ) {
744 preg_match(
'/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
755 $inner .= substr( $text, $startPos, $pos - $startPos );
763 if ( $depth >= $this->mMaxDepth ) {
765 if ( !$warningDone ) {
766 $inner .=
'<span class="error">' .
767 wfMessage(
'language-converter-depth-warning' )
768 ->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
782 $rule->parse( $variant );
784 return $rule->getDisplay();
786 throw new MWException( __METHOD__ .
': invalid regex match' );
791 if ( $startPos < $length ) {
792 $inner .= substr( $text, $startPos );
795 return '-{' . $this->
autoConvert( $inner, $variant );
810 # If the article has already existed, there is no need to
811 # check it again, otherwise it may cause a fault.
812 if ( is_object( $nt ) && $nt->exists() ) {
817 $isredir =
$wgRequest->getText(
'redirect',
'yes' );
819 if ( $action ==
'edit' &&
$wgRequest->getBool(
'redlink' ) ) {
822 $linkconvert =
$wgRequest->getText(
'linkconvert',
'yes' );
829 if ( $disableLinkConversion ||
830 ( !$ignoreOtherCond &&
833 || $action ==
'submit'
834 || $linkconvert ==
'no' ) ) ) {
838 if ( is_object( $nt ) ) {
839 $ns = $nt->getNamespace();
849 foreach ( $variants as $v ) {
851 $varnt = Title::newFromText( $v, $ns );
852 if ( !is_null( $varnt ) ) {
853 $linkBatch->addObj( $varnt );
860 $linkBatch->execute();
862 foreach (
$titles as $varnt ) {
863 if ( $varnt->getArticleID() > 0 ) {
865 $link = $varnt->getText();
879 return '!' . $variant;
904 $class = static::class;
905 throw new MWException(
"Must implement loadDefaultTables() method in class $class" );
916 if ( $this->mTablesLoaded ) {
920 $this->mTablesLoaded =
true;
921 $this->mTables =
false;
923 $cacheKey =
$cache->makeKey(
'conversiontables', $this->mMainLanguageCode );
925 $this->mTables =
$cache->get( $cacheKey );
927 if ( !$this->mTables || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
932 foreach ( $this->mVariants as $var ) {
934 $this->mTables[$var]->mergeArray( $cached );
938 $this->mTables[self::CACHE_VERSION_KEY] =
true;
940 $cache->set( $cacheKey, $this->mTables, 43200 );
958 if ( $this->mTables ) {
959 unset( $this->mTables );
962 $this->mTablesLoaded =
false;
988 $key =
'Conversiontable/' .
$code;
990 $key .=
'/' . $subpage;
992 if ( array_key_exists( $key, $parsed ) ) {
996 $parsed[$key] =
true;
998 if ( $subpage ===
'' ) {
1002 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
1003 if ( $title && $title->exists() ) {
1004 $revision = Revision::newFromTitle( $title );
1007 $txt = $revision->getContent( Revision::RAW )->getNativeData();
1015 # Nothing to parse if there's no text
1016 if ( $txt ===
false || $txt ===
null || $txt ===
'' ) {
1022 $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
1026 foreach ( $subs as $sub ) {
1027 $link = explode(
']]', $sub, 2 );
1028 if ( count(
$link ) != 2 ) {
1031 $b = explode(
'|',
$link[0], 2 );
1032 $b = explode(
'/', trim( $b[0] ), 3 );
1033 if ( count( $b ) == 3 ) {
1039 if ( $b[0] == $linkhead && $b[1] ==
$code ) {
1040 $sublinks[] = $sublink;
1048 foreach ( $blocks as $block ) {
1054 $mappings = explode(
'}-', $block, 2 )[0];
1055 $stripped = str_replace( [
"'",
'"',
'*',
'#' ],
'', $mappings );
1057 foreach ( $table as
$t ) {
1058 $m = explode(
'=>',
$t, 3 );
1059 if ( count( $m ) != 2 ) {
1063 $tt = explode(
'//', $m[1], 2 );
1064 $ret[trim( $m[0] )] = trim( $tt[0] );
1070 foreach ( $sublinks as
$link ) {
1076 if ( $this->mUcfirst ) {
1077 foreach (
$ret as $k => $v ) {
1078 $ret[$this->mLangObj->ucfirst( $k )] = $this->mLangObj->ucfirst( $v );
1093 # don't mark if already marked
1094 if ( strpos( $text,
'-{' ) || strpos( $text,
'}-' ) ) {
1098 $ret =
"-{R|$text}-";
1121 if ( $titleobj->getNamespace() == NS_MEDIAWIKI ) {
1122 $title = $titleobj->getDBkey();
1123 $t = explode(
'/', $title, 3 );
1125 if ( $c > 1 &&
$t[0] ==
'Conversiontable' ) {
1138 if ( is_null( $this->mVarSeparatorPattern ) ) {
1151 foreach ( $this->mVariants as $variant ) {
1153 $pat .= $variant .
'\s*:|';
1155 $pat .=
'[^;]*?=>\s*' . $variant .
'\s*:|';
1158 $this->mVarSeparatorPattern = $pat;
1160 return $this->mVarSeparatorPattern;
$wgDefaultLanguageVariant
Default variant code, if false, the default will be the language code.
$wgDisableLangConversion
Whether to enable language variant conversion.
$wgDisabledVariants
Disabled variants array of language variant conversion.
$wgLanguageConverterCacheType
The cache type for storing language conversion tables, which are used when parsing certain text and i...
$wgDisableTitleConversion
Whether to enable language variant conversion for links.
if(! $wgDBerrorLogTZ) $wgRequest
Parser for rules of language conversion , parse rules in -{ }- tag.
Base class for language conversion.
getPreferredVariant()
Get preferred language variant.
convertTitle( $title)
Auto convert a Title object to a readable string in the preferred variant.
validateVariant( $variant=null)
Validate the variant.
getDefaultVariant()
Get default variant.
__construct( $langobj, $maincode, $variants=[], $variantfallbacks=[], $flags=[], $manualLevel=[])
recursiveConvertTopLevel( $text, $variant, $depth=0)
Recursively convert text on the outside.
loadTables( $fromCache=true)
Load conversion tables either from the cache or the disk.
getHeaderVariant()
Determine the language variant from the Accept-Language header.
static array $languagesWithVariants
languages supporting variants
autoConvert( $text, $toVariant=false)
Dictionary-based conversion.
recursiveConvertRule( $text, $variant, &$startPos, $depth=0)
Recursively convert text on the inside.
parseCachedTable( $code, $subpage='', $recursive=true)
Parse the conversion table stored in the cache.
getVarSeparatorPattern()
Get the cached separator pattern for ConverterRule::parseRules()
convertNamespace( $index, $variant=null)
Get the namespace display name in the preferred variant.
getExtraHashOptions()
Returns language specific hash options.
getVariantFallbacks( $variant)
In case some variant is not defined in the markup, we need to have some fallback.
updateConversionTable(Title $titleobj)
Refresh the cache of conversion tables when MediaWiki:Conversiontable* is updated.
markNoConversion( $text, $noParse=false)
Enclose a string with the "no conversion" tag.
applyManualConv( $convRule)
Apply manual conversion rules.
translate( $text, $variant)
Translate a string to a variant.
getVariants()
Get all valid variants.
findVariantLink(&$link, &$nt, $ignoreOtherCond=false)
If a language supports multiple variants, it is possible that non-existing link in one variant actual...
convert( $text)
Convert text to different variants of a language.
postLoadTables()
Hook for post processing after conversion tables are loaded.
getURLVariant()
Get the variant specified in the URL.
loadDefaultTables()
Load default conversion tables.
autoConvertToAllVariants( $text)
Call translate() to convert text to all valid variants.
guessVariant( $text, $variant)
Guess if a text is written in a variant.
getUserVariant()
Determine if the user has a variant set.
convertTo( $text, $variant)
Same as convert() except a extra parameter to custom variant.
convertCategoryKey( $key)
Convert the sorting key for category links.
getConvRuleTitle()
Get the title produced by the conversion rule.
reloadTables()
Reload the conversion tables.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
static singleton()
Get the signleton instance of this class.
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
Represents a title within MediaWiki.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
this hook is for auditing only $req
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "<div ...>$1</div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
usually copyright or history_copyright This message must be in HTML not wikitext & $link
const CONTENT_MODEL_WIKITEXT
linkcache txt The LinkCache class maintains a list of article titles and the information about whether or not the article exists in the database This is used to mark up links when displaying a page If the same link appears more than once on any page then it only has to be looked up once In most cases link lookups are done in batches with the LinkBatch class or the equivalent in so the link cache is mostly useful for short snippets of parsed and for links in the navigation areas of the skin The link cache was formerly used to track links used in a document for the purposes of updating the link tables This application is now deprecated To create a you can use the following $titles
switch( $options['output']) $languages