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 ] );
168 if ( $wgUser->isSafeToLoad() && $wgUser->isLoggedIn() && !
$req ) {
187 return $this->mMainLanguageCode;
211 return $this->mMainLanguageCode;
224 if ( $variant ===
null ) {
229 $variant = LanguageCode::replaceDeprecatedCodes( strtolower( $variant ) );
230 if ( in_array( $variant, $this->mVariants ) ) {
237 foreach ( $this->mVariants
as $v ) {
239 if ( strtolower( LanguageCode::bcp47( $v ) ) === $variant ) {
254 if ( $this->mURLVariant ) {
255 return $this->mURLVariant;
266 return $this->mURLVariant;
287 if ( !$wgUser->isSafeToLoad() ) {
290 if ( $wgUser->isLoggedIn() ) {
292 $this->mMainLanguageCode ==
293 MediaWikiServices::getInstance()->getContentLanguage()->getCode()
295 $ret = $wgUser->getOption(
'variant' );
297 $ret = $wgUser->getOption(
'variant-' . $this->mMainLanguageCode );
302 $ret = $wgUser->getOption(
'language' );
306 return $this->mUserVariant;
317 if ( $this->mHeaderVariant ) {
318 return $this->mHeaderVariant;
328 $fallbackLanguages = [];
331 if ( $this->mHeaderVariant ) {
339 if ( is_string( $fallbacks ) && $fallbacks !== $this->mMainLanguageCode ) {
340 $fallbackLanguages[] = $fallbacks;
341 } elseif ( is_array( $fallbacks ) ) {
343 array_merge( $fallbackLanguages, $fallbacks );
347 if ( !$this->mHeaderVariant ) {
349 $fallback_languages = array_unique( $fallbackLanguages );
350 foreach ( $fallback_languages
as $language ) {
352 if ( $this->mHeaderVariant ) {
358 return $this->mHeaderVariant;
391 $marker =
'|' . Parser::MARKER_PREFIX .
'[^\x7f]++\x7f';
394 $htmlfix =
'|<[^>\004]++(?=\004$)|^[^<>]*+>';
401 $codefix =
'<code>[^<]*+(?:(?:(?!<\/code>).)[^<]*+)*+<\/code>|';
403 $scriptfix =
'<script[^>]*+>[^<]*+(?:(?:(?!<\/script>).)[^<]*+)*+<\/script>|';
405 $prefix =
'<pre[^>]*+>[^<]*+(?:(?:(?!<\/pre>).)[^<]*+)*+<\/pre>|';
408 $htmlFullTag =
'<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)|';
410 $reg =
'/' . $codefix . $scriptfix . $prefix . $htmlFullTag .
411 '&[a-zA-Z#][a-z0-9]++;' . $marker . $htmlfix .
'|\004$/s';
418 $text = str_replace(
"\000",
'', $text );
419 $text = str_replace(
"\004",
'', $text );
421 $markupMatches =
null;
422 $elementMatches =
null;
426 while ( $startPos < strlen( $text ) ) {
427 if ( preg_match( $reg, $text .
"\004", $markupMatches, PREG_OFFSET_CAPTURE, $startPos ) ) {
428 $elementPos = $markupMatches[0][1];
429 $element = $markupMatches[0][0];
430 if ( $element ===
"\004" ) {
432 $elementPos = strlen( $text );
434 } elseif ( substr( $element, -1 ) ===
"\004" ) {
440 $element = substr( $element, 0, -1 );
447 $log = LoggerFactory::getInstance(
'languageconverter' );
448 $log->error(
"Hit pcre.backtrack_limit in " . __METHOD__
449 .
". Disabling language conversion for this page.",
451 "method" => __METHOD__,
452 "variant" => $toVariant,
453 "startOfText" => substr( $text, 0, 500 )
459 $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) .
"\000";
462 $startPos = $elementPos + strlen( $element );
466 && preg_match(
'/^(<[^>\s]*+)\s([^>]*+)(.*+)$/', $element, $elementMatches )
473 $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
475 $close = substr( $elementMatches[2], -1 ) ===
'/' ?
' /' :
'';
477 foreach ( [
'title',
'alt' ]
as $attrName ) {
478 if ( !isset( $attrs[$attrName] ) ) {
481 $attr = $attrs[$attrName];
483 if ( !strpos( $attr,
'://' ) ) {
487 if ( $attr !== $attrs[$attrName] ) {
488 $attrs[$attrName] = $attr;
493 $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
494 $close . $elementMatches[3];
497 $literalBlob .= $element .
"\000";
501 $translatedBlob = $this->
translate( $sourceBlob, $toVariant );
507 while ( $translatedIter->valid() && $literalIter->valid() ) {
508 $output .= $translatedIter->current();
509 $output .= $literalIter->current();
510 $translatedIter->next();
511 $literalIter->next();
529 if ( trim( $text ) ) {
531 $text = $this->mTables[$variant]->replace( $text );
546 foreach ( $this->mVariants
as $variant ) {
563 $newConvRuleTitle = $convRule->getTitle();
564 if ( $newConvRuleTitle ) {
566 $this->mConvRuleTitle = $newConvRuleTitle;
570 $convTable = $convRule->getConvTable();
571 $action = $convRule->getRulesAction();
572 foreach ( $convTable
as $variant => $pair ) {
578 if ( $action ==
'add' ) {
580 foreach ( $pair
as $from => $to ) {
581 $this->mTables[$v]->setPair( $from, $to );
583 } elseif ( $action ==
'remove' ) {
584 $this->mTables[$v]->removeArray( $pair );
598 $index =
$title->getNamespace();
620 if ( $variant ===
null ) {
624 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
625 $key =
$cache->makeKey(
'languageconverter',
'namespace-text', $index, $variant );
626 $nsVariantText =
$cache->get( $key );
627 if ( $nsVariantText !==
false ) {
628 return $nsVariantText;
632 $nsConvMsg =
wfMessage(
'conversion-ns' . $index )->inLanguage( $variant );
633 if ( $nsConvMsg->exists() ) {
634 $nsVariantText = $nsConvMsg->plain();
639 if ( $nsVariantText ===
false ) {
640 $nsConvMsg =
wfMessage(
'conversion-ns' . $index )->inContentLanguage();
641 if ( $nsConvMsg->exists() ) {
642 $nsVariantText = $this->
translate( $nsConvMsg->plain(), $variant );
646 if ( $nsVariantText ===
false ) {
648 $langObj = $this->mLangObj->factory( $variant );
649 $nsVariantText = $langObj->getFormattedNsText( $index );
652 $cache->set( $key, $nsVariantText, 60 );
654 return $nsVariantText;
677 return $this->
convertTo( $text, $variant );
695 $this->mConvRuleTitle =
false;
711 $length = strlen( $text );
712 $shouldConvert = !$this->
guessVariant( $text, $variant );
715 $noScript =
'<script.*?>.*?<\/script>(*SKIP)(*FAIL)';
716 $noStyle =
'<style.*?>.*?<\/style>(*SKIP)(*FAIL)';
718 $noHtml =
'<(?:[^>=]*+(?>[^>=]*+=\s*+(?:"[^"]*"|\'[^\']*\'|[^\'">\s]*+))*+[^>=]*+>|.*+)(*SKIP)(*FAIL)';
719 while ( $startPos < $length && $continue ) {
720 $continue = preg_match(
722 "/$noScript|$noStyle|$noHtml|-\{/",
731 $fragment = substr( $text, $startPos );
732 $out .= $shouldConvert ? $this->
autoConvert( $fragment, $variant ) : $fragment;
740 $fragment = substr( $text, $startPos, $pos - $startPos );
741 $out .= $shouldConvert ? $this->
autoConvert( $fragment, $variant ) : $fragment;
765 if ( $text[$startPos] !==
'-' || $text[$startPos + 1] !==
'{' ) {
766 throw new MWException( __METHOD__ .
': invalid input string' );
771 $warningDone =
false;
772 $length = strlen( $text );
774 while ( $startPos < $length ) {
776 preg_match(
'/-\{|\}-/', $text, $m, PREG_OFFSET_CAPTURE, $startPos );
787 $inner .= substr( $text, $startPos, $pos - $startPos );
795 if ( $depth >= $this->mMaxDepth ) {
797 if ( !$warningDone ) {
798 $inner .=
'<span class="error">' .
799 wfMessage(
'language-converter-depth-warning' )
800 ->numParams( $this->mMaxDepth )->inContentLanguage()->text() .
814 $rule->parse( $variant );
816 return $rule->getDisplay();
818 throw new MWException( __METHOD__ .
': invalid regex match' );
823 if ( $startPos < $length ) {
824 $inner .= substr( $text, $startPos );
827 return '-{' . $this->
autoConvert( $inner, $variant );
842 # If the article has already existed, there is no need to
843 # check it again, otherwise it may cause a fault.
844 if ( is_object( $nt ) && $nt->exists() ) {
849 $isredir =
$wgRequest->getText(
'redirect',
'yes' );
851 if ( $action ==
'edit' &&
$wgRequest->getBool(
'redlink' ) ) {
854 $linkconvert =
$wgRequest->getText(
'linkconvert',
'yes' );
861 if ( $disableLinkConversion ||
862 ( !$ignoreOtherCond &&
865 || $action ==
'submit'
866 || $linkconvert ==
'no' ) ) ) {
870 if ( is_object( $nt ) ) {
871 $ns = $nt->getNamespace();
881 foreach ( $variants
as $v ) {
883 $varnt = Title::newFromText( $v, $ns );
884 if ( !is_null( $varnt ) ) {
885 $linkBatch->addObj( $varnt );
892 $linkBatch->execute();
895 if ( $varnt->getArticleID() > 0 ) {
897 $link = $varnt->getText();
911 return '!' . $variant;
936 $class = static::class;
937 throw new MWException(
"Must implement loadDefaultTables() method in class $class" );
948 if ( $this->mTablesLoaded ) {
952 $this->mTablesLoaded =
true;
953 $this->mTables =
false;
955 $cacheKey =
$cache->makeKey(
'conversiontables', $this->mMainLanguageCode );
957 $this->mTables =
$cache->get( $cacheKey );
959 if ( !$this->mTables || !array_key_exists( self::CACHE_VERSION_KEY, $this->mTables ) ) {
964 foreach ( $this->mVariants
as $var ) {
966 $this->mTables[$var]->mergeArray( $cached );
970 $this->mTables[self::CACHE_VERSION_KEY] =
true;
972 $cache->set( $cacheKey, $this->mTables, 43200 );
990 if ( $this->mTables ) {
991 unset( $this->mTables );
994 $this->mTablesLoaded =
false;
1018 static $parsed = [];
1020 $key =
'Conversiontable/' .
$code;
1022 $key .=
'/' . $subpage;
1024 if ( array_key_exists( $key, $parsed ) ) {
1028 $parsed[$key] =
true;
1030 if ( $subpage ===
'' ) {
1031 $txt = MessageCache::singleton()->getMsgFromNamespace( $key,
$code );
1039 $txt = $revision->getContent(
Revision::RAW )->getNativeData();
1047 # Nothing to parse if there's no text
1048 if ( $txt ===
false || $txt ===
null || $txt ===
'' ) {
1054 $linkhead = $this->mLangObj->getNsText(
NS_MEDIAWIKI ) .
1058 foreach ( $subs
as $sub ) {
1059 $link = explode(
']]', $sub, 2 );
1060 if ( count(
$link ) != 2 ) {
1063 $b = explode(
'|',
$link[0], 2 );
1064 $b = explode(
'/', trim( $b[0] ), 3 );
1065 if ( count( $b ) == 3 ) {
1071 if ( $b[0] == $linkhead && $b[1] ==
$code ) {
1072 $sublinks[] = $sublink;
1080 foreach ( $blocks
as $block ) {
1086 $mappings = explode(
'}-', $block, 2 )[0];
1087 $stripped = str_replace( [
"'",
'"',
'*',
'#' ],
'', $mappings );
1089 foreach ( $table
as $t ) {
1090 $m = explode(
'=>',
$t, 3 );
1091 if ( count( $m ) != 2 ) {
1095 $tt = explode(
'//', $m[1], 2 );
1096 $ret[trim( $m[0] )] = trim( $tt[0] );
1102 foreach ( $sublinks
as $link ) {
1108 if ( $this->mUcfirst ) {
1109 foreach (
$ret as $k => $v ) {
1110 $ret[$this->mLangObj->ucfirst( $k )] = $this->mLangObj->ucfirst( $v );
1125 # don't mark if already marked
1126 if ( strpos( $text,
'-{' ) || strpos( $text,
'}-' ) ) {
1130 $ret =
"-{R|$text}-";
1157 if ( $c > 1 &&
$t[0] ==
'Conversiontable' ) {
1170 if ( is_null( $this->mVarSeparatorPattern ) ) {
1183 foreach ( $this->mVariants
as $variant ) {
1185 $pat .= $variant .
'\s*:|';
1187 $pat .=
'[^;]*?=>\s*' . $variant .
'\s*:|';
1190 $this->mVarSeparatorPattern = $pat;
1192 return $this->mVarSeparatorPattern;
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$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 and return an appropriate strict internal variant code if one exists.
getDefaultVariant()
Get default variant.
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.
__construct(Language $langobj, $maincode, $variants=[], $variantfallbacks=[], $flags=[], $manualLevel=[])
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.
Internationalisation code.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
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.
getNamespace()
Get the namespace index, i.e.
getDBkey()
Get the main part with underscores.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const CONTENT_MODEL_WIKITEXT
this hook is for auditing only $req
namespace and then decline to actually register it file or subcat img or subcat $title
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
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
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 use $formDescriptor instead 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
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
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
switch( $options['output']) $languages