Completed
Pull Request — master (#36)
by
unknown
20:47
created

HookRegistry::getHandlerFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace SEQL;
4
5
use Hooks;
6
use SMW\Store;
7
8
/**
9
 * @license GNU GPL v2+
10
 * @since 1.0
11
 *
12
 * @author mwjames
13
 */
14
class HookRegistry {
15
16
	/**
17
	 * @var array
18
	 */
19
	private $handlers = array();
20
21
	/**
22
	 * @since 1.0
23
	 *
24
	 * @param array $options
25
	 */
26 10
	public function __construct( array $options ) {
27 10
		$this->addCallbackHandlers( $options );
28 10
	}
29
30
	/**
31
	 * @since  1.0
32
	 */
33 9
	public function register() {
34 9
		foreach ( $this->handlers as $name => $callback ) {
35 9
			Hooks::register( $name, $callback );
36 9
		}
37 9
	}
38
39
	/**
40
	 * @since  1.0
41
	 *
42
	 * @param string $name
43
	 *
44
	 * @return boolean
45
	 */
46 1
	public function isRegistered( $name ) {
47 1
		return Hooks::isRegistered( $name );
48
	}
49
50
	/**
51
	 * @since  1.0
52
	 *
53
	 * @param string $name
54
	 *
55
	 * @return Callable|false
56
	 */
57 1
	public function getHandlerFor( $name ) {
58 1
		return isset( $this->handlers[$name] ) ? $this->handlers[$name] : false;
59
	}
60
61 10
	private function addCallbackHandlers( $options ) {
62
63 10
		$dynamicInterwikiPrefixLoader = new DynamicInterwikiPrefixLoader(
64 10
			$options['externalRepositoryEndpoints']
65 10
		);
66
67
		$this->handlers['InterwikiLoadPrefix'] = function( $prefix, &$interwiki ) use( $dynamicInterwikiPrefixLoader ) {
68 2
			return $dynamicInterwikiPrefixLoader->tryToLoadIwMapForExternalRepository( $prefix, $interwiki );
69
		};
70 10
71
		/**
72
		 * Prevents ask parser function with "source" parameter defined from
73
		 * being executed outside of allowed namespaces. This supports transclusion too.
74
		 *
75
		 * @param \Parser $parser
76
		 * @param \PPFrame $frame
77
		 * @param $args
78
		 * @param $override
79
		 */
80
		$this->handlers['smwAskParserFunction'] = $this->handlers['smwShowParserFunction'] = function( $parser, $frame, $args, &$override ) {
81
			if( $frame ) {
82
				$params = [];
83
				foreach ($args as $key => $value) {
84
					if ( $key == 0 || ( $value !== '' && $value{0} === '?' ) ) {
85
						continue;
86
					}
87
					if ( strpos( $value, '=' ) === false ) {
88
						continue;
89
					}
90
					$pair = explode('=', $value);
91
					$params[$pair[0]] = $pair[1];
92
				}
93
94
				if( array_key_exists('source', $params) && !in_array( $frame->getTitle()->getNamespace(), $GLOBALS['seqlgExternalQueryEnabledNamespaces'] ) ) {
95
					$override = 'Warning: source parameter is not allowed in the namespace!';
96
				}
97
			}
98
		};
99
100
	}
101
102
}
103