Completed
Pull Request — master (#75)
by mw
05:07 queued 03:21
created

HookRegistry::onBeforeConfigCompletion()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.1707

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
nc 4
nop 1
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
ccs 11
cts 15
cp 0.7332
crap 3.1707
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 2
			wfGetDB( DB_SLAVE ),
111
			$configuration
112 2
		);
113
114 2
		$appFactory->setLogger(
115 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...
116 2
		);
117
118 2
		$propertyRegistry = new PropertyRegistry(
119
			$appFactory
120 2
		);
121
122
		/**
123
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties
124
		 */
125
		$this->handlers['SMW::Property::initProperties'] = function ( $basePropertyRegistry ) use ( $propertyRegistry ) {
126
127 1
			$propertyRegistry->registerOn(
128
				$basePropertyRegistry
129 1
			);
130
131 1
			return true;
132
		};
133
134
		/**
135
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables
136
		 */
137
		$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties, &$fixedPropertyTablePrefix ) use( $propertyRegistry ) {
138
139 1
			$propertyRegistry->registerAsFixedProperties(
140 1
				$customFixedProperties,
141
				$fixedPropertyTablePrefix
142 1
			);
143
144 1
			return true;
145
		};
146
147
		/**
148
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMWStore::updateDataBefore
149
		 */
150 1
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $appFactory ) {
151
152 1
			$extraPropertyAnnotator = new ExtraPropertyAnnotator(
153
				$appFactory
154 1
			);
155
156 1
			$extraPropertyAnnotator->addAnnotation( $semanticData );
157
158 1
			return true;
159
		};
160 2
	}
161
162
}
163