Use ParserFactory in a bunch of places
authorAryeh Gregor <ayg@aryeh.name>
Fri, 3 Aug 2018 08:43:00 +0000 (11:43 +0300)
committerKunal Mehta <legoktm@member.fsf.org>
Sat, 11 Aug 2018 06:16:17 +0000 (00:16 -0600)
I wasn't sure how to convert the rest of the occurrences in core (there
are a significant number).

Bug: T200881
Change-Id: I114bba946cd3ea8a293121e275588c3c4d174f94

includes/ServiceWiring.php
includes/parser/Parser.php
includes/parser/ParserFactory.php
maintenance/parse.php
tests/phpunit/includes/MessageTest.php
tests/phpunit/includes/parser/ParserFactoryTest.php
tests/phpunit/includes/parser/TagHooksTest.php
tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php

index 5b64a60..e5ebe2d 100644 (file)
@@ -56,7 +56,6 @@ use MediaWiki\Storage\RevisionLookup;
 use MediaWiki\Storage\RevisionStore;
 use MediaWiki\Storage\RevisionStoreFactory;
 use MediaWiki\Storage\SqlBlobStore;
 use MediaWiki\Storage\RevisionStore;
 use MediaWiki\Storage\RevisionStoreFactory;
 use MediaWiki\Storage\SqlBlobStore;
