Completed
Push — master ( 9e1461...cdd4b1 )
by
unknown
13:17
created

HookRegistry::addCallbackHandlers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 18
cts 18
cp 1
rs 9.248
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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\LanguageFilterCategoryPage;
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
			$languageFilterCategoryPage = new LanguageFilterCategoryPage( $title );
120 3
			$languageFilterCategoryPage->isCategoryFilterByLanguage( $GLOBALS['silgEnabledCategoryFilterByLanguage'] );
121 3
			$languageFilterCategoryPage->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 )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\InMemoryPoolCache::getPoolCacheFor() has been deprecated with message: since 2.5, use InMemoryPoolCache::getPoolCacheById

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
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
			list( $name, $definition, $flag ) = $parserFunctionFactory->newAnnotatedLanguageParserFunctionDefinition(
158
				$interlanguageLinksLookup
159 1
			);
160
161 1
			$parser->setFunctionHook( $name, $definition, $flag );
162
163 1
			return true;
164
		};
165
166
		/**
167
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDelete
168
		 */
169
		$this->handlers['SMW::SQLStore::BeforeDeleteSubjectComplete'] = function ( $store, $title ) use ( $interlanguageLinksLookup ) {
170
171 9
			$interlanguageLinksLookup->setStore( $store );
172 9
			$interlanguageLinksLookup->resetLookupCacheBy( $title );
173
174 9
			return true;
175
		};
176
177
		/**
178
		 * https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
179
		 */
180
		$this->handlers['SMW::SQLStore::BeforeChangeTitleComplete'] = function ( $store, $oldTitle, $newTitle, $pageid, $redirid ) use ( $interlanguageLinksLookup ) {
181
182 2
			$interlanguageLinksLookup->setStore( $store );
183
184 2
			$interlanguageLinksLookup->resetLookupCacheBy( $oldTitle );
185 2
			$interlanguageLinksLookup->resetLookupCacheBy( $newTitle );
186
187 2
			return true;
188
		};
189
190
		/**
191
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePurge
192
		 */
193
		$this->handlers['ArticlePurge']= function ( &$wikiPage ) use ( $interlanguageLinksLookup ) {
194
195 1
			$interlanguageLinksLookup->resetLookupCacheBy(
196 1
				$wikiPage->getTitle()
197 1
			);
198
199 1
			return true;
200
		};
201
202
		/**
203
		 * https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
204
		 */
205
		$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage ) use ( $interlanguageLinksLookup ) {
206
207 9
			$interlanguageLinksLookup->resetLookupCacheBy(
208 9
				$wikiPage->getTitle()
209 9
			);
210
211 9
			return true;
212
		};
213
214
		/**
215
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateGetLanguageLink
216
		 */
217
		$this->handlers['SkinTemplateGetLanguageLink'] = function ( &$languageLink, $languageLinkTitle, $title ) {
218
219 1
			$siteLanguageLinkModifier = new SiteLanguageLinkModifier(
220 1
				$languageLinkTitle,
221
				$title
222 1
			);
223
224 1
			$siteLanguageLinkModifier->modifyLanguageLink( $languageLink );
225
226 1
			return true;
227
		};
228
229
		/**
230
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentLanguage
231
		 */
232
		$this->handlers['PageContentLanguage'] = function ( $title, &$pageLang ) use ( $pageContentLanguageOnTheFlyModifier ) {
233
234 9
			$pageLang = $pageContentLanguageOnTheFlyModifier->getPageContentLanguage(
235 9
				$title,
236
				$pageLang
237 9
			);
238
239 9
			return true;
240
		};
241
242
		/**
243
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
244
		 */
245
		$this->handlers['ParserAfterTidy'] = function ( &$parser, &$text ) {
246
247 9
			$parserData = ApplicationFactory::getInstance()->newParserData(
248 9
				$parser->getTitle(),
249 9
				$parser->getOutput()
250 9
			);
251
252 9
			$languageLinkAnnotator = new LanguageLinkAnnotator(
253
				$parserData
254 9
			);
255
256 9
			$interwikiLanguageLinkFetcher = new InterwikiLanguageLinkFetcher(
257
				$languageLinkAnnotator
258 9
			);
259
260 9
			$interwikiLanguageLinkFetcher->fetchLanguagelinksFromParserOutput(
261 9
				$parser->getOutput()
262 9
			);
263
264 9
			return true;
265
		};
266 2
	}
267
268 2
	private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
269
270 2
		$searchResultModifier = new SearchResultModifier(
271 2
			new LanguageResultMatchFinder( $interlanguageLinksLookup )
272 2
		);
273
274
		/**
275
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfiles
276
		 */
277
		$this->handlers['SpecialSearchProfiles'] = function ( array &$profiles ) use ( $searchResultModifier ) {
278
279 1
			$searchResultModifier->addSearchProfile(
280
				$profiles
281 1
			);
282
283 1
			return true;
284
		};
285
286
		/**
287
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfileForm
288
		 */
289
		$this->handlers['SpecialSearchProfileForm'] = function ( $search, &$form, $profile, $term, $opts ) use ( $searchResultModifier ) {
290
291 1
			$searchResultModifier->addSearchProfileForm(
292 1
				$search,
293 1
				$profile,
294 1
				$form,
295
				$opts
296 1
			);
297
298 1
			return true;
299
		};
300
301
		/**
302
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchPowerBox
303
		 */
304
		$this->handlers['SpecialSearchPowerBox'] = function ( &$showSections, $term, $opts ) use ( $searchResultModifier ) {
305
306 1
			$searchResultModifier->addLanguageFilterToPowerBox(
307 1
				$GLOBALS['wgRequest'],
308
				$showSections
309 1
			);
310
311 1
			return true;
312
		};
313
314
		/**
315
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchSetupEngine
316
		 */
317
		$this->handlers['SpecialSearchSetupEngine'] = function ( \SpecialSearch $search, $profile, \SearchEngine $searchEngine ) use ( $searchResultModifier ) {
318
319 1
			if ( $search->getContext()->getRequest()->getVal( 'nsflag') ) {
320 1
				$searchEngine->setNamespaces( \SearchEngine::defaultNamespaces() );
321 1
			}
322
323 1
			return true;
324
		};
325
326
		/**
327
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchResults
328
		 */
329 1
		$this->handlers['SpecialSearchResults'] = function ( $term, &$titleMatches, &$textMatches ) use ( $searchResultModifier ) {
330
331 1
			$searchResultModifier->applyLanguageFilterToResultMatches(
332 1
				$GLOBALS['wgRequest'],
333 1
				$titleMatches,
334
				$textMatches
335 1
			);
336
337 1
			return true;
338
		};
339 2
	}
340
341
}
342