Completed
Pull Request — master (#41)
by mw
02:26
created

HookRegistry   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 309
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 100%

Importance

Changes 20
Bugs 1 Features 11
Metric Value
wmc 11
c 20
b 1
f 11
lcom 1
cbo 14
dl 0
loc 309
ccs 109
cts 109
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isRegistered() 0 3 1
A getHandlerFor() 0 3 2
A __construct() 0 3 1
A register() 0 5 2
A addCallbackHandlers() 0 53 2
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 9
	private function addCallbackHandlers( $store, $cache, $cacheKeyProvider ) {
70
71 2
		$languageTargetLinksCache = new LanguageTargetLinksCache(
72 2
			$cache,
73
			$cacheKeyProvider
74 2
		);
75
76 2
		$interlanguageLinksLookup = new InterlanguageLinksLookup(
77
			$languageTargetLinksCache
78 2
		);
79
80 2
		$interlanguageLinksLookup->setStore( $store );
81
82
		$applicationFactory = ApplicationFactory::getInstance();
83
84
		// SMW 2.5+
85
		// Avoid indexing fields that do not require an extra fulltext index
86
		if ( $applicationFactory->getSettings()->has( 'smwgFulltextSearchPropertyExemptionList' ) ) {
87 9
			$applicationFactory->getSettings()->add(
0 ignored issues
show
Bug introduced by
The method add() does not seem to exist on object<SMW\Settings>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
				'smwgFulltextSearchPropertyExemptionList',
89 9
				array( PropertyRegistry::SIL_IWL_LANG, PropertyRegistry::SIL_ILL_LANG )
90
			);
91 9
		}
92
93 9
		/**
94
		 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
95
		 */
96
		$this->handlers['SMW::Property::initProperties'] = function ( $baseRegistry ) {
97
98
			$propertyRegistry = new PropertyRegistry();
99
100
			$propertyRegistry->register(
101 3
				$baseRegistry
102 3
			);
103 3
104
			return true;
105 3
		};
106
107
		/**
108 2
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleFromTitle
109 2
		 */
110 2
		$this->handlers['ArticleFromTitle'] = function ( $title, &$page ) use( $interlanguageLinksLookup ) {
111
112 9
			$byLanguageCategoryPage = new ByLanguageCategoryPage( $title );
113
			$byLanguageCategoryPage->setCategoryFilterByLanguageState( $GLOBALS['egSILEnabledCategoryFilterByLanguage'] );
114 2
			$byLanguageCategoryPage->modifyCategoryView( $page, $interlanguageLinksLookup );
115 2
116 2
			return true;
117 2
		};
118
119
		$this->registerInterlanguageParserHooks( $interlanguageLinksLookup );
120
		$this->registerSpecialSearchHooks( $interlanguageLinksLookup );
121
	}
122
123
	private function registerInterlanguageParserHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
124 1
125
		$pageContentLanguageOnTheFlyModifier = new PageContentLanguageOnTheFlyModifier(
126 1
			$interlanguageLinksLookup,
127 1
			InMemoryPoolCache::getInstance()->getPoolCacheFor( PageContentLanguageOnTheFlyModifier::POOLCACHE_ID )
128
		);
129 1
130
		/**
131 1
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
132
		 */
133 1
		$this->handlers['ParserFirstCallInit'] = function ( &$parser ) use( $interlanguageLinksLookup, $pageContentLanguageOnTheFlyModifier ) {
134
135 1
			$parserFunctionFactory = new ParserFunctionFactory();
136
137 1
			list( $name, $definition, $flag ) = $parserFunctionFactory->newInterlanguageLinkParserFunctionDefinition(
138
				$interlanguageLinksLookup,
139 1
				$pageContentLanguageOnTheFlyModifier
140
			);
141
142
			$parser->setFunctionHook( $name, $definition, $flag );
143
144
			list( $name, $definition, $flag ) = $parserFunctionFactory->newInterlanguageListParserFunctionDefinition(
145
				$interlanguageLinksLookup
146
			);
147 9
148 9
			$parser->setFunctionHook( $name, $definition, $flag );
149
150 9
			return true;
151
		};
152
153
		/**
154
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDelete
155
		 */
156
		$this->handlers['SMW::SQLStore::BeforeDeleteSubjectComplete'] = function ( $store, $title ) use ( $interlanguageLinksLookup ) {
157
158 2
			$interlanguageLinksLookup->setStore( $store );
159
			$interlanguageLinksLookup->invalidateLookupCache( $title );
160 2
161 2
			return true;
162
		};
163 2
164
		/**
165
		 * https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
166
		 */
167
		$this->handlers['SMW::SQLStore::BeforeChangeTitleComplete'] = function ( $store, $oldTitle, $newTitle, $pageid, $redirid ) use ( $interlanguageLinksLookup ) {
168
169
			$interlanguageLinksLookup->setStore( $store );
170
171 1
			$interlanguageLinksLookup->invalidateLookupCache( $oldTitle );
172 1
			$interlanguageLinksLookup->invalidateLookupCache( $newTitle );
173 1
174
			return true;
175 1
		};
176
177
		/**
178
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePurge
179
		 */
180
		$this->handlers['ArticlePurge']= function ( &$wikiPage ) use ( $interlanguageLinksLookup ) {
181
182
			$interlanguageLinksLookup->invalidateLookupCache(
183 9
				$wikiPage->getTitle()
184 9
			);
185 9
186
			return true;
187 9
		};
188
189
		/**
190
		 * https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
191
		 */
192
		$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage ) use ( $interlanguageLinksLookup ) {
193
194
			$interlanguageLinksLookup->invalidateLookupCache(
195 1
				$wikiPage->getTitle()
196 1
			);
197
198 1
			return true;
199
		};
