Skip to content

Commit dd8cb7b

Browse files
committed
Tidy HookRegistry
1 parent c1a72bb commit dd8cb7b

File tree

4 files changed

+169
-123
lines changed

4 files changed

+169
-123
lines changed

SemanticInterlanguageLinks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
$cachePrefix
7575
);
7676

77-
$hookRegistry->register( $GLOBALS['wgHooks'] );
77+
$hookRegistry->register();
7878
};
7979

8080
} );

src/HookRegistry.php

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SIL\Search\SearchResultModifier;
1010
use SIL\Search\LanguageResultMatchFinder;
1111
use SIL\Category\ByLanguageCategoryPage;
12+
use Hooks;
1213

1314
/**
1415
* @license GNU GPL v2+
@@ -19,19 +20,9 @@
1920
class HookRegistry {
2021

2122
/**
22-
* @var Store
23+
* @var array
2324
*/
24-
private $store;
25-
26-
/**
27-
* @var Cache
28-
*/
29-
private $cache;
30-
31-
/**
32-
* @var string
33-
*/
34-
private $cachePrefix;
25+
private $handlers = array();
3526

3627
/**
3728
* @since 1.0
@@ -41,48 +32,70 @@ class HookRegistry {
4132
* @param string $cachePrefix
4233
*/
4334
public function __construct( Store $store, Cache $cache, $cachePrefix ) {
44-
$this->store = $store;
45-
$this->cache = $cache;
46-
$this->cachePrefix = $cachePrefix;
35+
$this->addCallbackHandlers( $store, $cache, $cachePrefix );
4736
}
4837

