From e6df285854622144df973764af908d34b4befbe9 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Thu, 11 Apr 2019 16:36:15 +0300 Subject: [PATCH] Remove all $wgParser use from core Bug: T160811 Change-Id: I0556c04d33386d0339e02e2bf7a1ee74d97c2abd --- .phpcs.xml | 1 - includes/DefaultSettings.php | 2 +- includes/EditPage.php | 24 +++++----- includes/OutputPage.php | 4 +- includes/actions/RawAction.php | 5 +- includes/api/ApiExpandTemplates.php | 14 +++--- includes/api/ApiFeedWatchlist.php | 6 ++- includes/api/ApiParse.php | 13 ++--- includes/api/ApiQueryRevisionsBase.php | 9 ++-- includes/api/ApiQuerySiteinfo.php | 6 +-- includes/cache/MessageCache.php | 10 ++-- includes/content/CssContent.php | 6 ++- includes/content/JavaScriptContent.php | 6 ++- includes/content/TextContent.php | 5 +- includes/content/WikitextContent.php | 30 +++++------- includes/parser/Parser.php | 8 ++-- includes/session/SessionProvider.php | 8 ++-- includes/specials/SpecialExpandTemplates.php | 15 +++--- includes/specials/SpecialVersion.php | 10 ++-- maintenance/preprocessDump.php | 4 +- maintenance/preprocessorFuzzTest.php | 4 +- tests/parser/ParserTestRunner.php | 18 +++---- tests/phpunit/includes/MessageTest.php | 4 +- .../includes/api/ApiQuerySiteinfoTest.php | 9 ++-- .../includes/parser/ParserMethodsTest.php | 47 +++++++++---------- .../phpunit/suites/UploadFromUrlTestSuite.php | 4 +- 26 files changed, 127 insertions(+), 145 deletions(-) diff --git a/.phpcs.xml b/.phpcs.xml index cc9e53c2fd..fef07e6204 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -14,7 +14,6 @@ - diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 4547009b5c..418b1b169d 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -4111,7 +4111,7 @@ $wgInvalidRedirectTargets = [ 'Filepath', 'Mypage', 'Mytalk', 'Redirect' ]; * * The entire associative array will be passed through to the constructor as * the first parameter. Note that only Setup.php can use this variable -- - * the configuration will change at runtime via $wgParser member functions, so + * the configuration will change at runtime via Parser member functions, so * the contents of this variable will be out-of-date. The variable can only be * changed during LocalSettings.php, in particular, it can't be changed during * an extension setup function. diff --git a/includes/EditPage.php b/includes/EditPage.php index add48d99b9..6990934c1d 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -1816,15 +1816,14 @@ ERROR; * @return string */ private function newSectionSummary( &$sectionanchor = null ) { - global $wgParser; - if ( $this->sectiontitle !== '' ) { $sectionanchor = $this->guessSectionName( $this->sectiontitle ); // If no edit summary was specified, create one automatically from the section // title and have it link to the new section. Otherwise, respect the summary as // passed. if ( $this->summary === '' ) { - $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle ); + $cleanSectionTitle = MediaWikiServices::getInstance()->getParser() + ->stripSectionName( $this->sectiontitle ); return $this->context->msg( 'newsectionsummary' ) ->plaintextParams( $cleanSectionTitle )->inContentLanguage()->text(); } @@ -1832,7 +1831,8 @@ ERROR; $sectionanchor = $this->guessSectionName( $this->summary ); # This is a new section, so create a link to the new section # in the revision summary. - $cleanSummary = $wgParser->stripSectionName( $this->summary ); + $cleanSummary = MediaWikiServices::getInstance()->getParser() + ->stripSectionName( $this->summary ); return $this->context->msg( 'newsectionsummary' ) ->plaintextParams( $cleanSummary )->inContentLanguage()->text(); } @@ -3058,8 +3058,8 @@ ERROR; public static function extractSectionTitle( $text ) { preg_match( "/^(=+)(.+)\\1\\s*(\n|$)/i", $text, $matches ); if ( !empty( $matches[2] ) ) { - global $wgParser; - return $wgParser->stripSectionName( trim( $matches[2] ) ); + return MediaWikiServices::getInstance()->getParser() + ->stripSectionName( trim( $matches[2] ) ); } else { return false; } @@ -3329,11 +3329,10 @@ ERROR; return ""; } - global $wgParser; - if ( $isSubjectPreview ) { $summary = $this->context->msg( 'newsectionsummary' ) - ->rawParams( $wgParser->stripSectionName( $summary ) ) + ->rawParams( MediaWikiServices::getInstance()->getParser() + ->stripSectionName( $summary ) ) ->inContentLanguage()->text(); } @@ -4538,16 +4537,15 @@ ERROR; * @return string */ private function guessSectionName( $text ) { - global $wgParser; - // Detect Microsoft browsers $userAgent = $this->context->getRequest()->getHeader( 'User-Agent' ); + $parser = MediaWikiServices::getInstance()->getParser(); if ( $userAgent && preg_match( '/MSIE|Edge/', $userAgent ) ) { // ...and redirect them to legacy encoding, if available - return $wgParser->guessLegacySectionNameFromWikiText( $text ); + return $parser->guessLegacySectionNameFromWikiText( $text ); } // Meanwhile, real browsers get real anchors - $name = $wgParser->guessSectionNameFromWikiText( $text ); + $name = $parser->guessSectionNameFromWikiText( $text ); // With one little caveat: per T216029, fragments in HTTP redirects need to be urlencoded, // otherwise Chrome double-escapes the rest of the URL. return '#' . urlencode( mb_substr( $name, 1 ) ); diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 859593b122..3e91fb346c 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -2198,8 +2198,6 @@ class OutputPage extends ContextSource { * @return ParserOutput */ private function parseInternal( $text, $title, $linestart, $tidy, $interface, $language ) { - global $wgParser; - if ( is_null( $title ) ) { throw new MWException( 'Empty $mTitle in ' . __METHOD__ ); } @@ -2212,7 +2210,7 @@ class OutputPage extends ContextSource { $oldLang = $popts->setTargetLanguage( $language ); } - $parserOutput = $wgParser->getFreshParser()->parse( + $parserOutput = MediaWikiServices::getInstance()->getParser()->getFreshParser()->parse( $text, $title, $popts, $linestart, true, $this->mRevisionId ); diff --git a/includes/actions/RawAction.php b/includes/actions/RawAction.php index c9d0ae968f..14f7603785 100644 --- a/includes/actions/RawAction.php +++ b/includes/actions/RawAction.php @@ -27,6 +27,7 @@ */ use MediaWiki\Logger\LoggerFactory; +use MediaWiki\MediaWikiServices; /** * A simple method to retrieve the plain source of an article, @@ -181,8 +182,6 @@ class RawAction extends FormlessAction { * @return string|bool */ public function getRawText() { - global $wgParser; - $text = false; $title = $this->getTitle(); $request = $this->getRequest(); @@ -221,7 +220,7 @@ class RawAction extends FormlessAction { } if ( $text !== false && $text !== '' && $request->getRawVal( 'templates' ) === 'expand' ) { - $text = $wgParser->preprocess( + $text = MediaWikiServices::getInstance()->getParser()->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) diff --git a/includes/api/ApiExpandTemplates.php b/includes/api/ApiExpandTemplates.php index 22f52356f2..851373d5e3 100644 --- a/includes/api/ApiExpandTemplates.php +++ b/includes/api/ApiExpandTemplates.php @@ -86,7 +86,6 @@ class ApiExpandTemplates extends ApiBase { $result = $this->getResult(); // Parse text - global $wgParser; $options = ParserOptions::newFromContext( $this->getContext() ); if ( $params['includecomments'] ) { @@ -100,9 +99,10 @@ class ApiExpandTemplates extends ApiBase { $retval = []; + $parser = MediaWikiServices::getInstance()->getParser(); if ( isset( $prop['parsetree'] ) || $params['generatexml'] ) { - $wgParser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS ); - $dom = $wgParser->preprocessToDom( $params['text'] ); + $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS ); + $dom = $parser->preprocessToDom( $params['text'] ); if ( is_callable( [ $dom, 'saveXML' ] ) ) { $xml = $dom->saveXML(); } else { @@ -121,14 +121,14 @@ class ApiExpandTemplates extends ApiBase { // if they didn't want any output except (probably) the parse tree, // then don't bother actually fully expanding it if ( $prop || $params['prop'] === null ) { - $wgParser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS ); - $frame = $wgParser->getPreprocessor()->newFrame(); - $wikitext = $wgParser->preprocess( $params['text'], $titleObj, $options, $revid, $frame ); + $parser->startExternalParse( $titleObj, $options, Parser::OT_PREPROCESS ); + $frame = $parser->getPreprocessor()->newFrame(); + $wikitext = $parser->preprocess( $params['text'], $titleObj, $options, $revid, $frame ); if ( $params['prop'] === null ) { // the old way ApiResult::setContentValue( $retval, 'wikitext', $wikitext ); } else { - $p_output = $wgParser->getOutput(); + $p_output = $parser->getOutput(); if ( isset( $prop['categories'] ) ) { $categories = $p_output->getCategories(); if ( $categories ) { diff --git a/includes/api/ApiFeedWatchlist.php b/includes/api/ApiFeedWatchlist.php index 11b5d91854..c4977f4115 100644 --- a/includes/api/ApiFeedWatchlist.php +++ b/includes/api/ApiFeedWatchlist.php @@ -20,6 +20,8 @@ * @file */ +use MediaWiki\MediaWikiServices; + /** * This action allows users to get their watchlist items in RSS/Atom formats. * When executed, it performs a nested call to the API to get the needed data, @@ -209,8 +211,8 @@ class ApiFeedWatchlist extends ApiBase { if ( $this->linkToSections && $comment !== null && preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) ) { - global $wgParser; - $titleUrl .= $wgParser->guessSectionNameFromWikiText( $matches[ 2 ] ); + $titleUrl .= MediaWikiServices::getInstance()->getParser() + ->guessSectionNameFromWikiText( $matches[ 2 ] ); } $timestamp = $info['timestamp']; diff --git a/includes/api/ApiParse.php b/includes/api/ApiParse.php index 5e4639d6f5..84fff968d2 100644 --- a/includes/api/ApiParse.php +++ b/includes/api/ApiParse.php @@ -83,7 +83,7 @@ class ApiParse extends ApiBase { // The parser needs $wgTitle to be set, apparently the // $title parameter in Parser::parse isn't enough *sigh* // TODO: Does this still need $wgTitle? - global $wgParser, $wgTitle; + global $wgTitle; $redirValues = null; @@ -488,8 +488,9 @@ class ApiParse extends ApiBase { $this->dieWithError( 'apierror-parsetree-notwikitext', 'notwikitext' ); } - $wgParser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS ); - $xml = $wgParser->preprocessToDom( $this->content->getText() )->__toString(); + $parser = MediaWikiServices::getInstance()->getParser(); + $parser->startExternalParse( $titleObj, $popts, Parser::OT_PREPROCESS ); + $xml = $parser->preprocessToDom( $this->content->getText() )->__toString(); $result_array['parsetree'] = $xml; $result_array[ApiResult::META_BC_SUBELEMENTS][] = 'parsetree'; } @@ -631,7 +632,6 @@ class ApiParse extends ApiBase { * @return Content|bool */ private function formatSummary( $title, $params ) { - global $wgParser; $summary = $params['summary'] ?? ''; $sectionTitle = $params['sectiontitle'] ?? ''; @@ -641,8 +641,9 @@ class ApiParse extends ApiBase { } if ( $summary !== '' ) { $summary = wfMessage( 'newsectionsummary' ) - ->rawParams( $wgParser->stripSectionName( $summary ) ) - ->inContentLanguage()->text(); + ->rawParams( MediaWikiServices::getInstance()->getParser() + ->stripSectionName( $summary ) ) + ->inContentLanguage()->text(); } } return Linker::formatComment( $summary, $title, $this->section === 'new' ); diff --git a/includes/api/ApiQueryRevisionsBase.php b/includes/api/ApiQueryRevisionsBase.php index 565e615edd..d0b152edb1 100644 --- a/includes/api/ApiQueryRevisionsBase.php +++ b/includes/api/ApiQueryRevisionsBase.php @@ -496,8 +496,6 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { * @return array */ private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) { - global $wgParser; - $vals = []; $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() ); @@ -505,12 +503,13 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) { $t = $content->getText(); # note: don't set $text - $wgParser->startExternalParse( + $parser = MediaWikiServices::getInstance()->getParser(); + $parser->startExternalParse( $title, ParserOptions::newFromContext( $this->getContext() ), Parser::OT_PREPROCESS ); - $dom = $wgParser->preprocessToDom( $t ); + $dom = $parser->preprocessToDom( $t ); if ( is_callable( [ $dom, 'saveXML' ] ) ) { $xml = $dom->saveXML(); } else { @@ -537,7 +536,7 @@ abstract class ApiQueryRevisionsBase extends ApiQueryGeneratorBase { if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) { $text = $content->getText(); - $text = $wgParser->preprocess( + $text = MediaWikiServices::getInstance()->getParser()->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) diff --git a/includes/api/ApiQuerySiteinfo.php b/includes/api/ApiQuerySiteinfo.php index ea2f31b761..68ab725636 100644 --- a/includes/api/ApiQuerySiteinfo.php +++ b/includes/api/ApiQuerySiteinfo.php @@ -787,12 +787,11 @@ class ApiQuerySiteinfo extends ApiQueryBase { } public function appendExtensionTags( $property ) { - global $wgParser; $tags = array_map( function ( $item ) { return "<$item>"; }, - $wgParser->getTags() + MediaWikiServices::getInstance()->getParser()->getTags() ); ApiResult::setArrayType( $tags, 'BCarray' ); ApiResult::setIndexedTagName( $tags, 't' ); @@ -801,8 +800,7 @@ class ApiQuerySiteinfo extends ApiQueryBase { } public function appendFunctionHooks( $property ) { - global $wgParser; - $hooks = $wgParser->getFunctionHooks(); + $hooks = MediaWikiServices::getInstance()->getParser()->getFunctionHooks(); ApiResult::setArrayType( $hooks, 'BCarray' ); ApiResult::setIndexedTagName( $hooks, 'h' ); diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index 157d88eb34..fb4c7b6d51 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -1203,18 +1203,18 @@ class MessageCache { * @return Parser */ public function getParser() { - global $wgParser, $wgParserConf; - - if ( !$this->mParser && isset( $wgParser ) ) { + global $wgParserConf; + if ( !$this->mParser ) { + $parser = MediaWikiServices::getInstance()->getParser(); # Do some initialisation so that we don't have to do it twice - $wgParser->firstCallInit(); + $parser->firstCallInit(); # Clone it and store it $class = $wgParserConf['class']; if ( $class == ParserDiffTest::class ) { # Uncloneable $this->mParser = new $class( $wgParserConf ); } else { - $this->mParser = clone $wgParser; + $this->mParser = clone $parser; } } diff --git a/includes/content/CssContent.php b/includes/content/CssContent.php index d32fa88e59..87c5ff2eae 100644 --- a/includes/content/CssContent.php +++ b/includes/content/CssContent.php @@ -25,6 +25,8 @@ * @author Daniel Kinzler */ +use MediaWiki\MediaWikiServices; + /** * Content object for CSS pages. * @@ -58,11 +60,11 @@ class CssContent extends TextContent { * @see TextContent::preSaveTransform */ public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { - global $wgParser; // @todo Make pre-save transformation optional for script pages $text = $this->getText(); - $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); + $pst = MediaWikiServices::getInstance()->getParser() + ->preSaveTransform( $text, $title, $user, $popts ); return new static( $pst ); } diff --git a/includes/content/JavaScriptContent.php b/includes/content/JavaScriptContent.php index e6377980f9..4804758fe0 100644 --- a/includes/content/JavaScriptContent.php +++ b/includes/content/JavaScriptContent.php @@ -25,6 +25,8 @@ * @author Daniel Kinzler */ +use MediaWiki\MediaWikiServices; + /** * Content for JavaScript pages. * @@ -56,12 +58,12 @@ class JavaScriptContent extends TextContent { * @return JavaScriptContent */ public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { - global $wgParser; // @todo Make pre-save transformation optional for script pages // See T34858 $text = $this->getText(); - $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); + $pst = MediaWikiServices::getInstance()->getParser() + ->preSaveTransform( $text, $title, $user, $popts ); return new static( $pst ); } diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php index 750b958eab..71dd35c84d 100644 --- a/includes/content/TextContent.php +++ b/includes/content/TextContent.php @@ -253,11 +253,12 @@ class TextContent extends AbstractContent { protected function fillParserOutput( Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output ) { - global $wgParser, $wgTextModelsToParse; + global $wgTextModelsToParse; if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) { // parse just to get links etc into the database, HTML is replaced below. - $output = $wgParser->parse( $this->getText(), $title, $options, true, true, $revId ); + $output = MediaWikiServices::getInstance()->getParser() + ->parse( $this->getText(), $title, $options, true, true, $revId ); } if ( $generateHtml ) { diff --git a/includes/content/WikitextContent.php b/includes/content/WikitextContent.php index 3e2313c4b6..455eb0de4f 100644 --- a/includes/content/WikitextContent.php +++ b/includes/content/WikitextContent.php @@ -59,10 +59,9 @@ class WikitextContent extends TextContent { * @see Content::getSection() */ public function getSection( $sectionId ) { - global $wgParser; - $text = $this->getText(); - $sect = $wgParser->getSection( $text, $sectionId, false ); + $sect = MediaWikiServices::getInstance()->getParser() + ->getSection( $text, $sectionId, false ); if ( $sect === false ) { return false; @@ -109,9 +108,8 @@ class WikitextContent extends TextContent { } } else { # Replacing an existing section; roll out the big guns - global $wgParser; - - $text = $wgParser->replaceSection( $oldtext, $sectionId, $text ); + $text = MediaWikiServices::getInstance()->getParser() + ->replaceSection( $oldtext, $sectionId, $text ); } $newContent = new static( $text ); @@ -147,10 +145,10 @@ class WikitextContent extends TextContent { * @return Content */ public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { - global $wgParser; - $text = $this->getText(); - $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); + + $parser = MediaWikiServices::getInstance()->getParser(); + $pst = $parser->preSaveTransform( $text, $title, $user, $popts ); if ( $text === $pst ) { return $this; @@ -158,7 +156,7 @@ class WikitextContent extends TextContent { $ret = new static( $pst ); - if ( $wgParser->getOutput()->getFlag( 'user-signature' ) ) { + if ( $parser->getOutput()->getFlag( 'user-signature' ) ) { $ret->hadSignature = true; } @@ -176,10 +174,9 @@ class WikitextContent extends TextContent { * @return Content */ public function preloadTransform( Title $title, ParserOptions $popts, $params = [] ) { - global $wgParser; - $text = $this->getText(); - $plt = $wgParser->getPreloadText( $text, $title, $popts, $params ); + $plt = MediaWikiServices::getInstance()->getParser() + ->getPreloadText( $text, $title, $popts, $params ); return new static( $plt ); } @@ -329,7 +326,7 @@ class WikitextContent extends TextContent { /** * Returns a ParserOutput object resulting from parsing the content's text - * using $wgParser. + * using the global Parser service. * * @param Title $title * @param int $revId Revision to pass to the parser (default: null) @@ -341,8 +338,6 @@ class WikitextContent extends TextContent { protected function fillParserOutput( Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output ) { - global $wgParser; - $stackTrace = ( new RuntimeException() )->getTraceAsString(); if ( $this->previousParseStackTrace ) { // NOTE: there may be legitimate changes to re-parse the same WikiText content, @@ -366,7 +361,8 @@ class WikitextContent extends TextContent { $this->previousParseStackTrace = $stackTrace; list( $redir, $text ) = $this->getRedirectTargetAndText(); - $output = $wgParser->parse( $text, $title, $options, true, true, $revId ); + $output = MediaWikiServices::getInstance()->getParser() + ->parse( $text, $title, $options, true, true, $revId ); // Add redirect indicator at the top if ( $redir ) { diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index c28d842af2..cb7d842778 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -4672,7 +4672,7 @@ class Parser { * If you have pre-fetched the nickname or the fancySig option, you can * specify them here to save a database query. * Do not reuse this parser instance after calling getUserSig(), - * as it may have changed if it's the $wgParser. + * as it may have changed. * * @param User &$user * @param string|bool $nickname Nickname to use or false to use user's default nickname @@ -6359,9 +6359,9 @@ class Parser { /** * Return this parser if it is not doing anything, otherwise * get a fresh parser. You can use this method by doing - * $myParser = $wgParser->getFreshParser(), or more simply - * $wgParser->getFreshParser()->parse( ... ); - * if you're unsure if $wgParser is safe to use. + * $newParser = $oldParser->getFreshParser(), or more simply + * $oldParser->getFreshParser()->parse( ... ); + * if you're unsure if $oldParser is safe to use. * * @since 1.24 * @return Parser A parser object that is not parsing anything diff --git a/includes/session/SessionProvider.php b/includes/session/SessionProvider.php index 781fc337a0..def3bc3125 100644 --- a/includes/session/SessionProvider.php +++ b/includes/session/SessionProvider.php @@ -141,9 +141,9 @@ abstract class SessionProvider implements SessionProviderInterface, LoggerAwareI * unless only max-priority makes sense. * * @warning This will be called early in the MediaWiki setup process, - * before $wgUser, $wgLang, $wgOut, $wgParser, $wgTitle, and corresponding - * pieces of the main RequestContext are set up! If you try to use these, - * things *will* break. + * before $wgUser, $wgLang, $wgOut, $wgTitle, the global parser, and + * corresponding pieces of the main RequestContext are set up! If you try + * to use these, things *will* break. * @note The SessionProvider must not attempt to auto-create users. * MediaWiki will do this later (when it's safe) if the chosen session has * a user with a valid name but no ID. @@ -469,7 +469,7 @@ abstract class SessionProvider implements SessionProviderInterface, LoggerAwareI * @note If self::__toString() is overridden, this will likely need to be * overridden as well. * @warning This will be called early during MediaWiki startup. Do not - * use $wgUser, $wgLang, $wgOut, $wgParser, or their equivalents via + * use $wgUser, $wgLang, $wgOut, the global Parser, or their equivalents via * RequestContext from this method! * @return \Message */ diff --git a/includes/specials/SpecialExpandTemplates.php b/includes/specials/SpecialExpandTemplates.php index 9ea5e089a6..ceba987f8a 100644 --- a/includes/specials/SpecialExpandTemplates.php +++ b/includes/specials/SpecialExpandTemplates.php @@ -21,6 +21,8 @@ * @ingroup SpecialPage */ +use MediaWiki\MediaWikiServices; + /** * A special page that expands submitted templates, parser functions, * and variables, allowing easier debugging of these. @@ -53,8 +55,6 @@ class SpecialExpandTemplates extends SpecialPage { * @param string|null $subpage */ function execute( $subpage ) { - global $wgParser; - $this->setHeaders(); $this->addHelpLink( 'Help:ExpandTemplates' ); @@ -77,9 +77,10 @@ class SpecialExpandTemplates extends SpecialPage { $options->setTidy( true ); $options->setMaxIncludeSize( self::MAX_INCLUDE_SIZE ); + $parser = MediaWikiServices::getInstance()->getParser(); if ( $this->generateXML ) { - $wgParser->startExternalParse( $title, $options, Parser::OT_PREPROCESS ); - $dom = $wgParser->preprocessToDom( $input ); + $parser->startExternalParse( $title, $options, Parser::OT_PREPROCESS ); + $dom = $parser->preprocessToDom( $input ); if ( method_exists( $dom, 'saveXML' ) ) { $xml = $dom->saveXML(); @@ -88,7 +89,7 @@ class SpecialExpandTemplates extends SpecialPage { } } - $output = $wgParser->preprocess( $input, $title, $options ); + $output = $parser->preprocess( $input, $title, $options ); } else { $this->removeComments = $request->getBool( 'wpRemoveComments', true ); $this->removeNowiki = $request->getBool( 'wpRemoveNowiki', false ); @@ -246,11 +247,9 @@ class SpecialExpandTemplates extends SpecialPage { * @return ParserOutput */ private function generateHtml( Title $title, $text ) { - global $wgParser; - $popts = ParserOptions::newFromContext( $this->getContext() ); $popts->setTargetLanguage( $title->getPageLanguage() ); - return $wgParser->parse( $text, $title, $popts ); + return MediaWikiServices::getInstance()->getParser()->parse( $text, $title, $popts ); } /** diff --git a/includes/specials/SpecialVersion.php b/includes/specials/SpecialVersion.php index 2632092cd9..c4dd6e3108 100644 --- a/includes/specials/SpecialVersion.php +++ b/includes/specials/SpecialVersion.php @@ -23,6 +23,8 @@ * @ingroup SpecialPage */ +use MediaWiki\MediaWikiServices; + /** * Give information about the version of MediaWiki, PHP, the DB and extensions * @@ -555,9 +557,7 @@ class SpecialVersion extends SpecialPage { * @return string HTML output */ protected function getParserTags() { - global $wgParser; - - $tags = $wgParser->getTags(); + $tags = MediaWikiServices::getInstance()->getParser()->getTags(); if ( count( $tags ) ) { $out = Html::rawElement( @@ -599,9 +599,7 @@ class SpecialVersion extends SpecialPage { * @return string HTML output */ protected function getParserFunctionHooks() { - global $wgParser; - - $fhooks = $wgParser->getFunctionHooks(); + $fhooks = MediaWikiServices::getInstance()->getParser()->getFunctionHooks(); if ( count( $fhooks ) ) { $out = Html::rawElement( 'h2', diff --git a/maintenance/preprocessDump.php b/maintenance/preprocessDump.php index 75904d0c68..a62e0196a5 100644 --- a/maintenance/preprocessDump.php +++ b/maintenance/preprocessDump.php @@ -58,7 +58,7 @@ class PreprocessDump extends DumpIterator { } public function checkOptions() { - global $wgParser, $wgParserConf, $wgPreprocessorCacheThreshold; + global $wgParserConf, $wgPreprocessorCacheThreshold; if ( !$this->hasOption( 'cache' ) ) { $wgPreprocessorCacheThreshold = false; @@ -72,7 +72,7 @@ class PreprocessDump extends DumpIterator { $name = Preprocessor_DOM::class; } - $wgParser->firstCallInit(); + MediaWikiServices::getInstance()->getParser()->firstCallInit(); $this->mPreprocessor = new $name( $this ); } diff --git a/maintenance/preprocessorFuzzTest.php b/maintenance/preprocessorFuzzTest.php index 8df01e66fb..e57e977511 100644 --- a/maintenance/preprocessorFuzzTest.php +++ b/maintenance/preprocessorFuzzTest.php @@ -195,7 +195,7 @@ class PPFuzzTest { } function execute() { - global $wgParser, $wgUser; + global $wgUser; $wgUser = new PPFuzzUser; $wgUser->mName = 'Fuzz'; @@ -206,7 +206,7 @@ class PPFuzzTest { $options->setTemplateCallback( [ $this, 'templateHook' ] ); $options->setTimestamp( wfTimestampNow() ); $this->output = call_user_func( - [ $wgParser, $this->entryPoint ], + [ MediaWikiServices::getInstance()->getParser(), $this->entryPoint ], $this->mainText, $this->title, $options diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index b40b76927e..3eb25a9c95 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -1719,10 +1719,10 @@ class ParserTestRunner { * @return bool True if tag hook is present */ public function requireHook( $name ) { - global $wgParser; + $parser = MediaWikiServices::getInstance()->getParser(); - $wgParser->firstCallInit(); // make sure hooks are loaded. - if ( isset( $wgParser->mTagHooks[$name] ) ) { + $parser->firstCallInit(); // make sure hooks are loaded. + if ( isset( $parser->mTagHooks[$name] ) ) { return true; } else { $this->recorder->warning( " This test suite requires the '$name' hook " . @@ -1738,11 +1738,11 @@ class ParserTestRunner { * @return bool True if function hook is present */ public function requireFunctionHook( $name ) { - global $wgParser; + $parser = MediaWikiServices::getInstance()->getParser(); - $wgParser->firstCallInit(); // make sure hooks are loaded. + $parser->firstCallInit(); // make sure hooks are loaded. - if ( isset( $wgParser->mFunctionHooks[$name] ) ) { + if ( isset( $parser->mFunctionHooks[$name] ) ) { return true; } else { $this->recorder->warning( " This test suite requires the '$name' function " . @@ -1758,11 +1758,11 @@ class ParserTestRunner { * @return bool True if function hook is present */ public function requireTransparentHook( $name ) { - global $wgParser; + $parser = MediaWikiServices::getInstance()->getParser(); - $wgParser->firstCallInit(); // make sure hooks are loaded. + $parser->firstCallInit(); // make sure hooks are loaded. - if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) { + if ( isset( $parser->mTransparentTagHooks[$name] ) ) { return true; } else { $this->recorder->warning( " This test suite requires the '$name' transparent " . diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index 5d77cebac8..e745960a45 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -1,6 +1,5 @@ setMwGlobals( 'wgParser', - MediaWikiServices::getInstance()->getParserFactory()->create() ); + $this->overrideMwServices(); $msg = new RawMessage( '' ); $txt = '<html> tags cannot be' . diff --git a/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php b/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php index 55f4a330ce..282188d264 100644 --- a/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php +++ b/tests/phpunit/includes/api/ApiQuerySiteinfoTest.php @@ -571,22 +571,19 @@ class ApiQuerySiteinfoTest extends ApiTestCase { } public function testExtensionTags() { - global $wgParser; - $expected = array_map( function ( $tag ) { return "<$tag>"; }, - $wgParser->getTags() + MediaWikiServices::getInstance()->getParser()->getTags() ); $this->assertSame( $expected, $this->doQuery( 'extensiontags' ) ); } public function testFunctionHooks() { - global $wgParser; - - $this->assertSame( $wgParser->getFunctionHooks(), $this->doQuery( 'functionhooks' ) ); + $this->assertSame( MediaWikiServices::getInstance()->getParser()->getFunctionHooks(), + $this->doQuery( 'functionhooks' ) ); } public function testVariables() { diff --git a/tests/phpunit/includes/parser/ParserMethodsTest.php b/tests/phpunit/includes/parser/ParserMethodsTest.php index e102b9ba38..cf0b650829 100644 --- a/tests/phpunit/includes/parser/ParserMethodsTest.php +++ b/tests/phpunit/includes/parser/ParserMethodsTest.php @@ -1,4 +1,5 @@ setName( "127.0.0.1" ); $popts = ParserOptions::newFromUser( $user ); - $text = $wgParser->preSaveTransform( $text, $title, $user, $popts ); + $text = MediaWikiServices::getInstance()->getParser() + ->preSaveTransform( $text, $title, $user, $popts ); $this->assertEquals( $expected, $text ); } @@ -78,11 +78,11 @@ class ParserMethodsTest extends MediaWikiLangTestCase { * Did you call Parser::parse recursively? */ public function testRecursiveParse() { - global $wgParser; $title = Title::newFromText( 'foo' ); + $parser = MediaWikiServices::getInstance()->getParser(); $po = new ParserOptions; - $wgParser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] ); - $wgParser->parse( 'baz', $title, $po ); + $parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] ); + $parser->parse( 'baz', $title, $po ); } public function helperParserFunc( $input, $args, $parser ) { @@ -93,16 +93,15 @@ class ParserMethodsTest extends MediaWikiLangTestCase { } public function testCallParserFunction() { - global $wgParser; - // Normal parses test passing PPNodes. Test passing an array. $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) ); - $wgParser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML ); - $frame = $wgParser->getPreprocessor()->newFrame(); - $ret = $wgParser->callParserFunction( $frame, '#tag', + $parser = MediaWikiServices::getInstance()->getParser(); + $parser->startExternalParse( $title, new ParserOptions(), Parser::OT_HTML ); + $frame = $parser->getPreprocessor()->newFrame(); + $ret = $parser->callParserFunction( $frame, '#tag', [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ] ); - $ret['text'] = $wgParser->mStripState->unstripBoth( $ret['text'] ); + $ret['text'] = $parser->mStripState->unstripBoth( $ret['text'] ); $this->assertSame( [ 'found' => true, 'text' => '
foo
', @@ -114,10 +113,9 @@ class ParserMethodsTest extends MediaWikiLangTestCase { * @covers ParserOutput::getSections */ public function testGetSections() { - global $wgParser; - $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) ); - $out = $wgParser->parse( "==foo==\n