200 1
201
		/**
202 1
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateGetLanguageLink
203
		 */
204
		$this->handlers['SkinTemplateGetLanguageLink'] = function ( &$languageLink, $languageLinkTitle, $title ) {
205
206
			$siteLanguageLinkModifier = new SiteLanguageLinkModifier(
207
				$languageLinkTitle,
208
				$title
209
			);
210 9
211 9
			$siteLanguageLinkModifier->modifyLanguageLink( $languageLink );
212
213 9
			return true;
214
		};
215 9
216
		/**
217
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentLanguage
218
		 */
219
		$this->handlers['PageContentLanguage'] = function ( $title, &$pageLang ) use ( $pageContentLanguageOnTheFlyModifier ) {
220
221
			$pageLang = $pageContentLanguageOnTheFlyModifier->getPageContentLanguage(
222
				$title,
223 9
				$pageLang
224 9
			);
225 9
226 9
			return true;
227
		};
228 9
229
		/**
230 9
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
231
		 */
232 9
		$this->handlers['ParserAfterTidy'] = function ( &$parser, &$text ) {
233
234 9
			$parserData = ApplicationFactory::getInstance()->newParserData(
235
				$parser->getTitle(),
236 9
				$parser->getOutput()
237 9
			);
238 9
239
			$languageLinkAnnotator = new LanguageLinkAnnotator(
240 9
				$parserData
241
			);
242 2
243
			$interwikiLanguageLinkFetcher = new InterwikiLanguageLinkFetcher(
244 2
				$languageLinkAnnotator
245
			);
246 2
247 2
			$interwikiLanguageLinkFetcher->fetchLanguagelinksFromParserOutput(
248 2
				$parser->getOutput()
249
			);
250
251
			return true;
252
		};
253
	}
254
255 1
	private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
256
257 1
		$searchResultModifier = new SearchResultModifier(
258
			new LanguageResultMatchFinder( $interlanguageLinksLookup )
259 1
		);
260
261
		/**
262
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfiles
263
		 */
264
		$this->handlers['SpecialSearchProfiles'] = function ( array &$profiles ) use ( $searchResultModifier ) {
265
266
			$searchResultModifier->addSearchProfile(
267 1
				$profiles
268 1
			);
269 1
270 1
			return true;
271
		};
272 1
273
		/**
274 1
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfileForm
275
		 */
276
		$this->handlers['SpecialSearchProfileForm'] = function ( $search, &$form, $profile, $term, $opts ) use ( $searchResultModifier ) {
277
278
			$searchResultModifier->addSearchProfileForm(
279
				$search,
280
				$profile,
281
				$form,
282 1
				$opts
283 1
			);
284
285 1
			return true;
286
		};
287 1
288
		/**
289
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchPowerBox
290
		 */
291
		$this->handlers['SpecialSearchPowerBox'] = function ( &$showSections, $term, $opts ) use ( $searchResultModifier ) {
292
293
			$searchResultModifier->addLanguageFilterToPowerBox(
294
				$GLOBALS['wgRequest'],
295 1
				$showSections
296 1
			);
297 1
298
			return true;
299 1
		};
300
301
		/**
302
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchSetupEngine
303
		 */
304
		$this->handlers['SpecialSearchSetupEngine'] = function ( \SpecialSearch $search, $profile, \SearchEngine $searchEngine ) use ( $searchResultModifier ) {
305 1
306
			if ( $search->getContext()->getRequest()->getVal( 'nsflag') ) {
307 1
				$searchEngine->setNamespaces( \SearchEngine::defaultNamespaces() );
308 1
			}
309 1
310
			return true;
311 1
		};
312
313 1
		/**
314
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchResults
315 2
		 */
316
		$this->handlers['SpecialSearchResults'] = function ( $term, &$titleMatches, &$textMatches ) use ( $searchResultModifier ) {
317
318
			$searchResultModifier->applyLanguageFilterToResultMatches(
319
				$GLOBALS['wgRequest'],
320
				$titleMatches,
321
				$textMatches
322
			);
323
324
			return true;
325
		};
326
	}
327
328
}
329