4938
/**
50-
* @since 1.0
39+
* @since 1.1
5140
*
52-
* @param array &$wgHooks
41+
* @param string $name
5342
*
5443
* @return boolean
5544
*/
56-
public function register( &$wgHooks ) {
45+
public function isRegistered( $name ) {
46+
return Hooks::isRegistered( $name );
47+
}
48+
49+
/**
50+
* @since 1.1
51+
*
52+
* @param string $name
53+
*
54+
* @return Callable|false
55+
*/
56+
public function getHandlersFor( $name ) {
57+
return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
58+
}
59+
60+
/**
61+
* @since 1.0
62+
*/
63+
public function register() {
64+
foreach ( $this->handlers as $name => $callback ) {
65+
Hooks::register( $name, $callback );
66+
}
67+
}
68+
69+
private function addCallbackHandlers( $store, $cache, $cachePrefix ) {
5770

5871
$cacheKeyGenerator = new CacheKeyGenerator();
5972
$cacheKeyGenerator->setAuxiliaryKeyModifier( '20150122' );
60-
$cacheKeyGenerator->setCachePrefix( $this->cachePrefix );
73+
$cacheKeyGenerator->setCachePrefix( $cachePrefix );
6174

6275
$languageTargetLinksCache = new LanguageTargetLinksCache(
63-
$this->cache,
76+
$cache,
6477
$cacheKeyGenerator
6578
);
6679

6780
$interlanguageLinksLookup = new InterlanguageLinksLookup(
6881
$languageTargetLinksCache
6982
);
7083

71-
$interlanguageLinksLookup->setStore( $this->store );
84+
$interlanguageLinksLookup->setStore( $store );
7285

7386
$propertyRegistry = new PropertyRegistry();
7487

7588
/**
7689
* @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
7790
*/
78-
$wgHooks['smwInitProperties'][] = function () use ( $propertyRegistry ) {
91+
$this->handlers['SMW::Property::initProperties'] = function () use ( $propertyRegistry ) {
7992
return $propertyRegistry->register();
8093
};
8194

8295
/**
8396
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleFromTitle
8497
*/
85-
$wgHooks['ArticleFromTitle'][] = function ( $title, &$page ) use( $interlanguageLinksLookup ) {
98+
$this->handlers['ArticleFromTitle'] = function ( $title, &$page ) use( $interlanguageLinksLookup ) {
8699

87100
$byLanguageCategoryPage = new ByLanguageCategoryPage( $title );
88101
$byLanguageCategoryPage->setCategoryFilterByLanguageState( $GLOBALS['egSILEnabledCategoryFilterByLanguage'] );
@@ -91,18 +104,16 @@ public function register( &$wgHooks ) {
91104
return true;
92105
};
93106

94-
$this->registerInterlanguageParserHooks( $interlanguageLinksLookup, $wgHooks );
95-
$this->registerSpecialSearchHooks( $interlanguageLinksLookup, $wgHooks );
96-
97-
return true;
107+
$this->registerInterlanguageParserHooks( $interlanguageLinksLookup );
108+
$this->registerSpecialSearchHooks( $interlanguageLinksLookup );
98109
}
99110

100-
private function registerInterlanguageParserHooks( InterlanguageLinksLookup $interlanguageLinksLookup, &$wgHooks ) {
111+
private function registerInterlanguageParserHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
101112

102113
/**
103114
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
104115
*/
105-
$wgHooks['ParserFirstCallInit'][] = function ( &$parser ) use( $interlanguageLinksLookup ) {
116+
$this->handlers['ParserFirstCallInit'] = function ( &$parser ) use( $interlanguageLinksLookup ) {
106117

107118
$parserFunctionFactory = new ParserFunctionFactory();
108119

@@ -124,7 +135,7 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
124135
/**
125136
* https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDelete
126137
*/
127-
$wgHooks['SMW::SQLStore::BeforeDeleteSubjectComplete'][] = function ( $store, $title ) use ( $interlanguageLinksLookup ) {
138+
$this->handlers['SMW::SQLStore::BeforeDeleteSubjectComplete'] = function ( $store, $title ) use ( $interlanguageLinksLookup ) {
128139

129140
$interlanguageLinksLookup->setStore( $store );
130141
$interlanguageLinksLookup->invalidateLookupCache( $title );
@@ -135,7 +146,7 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
135146
/**
136147
* https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
137148
*/
138-
$wgHooks['SMW::SQLStore::BeforeChangeTitleComplete'][] = function ( $store, $oldTitle, $newTitle, $pageid, $redirid ) use ( $interlanguageLinksLookup ) {
149+
$this->handlers['SMW::SQLStore::BeforeChangeTitleComplete'] = function ( $store, $oldTitle, $newTitle, $pageid, $redirid ) use ( $interlanguageLinksLookup ) {
139150

140151
$interlanguageLinksLookup->setStore( $store );
141152

@@ -148,17 +159,19 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
148159
/**
149160
* https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
150161
*/
151-
$wgHooks['NewRevisionFromEditComplete'][] = function ( $wikiPage ) use ( $interlanguageLinksLookup ) {
162+
$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage ) use ( $interlanguageLinksLookup ) {
152163

153-
$interlanguageLinksLookup->invalidateLookupCache( $wikiPage->getTitle() );
164+
$interlanguageLinksLookup->invalidateLookupCache(
165+
$wikiPage->getTitle()
166+
);
154167

155168
return true;
156169
};
157170

158171
/**
159172
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateGetLanguageLink
160173
*/
161-
$wgHooks['SkinTemplateGetLanguageLink'][] = function ( &$languageLink, $languageLinkTitle, $title ) {
174+
$this->handlers['SkinTemplateGetLanguageLink'] = function ( &$languageLink, $languageLinkTitle, $title ) {
162175

163176
$siteLanguageLinkModifier = new SiteLanguageLinkModifier(
164177
$languageLinkTitle,
@@ -173,7 +186,7 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
173186
/**
174187
* @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentLanguage
175188
*/
176-
$wgHooks['PageContentLanguage'][] = function ( $title, &$pageLang ) use ( $interlanguageLinksLookup ) {
189+
$this->handlers['PageContentLanguage'] = function ( $title, &$pageLang ) use ( $interlanguageLinksLookup ) {
177190

178191
$pageContentLanguageModifier = new PageContentLanguageModifier(
179192
$interlanguageLinksLookup,
@@ -188,7 +201,7 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
188201
/**
189202
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
190203
*/
191-
$wgHooks['ParserAfterTidy'][] = function ( &$parser, &$text ) {
204+
$this->handlers['ParserAfterTidy'] = function ( &$parser, &$text ) {
192205

193206
$parserData = ApplicationFactory::getInstance()->newParserData(
194207
$parser->getTitle(),
@@ -211,7 +224,7 @@ private function registerInterlanguageParserHooks( InterlanguageLinksLookup $int
211224
};
212225
}
213226

214-
private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlanguageLinksLookup, &$wgHooks ) {
227+
private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
215228

216229
$searchResultModifier = new SearchResultModifier(
217230
new LanguageResultMatchFinder( $interlanguageLinksLookup )
@@ -220,7 +233,7 @@ private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlang
220233
/**
221234
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfiles
222235
*/
223-
$wgHooks['SpecialSearchProfiles'][] = function ( array &$profiles ) use ( $searchResultModifier ) {
236+
$this->handlers['SpecialSearchProfiles'] = function ( array &$profiles ) use ( $searchResultModifier ) {
224237

225238
$searchProfile = $searchResultModifier->addSearchProfile(
226239
$profiles
@@ -232,7 +245,7 @@ private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlang
232245
/**
233246
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfileForm
234247
*/
235-
$wgHooks['SpecialSearchProfileForm'][] = function ( $search, &$form, $profile, $term, $opts ) use ( $searchResultModifier ) {
248+
$this->handlers['SpecialSearchProfileForm'] = function ( $search, &$form, $profile, $term, $opts ) use ( $searchResultModifier ) {
236249

237250
$searchProfileForm = $searchResultModifier->addSearchProfileForm(
238251
$search,
@@ -247,7 +260,7 @@ private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlang
247260
/**
248261
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchPowerBox
249262
*/
250-
$wgHooks['SpecialSearchPowerBox'][] = function ( &$showSections, $term, $opts ) use ( $searchResultModifier ) {
263+
$this->handlers['SpecialSearchPowerBox'] = function ( &$showSections, $term, $opts ) use ( $searchResultModifier ) {
251264

252265
$searchResultModifier->addLanguageFilterToPowerBox(
253266
$GLOBALS['wgRequest'],
@@ -260,7 +273,7 @@ private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlang
260273
/**
261274
* @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchResults
262275
*/
263-
$wgHooks['SpecialSearchResults'][] = function ( $term, &$titleMatches, &$textMatches ) use ( $searchResultModifier ) {
276+
$this->handlers['SpecialSearchResults'] = function ( $term, &$titleMatches, &$textMatches ) use ( $searchResultModifier ) {
264277

265278
$resultMatches = $searchResultModifier->applyLanguageFilterToResultMatches(
266279
$GLOBALS['wgRequest'],

0 commit comments

Comments
 (0)