87 public function __construct( $langobj, $maincode, $variants = [],
88 $variantfallbacks = [],
$flags = [],
91 $this->mLangObj = $langobj;
92 $this->mMainLanguageCode = $maincode;
94 $this->mVariantFallbacks = $variantfallbacks;
95 $this->mVariantNames = Language::fetchLanguageNames();
109 $this->mFlags = array_merge( $defaultflags,
$flags );
110 foreach ( $this->mVariants as $v ) {
111 if ( array_key_exists( $v, $manualLevel ) ) {
112 $this->mManualLevel[$v] = $manualLevel[$v];
114 $this->mManualLevel[$v] =
'bidirectional';
116 $this->mFlags[$v] = $v;
142 if ( isset( $this->mVariantFallbacks[$variant] ) ) {
143 return $this->mVariantFallbacks[$variant];
165 if ( $wgUser->isSafeToLoad() &&
$wgUser->isLoggedIn() && !
$req ) {
171 if ( $wgDefaultLanguageVariant && !
$req ) {
199 if ( $wgDefaultLanguageVariant && !
$req ) {
215 if ( $variant !==
null && in_array( $variant, $this->mVariants ) ) {
229 if ( $this->mURLVariant ) {
262 if ( !
$wgUser->isSafeToLoad() ) {
266 if ( $this->mMainLanguageCode ==
$wgContLang->getCode() ) {
269 $ret =
$wgUser->getOption(
'variant-' . $this->mMainLanguageCode );
289 if ( $this->mHeaderVariant ) {
300 $fallbackLanguages = [];
303 if ( $this->mHeaderVariant ) {
311 if ( is_string( $fallbacks ) && $fallbacks !== $this->mMainLanguageCode ) {
312 $fallbackLanguages[] = $fallbacks;
313 } elseif ( is_array( $fallbacks ) ) {
315 array_merge( $fallbackLanguages, $fallbacks );
319 if ( !$this->mHeaderVariant ) {
321 $fallback_languages = array_unique( $fallbackLanguages );
322 foreach ( $fallback_languages as $language ) {
324 if ( $this->mHeaderVariant ) {
363 $marker =
'|' . Parser::MARKER_PREFIX .
'[^\x7f]++\x7f';
366 $htmlfix =
'|<[^>\004]++(?=\004$)|^[^<>]*+>';
373 $codefix =
'<code>[^<]*+(?:(?:(?!<\/code>).)[^<]*+)*+<\/code>|';
375 $scriptfix =
'<script[^>]*+>[^<]*+(?:(?:(?!<\/script>).)[^<]*+)*+<\/script>|';
377 $prefix =
'<pre[^>]*+>[^<]*+(?:(?:(?!<\/pre>).)[^<]*+)*+<\/pre>|';
380 $htmlFullTag =
'<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)|';
382 $reg =
'/' . $codefix . $scriptfix . $prefix . $htmlFullTag .
383 '&[a-zA-Z#][a-z0-9]++;' . $marker . $htmlfix .
'|\004$/s';
390 $text = str_replace(
"\000",
'', $text );
391 $text = str_replace(
"\004",
'', $text );
393 $markupMatches =
null;
394 $elementMatches =
null;
398 while ( $startPos < strlen( $text ) ) {
399 if ( preg_match( $reg, $text .
"\004", $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
400 $elementPos = $markupMatches[0][1];
401 $element = $markupMatches[0][0];
402 if ( $element ===
"\004" ) {
404 $elementPos = strlen( $text );
406 } elseif ( substr( $element, -1 ) ===
"\004" ) {
412 $element = substr( $element, 0, -1 );
419 $log = LoggerFactory::getInstance(
'languageconverter' );
420 $log->error(
"Hit pcre.backtrack_limit in " . __METHOD__
421 .
". Disabling language conversion for this page.",
423 "method" => __METHOD__,
424 "variant" => $toVariant,
425 "startOfText" => substr( $text, 0, 500 )
431 $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) .
"\000";
434 $startPos = $elementPos + strlen( $element );
438 && preg_match(
'/^(<[^>\s]*+)\s([^>]*+)(.*+)$/', $element, $elementMatches )
445 $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
447 $close = substr( $elementMatches[2], -1 ) ===
'/' ?
' /' :
'';
449 foreach ( [
'title',
'alt' ] as $attrName ) {
450 if ( !isset( $attrs[$attrName] ) ) {
453 $attr = $attrs[$attrName];
455 if ( !strpos( $attr,
'://' ) ) {
459 if ( $attr !== $attrs[$attrName] ) {
460 $attrs[$attrName] = $attr;
465 $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
466 $close . $elementMatches[3];
469 $literalBlob .= $element .
"\000";
473 $translatedBlob = $this->
translate( $sourceBlob, $toVariant );
479 while ( $translatedIter->valid() && $literalIter->valid() ) {
480 $output .= $translatedIter->current();
481 $output .= $literalIter->current();
482 $translatedIter->next();
483 $literalIter->next();
501 if ( trim( $text ) ) {
503 $text = $this->mTables[$variant]->replace( $text );
518 foreach ( $this->mVariants as $variant ) {
535 $newConvRuleTitle = $convRule->getTitle();
536 if ( $newConvRuleTitle ) {
538 $this->mConvRuleTitle = $newConvRuleTitle;
542 $convTable = $convRule->getConvTable();
543 $action = $convRule->getRulesAction();
544 foreach ( $convTable as $variant => $pair ) {
549 if ( $action ==
'add' ) {
551 foreach ( $pair as $from => $to ) {
552 $this->mTables[$variant]->setPair( $from, $to );
554 } elseif ( $action ==
'remove' ) {
555 $this->mTables[$variant]->removeArray( $pair );
569 $index = $title->getNamespace();
575 $text .= $this->
translate( $title->getText(), $variant );
591 if ( $variant ===
null ) {
595 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
596 $key =
$cache->makeKey(
'languageconverter',
'namespace-text', $index, $variant );
597 $nsVariantText =
$cache->get( $key );
598 if ( $nsVariantText !==
false ) {
599 return $nsVariantText;
603 $nsConvMsg =
wfMessage(
'conversion-ns' . $index )->inLanguage( $variant );
604 if ( $nsConvMsg->exists() ) {
605 $nsVariantText = $nsConvMsg->plain();
610 if ( $nsVariantText ===
false ) {
611 $nsConvMsg =
wfMessage(
'conversion-ns' . $index )->inContentLanguage();
612 if ( $nsConvMsg->exists() ) {
613 $nsVariantText = $this->
translate( $nsConvMsg->plain(), $variant );
617 if ( $nsVariantText ===
false ) {
619 $langObj = $this->mLangObj->factory( $variant );
620 $nsVariantText = $langObj->getFormattedNsText( $index );
623 $cache->set( $key, $nsVariantText, 60 );
625 return $nsVariantText;
644 return $this->
convertTo( $text, $variant );
660 $this->mConvRuleTitle =
false;
676 $length = strlen( $text );
677 $shouldConvert = !$this->
guessVariant( $text, $variant );
680 $noScript =
'<script.*?>.*?<\/script>(*SKIP)(*FAIL)';
681 $noStyle =
'<style.*?>.*?<\/style>(*SKIP)(*FAIL)';
683 $noHtml =
'<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)(*SKIP)(*FAIL)';
685 while ( $startPos < $length && $continue ) {
686 $continue = preg_match(
688 "/$noScript|$noStyle|$noHtml|-\{/",
697 $fragment = substr( $text, $startPos );
698 $out .= $shouldConvert ? $this->
autoConvert( $fragment, $variant ) : $fragment;
706 $fragment = substr( $text, $startPos, $pos - $startPos );
707 $out .= $shouldConvert ? $this->
autoConvert( $fragment, $variant ) : $fragment;
731 if ( $text[$startPos] !==
'-' || $text[$startPos + 1] !==
'{' ) {
732 throw new MWException( __METHOD__ .
': invalid input string' );
737 $warningDone =
false;
738 $length = strlen( $text );
740 while ( $startPos < $length ) {
742 preg_match(
'/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
753 $inner .= substr( $text, $startPos, $pos - $startPos );
761 if ( $depth >= $this->mMaxDepth ) {
763 if ( !$warningDone ) {
764 $inner .=
'<span class="error">' .
765 wfMessage(
'language-converter-depth-warning' )
766 ->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
780 $rule->parse( $variant );
782 return $rule->getDisplay();
784 throw new MWException( __METHOD__ .
': invalid regex match' );
789 if ( $startPos < $length ) {
790 $inner .= substr( $text, $startPos );
793 return '-{' . $this->
autoConvert( $inner, $variant );
808 # If the article has already existed, there is no need to
809 # check it again, otherwise it may cause a fault.
810 if ( is_object( $nt ) && $nt->exists() ) {
815 $isredir =
$wgRequest->getText(
'redirect',
'yes' );
817 if ( $action ==
'edit' &&
$wgRequest->getBool(
'redlink' ) ) {
820 $linkconvert =
$wgRequest->getText(
'linkconvert',
'yes' );
827 if ( $disableLinkConversion ||
828 ( !$ignoreOtherCond &&
831 || $action ==
'submit'
832 || $linkconvert ==
'no' ) ) ) {
836 if ( is_object( $nt ) ) {
837 $ns = $nt->getNamespace();
847 foreach ( $variants as $v ) {
849 $varnt = Title::newFromText( $v, $ns );
850 if ( !is_null( $varnt ) ) {
851 $linkBatch->addObj( $varnt );
858 $linkBatch->execute();
860 foreach (
$titles as $varnt ) {
861 if ( $varnt->getArticleID() > 0 ) {
863 $link = $varnt->getText();
877 return '!' . $variant;
902 $class = static::class;
903 throw new MWException(
"Must implement loadDefaultTables() method in class $class" );
914 if ( $this->mTablesLoaded ) {
918 $this->mTablesLoaded =
true;
919 $this->mTables =
false;
921 $cacheKey =
$cache->makeKey(
'conversiontables', $this->mMainLanguageCode );
923 $this->mTables =
$cache->get( $cacheKey );
925 if ( !$this->mTables || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
930 foreach ( $this->mVariants as $var ) {
932 $this->mTables[$var]->mergeArray( $cached );
938 $cache->set( $cacheKey, $this->mTables, 43200 );
956 if ( $this->mTables ) {
957 unset( $this->mTables );
960 $this->mTablesLoaded =
false;
986 $key =
'Conversiontable/' .
$code;
988 $key .=
'/' . $subpage;
990 if ( array_key_exists( $key, $parsed ) ) {
994 $parsed[$key] =
true;
996 if ( $subpage ===
'' ) {
1000 $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
1001 if ( $title && $title->exists() ) {
1005 $txt = $revision->getContent(
Revision::RAW )->getNativeData();
1013 # Nothing to parse if there's no text
1014 if ( $txt ===
false || $txt ===
null || $txt ===
'' ) {
1020 $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
1024 foreach ( $subs as $sub ) {
1025 $link = explode(
']]', $sub, 2 );
1026 if ( count(
$link ) != 2 ) {
1029 $b = explode(
'|',
$link[0], 2 );
1030 $b = explode(
'/', trim( $b[0] ), 3 );
1031 if ( count( $b ) == 3 ) {
1037 if ( $b[0] == $linkhead && $b[1] ==
$code ) {
1038 $sublinks[] = $sublink;
1046 foreach ( $blocks as $block ) {
1052 $mappings = explode(
'}-', $block, 2 )[0];
1053 $stripped = str_replace( [
"'",
'"',
'*',
'#' ],
'', $mappings );
1055 foreach ( $table as
$t ) {
1056 $m = explode(
'=>',
$t, 3 );
1057 if ( count( $m ) != 2 ) {
1061 $tt = explode(
'//', $m[1], 2 );
1062 $ret[trim( $m[0] )] = trim( $tt[0] );
1068 foreach ( $sublinks as
$link ) {
1074 if ( $this->mUcfirst ) {
1075 foreach (
$ret as $k => $v ) {
1076 $ret[$this->mLangObj->ucfirst( $k )] = $this->mLangObj->ucfirst( $v );
1091 # don't mark if already marked
1092 if ( strpos( $text,
'-{' ) || strpos( $text,
'}-' ) ) {
1096 $ret =
"-{R|$text}-";
1119 if ( $titleobj->getNamespace() == NS_MEDIAWIKI ) {
1120 $title = $titleobj->getDBkey();
1121 $t = explode(
'/', $title, 3 );
1123 if ( $c > 1 &&
$t[0] ==
'Conversiontable' ) {
1136 if ( is_null( $this->mVarSeparatorPattern ) ) {
1149 foreach ( $this->mVariants as $variant ) {
1151 $pat .= $variant .
'\s*:|';
1153 $pat .=
'[^;]*?=>\s*' . $variant .
'\s*:|';
1156 $this->mVarSeparatorPattern = $pat;
$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 newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target.
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
it s the revision text itself In either if gzip is the revision text is gzipped $flags
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