Remove all $wgParser use from core
authorAryeh Gregor <ayg@aryeh.name>
Thu, 11 Apr 2019 13:36:15 +0000 (16:36 +0300)
committerAryeh Gregor <ayg@aryeh.name>
Wed, 17 Apr 2019 12:16:50 +0000 (15:16 +0300)
Bug: T160811

Change-Id: I0556c04d33386d0339e02e2bf7a1ee74d97c2abd

26 files changed:
.phpcs.xml
includes/DefaultSettings.php
includes/EditPage.php
includes/OutputPage.php
includes/actions/RawAction.php
includes/api/ApiExpandTemplates.php
includes/api/ApiFeedWatchlist.php
includes/api/ApiParse.php
includes/api/ApiQueryRevisionsBase.php
includes/api/ApiQuerySiteinfo.php
includes/cache/MessageCache.php
includes/content/CssContent.php
includes/content/JavaScriptContent.php
includes/content/TextContent.php
includes/content/WikitextContent.php
includes/parser/Parser.php
includes/session/SessionProvider.php
includes/specials/SpecialExpandTemplates.php
includes/specials/SpecialVersion.php
maintenance/preprocessDump.php
maintenance/preprocessorFuzzTest.php
tests/parser/ParserTestRunner.php
tests/phpunit/includes/MessageTest.php
tests/phpunit/includes/api/ApiQuerySiteinfoTest.php
tests/phpunit/includes/parser/ParserMethodsTest.php
tests/phpunit/suites/UploadFromUrlTestSuite.php

index cc9e53c..fef07e6 100644 (file)
@@ -14,7 +14,6 @@
                <exclude name="MediaWiki.NamingConventions.LowerCamelFunctionsName.FunctionName" />
                <exclude name="MediaWiki.Usage.DbrQueryUsage.DbrQueryFound" />
                <exclude name="MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgContLang" />
-               <exclude name="MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgParser" />
                <exclude name="MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle" />
                <exclude name="MediaWiki.Usage.ForbiddenFunctions.passthru" />
                <exclude name="MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment" />
index 4547009..418b1b1 100644 (file)
@@ -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.
index add48d9..6990934 100644 (file)
@@ -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 ) );
index 859593b..3e91fb3 100644 (file)
@@ -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
                );
index c9d0ae9..14f7603 100644 (file)
@@ -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() )
index 22f5235..851373d 100644 (file)
@@ -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 ) {
index 11b5d91..c4977f4 100644 (file)
@@ -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'];
index 5e4639d..84fff96 100644 (file)
@@ -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' );
index 565e615..d0b152e 100644 (file)
@@ -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() )
index ea2f31b..68ab725 100644 (file)
@@ -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' );
 
index 157d88e..fb4c7b6 100644 (file)
@@ -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;
                        }
                }
 
index d32fa88..87c5ff2 100644 (file)
@@ -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 );
        }
index e637798..4804758 100644 (file)
@@ -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 );
        }
index 750b958..71dd35c 100644 (file)
@@ -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 ) {
index 3e2313c..455eb0d 100644 (file)
@@ -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 ) {
index c28d842..cb7d842 100644 (file)
@@ -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
index 781fc33..def3bc3 100644 (file)
@@ -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
         */
index 9ea5e08..ceba987 100644 (file)
@@ -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 );
        }
 
        /**
index 2632092..c4dd6e3 100644 (file)
@@ -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',
index 75904d0..a62e019 100644 (file)
@@ -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 );
        }
 
index 8df01e6..e57e977 100644 (file)
@@ -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
index b40b769..3eb25a9 100644 (file)
@@ -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 " .
index 5d77ceb..e745960 100644 (file)
@@ -1,6 +1,5 @@
 <?php
 
-use MediaWiki\MediaWikiServices;
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -408,8 +407,7 @@ class MessageTest extends MediaWikiLangTestCase {
                // We have to reset the core hook registration.
                // to register the html hook
                MessageCache::destroyInstance();
-               $this->setMwGlobals( 'wgParser',
-                       MediaWikiServices::getInstance()->getParserFactory()->create() );
+               $this->overrideMwServices();
 
                $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
                $txt = '<span class="error">&lt;html&gt; tags cannot be' .
index 55f4a33..282188d 100644 (file)
@@ -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() {
index e102b9b..cf0b650 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Revision\MutableRevisionRecord;
 use MediaWiki\Revision\RevisionStore;
 use MediaWiki\Revision\SlotRecord;
@@ -26,13 +27,12 @@ class ParserMethodsTest extends MediaWikiLangTestCase {
         * @dataProvider providePreSaveTransform
         */
        public function testPreSaveTransform( $text, $expected ) {
-               global $wgParser;
-
                $title = Title::newFromText( str_replace( '::', '__', __METHOD__ ) );
                $user = new User();
                $user->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( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
+               $parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
+               $parser->parse( '<recursivecallparser>baz</recursivecallparser>', $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' => '<pre style="margin-left: 1.6em">foo</pre>',
@@ -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<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
+               $out = MediaWikiServices::getInstance()->getParser()
+                       ->parse( "==foo==\n<h2>bar</h2>\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( '<div', $text );
@@ -330,8 +328,6 @@ class ParserMethodsTest extends MediaWikiLangTestCase {
                $expectedInHtml,
                $expectedInPst = null
        ) {
-               global $wgParser;
-
                $title = $this->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 );
        }
 
index 556c754..00d607f 100644 (file)
@@ -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 ) {