Replace global configuration variables

Use overrideConfigValues instead of setMwGlobals in tests.

Change-Id: Icaf7d8d9bb3a4c1eb981ef83a12719c2619039fa
This commit is contained in:
Fomafix 2024-03-10 12:42:04 +00:00 committed by jenkins-bot
parent 076bcb2711
commit 70605bf630
11 changed files with 61 additions and 53 deletions

View file

@ -3,6 +3,7 @@
namespace MediaWiki\Extension\Scribunto;
use MediaWiki\Extension\CodeEditor\Hooks\CodeEditorGetPageLanguageHook;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
/**
@ -19,8 +20,8 @@ class CodeEditorHooks implements CodeEditorGetPageLanguageHook {
* @return bool
*/
public function onCodeEditorGetPageLanguage( Title $title, ?string &$languageCode, string $model, string $format ) {
global $wgScribuntoUseCodeEditor;
if ( $wgScribuntoUseCodeEditor && $title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
$useCodeEditor = MediaWikiServices::getInstance()->getMainConfig()->get( 'ScribuntoUseCodeEditor' );
if ( $useCodeEditor && $title->hasContentModel( CONTENT_MODEL_SCRIBUNTO ) ) {
$engine = Scribunto::newDefaultEngine();
if ( $engine->getCodeEditorLanguage() ) {
$languageCode = $engine->getCodeEditorLanguage();

View file

@ -8,6 +8,7 @@ use Exception;
use Language;
use LanguageCode;
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
@ -395,9 +396,9 @@ class LanguageLibrary extends LibraryBase {
# Set output timezone.
if ( $local ) {
global $wgLocaltimezone;
if ( isset( $wgLocaltimezone ) ) {
$tz = new DateTimeZone( $wgLocaltimezone );
$localtimezone = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone );
if ( isset( $localtimezone ) ) {
$tz = new DateTimeZone( $localtimezone );
} else {
$tz = new DateTimeZone( date_default_timezone_get() );
}

View file

@ -82,7 +82,7 @@ abstract class LuaEngine extends ScribuntoEngineBase {
* @return LuaEngine
*/
public static function newAutodetectEngine( array $options ) {
global $wgScribuntoEngineConf;
$engineConf = MediaWikiServices::getInstance()->getMainConfig()->get( 'ScribuntoEngineConf' );
$engine = 'luastandalone';
try {
LuaSandboxInterpreter::checkLuaSandboxVersion();
@ -94,7 +94,7 @@ abstract class LuaEngine extends ScribuntoEngineBase {
unset( $options['factory'] );
// @phan-suppress-next-line PhanTypeMismatchReturnSuperType
return Scribunto::newEngine( $options + $wgScribuntoEngineConf[$engine] );
return Scribunto::newEngine( $options + $engineConf[$engine] );
}
/**

View file

@ -3,6 +3,7 @@
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
use MediaWiki\Category\Category;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use MediaWiki\SiteStats\SiteStats;
use MediaWiki\Specials\SpecialVersion;
@ -19,8 +20,6 @@ class SiteLibrary extends LibraryBase {
private $pagesInCategoryCache = [];
public function register() {
global $wgNamespaceAliases;
$lib = [
'getNsIndex' => [ $this, 'getNsIndex' ],
'pagesInCategory' => [ $this, 'pagesInCategory' ],
@ -30,12 +29,13 @@ class SiteLibrary extends LibraryBase {
];
$parser = $this->getParser();
$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
$contLang = $services->getContentLanguage();
$info = [
'siteName' => $GLOBALS['wgSitename'],
'server' => $GLOBALS['wgServer'],
'scriptPath' => $GLOBALS['wgScriptPath'],
'stylePath' => $GLOBALS['wgStylePath'],
'siteName' => $config->get( MainConfigNames::Sitename ),
'server' => $config->get( MainConfigNames::Server ),
'scriptPath' => $config->get( MainConfigNames::ScriptPath ),
'stylePath' => $config->get( MainConfigNames::StylePath ),
'currentVersion' => SpecialVersion::getVersion(
'', $parser ? $parser->getTargetLanguage() : $contLang
),
@ -75,7 +75,8 @@ class SiteLibrary extends LibraryBase {
}
}
$aliases = array_merge( $wgNamespaceAliases, $contLang->getNamespaceAliases() );
$namespaceAliases = $config->get( MainConfigNames::NamespaceAliases );
$aliases = array_merge( $namespaceAliases, $contLang->getNamespaceAliases() );
foreach ( $aliases as $title => $ns ) {
if ( !isset( $namespacesByName[$title] ) && isset( $namespaces[$ns] ) ) {
$ct = count( $namespaces[$ns]['aliases'] );
@ -197,7 +198,6 @@ class SiteLibrary extends LibraryBase {
* @return array[]
*/
public function interwikiMap( $filter = null ) {
global $wgLocalInterwikis, $wgExtraInterlanguageLinkPrefixes;
$this->checkTypeOptional( 'interwikiMap', 1, $filter, 'string', null );
$local = null;
if ( $filter === 'local' ) {
@ -216,6 +216,9 @@ class SiteLibrary extends LibraryBase {
$interwikiMap = [];
$lookup = MediaWikiServices::getInstance()->getInterwikiLookup();
$prefixes = $lookup->getAllPrefixes( $local );
$config = MediaWikiServices::getInstance()->getMainConfig();
$localInterwikis = $config->get( MainConfigNames::LocalInterwikis );
$extraInterlanguageLinkPrefixes = $config->get( MainConfigNames::ExtraInterlanguageLinkPrefixes );
foreach ( $prefixes as $row ) {
$prefix = $row['iw_prefix'];
$val = [
@ -224,8 +227,8 @@ class SiteLibrary extends LibraryBase {
'isProtocolRelative' => substr( $row['iw_url'], 0, 2 ) === '//',
'isLocal' => isset( $row['iw_local'] ) && $row['iw_local'] == '1',
'isTranscludable' => isset( $row['iw_trans'] ) && $row['iw_trans'] == '1',
'isCurrentWiki' => in_array( $prefix, $wgLocalInterwikis ),
'isExtraLanguageLink' => in_array( $prefix, $wgExtraInterlanguageLinkPrefixes ),
'isCurrentWiki' => in_array( $prefix, $localInterwikis ),
'isExtraLanguageLink' => in_array( $prefix, $extraInterlanguageLinkPrefixes ),
];
if ( $val['isExtraLanguageLink'] ) {
$displayText = wfMessage( "interlanguage-link-$prefix" );

View file

@ -4,6 +4,8 @@ namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
use CoreTagHooks;
use FormatJson;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
class TextLibrary extends LibraryBase {
// Matches Lua mw.text constants
@ -12,8 +14,6 @@ class TextLibrary extends LibraryBase {
private const JSON_PRETTY = 4;
public function register() {
global $wgUrlProtocols;
$lib = [
'unstrip' => [ $this, 'textUnstrip' ],
'unstripNoWiki' => [ $this, 'textUnstripNoWiki' ],
@ -30,7 +30,8 @@ class TextLibrary extends LibraryBase {
'nowiki_protocols' => [],
];
foreach ( $wgUrlProtocols as $prot ) {
$urlProtocols = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UrlProtocols );
foreach ( $urlProtocols as $prot ) {
if ( substr( $prot, -1 ) === ':' ) {
// To convert the protocol into a case-insensitive Lua pattern,
// we need to replace letters with a character class like [Xx]

View file

@ -4,6 +4,8 @@ namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
use LogicException;
use MapCacheLRU;
use MediaWiki\MainConfigNames;
use MediaWiki\MediaWikiServices;
use UtfNormal\Validator;
class UstringLibrary extends LibraryBase {
@ -34,8 +36,8 @@ class UstringLibrary extends LibraryBase {
/** @inheritDoc */
public function __construct( $engine ) {
global $wgMaxArticleSize;
$this->stringLengthLimit = $wgMaxArticleSize * 1024;
$maxArticleSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxArticleSize );
$this->stringLengthLimit = $maxArticleSize * 1024;
$this->phpBug53823 = preg_replace( '//us', 'x', "\xc3\xa1" ) === "x\xc3x\xa1x";
$this->patternRegexCache = new MapCacheLRU( 100 );

View file

@ -135,8 +135,6 @@ class Hooks implements
* @return string
*/
public static function invokeHook( Parser $parser, PPFrame $frame, array $args ) {
global $wgScribuntoGatherFunctionStats;
try {
if ( count( $args ) < 2 ) {
throw new ScribuntoException( 'scribunto-common-nofunction' );
@ -165,7 +163,7 @@ class Hooks implements
// have an index, we don't need the index offset.
$childFrame = $frame->newChild( $args, $title, $bits['index'] === '' ? 0 : 1 );
if ( $wgScribuntoGatherFunctionStats ) {
if ( MediaWikiServices::getInstance()->getMainConfig()->get( 'ScribuntoGatherFunctionStats' ) ) {
$u0 = $engine->getResourceUsage( $engine::CPU_SECONDS );
$result = $module->invoke( $functionName, $childFrame );
$u1 = $engine->getResourceUsage( $engine::CPU_SECONDS );
@ -233,13 +231,12 @@ class Hooks implements
* @param int $timing Function execution time in milliseconds.
*/
public static function reportTiming( $moduleName, $functionName, $timing ) {
global $wgScribuntoGatherFunctionStats, $wgScribuntoSlowFunctionThreshold;
if ( !$wgScribuntoGatherFunctionStats ) {
$config = MediaWikiServices::getInstance()->getMainConfig();
if ( !$config->get( 'ScribuntoGatherFunctionStats' ) ) {
return;
}
$threshold = $wgScribuntoSlowFunctionThreshold;
$threshold = $config->get( 'ScribuntoSlowFunctionThreshold' );
if ( !( is_float( $threshold ) && $threshold > 0 && $threshold < 1 ) ) {
return;
}

View file

@ -3,6 +3,7 @@
namespace MediaWiki\Extension\Scribunto;
use MediaWiki\Config\ConfigException;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
use Parser;
@ -33,17 +34,19 @@ class Scribunto {
* @return ScribuntoEngineBase
*/
public static function newDefaultEngine( $extraOptions = [] ) {
global $wgScribuntoDefaultEngine, $wgScribuntoEngineConf;
if ( !$wgScribuntoDefaultEngine ) {
$config = MediaWikiServices::getInstance()->getMainConfig();
$defaultEngine = $config->get( 'ScribuntoDefaultEngine' );
if ( !$defaultEngine ) {
throw new ConfigException(
'Scribunto extension is enabled but $wgScribuntoDefaultEngine is not set'
);
}
if ( !isset( $wgScribuntoEngineConf[$wgScribuntoDefaultEngine] ) ) {
$engineConf = $config->get( 'ScribuntoEngineConf' );
if ( !isset( $engineConf[$defaultEngine] ) ) {
throw new ConfigException( 'Invalid scripting engine is specified in $wgScribuntoDefaultEngine' );
}
$options = $extraOptions + $wgScribuntoEngineConf[$wgScribuntoDefaultEngine];
$options = $extraOptions + $engineConf[$defaultEngine];
// @phan-suppress-next-line PhanTypeMismatchArgument false positive
return self::newEngine( $options );
}

View file

@ -213,9 +213,9 @@ class ScribuntoContentHandler extends CodeContentHandler {
* @return string HTML
*/
private function highlight( $source, ParserOutput $parserOutput, $codeLang ) {
global $wgScribuntoUseGeSHi;
$useGeSHi = MediaWikiServices::getInstance()->getMainConfig()->get( 'ScribuntoUseGeSHi' );
if (
$wgScribuntoUseGeSHi && $codeLang && ExtensionRegistry::getInstance()->isLoaded( 'SyntaxHighlight' )
$useGeSHi && $codeLang && ExtensionRegistry::getInstance()->isLoaded( 'SyntaxHighlight' )
) {
$status = SyntaxHighlight::highlight( $source, $codeLang, [ 'line' => true, 'linelinks' => 'L' ] );
if ( $status->isGood() ) {

View file

@ -28,15 +28,15 @@ class TitleLibraryTest extends LuaEngineTestBase {
parent::setUp();
// Set up interwikis (via wgInterwikiCache) before creating any Titles
$this->setMwGlobals( [
'wgServer' => '//wiki.local',
'wgCanonicalServer' => 'http://wiki.local',
'wgUsePathInfo' => true,
'wgActionPaths' => [],
'wgScript' => '/w/index.php',
'wgScriptPath' => '/w',
'wgArticlePath' => '/wiki/$1',
'wgInterwikiCache' => ClassicInterwikiLookup::buildCdbHash( [
$this->overrideConfigValues( [
'Server' => '//wiki.local',
'CanonicalServer' => 'http://wiki.local',
'UsePathInfo' => true,
'ActionPaths' => [],
'Script' => '/w/index.php',
'ScriptPath' => '/w',
'ArticlePath' => '/wiki/$1',
'InterwikiCache' => ClassicInterwikiLookup::buildCdbHash( [
[
'iw_prefix' => 'interwikiprefix',
'iw_url' => '//test.wikipedia.org/wiki/$1',

View file

@ -12,15 +12,15 @@ class UriLibraryTest extends LuaEngineTestBase {
protected function setUp(): void {
parent::setUp();
$this->setMwGlobals( [
'wgServer' => '//wiki.local',
'wgCanonicalServer' => 'http://wiki.local',
'wgUsePathInfo' => true,
'wgActionPaths' => [],
'wgScript' => '/w/index.php',
'wgScriptPath' => '/w',
'wgArticlePath' => '/wiki/$1',
'wgFragmentMode' => [ 'legacy', 'html5' ],
$this->overrideConfigValues( [
'Server' => '//wiki.local',
'CanonicalServer' => 'http://wiki.local',
'UsePathInfo' => true,
'ActionPaths' => [],
'Script' => '/w/index.php',
'ScriptPath' => '/w',
'ArticlePath' => '/wiki/$1',
'FragmentMode' => [ 'legacy', 'html5' ],
] );
}