Completed
Push — master ( 4a77d8...fe4b56 )
by mw
03:31
created

HookRegistry::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
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 SIL\Search\SearchResultModifier;
9
use SIL\Search\LanguageResultMatchFinder;
10
use SIL\Category\ByLanguageCategoryPage;
11
use Hooks;
12
13
/**
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class HookRegistry {
20
21
	/**
22
	 * @var array
23
	 */
24
	private $handlers = array();
25
26
	/**
27
	 * @since 1.0
28
	 *
29
	 * @param Store $store
30
	 * @param Cache $cache
31
	 * @param CacheKeyProvider $cacheKeyProvider
32
	 */
33 2
	public function __construct( Store $store, Cache $cache, CacheKeyProvider $cacheKeyProvider ) {
34 2
		$this->addCallbackHandlers( $store, $cache, $cacheKeyProvider );
35 2
	}
36
37
	/**
38
	 * @since  1.1
39
	 *
40
	 * @param string $name
41
	 *
42
	 * @return boolean
43
	 */
44 1
	public function isRegistered( $name ) {
45 1
		return Hooks::isRegistered( $name );
46
	}
47
48
	/**
49
	 * @since  1.1
50
	 *
51
	 * @param string $name
52
	 *
53
	 * @return Callable|false
54
	 */
55 1
	public function getHandlerFor( $name ) {
56 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
57
	}
58
59
	/**
60
	 * @since  1.0
61
	 */
62 1
	public function register() {
63 1
		foreach ( $this->handlers as $name => $callback ) {
64 1
			Hooks::register( $name, $callback );
65 1
		}
66 1
	}
67
68 9
	private function addCallbackHandlers( $store, $cache, $cacheKeyProvider ) {
69
70 2
		$languageTargetLinksCache = new LanguageTargetLinksCache(
71 2
			$cache,
72
			$cacheKeyProvider
73 2
		);
74
75 2
		$interlanguageLinksLookup = new InterlanguageLinksLookup(
76
			$languageTargetLinksCache
77 2
		);
78
79 2
		$interlanguageLinksLookup->setStore( $store );
80
81 2
		$propertyRegistry = new PropertyRegistry();
82
83
		/**
84
		 * @see https://github.com/SemanticMediaWiki/SemanticMediaWiki/blob/master/docs/technical/hooks.md
85
		 */
86
		$this->handlers['SMW::Property::initProperties'] = function () use ( $propertyRegistry ) {
87 9
			return $propertyRegistry->register();
88
		};
89
90
		/**
91
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticleFromTitle
92
		 */
93
		$this->handlers['ArticleFromTitle'] = function ( $title, &$page ) use( $interlanguageLinksLookup ) {
94
95 3
			$byLanguageCategoryPage = new ByLanguageCategoryPage( $title );
96 3
			$byLanguageCategoryPage->setCategoryFilterByLanguageState( $GLOBALS['egSILEnabledCategoryFilterByLanguage'] );
97 3
			$byLanguageCategoryPage->modifyCategoryView( $page, $interlanguageLinksLookup );
98
99 3
			return true;
100
		};
101
102 2
		$this->registerInterlanguageParserHooks( $interlanguageLinksLookup );
103 2
		$this->registerSpecialSearchHooks( $interlanguageLinksLookup );
104 2
	}
105
106 9
	private function registerInterlanguageParserHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
107
108
		/**
109
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
110
		 */
111
		$this->handlers['ParserFirstCallInit'] = function ( &$parser ) use( $interlanguageLinksLookup ) {
112
113 2
			$parserFunctionFactory = new ParserFunctionFactory();
114
115 2
			list( $name, $definition, $flag ) = $parserFunctionFactory->newInterlanguageLinkParserFunctionDefinition(
116
				$interlanguageLinksLookup
117 2
			);
118
119 2
			$parser->setFunctionHook( $name, $definition, $flag );
120
121 2
			list( $name, $definition, $flag ) = $parserFunctionFactory->newInterlanguageListParserFunctionDefinition(
122
				$interlanguageLinksLookup
123 2
			);
124
125 2
			$parser->setFunctionHook( $name, $definition, $flag );
126
127 2
			return true;
128
		};
129
130
		/**
131
		 * https://www.mediawiki.org/wiki/Manual:Hooks/ArticleDelete
132
		 */
133
		$this->handlers['SMW::SQLStore::BeforeDeleteSubjectComplete'] = function ( $store, $title ) use ( $interlanguageLinksLookup ) {
134
135 9
			$interlanguageLinksLookup->setStore( $store );
136 9
			$interlanguageLinksLookup->invalidateLookupCache( $title );
137
138 9
			return true;
139
		};
140
141
		/**
142
		 * https://www.mediawiki.org/wiki/Manual:Hooks/TitleMoveComplete
143
		 */
144
		$this->handlers['SMW::SQLStore::BeforeChangeTitleComplete'] = function ( $store, $oldTitle, $newTitle, $pageid, $redirid ) use ( $interlanguageLinksLookup ) {
145
146 2
			$interlanguageLinksLookup->setStore( $store );
147
148 2
			$interlanguageLinksLookup->invalidateLookupCache( $oldTitle );
149 2
			$interlanguageLinksLookup->invalidateLookupCache( $newTitle );
150
151 2
			return true;
152
		};
153
154
		/**
155
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ArticlePurge
156
		 */
