Completed
Push — master ( 9d6a7e...b029df )
by mw
06:37
created

HookRegistry::initExtension()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 38
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.7691

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
ccs 14
cts 22
cp 0.6364
rs 8.5806
cc 4
eloc 18
nc 1
nop 1
crap 4.7691
1
<?php
2
3
namespace SESP;
4
5
use SMW\ApplicationFactory;
6
use Hooks;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 1.3
11
 *
12
 * @author mwjames
13
 */
14
class HookRegistry {
15
16
	/**
17
	 * @var array
18
	 */
19
	private $handlers = [];
20
21
	/**
22
	 * @since 1.0
23
	 *
24
	 * @param array $config
25 2
	 */
26 2
	public function __construct( $config ) {
27 2
		$this->registerCallbackHandlers( $config );
28
	}
29
30
	/**
31
	 * @since  1.0
32 1
	 */
33 1
	public function register() {
34 1
		foreach ( $this->handlers as $name => $callback ) {
35 1
			Hooks::register( $name, $callback );
36 1
		}
37
	}
38
39
	/**
40
	 * @since  1.0
41 1
	 */
42 1
	public function deregister() {
43
		foreach ( array_keys( $this->handlers ) as $name ) {
44 1
45
			Hooks::clear( $name );
46
47
			// Remove registered `wgHooks` hooks that are not cleared by the
48 1
			// previous call
49
			if ( isset( $GLOBALS['wgHooks'][$name] ) ) {
50
				unset( $GLOBALS['wgHooks'][$name] );
51 1
			}
52 1
		}
53
	}
54
55
	/**
56
	 * @since  1.0
57
	 *
58
	 * @param string $name
59
	 *
60
	 * @return boolean
61 1
	 */
62 1
	public function isRegistered( $name ) {
63
		return Hooks::isRegistered( $name );
64
	}
65
66
	/**
67
	 * @since  1.0
68
	 *
69
	 * @param string $name
70
	 *
71
	 * @return array
72 1
	 */
73 1
	public function getHandlers( $name ) {
74
		return Hooks::getHandlers( $name );
75
	}
76
77
	/**
78
	 * @since 2.0
79
	 *
80
	 * @param array &$vars
81 1
	 */
82
	public static function initExtension( &$vars ) {
83
84 1
		$vars['wgHooks']['SMW::Config::BeforeCompletion'][] = function( &$config ) {
85 1
86 1
			$exemptionlist = [
87
				'___EUSER', '___CUSER', '___SUBP', '___REVID', '___VIEWS',
88
				'___NREV', '___NTREV', '___USEREDITCNT', '___EXIFDATA'
89 1
			];
90 1
91 1
			// Exclude listed properties from indexing
92
			if ( isset( $config['smwgFulltextSearchPropertyExemptionList'] ) ) {
93 1
				$config['smwgFulltextSearchPropertyExemptionList'] = array_merge(
94 1
					$config['smwgFulltextSearchPropertyExemptionList'],
95
					$exemptionlist
96
				);
97
			}
98
99 1
			// Exclude listed properties from dependency detection as each of the
100
			// selected object would trigger an automatic change without the necessary
101
			// human intervention and can therefore produce unwanted query updates
102
			if ( isset( $config['smwgQueryDependencyPropertyExemptionlist'] ) ) {
103
				$config['smwgQueryDependencyPropertyExemptionlist'] = array_merge(
104
					$config['smwgQueryDependencyPropertyExemptionlist'],
105
					$exemptionlist
106
				);
107 1
			}
108
109
			// #93
110
			if ( isset( $config['smwgQueryDependencyPropertyExemptionList'] ) ) {
111
				$config['smwgQueryDependencyPropertyExemptionList'] = array_merge(
112
					$config['smwgQueryDependencyPropertyExemptionList'],
113 1
					$exemptionlist
114
				);
115 2
			}
116
117 2
			return true;
118
		};
119 2
	}
120 2
121 2
	private function registerCallbackHandlers( $config ) {
122 2
123
		$applicationFactory = ApplicationFactory::getInstance();
124 2
125 2
		$appFactory = new AppFactory(
126 2
			$config,
127
			$applicationFactory->getCache()
128 2
		);
129
130 2
		$appFactory->setLogger(
131
			$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...
132
		);
133
134
		$propertyRegistry = new PropertyRegistry(
135
			$appFactory
136
		);
137 1
138
		/**
139 1
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties
140
		 */
141 1
		$this->handlers['SMW::Property::initProperties'] = function ( $registry ) use ( $propertyRegistry ) {
142
143
			$propertyRegistry->register(
144
				$registry
145
			);
146
147
			return true;
148
		};
149 1
150 1
		/**
151
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables
152 1
		 */
153
		$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties, &$fixedPropertyTablePrefix ) use( $propertyRegistry ) {
154 1
155
			$propertyRegistry->registerFixedProperties(
156
				$customFixedProperties,
157
				$fixedPropertyTablePrefix
158
			);
159
160 1
			return true;
161
		};
162 1
163
		/**
164 1
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMWStore::updateDataBefore
165
		 */
166 1
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $appFactory ) {
167
168 1
			$extraPropertyAnnotator = new ExtraPropertyAnnotator(
169
				$appFactory
170 2
			);
171
172
			$extraPropertyAnnotator->addAnnotation( $semanticData );
173
174
			return true;
175
		};
176
	}
177
178
}
179