Completed
Push — master ( 6f7547...dda6b2 )
by mw
02:42
created

HookRegistry   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 84.38%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 160
ccs 54
cts 64
cp 0.8438
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A register() 0 5 2
A deregister() 0 12 3
A isRegistered() 0 3 1
A getHandlers() 0 3 1
B onBeforeConfigCompletion() 0 33 4
A registerCallbackHandlers() 0 56 1
1
<?php
2
3
namespace SESP;
4
5
use Hooks;
6
7
/**
8
 * @license GNU GPL v2+
9
 * @since 1.3
10
 *
11
 * @author mwjames
12
 */
13
class HookRegistry {
14
15
	/**
16
	 * @var array
17
	 */
18
	private $handlers = [];
19
20
	/**
21
	 * @since 1.0
22
	 *
23
	 * @param array $configuration
24
	 */
25 2
	public function __construct( $configuration ) {
26 2
		$this->registerCallbackHandlers( $configuration );
27 2
	}
28
29
	/**
30
	 * @since  1.0
31
	 */
32 1
	public function register() {
33 1
		foreach ( $this->handlers as $name => $callback ) {
34 1
			Hooks::register( $name, $callback );
35 1
		}
36 1
	}
37
38
	/**
39
	 * @since  1.0
40
	 */
41 1
	public function deregister() {
42 1
		foreach ( array_keys( $this->handlers ) as $name ) {
43
44 1
			Hooks::clear( $name );
45
46
			// Remove registered `wgHooks` hooks that are not cleared by the
47
			// previous call
48 1
			if ( isset( $GLOBALS['wgHooks'][$name] ) ) {
49
				unset( $GLOBALS['wgHooks'][$name] );
50
			}
51 1
		}
52 1
	}
53
54
	/**
55
	 * @since  1.0
56
	 *
57
	 * @param string $name
58
	 *
59
	 * @return boolean
60
	 */
61 1
	public function isRegistered( $name ) {
62 1
		return Hooks::isRegistered( $name );
63
	}
64
65
	/**
66
	 * @since  1.0
67
	 *
68
	 * @param string $name
69
	 *
70
	 * @return array
71
	 */
72 1
	public function getHandlers( $name ) {
73 1
		return Hooks::getHandlers( $name );
74
	}
75
76
	/**
77
	 * @since  1.4
78
	 *
79
	 * @param array &$config
80
	 */
81 1
	public static function onBeforeConfigCompletion( &$config ) {
82
83
		$exemptionlist = [
84 1
			'___EUSER', '___CUSER', '___SUBP', '___REVID', '___VIEWS',
85 1
			'___NREV', '___NTREV', '___USEREDITCNT', '___EXIFDATA'
86 1
		];
87
88
		// Exclude listed properties from indexing
89 1
		if ( isset( $config['smwgFulltextSearchPropertyExemptionList'] ) ) {
90 1
			$config['smwgFulltextSearchPropertyExemptionList'] = array_merge(
91 1
				$config['smwgFulltextSearchPropertyExemptionList'],
92
				$exemptionlist
93 1
			);
94 1
		}
95
96
		// Exclude listed properties from dependency detection as each of the
97
		// selected object would trigger an automatic change without the necessary
98
		// human intervention and can therefore produce unwanted query updates
99 1
		if ( isset( $config['smwgQueryDependencyPropertyExemptionlist'] ) ) {
100
			$config['smwgQueryDependencyPropertyExemptionlist'] = array_merge(
101
				$config['smwgQueryDependencyPropertyExemptionlist'],
102
				$exemptionlist
103
			);
104
		}
105
106
		// #93
107 1
		if ( isset( $config['smwgQueryDependencyPropertyExemptionList'] ) ) {
108
			$config['smwgQueryDependencyPropertyExemptionList'] = array_merge(
109
				$config['smwgQueryDependencyPropertyExemptionList'],
110
				$exemptionlist
111
			);
112
		}
113 1
	}
114
115 2
	private function registerCallbackHandlers( $configuration ) {
116
117 2
		$applicationFactory = \SMW\ApplicationFactory::getInstance();
118
119 2
		$appFactory = new AppFactory(
120 2
			$configuration,
121 2
			$applicationFactory->getCache()
122 2
		);
123
124 2
		$appFactory->setLogger(
125 2
			$applicationFactory->getMediaWikiLogger( 'sesp' )
0 ignored issues
show
Unused Code introduced by
The call to ApplicationFactory::getMediaWikiLogger() has too many arguments starting with 'sesp'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
126 2
		);
127
128 2
		$propertyRegistry = new PropertyRegistry(
129
			$appFactory
130 2
		);
131
132
		/**
133
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties
134
		 */
135
		$this->handlers['SMW::Property::initProperties'] = function ( $basePropertyRegistry ) use ( $propertyRegistry ) {
136
137 1
			$propertyRegistry->registerOn(
138
				$basePropertyRegistry
139 1
			);
140
141 1
			return true;
142
		};
143
144
		/**
145
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables
146
		 */
147
		$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties, &$fixedPropertyTablePrefix ) use( $propertyRegistry ) {
148
149 1
			$propertyRegistry->registerAsFixedProperties(
150 1
				$customFixedProperties,
151
				$fixedPropertyTablePrefix
152 1
			);
153
154 1
			return true;
155
		};
156
157
		/**
158
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMWStore::updateDataBefore
159
		 */
160 1
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $appFactory ) {
161
162 1
			$extraPropertyAnnotator = new ExtraPropertyAnnotator(
163
				$appFactory
164 1
			);
165
166 1
			$extraPropertyAnnotator->addAnnotation( $semanticData );
167
168 1
			return true;
169
		};
170 2
	}
171
172
}
173