From 8bbbf39bbd5ce6312981deb3dc48f41c66a4f385 Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Fri, 19 May 2023 09:36:50 +0200 Subject: [PATCH] =?UTF-8?q?Make=20use=20of=20named=20MainConfigNames::?= =?UTF-8?q?=E2=80=A6=20constants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Also merge setMwGlobals() calls because they are really expensive. Also utilize the more readable str_contains() and related. Change-Id: Iebde6aa17c2e366f0c0a98fe13a454f6a06c299b --- includes/CommentParser.php | 2 +- includes/CommentUtils.php | 10 ++++++---- includes/Hooks/HookUtils.php | 2 +- includes/Hooks/PageHooks.php | 2 +- includes/LanguageData.php | 7 ++++--- tests/phpunit/IntegrationTestCase.php | 4 +--- tests/phpunit/TestUtils.php | 2 +- .../integration/ApiDiscussionToolsPageInfoTest.php | 4 +--- 8 files changed, 16 insertions(+), 17 deletions(-) diff --git a/includes/CommentParser.php b/includes/CommentParser.php index ddb3fced7..a2d1ac4aa 100644 --- a/includes/CommentParser.php +++ b/includes/CommentParser.php @@ -548,7 +548,7 @@ class CommentParser { if ( $namespaceId === NS_USER || $namespaceId === NS_USER_TALK ) { $username = $mainText; - if ( strpos( $username, '/' ) !== false ) { + if ( str_contains( $username, '/' ) ) { return null; } if ( $namespaceId === NS_USER ) { diff --git a/includes/CommentUtils.php b/includes/CommentUtils.php index c101bc882..2df08f0ce 100644 --- a/includes/CommentUtils.php +++ b/includes/CommentUtils.php @@ -6,6 +6,7 @@ use Config; use LogicException; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentCommentItem; use MediaWiki\Extension\DiscussionTools\ThreadItem\ContentThreadItem; +use MediaWiki\MainConfigNames; use Title; use Wikimedia\Assert\Assert; use Wikimedia\Parsoid\DOM\Comment; @@ -480,16 +481,17 @@ class CommentUtils { } // TODO: Set the correct base in the document? - if ( strpos( $url, './' ) === 0 ) { - $url = 'https://local' . str_replace( '$1', substr( $url, 2 ), $config->get( 'ArticlePath' ) ); - } elseif ( strpos( $url, '://' ) === false ) { + if ( str_starts_with( $url, './' ) ) { + $url = substr( $url, 2 ); + $url = 'https://local' . str_replace( '$1', $url, $config->get( MainConfigNames::ArticlePath ) ); + } elseif ( !str_contains( $url, '://' ) ) { $url = 'https://local' . $url; } $articlePathRegexp = '/' . str_replace( preg_quote( '$1', '/' ), '([^?]*)', - preg_quote( $config->get( 'ArticlePath' ), '/' ) + preg_quote( $config->get( MainConfigNames::ArticlePath ), '/' ) ) . '/'; $matches = null; if ( preg_match( $articlePathRegexp, $url, $matches ) ) { diff --git a/includes/Hooks/HookUtils.php b/includes/Hooks/HookUtils.php index d3ac64e19..bedb98d28 100644 --- a/includes/Hooks/HookUtils.php +++ b/includes/Hooks/HookUtils.php @@ -571,7 +571,7 @@ class HookUtils { $namespaceInfo = $services->getNamespaceInfo(); Assert::precondition( $namespaceInfo->isTalk( $talkPage->getNamespace() ), "Page is a talk page" ); - if ( $talkPage->getNamespace() === NS_USER_TALK && strpos( $talkPage->getText(), '/' ) === false ) { + if ( $talkPage->getNamespace() === NS_USER_TALK && !str_contains( $talkPage->getText(), '/' ) ) { if ( $services->getUserNameUtils()->isIP( $talkPage->getText() ) ) { return true; } diff --git a/includes/Hooks/PageHooks.php b/includes/Hooks/PageHooks.php index e2560c64e..ed590c644 100644 --- a/includes/Hooks/PageHooks.php +++ b/includes/Hooks/PageHooks.php @@ -208,7 +208,7 @@ class PageHooks implements // Preload jquery.makeCollapsible for LedeSectionDialog. // Using the same approach as in Skin::getDefaultModules in MediaWiki core. - if ( strpos( $output->getHTML(), 'mw-collapsible' ) !== false ) { + if ( str_contains( $output->getHTML(), 'mw-collapsible' ) ) { $output->addModules( 'jquery.makeCollapsible' ); $output->addModuleStyles( 'jquery.makeCollapsible.styles' ); } diff --git a/includes/LanguageData.php b/includes/LanguageData.php index 60c9b1280..f23da3ef3 100644 --- a/includes/LanguageData.php +++ b/includes/LanguageData.php @@ -14,6 +14,7 @@ use DateTimeZone; use ILanguageConverter; use Language; use MediaWiki\Languages\LanguageConverterFactory; +use MediaWiki\MainConfigNames; use MediaWiki\SpecialPage\SpecialPageFactory; class LanguageData { @@ -58,7 +59,7 @@ class LanguageData { foreach ( $langConv->getVariants() as $variant ) { $data['digits'][$variant] = []; foreach ( str_split( '0123456789' ) as $digit ) { - if ( $config->get( 'TranslateNumerals' ) ) { + if ( $config->get( MainConfigNames::TranslateNumerals ) ) { $localDigit = $lang->formatNumNoSeparators( $digit ); } else { $localDigit = $digit; @@ -69,7 +70,7 @@ class LanguageData { } // ApiQuerySiteinfo - $data['localTimezone'] = $config->get( 'Localtimezone' ); + $data['localTimezone'] = $config->get( MainConfigNames::Localtimezone ); // special page names compared against Title::getText, which contains space // But aliases are stored with underscores (db key) in the alias files @@ -78,7 +79,7 @@ class LanguageData { $data['specialNewSectionName'] = str_replace( '_', ' ', $this->specialPageFactory ->getLocalNameFor( 'NewSection' ) ); - $localTimezone = $config->get( 'Localtimezone' ); + $localTimezone = $config->get( MainConfigNames::Localtimezone ); // Return all timezone abbreviations for the local timezone (there will often be two, for // non-DST and DST timestamps, and sometimes more due to historical data, but that's okay). // Avoid DateTimeZone::listAbbreviations(), it returns some half-baked list that is different diff --git a/tests/phpunit/IntegrationTestCase.php b/tests/phpunit/IntegrationTestCase.php index d74eff422..a91fe6c6c 100644 --- a/tests/phpunit/IntegrationTestCase.php +++ b/tests/phpunit/IntegrationTestCase.php @@ -15,9 +15,7 @@ abstract class IntegrationTestCase extends MediaWikiIntegrationTestCase { * @param array $data */ protected function setupEnv( array $config, array $data ): void { - $this->setMwGlobals( $config ); $this->setMwGlobals( [ - 'wgArticlePath' => $config['wgArticlePath'], 'wgNamespaceAliases' => $config['wgNamespaceIds'], 'wgMetaNamespace' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT], ' ', '_' ), 'wgMetaNamespaceTalk' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT_TALK], ' ', '_' ), @@ -26,7 +24,7 @@ abstract class IntegrationTestCase extends MediaWikiIntegrationTestCase { // Data used for the tests assumes there are no variants for English. // Language variants are tested using other languages. 'wgUsePigLatinVariant' => false, - ] ); + ] + $config ); $this->setUserLang( $config['wgContentLanguage'] ); $this->setContentLang( $config['wgContentLanguage'] ); } diff --git a/tests/phpunit/TestUtils.php b/tests/phpunit/TestUtils.php index bccebef0a..5ba068698 100644 --- a/tests/phpunit/TestUtils.php +++ b/tests/phpunit/TestUtils.php @@ -117,7 +117,7 @@ trait TestUtils { $quotedNewInnerHtml = strtr( $newInnerHtml, [ '\\' => '\\\\', '$' => '\\$' ] ); if ( strtolower( $container->tagName ) === 'body' ) { - if ( strpos( $html, ']*>)(.*)()`s', '$1' . $quotedNewInnerHtml . '$3', diff --git a/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php b/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php index c89b91b18..f98e2d6ef 100644 --- a/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php +++ b/tests/phpunit/integration/ApiDiscussionToolsPageInfoTest.php @@ -23,9 +23,7 @@ class ApiDiscussionToolsPageInfoTest extends ApiTestCase { * @param array $data */ protected function setupEnv( array $config, array $data ): void { - $this->setMwGlobals( $config ); $this->setMwGlobals( [ - 'wgArticlePath' => $config['wgArticlePath'], 'wgNamespaceAliases' => $config['wgNamespaceIds'], 'wgMetaNamespace' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT], ' ', '_' ), 'wgMetaNamespaceTalk' => strtr( $config['wgFormattedNamespaces'][NS_PROJECT_TALK], ' ', '_' ), @@ -34,7 +32,7 @@ class ApiDiscussionToolsPageInfoTest extends ApiTestCase { // Data used for the tests assumes there are no variants for English. // Language variants are tested using other languages. 'wgUsePigLatinVariant' => false, - ] ); + ] + $config ); $this->setUserLang( $config['wgContentLanguage'] ); $this->setContentLang( $config['wgContentLanguage'] ); }