Completed
Push — master ( deaef9...81d09c )
by mw
06:04
created

HookRegistry   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 316
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 99.15%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 13
dl 0
loc 316
rs 10
c 0
b 0
f 0
ccs 116
cts 117
cp 0.9915

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandlerFor() 0 3 2
A __construct() 0 3 1
A isRegistered() 0 3 1
A register() 0 5 2
A onBeforeConfigCompletion() 0 12 2
B addCallbackHandlers() 0 42 1
B registerInterlanguageParserHooks() 0 131 1
A registerSpecialSearchHooks() 0 72 2
1
<?php
2
3
namespace SIL;
4
5
use Onoi\Cache\Cache;
6
use SMW\Store;
7
use SMW\ApplicationFactory;
8
use SMW\InMemoryPoolCache;
9
use SIL\Search\SearchResultModifier;
10
use SIL\Search\LanguageResultMatchFinder;
11
use SIL\Category\ByLanguageCategoryPage;
12
use Hooks;
13
14
/**
15
 * @license GNU GPL v2+
16
 * @since 1.0
17
 *
18
 * @author mwjames
19
 */
20
class HookRegistry {
21
22
	/**
23
	 * @var array
24
	 */
25
	private $handlers = array();
26
27
	/**
28
	 * @since 1.0
29
	 *
30
	 * @param Store $store
31
	 * @param Cache $cache
32
	 * @param CacheKeyProvider $cacheKeyProvider
33
	 */
34 2
	public function __construct( Store $store, Cache $cache, CacheKeyProvider $cacheKeyProvider ) {
35 2
		$this->addCallbackHandlers( $store, $cache, $cacheKeyProvider );
36 2
	}
37
38
	/**
39
	 * @since  1.1
40
	 *
41
	 * @param string $name
42
	 *
43
	 * @return boolean
44
	 */
45 1
	public function isRegistered( $name ) {
46 1
		return Hooks::isRegistered( $name );
47
	}
48
49
	/**
50
	 * @since  1.1
51
	 *
52
	 * @param string $name
53
	 *
54
	 * @return Callable|false
55
	 */
56 1
	public function getHandlerFor( $name ) {
57 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
58
	}
59
60
	/**
61
	 * @since  1.0
62
	 */
63 1
	public function register() {
64 1
		foreach ( $this->handlers as $name => $callback ) {
65 1
			Hooks::register( $name, $callback );
66 1
		}
67 1
	}
68
69
	/**
70
	 * @since  1.0
71
	 *
72
	 * @param array &$configuration
73
	 */
74 9
	public static function onBeforeConfigCompletion( &$config ) {
75
76 9
		if ( !isset( $config['smwgFulltextSearchPropertyExemptionList'] ) ) {
77
			return;
78
		}
79
80
		// Exclude those properties from indexing
81 9
		$config['smwgFulltextSearchPropertyExemptionList'] = array_merge(
82 9
			$config['smwgFulltextSearchPropertyExemptionList'],
83 9
			array( PropertyRegistry::SIL_IWL_LANG, PropertyRegistry::SIL_ILL_LANG )
84 9
		);
85 9
	}
86
87 9
	private function addCallbackHandlers( $store, $cache, $cacheKeyProvider ) {
88
89 2
		$languageTargetLinksCache = new LanguageTargetLinksCache(
90 2
			$cache,
91
			$cacheKeyProvider
92 2
		);
93
94 2
		$interlanguageLinksLookup = new InterlanguageLinksLookup(
95
			$languageTargetLinksCache
96 2
		);
97
98 2
		$interlanguageLinksLookup->setStore( $store );
99
100
		/**
101
		 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
102
		 */
103
		$this->handlers['SMW::Property::initProperties'] = function ( $baseRegistry ) {
104
105 9
			$propertyRegistry = new PropertyRegistry();
106
107 9
			$propertyRegistry->register(
108
				$baseRegistry
109 9
			);
110
111 9
			return true;
112
		};
113
114
		/**
115
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleFromTitle
116
		 */
117
		$this->handlers['ArticleFromTitle'] = function ( $title, &$page ) use( $interlanguageLinksLookup ) {
118
119 3
			$byLanguageCategoryPage = new ByLanguageCategoryPage( $title );
120 3
			$byLanguageCategoryPage->setCategoryFilterByLanguageState( $GLOBALS['egSILEnabledCategoryFilterByLanguage'] );
121 3
			$byLanguageCategoryPage->modifyCategoryView( $page, $interlanguageLinksLookup );
122
123 3
			return true;
124
		};
125
126 2
		$this->registerInterlanguageParserHooks( $interlanguageLinksLookup );
127 2
		$this->registerSpecialSearchHooks( $interlanguageLinksLookup );
128 2
	}
129
130 9
	private function registerInterlanguageParserHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
131
132 2
		$pageContentLanguageOnTheFlyModifier = new PageContentLanguageOnTheFlyModifier(
133 2
			$interlanguageLinksLookup,
134 2
			InMemoryPoolCache::getInstance()->getPoolCacheFor( PageContentLanguageOnTheFlyModifier::POOLCACHE_ID )
135 2
		);
136
137
		/**
138
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
139
		 */
140
		$this->handlers['ParserFirstCallInit'] = function ( &$parser ) use( $interlanguageLinksLookup, $pageContentLanguageOnTheFlyModifier ) {
141
142 1
			$parserFunctionFactory = new ParserFunctionFactory();
143
144 1
			list( $name, $definition, $flag ) = $parserFunctionFactory->newInterlanguageLinkParserFunctionDefinition(
145 1
				$interlanguageLinksLookup,
146
				$pageContentLanguageOnTheFlyModifier
147 1
			);
148
149 1
			$parser->setFunctionHook( $name, $definition, $flag );
150
151 1
			list( $name, $definition, $flag ) = $parserFunctionFactory->newInterlanguageListParserFunctionDefinition(
152
				$interlanguageLinksLookup
153 1
			);
154
155 1
			$parser->setFunctionHook( $name, $definition, $flag );
156
157 1
			return true;
158
		};
159
160
		/**
161
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDelete
162
		 */
163
		$this->handlers['SMW::SQLStore::BeforeDeleteSubjectComplete'] = function ( $store, $title ) use ( $interlanguageLinksLookup ) {
164
165 9
			$interlanguageLinksLookup->setStore( $store );
166 9
			$interlanguageLinksLookup->resetLookupCacheBy( $title );
167
168 9
			return true;
169
		};
170
171
		/**
172
		 * https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
173
		 */
174
		$this->handlers['SMW::SQLStore::BeforeChangeTitleComplete'] = function ( $store, $oldTitle, $newTitle, $pageid, $redirid ) use ( $interlanguageLinksLookup ) {
175
176 2
			$interlanguageLinksLookup->setStore( $store );
177
178 2
			$interlanguageLinksLookup->resetLookupCacheBy( $oldTitle );
179 2
			$interlanguageLinksLookup->resetLookupCacheBy( $newTitle );
180
181 2
			return true;
182
		};
183
184
		/**
185
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePurge
186
		 */
187
		$this->handlers['ArticlePurge']= function ( &$wikiPage ) use ( $interlanguageLinksLookup ) {
188
189 1
			$interlanguageLinksLookup->resetLookupCacheBy(
190 1
				$wikiPage->getTitle()
191 1
			);
192
193 1
			return true;
194
		};
195
196
		/**
197
		 * https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
198
		 */
199
		$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage ) use ( $interlanguageLinksLookup ) {
200
201 9
			$interlanguageLinksLookup->resetLookupCacheBy(
202 9
				$wikiPage->getTitle()
203 9
			);
204
205 9
			return true;
206
		};
