Completed
Push — master ( 4cfef8...0aaff2 )
by
unknown
08:02
created

HookRegistry::initExtension()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.8849

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
ccs 13
cts 21
cp 0.619
rs 9.312
cc 4
nc 1
nop 1
crap 4.8849
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
	 */
26 2
	public function __construct( $config ) {
27 2
		$this->registerCallbackHandlers( $config );
28 2
	}
29
30
	/**
31
	 * @since  1.0
32
	 */
33 1
	public function register() {
34 1
		foreach ( $this->handlers as $name => $callback ) {
35 1
			Hooks::register( $name, $callback );
36 1
		}
37 1
	}
38
39
	/**
40
	 * @since  1.0
41
	 */
42 1
	public function deregister() {
43 1
		foreach ( array_keys( $this->handlers ) as $name ) {
44
45 1
			Hooks::clear( $name );
46
47
			// Remove registered `wgHooks` hooks that are not cleared by the
48
			// previous call
49 1
			if ( isset( $GLOBALS['wgHooks'][$name] ) ) {
50
				unset( $GLOBALS['wgHooks'][$name] );
51
			}
52 1
		}
53 1
	}
54
55
	/**
56
	 * @since  1.0
57
	 *
58
	 * @param string $name
59
	 *
60
	 * @return boolean
61
	 */
62 1
	public function isRegistered( $name ) {
63 1
		return Hooks::isRegistered( $name );
64
	}
65
66
	/**
67
	 * @since  1.0
68
	 *
69
	 * @param string $name
70
	 *
71
	 * @return array
72
	 */
73 1
	public function getHandlers( $name ) {
74 1
		return Hooks::getHandlers( $name );
75
	}
76
77
	/**
78
	 * @since 2.0
79
	 *
80
	 * @param array &$vars
81
	 */
82 1
	public static function initExtension( &$vars ) {
83
84
		$vars['wgHooks']['SMW::Config::BeforeCompletion'][] = function( &$config ) {
85
86
			$exemptionlist = [
87 1
				'___EUSER', '___CUSER', '___SUBP', '___REVID', '___VIEWS',
88 1
				'___NREV', '___NTREV', '___USEREDITCNT', '___EXIFDATA'
89 1
			];
90
91
			// Exclude listed properties from indexing
92 1
			if ( isset( $config['smwgFulltextSearchPropertyExemptionList'] ) ) {
93 1
				$config['smwgFulltextSearchPropertyExemptionList'] = array_merge(
94 1
					$config['smwgFulltextSearchPropertyExemptionList'],
95
					$exemptionlist
96 1
				);
97 1
			}
98
99
			// 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 1
			if ( isset( $config['smwgQueryDependencyPropertyExemptionlist'] ) ) {
103
				$config['smwgQueryDependencyPropertyExemptionlist'] = array_merge(
104
					$config['smwgQueryDependencyPropertyExemptionlist'],
105
					$exemptionlist
106
				);
107
			}
108
109
			// #93
110 1
			if ( isset( $config['smwgQueryDependencyPropertyExemptionList'] ) ) {
111
				$config['smwgQueryDependencyPropertyExemptionList'] = array_merge(
112
					$config['smwgQueryDependencyPropertyExemptionList'],
113
					$exemptionlist
114
				);
115
			}
116
117 1
			return true;
118
		};
119 1
	}
120
121 2
	private function registerCallbackHandlers( $config ) {
122
123 2
		$applicationFactory = ApplicationFactory::getInstance();
124
125 2
		$appFactory = new AppFactory(
126 2
			$config,
127 2
			$applicationFactory->getCache()
128 2
		);
129
130 2
		$appFactory->setLogger(
131 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...
132 2
		);
133
134 2
		$propertyRegistry = new PropertyRegistry(
135
			$appFactory
136 2
		);
137
138
		/**
139
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties
140
		 */
141
		$this->handlers['SMW::Property::initProperties'] = function ( $registry ) use ( $propertyRegistry ) {
142
143 1
			$propertyRegistry->register(
144
				$registry
145 1
			);
146
147 1
			return true;
148
		};
149
150
		/**
151
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables
152
		 */
153
		$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties, &$fixedPropertyTablePrefix ) use( $propertyRegistry ) {
154
155 1
			$propertyRegistry->registerFixedProperties(
156 1
				$customFixedProperties,
157
				$fixedPropertyTablePrefix
158 1
			);
159
160 1
			return true;
161
		};
162
163
		/**
164
		 * @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 1
			);
171
172 1
			$extraPropertyAnnotator->addAnnotation( $semanticData );
173
174 1
			return true;
175
		};
176 2
	}
177
178
}
179