From bca608592079a5f8697ad35ad3188b62b73859d9 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Fri, 3 Aug 2018 11:43:00 +0300 Subject: [PATCH] Use ParserFactory in a bunch of places 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 | 5 +---- includes/parser/Parser.php | 11 ++++++++--- includes/parser/ParserFactory.php | 2 +- maintenance/parse.php | 7 ++++--- tests/phpunit/includes/MessageTest.php | 6 ++---- .../phpunit/includes/parser/ParserFactoryTest.php | 3 ++- tests/phpunit/includes/parser/TagHooksTest.php | 14 ++++++-------- .../preferences/DefaultPreferencesFactoryTest.php | 11 +++++------ 8 files changed, 29 insertions(+), 30 deletions(-) diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index 5b64a600a4..e5ebe2dc98 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -56,7 +56,6 @@ use MediaWiki\Storage\RevisionLookup; use MediaWiki\Storage\RevisionStore; use MediaWiki\Storage\RevisionStoreFactory; use MediaWiki\Storage\SqlBlobStore; -use Wikimedia\ObjectFactory; return [ 'ActorMigration' => function ( MediaWikiServices $services ) : ActorMigration { @@ -367,9 +366,7 @@ return [ }, '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 { diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index 833e5c8e7f..7599a1f1cf 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -266,15 +266,19 @@ class Parser { /** @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 ParserFactory|null $factory * @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(); @@ -301,6 +305,8 @@ class Parser { 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() { - global $wgParserConf; if ( $this->mInParse ) { - return new $wgParserConf['class']( $wgParserConf ); + return $this->factory->create(); } else { return $this; } diff --git a/includes/parser/ParserFactory.php b/includes/parser/ParserFactory.php index 7b66f9c6db..646f8552cd 100644 --- a/includes/parser/ParserFactory.php +++ b/includes/parser/ParserFactory.php @@ -57,7 +57,7 @@ class ParserFactory { * @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 ); } } diff --git a/maintenance/parse.php b/maintenance/parse.php index cf2fe541f2..d9a247c354 100644 --- a/maintenance/parse.php +++ b/maintenance/parse.php @@ -1,4 +1,7 @@ parser = new $parserClass(); + $this->parser = MediaWikiServices::getInstance()->getParserFactory()->create(); } /** diff --git a/tests/phpunit/includes/MessageTest.php b/tests/phpunit/includes/MessageTest.php index 3e3d04aeed..d75c0e5410 100644 --- a/tests/phpunit/includes/MessageTest.php +++ b/tests/phpunit/includes/MessageTest.php @@ -1,6 +1,6 @@ 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( '' ); $txt = '<html> tags cannot be' . diff --git a/tests/phpunit/includes/parser/ParserFactoryTest.php b/tests/phpunit/includes/parser/ParserFactoryTest.php index 55a1af47e2..f37bdfcbe3 100644 --- a/tests/phpunit/includes/parser/ParserFactoryTest.php +++ b/tests/phpunit/includes/parser/ParserFactoryTest.php @@ -12,7 +12,8 @@ class ParserFactoryTest extends MediaWikiTestCase { 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?' ); diff --git a/tests/phpunit/includes/parser/TagHooksTest.php b/tests/phpunit/includes/parser/TagHooksTest.php index bc09adc809..48312d98f9 100644 --- a/tests/phpunit/includes/parser/TagHooksTest.php +++ b/tests/phpunit/includes/parser/TagHooksTest.php @@ -1,5 +1,7 @@ getParserFactory()->create(); $parser->setHook( $tag, [ $this, 'tagCallback' ] ); $parserOutput = $parser->parse( @@ -73,8 +74,7 @@ class TagHooksTest extends MediaWikiTestCase { * @expectedException MWException */ public function testBadTagHooks( $tag ) { - global $wgParserConf; - $parser = new Parser( $wgParserConf ); + $parser = MediaWikiServices::getInstance()->getParserFactory()->create(); $parser->setHook( $tag, [ $this, 'tagCallback' ] ); $parser->parse( @@ -89,8 +89,7 @@ class TagHooksTest extends MediaWikiTestCase { * @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( @@ -108,8 +107,7 @@ class TagHooksTest extends MediaWikiTestCase { * @expectedException MWException */ public function testBadFunctionTagHooks( $tag ) { - global $wgParserConf; - $parser = new Parser( $wgParserConf ); + $parser = MediaWikiServices::getInstance()->getParserFactory()->create(); $parser->setFunctionTagHook( $tag, diff --git a/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php b/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php index 296691d331..43c678e928 100644 --- a/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php +++ b/tests/phpunit/includes/preferences/DefaultPreferencesFactoryTest.php @@ -3,7 +3,6 @@ use MediaWiki\Auth\AuthManager; use MediaWiki\MediaWikiServices; use MediaWiki\Preferences\DefaultPreferencesFactory; -use Wikimedia\ObjectFactory; use Wikimedia\TestingAccessWrapper; /** @@ -38,13 +37,13 @@ class DefaultPreferencesFactoryTest extends MediaWikiTestCase { public function setUp() { parent::setUp(); - global $wgParserConf; $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(); } /** -- 2.20.1