Completed
Push — master ( 14d2bd...06e609 )
by mw
81:37 queued 59:24
created

includes/NamespaceManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW;
4
5
/**
6
 * @license GNU GPL v2+
7
 * @since 1.9
8
 *
9
 * @author mwjames
10
 * @author others
11
 */
12
class NamespaceManager {
13
14
	/** @var array */
15
	protected $globalVars;
16
17
	/**
18
	 * @since 1.9
19
	 *
20
	 * @param array &$globalVars
21
	 * @param string|null &directory
22
	 */
23 6
	public function __construct( &$globalVars, $directory = null ) {
24 6
		$this->globalVars =& $globalVars;
25 6
		$this->directory = $directory;
26 6
	}
27
28
	/**
29
	 * @since 1.9
30
	 */
31 4
	public function run() {
32
33 4
		if ( !$this->isDefinedConstant( 'SMW_NS_PROPERTY' ) ) {
34 1
			$this->initCustomNamespace( $this->globalVars );
35
		}
36
37 4
		if ( empty( $this->globalVars['smwgContLang'] ) ) {
38 4
			$this->globalVars['smwgContLang'] = ExtraneousLanguage::getInstance()->fetchByLanguageCode( $this->globalVars['wgLanguageCode'] );
39
		}
40
41 4
		$this->addNamespaceSettings();
42
43 4
		return true;
44
	}
45
46
	/**
47
	 * @since 2.4
48
	 *
49
	 * @param string $languageCode
50
	 *
51
	 * @return array
52
	 */
53 231
	public static function getNamespacesByLanguageCode( $languageCode ) {
54 231
		$GLOBALS['smwgContLang'] = ExtraneousLanguage::getInstance()->fetchByLanguageCode( $languageCode );
55 231
		return $GLOBALS['smwgContLang']->getNamespaces();
56
	}
57
58
	/**
59
	 * @see Hooks:CanonicalNamespaces
60
	 *
61
	 * @since 1.9
62
	 *
63
	 * @return array
64
	 */
65 268
	public static function getCanonicalNames() {
66
67
		$canonicalNames = array(
68 268
			SMW_NS_PROPERTY      => 'Property',
69 268
			SMW_NS_PROPERTY_TALK => 'Property_talk',
70 268
			SMW_NS_TYPE          => 'Type',
71 268
			SMW_NS_TYPE_TALK     => 'Type_talk',
72 268
			SMW_NS_CONCEPT       => 'Concept',
73 268
			SMW_NS_CONCEPT_TALK  => 'Concept_talk'
74
		);
75
76 268
		return $canonicalNames;
77
	}
78
79
	/**
80
	 * @since 1.9
81
	 *
82
	 * @param integer offset
83
	 *
84
	 * @return array
85
	 */
86 4
	public static function buildNamespaceIndex( $offset ) {
87
88
		$namespaceIndex = array(
89 4
			'SMW_NS_PROPERTY'      => $offset + 2,
90 4
			'SMW_NS_PROPERTY_TALK' => $offset + 3,
91 4
			'SMW_NS_TYPE'          => $offset + 4,
92 4
			'SMW_NS_TYPE_TALK'     => $offset + 5,
93 4
			'SF_NS_FORM'           => $offset + 6,
94 4
			'SF_NS_FORM_TALK'      => $offset + 7,
95 4
			'SMW_NS_CONCEPT'       => $offset + 8,
96 4
			'SMW_NS_CONCEPT_TALK'  => $offset + 9,
97
		);
98
99 4
		return $namespaceIndex;
100
	}
101
102
	/**
103
	 * 100 and 101 used to be occupied by SMW's now obsolete namespaces
104
	 * "Relation" and "Relation_Talk"
105
	 *
106
	 * 106 and 107 are occupied by the Semantic Forms, we define them here
107
	 * to offer some (easy but useful) support to SF
108
	 *
109
	 * @since 1.9
110
	 *
111
	 * @param array $globalVars
112
	 */
113 2
	public static function initCustomNamespace( &$globalVars ) {
114
115 2
		$instance = new self( $globalVars );
116
117 2
		if ( !isset( $globalVars['smwgNamespaceIndex'] ) ) {
118 2
			$globalVars['smwgNamespaceIndex'] = 100;
119
		}
120
121 2
		foreach ( $instance->buildNamespaceIndex( $globalVars['smwgNamespaceIndex'] ) as $ns => $index ) {
122 2
			if ( !$instance->isDefinedConstant( $ns ) ) {
123 2
				define( $ns, $index );
124
			};
125
		}
126
127 2
		$globalVars['wgExtraNamespaces'] = ( isset( $globalVars['wgExtraNamespaces'] ) ? $globalVars['wgExtraNamespaces'] : array() ) + self::getCanonicalNames();
128 2
	}
129
130 4
	protected function addNamespaceSettings() {
131
132 4
		$this->isValidConfigurationOrSetDefault( 'wgExtraNamespaces', array() );
133 4
		$this->isValidConfigurationOrSetDefault( 'wgNamespaceAliases', array() );
134
135
		/**
136
		 * @var SMWLanguage $smwgContLang
137
		 */
138 4
		$this->globalVars['wgExtraNamespaces'] = $this->globalVars['smwgContLang']->getNamespaces() + $this->globalVars['wgExtraNamespaces'];
139 4
		$this->globalVars['wgNamespaceAliases'] = array_flip( $this->globalVars['smwgContLang']->getNamespaces() ) + $this->globalVars['wgNamespaceAliases'];
140
141
		// Support subpages only for talk pages by default
142 4
		$this->globalVars['wgNamespacesWithSubpages'] = $this->globalVars['wgNamespacesWithSubpages'] + array(
143 4
			SMW_NS_PROPERTY_TALK => true,
144 4
			SMW_NS_TYPE_TALK => true,
145 4
			SMW_NS_CONCEPT_TALK => true,
146
		);
147
148
		// not modified for Semantic MediaWiki
149
		/* $this->globalVars['wgNamespacesToBeSearchedDefault'] = array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
150
			NS_MAIN           => true,
151
			);
152
		*/
153
154
		/**
155
		 * Default settings for the SMW specific NS which can only
156
		 * be defined after SMW_NS_PROPERTY is declared
157
		 */
158
		$smwNamespacesSettings = array(
159 4
			SMW_NS_PROPERTY  => true,
160 4
			SMW_NS_PROPERTY_TALK  => false,
161 4
			SMW_NS_TYPE => true,
162 4
			SMW_NS_TYPE_TALK => false,
163 4
			SMW_NS_CONCEPT => true,
164 4
			SMW_NS_CONCEPT_TALK => false,
165
		);
166
167
		// Combine default values with values specified in other places
168
		// (LocalSettings etc.)
169 4
		$this->globalVars['smwgNamespacesWithSemanticLinks'] = array_replace(
170
			$smwNamespacesSettings,
171 4
			$this->globalVars['smwgNamespacesWithSemanticLinks']
172
		);
173
174 4
	}
175
176 4
	protected function isValidConfigurationOrSetDefault( $element, $default ) {
177 4
		if ( !isset( $this->globalVars[$element] ) || !is_array( $this->globalVars[$element] ) ) {
178 1
			$this->globalVars[$element] = $default;
179
		}
180 4
	}
181
182 5
	protected function isDefinedConstant( $constant ) {
183 5
		return defined( $constant );
184
	}
185
186
}
187