207
208
		/**
209
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateGetLanguageLink
210
		 */
211
		$this->handlers['SkinTemplateGetLanguageLink'] = function ( &$languageLink, $languageLinkTitle, $title ) {
212
213 1
			$siteLanguageLinkModifier = new SiteLanguageLinkModifier(
214 1
				$languageLinkTitle,
215
				$title
216 1
			);
217
218 1
			$siteLanguageLinkModifier->modifyLanguageLink( $languageLink );
219
220 1
			return true;
221
		};
222
223
		/**
224
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentLanguage
225
		 */
226
		$this->handlers['PageContentLanguage'] = function ( $title, &$pageLang ) use ( $pageContentLanguageOnTheFlyModifier ) {
227
228 9
			$pageLang = $pageContentLanguageOnTheFlyModifier->getPageContentLanguage(
229 9
				$title,
230
				$pageLang
231 9
			);
232
233 9
			return true;
234
		};
235
236
		/**
237
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
238
		 */
239
		$this->handlers['ParserAfterTidy'] = function ( &$parser, &$text ) {
240
241 9
			$parserData = ApplicationFactory::getInstance()->newParserData(
242 9
				$parser->getTitle(),
243 9
				$parser->getOutput()
244 9
			);
245
246 9
			$languageLinkAnnotator = new LanguageLinkAnnotator(
247
				$parserData
248 9
			);
249
250 9
			$interwikiLanguageLinkFetcher = new InterwikiLanguageLinkFetcher(
251
				$languageLinkAnnotator
252 9
			);
253
254 9
			$interwikiLanguageLinkFetcher->fetchLanguagelinksFromParserOutput(
255 9
				$parser->getOutput()
256 9
			);
257
258 9
			return true;
259
		};