157
		$this->handlers['ArticlePurge']= function ( &$wikiPage ) use ( $interlanguageLinksLookup ) {
158
159 1
			$interlanguageLinksLookup->invalidateLookupCache(
160 1
				$wikiPage->getTitle()
161 1
			);
162
163 1
			return true;
164
		};
165
166
		/**
167
		 * https://www.mediawiki.org/wiki/Manual:Hooks/NewRevisionFromEditComplete
168
		 */
169
		$this->handlers['NewRevisionFromEditComplete'] = function ( $wikiPage ) use ( $interlanguageLinksLookup ) {
170
171 9
			$interlanguageLinksLookup->invalidateLookupCache(
172 9
				$wikiPage->getTitle()
173 9
			);
174
175 9
			return true;
176
		};
177
178
		/**
179
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SkinTemplateGetLanguageLink
180
		 */
181
		$this->handlers['SkinTemplateGetLanguageLink'] = function ( &$languageLink, $languageLinkTitle, $title ) {
182
183 1
			$siteLanguageLinkModifier = new SiteLanguageLinkModifier(
184 1
				$languageLinkTitle,
185
				$title
186 1
			);
187
188 1
			$siteLanguageLinkModifier->modifyLanguageLink( $languageLink );
189
190 1
			return true;
191
		};
192
193
		/**
194
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/PageContentLanguage
195
		 */
196
		$this->handlers['PageContentLanguage'] = function ( $title, &$pageLang ) use ( $interlanguageLinksLookup ) {
197
198 9
			$pageContentLanguageModifier = new PageContentLanguageModifier(
199 9
				$interlanguageLinksLookup,
200
				$title
201 9
			);
202
203 9
			$pageContentLanguageModifier->modifyLanguage( $pageLang );
204
205 9
			return true;
206
		};
207
208
		/**
209
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
210
		 */
211
		$this->handlers['ParserAfterTidy'] = function ( &$parser, &$text ) {
212
213 9
			$parserData = ApplicationFactory::getInstance()->newParserData(
214 9
				$parser->getTitle(),
215 9
				$parser->getOutput()
216 9
			);
217
218 9
			$languageLinkAnnotator = new LanguageLinkAnnotator(
219
				$parserData
220 9
			);
221
222 9
			$interwikiLanguageLinkFetcher = new InterwikiLanguageLinkFetcher(
223
				$languageLinkAnnotator
224 9
			);
225
226 9
			$interwikiLanguageLinkFetcher->fetchLanguagelinksFromParserOutput(
227 9
				$parser->getOutput()
228 9
			);
229
230 9
			return true;
231
		};
232 2
	}
233
234 2
	private function registerSpecialSearchHooks( InterlanguageLinksLookup $interlanguageLinksLookup ) {
235
236 2
		$searchResultModifier = new SearchResultModifier(
237 2
			new LanguageResultMatchFinder( $interlanguageLinksLookup )
238 2
		);
239
240
		/**
241
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfiles
242
		 */
243
		$this->handlers['SpecialSearchProfiles'] = function ( array &$profiles ) use ( $searchResultModifier ) {
244
245 1
			$searchProfile = $searchResultModifier->addSearchProfile(
0 ignored issues
show
Unused Code introduced by
$searchProfile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
246
				$profiles
247 1
			);
248
249 1
			return true;
250
		};
251
252
		/**
253
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchProfileForm
254
		 */
255
		$this->handlers['SpecialSearchProfileForm'] = function ( $search, &$form, $profile, $term, $opts ) use ( $searchResultModifier ) {
256
257 1
			$searchProfileForm = $searchResultModifier->addSearchProfileForm(
0 ignored issues
show
Unused Code introduced by
$searchProfileForm is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
258 1
				$search,
259 1
				$profile,
260 1
				$form,
261
				$opts
262 1
			);
263
264 1
			return true;
265
		};
266
267
		/**
268
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchPowerBox
269
		 */
270
		$this->handlers['SpecialSearchPowerBox'] = function ( &$showSections, $term, $opts ) use ( $searchResultModifier ) {
271
272 1
			$searchResultModifier->addLanguageFilterToPowerBox(
273 1
				$GLOBALS['wgRequest'],
274
				$showSections
275 1
			);
276
277 1
			return true;
278
		};
279
280
		/**
281
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchSetupEngine
282
		 */
283
		$this->handlers['SpecialSearchSetupEngine'] = function ( \SpecialSearch $search, $profile, \SearchEngine $searchEngine ) use ( $searchResultModifier ) {
284
285 1
			if ( $search->getContext()->getRequest()->getVal( 'nsflag') ) {
286
				$searchEngine->setNamespaces( \SearchEngine::defaultNamespaces() );
287
			}
288
289 1
			return true;
290
		};
291
292
		/**
293
		 * @see https://www.mediawiki.org/wiki/Manual:Hooks/SpecialSearchResults
294
		 */
295 1
		$this->handlers['SpecialSearchResults'] = function ( $term, &$titleMatches, &$textMatches ) use ( $searchResultModifier ) {
296
297 1
			$resultMatches = $searchResultModifier->applyLanguageFilterToResultMatches(
0 ignored issues
show
Unused Code introduced by
$resultMatches is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
298 1
				$GLOBALS['wgRequest'],
299 1
				$titleMatches,
300
				$textMatches
301 1
			);
302
303 1
			return true;
304
		};
305 2
	}
306
307
}
308