diff --git a/src/HookRegistry.php b/src/HookRegistry.php index 78d7c46..e90422c 100644 --- a/src/HookRegistry.php +++ b/src/HookRegistry.php @@ -18,62 +18,19 @@ class HookRegistry { */ private $handlers = array(); - /** - * @var array - */ - private $configuration; - /** * @since 1.0 * * @param array $configuration */ public function __construct( $configuration ) { - $this->configuration = $configuration; + $this->registerCallbackHandlers( $configuration ); } /** * @since 1.0 */ public function register() { - - $configuration = $this->configuration; - - /** - * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput - */ - $this->handlers['OutputPageParserOutput'] = function ( &$outputPage, $parserOutput ) use( $configuration ) { - - $parserData = ApplicationFactory::getInstance()->newParserData( - $outputPage->getTitle(), - $parserOutput - ); - - $fallbackSemanticDataFetcher = new FallbackSemanticDataFetcher( - $parserData, - ApplicationFactory::getInstance()->getStore() - ); - - $outputPageTagFormatter = new OutputPageTagFormatter( $outputPage ); - $outputPageTagFormatter->setMetaTagsBlacklist( $configuration['metaTagsBlacklist'] ); - $outputPageTagFormatter->setViewActionState( \Action::getActionName( $outputPage->getContext() ) ); - - $propertyValuesContentFetcher = new PropertyValuesContentFetcher( $fallbackSemanticDataFetcher ); - $propertyValuesContentFetcher->useFallbackChainForMultipleProperties( $configuration['metaTagsFallbackUseForMultipleProperties'] ); - - $metaTagsModifier = new MetaTagsModifier( - $propertyValuesContentFetcher, - $outputPageTagFormatter - ); - - $metaTagsModifier->setMetaTagsContentPropertySelector( $configuration['metaTagsContentPropertySelector'] ); - $metaTagsModifier->setMetaTagsStaticContentDescriptor( $configuration['metaTagsStaticContentDescriptor'] ); - - $metaTagsModifier->addMetaTags(); - - return true; - }; - foreach ( $this->handlers as $name => $callback ) { Hooks::register( $name, $callback ); } @@ -87,6 +44,8 @@ public function deregister() { Hooks::clear( $name ); + // Remove registered `wgHooks` hooks that are not cleared by the + // previous call if ( isset( $GLOBALS['wgHooks'][$name] ) ) { unset( $GLOBALS['wgHooks'][$name] ); } @@ -115,4 +74,42 @@ public function getHandlers( $name ) { return Hooks::getHandlers( $name ); } + private function registerCallbackHandlers( $configuration ) { + + /** + * @see https://www.mediawiki.org/wiki/Manual:Hooks/OutputPageParserOutput + */ + $this->handlers['OutputPageParserOutput'] = function ( &$outputPage, $parserOutput ) use( $configuration ) { + + $parserData = ApplicationFactory::getInstance()->newParserData( + $outputPage->getTitle(), + $parserOutput + ); + + $fallbackSemanticDataFetcher = new FallbackSemanticDataFetcher( + $parserData, + ApplicationFactory::getInstance()->getStore() + ); + + $outputPageTagFormatter = new OutputPageTagFormatter( $outputPage ); + $outputPageTagFormatter->setMetaTagsBlacklist( $configuration['metaTagsBlacklist'] ); + $outputPageTagFormatter->setViewActionState( \Action::getActionName( $outputPage->getContext() ) ); + + $propertyValuesContentFetcher = new PropertyValuesContentFetcher( $fallbackSemanticDataFetcher ); + $propertyValuesContentFetcher->useFallbackChainForMultipleProperties( $configuration['metaTagsFallbackUseForMultipleProperties'] ); + + $metaTagsModifier = new MetaTagsModifier( + $propertyValuesContentFetcher, + $outputPageTagFormatter + ); + + $metaTagsModifier->setMetaTagsContentPropertySelector( $configuration['metaTagsContentPropertySelector'] ); + $metaTagsModifier->setMetaTagsStaticContentDescriptor( $configuration['metaTagsStaticContentDescriptor'] ); + + $metaTagsModifier->addMetaTags(); + + return true; + }; + } + } diff --git a/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php b/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php index 7c8f5bb..8aa862b 100644 --- a/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php +++ b/tests/phpunit/Integration/MetaTagsContentGenerationIntegrationTest.php @@ -52,11 +52,9 @@ protected function setUp() { 'metaTagsFallbackUseForMultipleProperties' => false ); - $hookRegistry = new HookRegistry( $configuration ); - $hookRegistry->register(); - // Deregister all hooks to ensure that only the one that is ought to be // tested is tested + $hookRegistry = new HookRegistry( $configuration ); $hookRegistry->deregister(); $hookRegistry->register(); } diff --git a/tests/phpunit/Unit/HookRegistryTest.php b/tests/phpunit/Unit/HookRegistryTest.php index bc6ec23..d0bfcb5 100644 --- a/tests/phpunit/Unit/HookRegistryTest.php +++ b/tests/phpunit/Unit/HookRegistryTest.php @@ -37,15 +37,13 @@ public function testRegister() { ); $instance = new HookRegistry( $configuration ); + $instance->deregister(); $instance->register(); - $this->doTestOutputPageParserOutput( $instance ); + $this->doTestRegisteredOutputPageParserOutputHandler( $instance ); } - public function doTestOutputPageParserOutput( $instance ) { - - $instance->deregister(); - $instance->register(); + public function doTestRegisteredOutputPageParserOutputHandler( $instance ) { $this->assertTrue( $instance->isRegistered( 'OutputPageParserOutput' )