260 2
	}
261
262 2
	private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
263
264 2
		$searchResultModifier = new SearchResultModifier(
265 2
			new LanguageResultMatchFinder( $interlanguageLinksLookup )
266 2
		);
267
268
		/**
269
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfiles
270
		 */
271
		$this->handlers['SpecialSearchProfiles'] = function ( array &$profiles ) use ( $searchResultModifier ) {
272
273 1
			$searchResultModifier->addSearchProfile(
274
				$profiles
275 1
			);
276
277 1
			return true;
278
		};
279
280
		/**
281
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfileForm
282
		 */
283
		$this->handlers['SpecialSearchProfileForm'] = function ( $search, &$form, $profile, $term, $opts ) use ( $searchResultModifier ) {
284
285 1
			$searchResultModifier->addSearchProfileForm(
286 1
				$search,
287 1
				$profile,
288 1
				$form,
289
				$opts
290 1
			);
291
292 1
			return true;
293
		};
294
295
		/**
296
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchPowerBox
297
		 */
298
		$this->handlers['SpecialSearchPowerBox'] = function ( &$showSections, $term, $opts ) use ( $searchResultModifier ) {
299
300 1
			$searchResultModifier->addLanguageFilterToPowerBox(
301 1
				$GLOBALS['wgRequest'],
302
				$showSections
303 1
			);
304
305 1
			return true;
306
		};
307
308
		/**
309
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchSetupEngine
310
		 */
311
		$this->handlers['SpecialSearchSetupEngine'] = function ( \SpecialSearch $search, $profile, \SearchEngine $searchEngine ) use ( $searchResultModifier ) {
312
313 1
			if ( $search->getContext()->getRequest()->getVal( 'nsflag') ) {
314 1
				$searchEngine->setNamespaces( \SearchEngine::defaultNamespaces() );
315 1
			}
316
317 1
			return true;
318
		};
319
320
		/**
321
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchResults
322
		 */
323 1
		$this->handlers['SpecialSearchResults'] = function ( $term, &$titleMatches, &$textMatches ) use ( $searchResultModifier ) {
324
325 1
			$searchResultModifier->applyLanguageFilterToResultMatches(
326 1
				$GLOBALS['wgRequest'],
327 1
				$titleMatches,
328
				$textMatches
329 1
			);
330
331 1
			return true;
332
		};
333 2
	}
334
335
}
336