Completed
Push — master ( e3a9b8...5ff978 )
by mw
02:24
created

HookRegistry::initExtension()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.8088

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 14
cts 24
cp 0.5833
rs 8.9368
c 0
b 0
f 0
cc 5
nc 1
nop 1
crap 6.8088
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
			if ( isset( $config['smwgImportFileDirs'] ) ) {
118
				$config['smwgImportFileDirs'] += [ 'sesp' => __DIR__ . '/../data/import' ];
119
			}
120
121 1
			return true;
122
		};
123 1
	}
124
125 2
	private function registerCallbackHandlers( $config ) {
126
127 2
		$applicationFactory = ApplicationFactory::getInstance();
128
129 2
		$appFactory = new AppFactory(
130 2
			$config,
131 2
			$applicationFactory->getCache()
132 2
		);
133
134 2
		$appFactory->setLogger(
135 2
			$applicationFactory->getMediaWikiLogger( 'sesp' )
136 2
		);
137
138 2
		$propertyRegistry = new PropertyRegistry(
139
			$appFactory
140 2
		);
141
142
		/**
143
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::Property::initProperties
144
		 */
145
		$this->handlers['SMW::Property::initProperties'] = function ( $registry ) use ( $propertyRegistry ) {
146
147 1
			$propertyRegistry->register(
148
				$registry
149 1
			);
150
151 1
			return true;
152
		};
153
154
		/**
155
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMW::SQLStore::AddCustomFixedPropertyTables
156
		 */
157
		$this->handlers['SMW::SQLStore::AddCustomFixedPropertyTables'] = function( array &$customFixedProperties, &$fixedPropertyTablePrefix ) use( $propertyRegistry ) {
158
159 1
			$propertyRegistry->registerFixedProperties(
160 1
				$customFixedProperties,
161
				$fixedPropertyTablePrefix
162 1
			);
163
164 1
			return true;
165
		};
166
167
		/**
168
		 * @see https://www.semantic-mediawiki.org/wiki/Hooks/SMWStore::updateDataBefore
169
		 */
170 1
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $appFactory ) {
171
172 1
			$extraPropertyAnnotator = new ExtraPropertyAnnotator(
173
				$appFactory
174 1
			);
175
176 1
			$extraPropertyAnnotator->addAnnotation( $semanticData );
177
178 1
			return true;
179
		};
180 2
	}
181
182
}
183