Completed
Push — master ( 0e54bc...7dc2d1 )
by
unknown
09:45
created

HookRegistry::addCallbackHandlers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 2

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 53
ccs 25
cts 25
cp 1
rs 9.5797
cc 2
eloc 24
nc 2
nop 3
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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