Completed
Push — refact-v2 ( 9bddae...f926b0 )
by mw
05:08
created

HookRegistry   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 89.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 153
ccs 53
cts 59
cp 0.8983
rs 10
c 1
b 0
f 0
wmc 12
lcom 1
cbo 4

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 25 3
A registerCallbackHandlers() 0 57 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 = array();
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 = array(
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 1
	}
106
107 2
	private function registerCallbackHandlers( $configuration ) {
108
109 2
		$appFactory = new AppFactory(
110
			$configuration
111 2
		);
112
113 2
		$appFactory->setConnection(
114 2
			wfGetDB( DB_SLAVE )
115 2
		);
116
117 2
		$appFactory->setLogger(
118 2
			\SMW\ApplicationFactory::getInstance()->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...
119 2
		);
120
121 2
		$propertyRegistry = new PropertyRegistry(
122
			$appFactory
123 2
		);
124
125
		/**
126
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties
127
		 */
128
		$this->handlers['SMW::Property::initProperties'] = function ( $basePropertyRegistry ) use ( $propertyRegistry ) {
129
130 1
			$propertyRegistry->registerOn(
131
				$basePropertyRegistry
132 1
			);
133
134 1
			return true;
135
		};
136
137
		/**
138
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables
139
		 */
140
		$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties, &$fixedPropertyTablePrefix ) use( $propertyRegistry ) {
141
142 1
			$propertyRegistry->registerAsFixedProperties(
143 1
				$customFixedProperties,
144
				$fixedPropertyTablePrefix
145 1
			);
146
147 1
			return true;
148
		};
149
150
		/**
151
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMWStore::updateDataBefore
152
		 */
153 1
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $appFactory ) {
154
155 1
			$extraPropertyAnnotator = new ExtraPropertyAnnotator(
156
				$appFactory
157 1
			);
158
159 1
			$extraPropertyAnnotator->addAnnotation( $semanticData );
160
161 1
			return true;
162
		};
163 2
	}
164
165
}
166