Completed
Pull Request — master (#70)
by mw
01:58 queued 24s
created

HookRegistry::onBeforeConfigCompletion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
crap 2.0078
1
<?php
2
3
namespace SESP;
4
5
use SESP\PropertyRegistry;
6
use SESP\Annotator\ExtraPropertyAnnotator;
7
use SESP\Annotator\ShortUrlAnnotator;
8
use Hooks;
9
10
/**
11
 * @license GNU GPL v2+
12
 * @since 1.3
13
 *
14
 * @author mwjames
15
 */
16
class HookRegistry {
17
18
	/**
19
	 * @var array
20
	 */
21
	private $handlers = array();
22
23
	/**
24
	 * @since 1.0
25
	 *
26
	 * @param array $configuration
27
	 */
28 2
	public function __construct( $configuration ) {
29 2
		$this->registerCallbackHandlers( $configuration );
30 2
	}
31
32
	/**
33
	 * @since  1.0
34
	 */
35 1
	public function register() {
36 1
		foreach ( $this->handlers as $name => $callback ) {
37 1
			Hooks::register( $name, $callback );
38 1
		}
39 1
	}
40
41
	/**
42
	 * @since  1.0
43
	 */
44 1
	public function deregister() {
45 1
		foreach ( array_keys( $this->handlers ) as $name ) {
46
47 1
			Hooks::clear( $name );
48
49
			// Remove registered `wgHooks` hooks that are not cleared by the
50
			// previous call
51 1
			if ( isset( $GLOBALS['wgHooks'][$name] ) ) {
52
				unset( $GLOBALS['wgHooks'][$name] );
53
			}
54 1
		}
55 1
	}
56
57
	/**
58
	 * @since  1.0
59
	 *
60
	 * @param string $name
61
	 *
62
	 * @return boolean
63
	 */
64 1
	public function isRegistered( $name ) {
65 1
		return Hooks::isRegistered( $name );
66
	}
67
68
	/**
69
	 * @since  1.0
70
	 *
71
	 * @param string $name
72
	 *
73
	 * @return array
74
	 */
75 1
	public function getHandlers( $name ) {
76 1
		return Hooks::getHandlers( $name );
77
	}
78
79
	/**
80
	 * @since  1.0
81
	 *
82
	 * @param array &$config
83
	 */
84 1
	public static function onBeforeConfigCompletion( &$config ) {
85
86 1
		if ( !isset( $config['smwgFulltextSearchPropertyExemptionList'] ) ) {
87
			return;
88
		}
89
90
		// Exclude those properties from indexing
91 1
		$config['smwgFulltextSearchPropertyExemptionList'] = array_merge(
92 1
			$config['smwgFulltextSearchPropertyExemptionList'],
93 1
			array( '___EUSER', '___CUSER', '___SUBP', '___EXIFDATA'	)
94 1
		);
95 1
	}
96
97 2
	private function registerCallbackHandlers( $configuration ) {
98
99 2
		$this->handlers['smwInitProperties'] = function () {
100 1
			return PropertyRegistry::getInstance()->registerPropertiesAndAliases();
101
		};
102
103
		$this->handlers['SMW::SQLStore::updatePropertyTableDefinitions'] = function ( &$propertyTableDefinitions ) use( $configuration ) {
104 1
			return PropertyRegistry::getInstance()->registerAsFixedTables( $propertyTableDefinitions, $configuration );
105
		};
106
107
		$this->handlers['SMWStore::updateDataBefore'] = function ( $store, $semanticData ) use ( $configuration ) {
108
109 1
			$appFactory = new AppFactory( $configuration['wgShortUrlPrefix'] );
110
111 1
			$propertyAnnotator = new ExtraPropertyAnnotator(
112 1
				$semanticData,
113 1
				$appFactory,
114
				$configuration
115 1
			);
116
117 1
			return $propertyAnnotator->addAnnotation();
118
		};
119 2
	}
120
121
}
122