-use Wikimedia\ObjectFactory;
 
 return [
        'ActorMigration' => function ( MediaWikiServices $services ) : ActorMigration {
 
 return [
        'ActorMigration' => function ( MediaWikiServices $services ) : ActorMigration {
@@ -367,9 +366,7 @@ return [
        },
 
        'Parser' => function ( MediaWikiServices $services ) : Parser {
        },
 
        'Parser' => function ( MediaWikiServices $services ) : Parser {
-               $conf = $services->getMainConfig()->get( 'ParserConf' );
-               return ObjectFactory::constructClassInstance( $conf['class'],
-                       [ $conf, $services->getMagicWordFactory() ] );
+               return $services->getParserFactory()->create();
        },
 
        'ParserCache' => function ( MediaWikiServices $services ) : ParserCache {
        },
 
        'ParserCache' => function ( MediaWikiServices $services ) : ParserCache {
index 833e5c8..7599a1f 100644 (file)
@@ -266,15 +266,19 @@ class Parser {
        /** @var Language */
        private $contLang;
 
        /** @var Language */
        private $contLang;
 
+       /** @var ParserFactory */
+       private $factory;
+
        /**
         * @param array $conf See $wgParserConf documentation
         * @param MagicWordFactory|null $magicWordFactory
         * @param Language|null $contLang Content language
        /**
         * @param array $conf See $wgParserConf documentation
         * @param MagicWordFactory|null $magicWordFactory
         * @param Language|null $contLang Content language
+        * @param ParserFactory|null $factory
         * @param string|null $urlProtocols As returned from wfUrlProtocols()
         */
        public function __construct(
                array $conf = [], MagicWordFactory $magicWordFactory = null, Language $contLang = null,
         * @param string|null $urlProtocols As returned from wfUrlProtocols()
         */
        public function __construct(
                array $conf = [], MagicWordFactory $magicWordFactory = null, Language $contLang = null,
-               $urlProtocols = null
+               ParserFactory $factory = null, $urlProtocols = null
        ) {
                $this->mConf = $conf;
                $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
        ) {
                $this->mConf = $conf;
                $this->mUrlProtocols = $urlProtocols ?? wfUrlProtocols();
@@ -301,6 +305,8 @@ class Parser {
                        MediaWikiServices::getInstance()->getMagicWordFactory();
 
                $this->contLang = $contLang ?? MediaWikiServices::getInstance()->getContentLanguage();
                        MediaWikiServices::getInstance()->getMagicWordFactory();
 
                $this->contLang = $contLang ?? MediaWikiServices::getInstance()->getContentLanguage();
+
+               $this->factory = $factory ?? MediaWikiServices::getInstance()->getParserFactory();
        }
 
        /**
        }
 
        /**
@@ -6199,9 +6205,8 @@ class Parser {
         * @return Parser A parser object that is not parsing anything
         */
        public function getFreshParser() {
         * @return Parser A parser object that is not parsing anything
         */
        public function getFreshParser() {
-               global $wgParserConf;
                if ( $this->mInParse ) {
                if ( $this->mInParse ) {
-                       return new $wgParserConf['class']( $wgParserConf );
+                       return $this->factory->create();
                } else {
                        return $this;
                }
                } else {
                        return $this;
                }
index 7b66f9c..646f855 100644 (file)
@@ -57,7 +57,7 @@ class ParserFactory {
         * @since 1.32
         */
        public function create() : Parser {
         * @since 1.32
         */
        public function create() : Parser {
-               return new Parser( $this->conf, $this->magicWordFactory, $this->contLang,
+               return new Parser( $this->conf, $this->magicWordFactory, $this->contLang, $this,
                        $this->urlProtocols );
        }
 }
                        $this->urlProtocols );
        }
 }
index cf2fe54..d9a247c 100644 (file)
@@ -1,4 +1,7 @@
 <?php
 <?php
+
+use MediaWiki\MediaWikiServices;
+
 /**
  * Parse some wikitext.
  *
 /**
  * Parse some wikitext.
  *
@@ -103,9 +106,7 @@ class CLIParser extends Maintenance {
        }
 
        protected function initParser() {
        }
 
        protected function initParser() {
-               global $wgParserConf;
-               $parserClass = $wgParserConf['class'];
-               $this->parser = new $parserClass();
+               $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create();
        }
 
        /**
        }
 
        /**
index 3e3d04a..d75c0e5 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
 <?php
 
-use Wikimedia\ObjectFactory;
+use MediaWiki\MediaWikiServices;
 use Wikimedia\TestingAccessWrapper;
 
 /**
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -401,14 +401,12 @@ class MessageTest extends MediaWikiLangTestCase {
        }
 
        public function testRawHtmlInMsg() {
        }
 
        public function testRawHtmlInMsg() {
-               global $wgParserConf;
                $this->setMwGlobals( 'wgRawHtml', true );
                // We have to reset the core hook registration.
                // to register the html hook
                MessageCache::destroyInstance();
                $this->setMwGlobals( 'wgParser',
                $this->setMwGlobals( 'wgRawHtml', true );
                // We have to reset the core hook registration.
                // to register the html hook
                MessageCache::destroyInstance();
                $this->setMwGlobals( 'wgParser',
-                       ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
-               );
+                       MediaWikiServices::getInstance()->getParserFactory()->create() );
 
                $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
                $txt = '<span class="error">&lt;html&gt; tags cannot be' .
 
                $msg = new RawMessage( '<html><script>alert("xss")</script></html>' );
                $txt = '<span class="error">&lt;html&gt; tags cannot be' .
index 55a1af4..f37bdfc 100644 (file)
@@ -12,7 +12,8 @@ class ParserFactoryTest extends MediaWikiTestCase {
        public function testConstructorArgNum() {
                $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
                $instanceConstructor = new ReflectionMethod( 'Parser', '__construct' );
        public function testConstructorArgNum() {
                $factoryConstructor = new ReflectionMethod( 'ParserFactory', '__construct' );
                $instanceConstructor = new ReflectionMethod( 'Parser', '__construct' );
-               $this->assertSame( $instanceConstructor->getNumberOfParameters(),
+               // Subtract one for the ParserFactory itself
+               $this->assertSame( $instanceConstructor->getNumberOfParameters() - 1,
                        $factoryConstructor->getNumberOfParameters(),
                        'Parser and ParserFactory constructors have an inconsistent number of parameters. ' .
                        'Did you add a parameter to one and not the other?' );
                        $factoryConstructor->getNumberOfParameters(),
                        'Parser and ParserFactory constructors have an inconsistent number of parameters. ' .
                        'Did you add a parameter to one and not the other?' );
index bc09adc..48312d9 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
 <?php
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * @group Database
  * @group Parser
 /**
  * @group Database
  * @group Parser
@@ -54,8 +56,7 @@ class TagHooksTest extends MediaWikiTestCase {
         * @dataProvider provideValidNames
         */
        public function testTagHooks( $tag ) {
         * @dataProvider provideValidNames
         */
        public function testTagHooks( $tag ) {
-               global $wgParserConf;
-               $parser = new Parser( $wgParserConf );
+               $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
 
                $parser->setHook( $tag, [ $this, 'tagCallback' ] );
                $parserOutput = $parser->parse(
 
                $parser->setHook( $tag, [ $this, 'tagCallback' ] );
                $parserOutput = $parser->parse(
@@ -73,8 +74,7 @@ class TagHooksTest extends MediaWikiTestCase {
         * @expectedException MWException
         */
        public function testBadTagHooks( $tag ) {
         * @expectedException MWException
         */
        public function testBadTagHooks( $tag ) {
-               global $wgParserConf;
-               $parser = new Parser( $wgParserConf );
+               $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
 
                $parser->setHook( $tag, [ $this, 'tagCallback' ] );
                $parser->parse(
 
                $parser->setHook( $tag, [ $this, 'tagCallback' ] );
                $parser->parse(
@@ -89,8 +89,7 @@ class TagHooksTest extends MediaWikiTestCase {
         * @dataProvider provideValidNames
         */
        public function testFunctionTagHooks( $tag ) {
         * @dataProvider provideValidNames
         */
        public function testFunctionTagHooks( $tag ) {
-               global $wgParserConf;
-               $parser = new Parser( $wgParserConf );
+               $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
 
                $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
                $parserOutput = $parser->parse(
 
                $parser->setFunctionTagHook( $tag, [ $this, 'functionTagCallback' ], 0 );
                $parserOutput = $parser->parse(
@@ -108,8 +107,7 @@ class TagHooksTest extends MediaWikiTestCase {
         * @expectedException MWException
         */
        public function testBadFunctionTagHooks( $tag ) {
         * @expectedException MWException
         */
        public function testBadFunctionTagHooks( $tag ) {
-               global $wgParserConf;
-               $parser = new Parser( $wgParserConf );
+               $parser = MediaWikiServices::getInstance()->getParserFactory()->create();
 
                $parser->setFunctionTagHook(
                        $tag,
 
                $parser->setFunctionTagHook(
                        $tag,
index 296691d..43c678e 100644 (file)
@@ -3,7 +3,6 @@
 use MediaWiki\Auth\AuthManager;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Preferences\DefaultPreferencesFactory;
 use MediaWiki\Auth\AuthManager;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Preferences\DefaultPreferencesFactory;
-use Wikimedia\ObjectFactory;
 use Wikimedia\TestingAccessWrapper;
 
 /**
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -38,13 +37,13 @@ class DefaultPreferencesFactoryTest extends MediaWikiTestCase {
 
        public function setUp() {
                parent::setUp();
 
        public function setUp() {
                parent::setUp();
-               global $wgParserConf;
                $this->context = new RequestContext();
                $this->context->setTitle( Title::newFromText( self::class ) );
                $this->context = new RequestContext();
                $this->context->setTitle( Title::newFromText( self::class ) );
-               $this->setMwGlobals( 'wgParser',
-                       ObjectFactory::constructClassInstance( $wgParserConf['class'], [ $wgParserConf ] )
-               );
-               $this->config = MediaWikiServices::getInstance()->getMainConfig();
+
+               $services = MediaWikiServices::getInstance();
+
+               $this->setMwGlobals( 'wgParser', $services->getParserFactory()->create() );
+               $this->config = $services->getMainConfig();
        }
 
        /**
        }
 
        /**