Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Feb 28, 2015
1 parent d0b8088 commit dd96cce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 52 deletions.
85 changes: 41 additions & 44 deletions src/HookRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand All @@ -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] );
}
Expand Down Expand Up @@ -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;
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 3 additions & 5 deletions tests/phpunit/Unit/HookRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand Down

0 comments on commit dd96cce

Please sign in to comment.