From 70605bf630aeaad87309f587dbc5537098b8c27a Mon Sep 17 00:00:00 2001 From: Fomafix Date: Sun, 10 Mar 2024 12:42:04 +0000 Subject: [PATCH] Replace global configuration variables Use overrideConfigValues instead of setMwGlobals in tests. Change-Id: Icaf7d8d9bb3a4c1eb981ef83a12719c2619039fa --- includes/CodeEditorHooks.php | 5 ++-- .../Engines/LuaCommon/LanguageLibrary.php | 7 +++--- includes/Engines/LuaCommon/LuaEngine.php | 4 ++-- includes/Engines/LuaCommon/SiteLibrary.php | 23 +++++++++++-------- includes/Engines/LuaCommon/TextLibrary.php | 7 +++--- includes/Engines/LuaCommon/UstringLibrary.php | 6 +++-- includes/Hooks.php | 11 ++++----- includes/Scribunto.php | 11 +++++---- includes/ScribuntoContentHandler.php | 4 ++-- .../Engines/LuaCommon/TitleLibraryTest.php | 18 +++++++-------- .../Engines/LuaCommon/UriLibraryTest.php | 18 +++++++-------- 11 files changed, 61 insertions(+), 53 deletions(-) diff --git a/includes/CodeEditorHooks.php b/includes/CodeEditorHooks.php index 05bfe279..f120b1db 100644 --- a/includes/CodeEditorHooks.php +++ b/includes/CodeEditorHooks.php @@ -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(); diff --git a/includes/Engines/LuaCommon/LanguageLibrary.php b/includes/Engines/LuaCommon/LanguageLibrary.php index 945e33ff..e2b40678 100644 --- a/includes/Engines/LuaCommon/LanguageLibrary.php +++ b/includes/Engines/LuaCommon/LanguageLibrary.php @@ -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() ); } diff --git a/includes/Engines/LuaCommon/LuaEngine.php b/includes/Engines/LuaCommon/LuaEngine.php index cd1d7772..e9f08b90 100644 --- a/includes/Engines/LuaCommon/LuaEngine.php +++ b/includes/Engines/LuaCommon/LuaEngine.php @@ -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] ); } /** diff --git a/includes/Engines/LuaCommon/SiteLibrary.php b/includes/Engines/LuaCommon/SiteLibrary.php index 0b404bf4..00c46731 100644 --- a/includes/Engines/LuaCommon/SiteLibrary.php +++ b/includes/Engines/LuaCommon/SiteLibrary.php @@ -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" ); diff --git a/includes/Engines/LuaCommon/TextLibrary.php b/includes/Engines/LuaCommon/TextLibrary.php index c3d6d436..6eb8fd04 100644 --- a/includes/Engines/LuaCommon/TextLibrary.php +++ b/includes/Engines/LuaCommon/TextLibrary.php @@ -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] diff --git a/includes/Engines/LuaCommon/UstringLibrary.php b/includes/Engines/LuaCommon/UstringLibrary.php index 9d519373..5719b217 100644 --- a/includes/Engines/LuaCommon/UstringLibrary.php +++ b/includes/Engines/LuaCommon/UstringLibrary.php @@ -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 ); diff --git a/includes/Hooks.php b/includes/Hooks.php index 59e8afcb..5b26746e 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -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; } diff --git a/includes/Scribunto.php b/includes/Scribunto.php index cb849136..00a075a1 100644 --- a/includes/Scribunto.php +++ b/includes/Scribunto.php @@ -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 ); } diff --git a/includes/ScribuntoContentHandler.php b/includes/ScribuntoContentHandler.php index 99733ce8..98f88428 100644 --- a/includes/ScribuntoContentHandler.php +++ b/includes/ScribuntoContentHandler.php @@ -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() ) { diff --git a/tests/phpunit/Engines/LuaCommon/TitleLibraryTest.php b/tests/phpunit/Engines/LuaCommon/TitleLibraryTest.php index 5a888311..95d9842a 100644 --- a/tests/phpunit/Engines/LuaCommon/TitleLibraryTest.php +++ b/tests/phpunit/Engines/LuaCommon/TitleLibraryTest.php @@ -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', diff --git a/tests/phpunit/Engines/LuaCommon/UriLibraryTest.php b/tests/phpunit/Engines/LuaCommon/UriLibraryTest.php index 48a6da84..78921454 100644 --- a/tests/phpunit/Engines/LuaCommon/UriLibraryTest.php +++ b/tests/phpunit/Engines/LuaCommon/UriLibraryTest.php @@ -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' ], ] ); }