bar

\n==baz==\n", $title, new ParserOptions() ); + $out = MediaWikiServices::getInstance()->getParser() + ->parse( "==foo==\n

bar

\n==baz==\n", $title, new ParserOptions() ); $this->assertSame( [ [ 'toclevel' => 1, @@ -195,11 +193,11 @@ class ParserMethodsTest extends MediaWikiLangTestCase { } public function testWrapOutput() { - global $wgParser; $title = Title::newFromText( 'foo' ); $po = new ParserOptions(); - $wgParser->parse( 'Hello World', $title, $po ); - $text = $wgParser->getOutput()->getText(); + $parser = MediaWikiServices::getInstance()->getParser(); + $parser->parse( 'Hello World', $title, $po ); + $text = $parser->getOutput()->getText(); $this->assertContains( 'Hello World', $text ); $this->assertContains( 'getMockTitle( 'ParserRevisionAccessTest' ); $po->enableLimitReport( false ); @@ -369,13 +365,14 @@ class ParserMethodsTest extends MediaWikiLangTestCase { $this->setService( 'RevisionStore', $revisionStore ); - $wgParser->parse( $text, $title, $po, true, true, $revId ); - $html = $wgParser->getOutput()->getText(); + $parser = MediaWikiServices::getInstance()->getParser(); + $parser->parse( $text, $title, $po, true, true, $revId ); + $html = $parser->getOutput()->getText(); $this->assertContains( $expectedInHtml, $html, 'In HTML' ); if ( $expectedInPst !== null ) { - $pst = $wgParser->preSaveTransform( $text, $title, $po->getUser(), $po ); + $pst = $parser->preSaveTransform( $text, $title, $po->getUser(), $po ); $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' ); } } @@ -390,8 +387,8 @@ class ParserMethodsTest extends MediaWikiLangTestCase { /** @dataProvider provideGuessSectionNameFromWikiText */ public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) { $this->setMwGlobals( [ 'wgFragmentMode' => [ $mode ] ] ); - global $wgParser; - $result = $wgParser->guessSectionNameFromWikiText( $input ); + $result = MediaWikiServices::getInstance()->getParser() + ->guessSectionNameFromWikiText( $input ); $this->assertEquals( $result, $expected ); } diff --git a/tests/phpunit/suites/UploadFromUrlTestSuite.php b/tests/phpunit/suites/UploadFromUrlTestSuite.php index 556c75412a..00d607f99a 100644 --- a/tests/phpunit/suites/UploadFromUrlTestSuite.php +++ b/tests/phpunit/suites/UploadFromUrlTestSuite.php @@ -16,8 +16,7 @@ class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite { } protected function setUp() { - global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc, $wgUser, - $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, + global $IP, $messageMemc, $wgMemc, $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory, $wgParserCacheType, $wgNamespaceAliases, $wgNamespaceProtection; $tmpDir = $this->getNewTempDirectory(); @@ -66,7 +65,6 @@ class UploadFromUrlTestSuite extends PHPUnit_Framework_TestSuite { $wgUser = new User; $wgLang = $context->getLanguage(); $wgOut = $context->getOutput(); - $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], [ $wgParserConf ] ); $wgRequest = $context->getRequest(); if ( $wgStyleDirectory === false ) { -- 2.20.1