summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/NamespaceExaminer.php
blob: 113110a522934b0f3c2abb51a8b809876b0807fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

namespace SMW;

use InvalidArgumentException;
use MWNamespace;

/**
 * Examines if a specific namespace is enabled for the usage of the
 * Semantic MediaWiki extension
 *
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 */
class NamespaceExaminer {

	/** @var array */
	private static $instance = null;

	/** @var array */
	private $registeredNamespaces = [];

	/**
	 * @since 1.9
	 *
	 * @param array $registeredNamespaces
	 */
	public function __construct( array $registeredNamespaces ) {
		$this->registeredNamespaces = $registeredNamespaces;
	}

	/**
	 * Returns a static instance with an invoked global settings array
	 *
	 * @par Example:
	 * @code
	 *  \SMW\NamespaceExaminer::getInstance()->isSemanticEnabled( NS_MAIN )
	 * @endcode
	 *
	 * @note Used in smwfIsSemanticsProcessed
	 *
	 * @since 1.9
	 *
	 * @return NamespaceExaminer
	 */
	public static function getInstance() {

		if ( self::$instance === null ) {
			self::$instance = self::newFromArray( Settings::newFromGlobals()->get( 'smwgNamespacesWithSemanticLinks' ) );
		}

		return self::$instance;
	}

	/**
	 * Registers an array of available namespaces
	 *
	 * @par Example:
	 * @code
	 *  \SMW\NamespaceExaminer::newFromArray( array( ... ) )->isSemanticEnabled( NS_MAIN )
	 * @endcode
	 *
	 * @since 1.9
	 *
	 * @return NamespaceExaminer
	 */
	public static function newFromArray( $registeredNamespaces ) {
		return new self( $registeredNamespaces );
	}

	/**
	 * Resets static instance
	 *
	 * @since 1.9
	 */
	public static function clear() {
		self::$instance = null;
	}

	/**
	 * Returns if a namespace is enabled for semantic processing
	 *
	 * @since 1.9
	 *
	 * @param integer $namespace
	 *
	 * @return boolean
	 * @throws InvalidArgumentException
	 */
	public function isSemanticEnabled( $namespace ) {

		if ( !is_int( $namespace ) ) {
			throw new InvalidArgumentException( "{$namespace} is not a number" );
		}

		if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
			// Bug 51435
			return false;
		}

		return $this->isEnabled( $namespace );
	}

	/**
	 * Asserts if a namespace is enabled
	 *
	 * @since 1.9
	 *
	 * @param integer $namespace
	 *
	 * @return boolean
	 */
	protected function isEnabled( $namespace ) {
		return !empty( $this->registeredNamespaces[$namespace] );
	}

}