summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/NamespaceManager.php
blob: d29c81daca94c36c68a5f4eeb5e2a3e47e31ade8 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

namespace SMW;

use SMW\Lang\Lang;

/**
 * @license GNU GPL v2+
 * @since 1.9
 *
 * @author mwjames
 * @author others
 */
class NamespaceManager {

	/**
	 * @var Lang
	 */
	private $lang;

	/**
	 * @since 1.9
	 *
	 * @param Lang|null $lang
	 */
	public function __construct( Lang $lang = null ) {
		$this->lang = $lang;

		if ( $this->lang === null ) {
			$this->lang = Lang::getInstance();
		}
	}

	/**
	 * @since 1.9
	 *
	 * @param &$vars
	 */
	public function init( &$vars ) {

		if ( !$this->isDefinedConstant( 'SMW_NS_PROPERTY' ) ) {
			$this->initCustomNamespace( $vars );
		}

		// Legacy seeting in case some extension request a `smwgContLang` reference
		if ( empty( $vars['smwgContLang'] ) ) {
			$vars['smwgContLang'] = $this->lang->fetch( $vars['wgLanguageCode'] );
		}

		$this->addNamespaceSettings( $vars );
		$this->addExtraNamespaceSettings( $vars );
	}

	/**
	 * @see Hooks:CanonicalNamespaces
	 * CanonicalNamespaces initialization
	 *
	 * @note According to T104954 registration via wgExtensionFunctions is to late
	 * and should happen before that
	 *
	 * @see https://phabricator.wikimedia.org/T104954#2391291
	 * @see https://www.mediawiki.org/wiki/Manual:Hooks/CanonicalNamespaces
	 * @Bug 34383
	 *
	 * @since 2.5
	 *
	 * @param array &$namespaces
	 */
	public static function initCanonicalNamespaces( array &$namespaces ) {

		$canonicalNames = self::initCustomNamespace( $GLOBALS )->getCanonicalNames();
		$namespacesByName = array_flip( $namespaces );

		// https://phabricator.wikimedia.org/T160665
		// Find any namespace that uses the same canonical name and remove it
		foreach ( $canonicalNames as $id => $name ) {
			if ( isset( $namespacesByName[$name] ) ) {
				unset( $namespaces[$namespacesByName[$name]] );
			}
		}

		$namespaces += $canonicalNames;

		return true;
	}

	/**
	 * @see Hooks:CanonicalNamespaces
	 *
	 * @since 1.9
	 *
	 * @return array
	 */
	public static function getCanonicalNames() {

		$canonicalNames = [
			SMW_NS_PROPERTY      => 'Property',
			SMW_NS_PROPERTY_TALK => 'Property_talk',
			SMW_NS_CONCEPT       => 'Concept',
			SMW_NS_CONCEPT_TALK  => 'Concept_talk',
			SMW_NS_SCHEMA        => 'smw/schema',
			SMW_NS_SCHEMA_TALK   => 'smw/schema_talk',
			SMW_NS_RULE          => 'Rule',
			SMW_NS_RULE_TALK     => 'Rule_talk'
		];

		return $canonicalNames;
	}

	/**
	 * @since 1.9
	 *
	 * @param integer offset
	 *
	 * @return array
	 */
	public static function buildNamespaceIndex( $offset ) {

		// 100 and 101 used to be occupied by SMW's now obsolete namespaces
		// "Relation" and "Relation_Talk"

		// 106 and 107 are occupied by the Semantic Forms, we define them here
		// to offer some (easy but useful) support to SF

		$namespaceIndex = [
			'SMW_NS_PROPERTY'      => $offset + 2,
			'SMW_NS_PROPERTY_TALK' => $offset + 3,
			//'SF_NS_FORM'           => $offset + 6,
			//'SF_NS_FORM_TALK'      => $offset + 7,
			'SMW_NS_CONCEPT'       => $offset + 8,
			'SMW_NS_CONCEPT_TALK'  => $offset + 9,

			// #3019 notes "Conflicts with the DPLforum extension ..."
			//'SMW_NS_SCHEMA'      => $offset + 10,
			//'SMW_NS_SCHEMA_TALK' => $offset + 11,

			'SMW_NS_SCHEMA'        => $offset + 12,
			'SMW_NS_SCHEMA_TALK'   => $offset + 13,

			'SMW_NS_RULE'          => $offset + 14,
			'SMW_NS_RULE_TALK'     => $offset + 15,
		];

		return $namespaceIndex;
	}

	/**
	 * @since 1.9
	 *
	 * @param array &$vars
	 * @param Lang|null $lang
	 */
	public static function initCustomNamespace( &$vars, Lang $lang = null ) {

		$instance = new self( $lang );

		if ( !isset( $vars['smwgNamespaceIndex'] ) ) {
			$vars['smwgNamespaceIndex'] = 100;
		}

		$defaultSettings = [
			'wgNamespaceAliases',
			'wgExtraNamespaces',
			'wgNamespacesWithSubpages',
			'smwgNamespacesWithSemanticLinks',
			'smwgNamespaceIndex',
			'wgCanonicalNamespaceNames'
		];

		foreach ( $defaultSettings as $key ) {
			$vars[$key] = !isset( $vars[$key] ) ? [] : $vars[$key];
		}

		foreach ( $instance->buildNamespaceIndex( $vars['smwgNamespaceIndex'] ) as $ns => $index ) {
			if ( !$instance->isDefinedConstant( $ns ) ) {
				define( $ns, $index );
			};
		}

		$extraNamespaces = $instance->getNamespacesByLanguageCode(
			$vars['wgLanguageCode']
		);

		$namespaceAliases = $instance->getNamespaceAliasesByLanguageCode(
			$vars['wgLanguageCode']
		);

		$vars['wgCanonicalNamespaceNames'] += $instance->getCanonicalNames();
		$vars['wgExtraNamespaces'] += $extraNamespaces + $instance->getCanonicalNames();
		$vars['wgNamespaceAliases'] = $namespaceAliases + array_flip( $extraNamespaces ) + array_flip( $instance->getCanonicalNames() ) + $vars['wgNamespaceAliases'];

		$instance->addNamespaceSettings( $vars );

		return $instance;
	}

	private function addNamespaceSettings( &$vars ) {

		/**
		 * Default settings for the SMW specific NS which can only
		 * be defined after SMW_NS_PROPERTY is declared
		 */
		$smwNamespacesSettings = [
			SMW_NS_PROPERTY  => true,
			SMW_NS_PROPERTY_TALK  => false,
			SMW_NS_CONCEPT => true,
			SMW_NS_CONCEPT_TALK => false,
			SMW_NS_SCHEMA => true,
			SMW_NS_SCHEMA_TALK => false,
		];

		// Combine default values with values specified in other places
		// (LocalSettings etc.)
		$vars['smwgNamespacesWithSemanticLinks'] = array_replace(
			$smwNamespacesSettings,
			$vars['smwgNamespacesWithSemanticLinks']
		);

		$vars['wgNamespaceContentModels'][SMW_NS_SCHEMA] = CONTENT_MODEL_SMW_SCHEMA;
	}

	private function addExtraNamespaceSettings( &$vars ) {

		/**
		 * Indicating which namespaces allow sub-pages
		 *
		 * @see https://www.mediawiki.org/wiki/Manual:$wgNamespacesWithSubpages
		 */
		$vars['wgNamespacesWithSubpages'] = $vars['wgNamespacesWithSubpages'] + [
			SMW_NS_PROPERTY_TALK => true,
			SMW_NS_CONCEPT_TALK => true,
		];

		/**
		 * Allow custom namespaces to be acknowledged as containing useful content
		 *
		 * @see https://www.mediawiki.org/wiki/Manual:$wgContentNamespaces
		 */
		$vars['wgContentNamespaces'] = $vars['wgContentNamespaces'] + [
			SMW_NS_PROPERTY,
			SMW_NS_CONCEPT
		];

		/**
		 * To indicate which namespaces are enabled for searching by default
		 *
		 * @see https://www.mediawiki.org/wiki/Manual:$wgNamespacesToBeSearchedDefault
		 */
		$vars['wgNamespacesToBeSearchedDefault'] = $vars['wgNamespacesToBeSearchedDefault'] + [
			SMW_NS_PROPERTY => true,
			SMW_NS_CONCEPT => true
		];
	}

	protected function isDefinedConstant( $constant ) {
		return defined( $constant );
	}

	protected function getNamespacesByLanguageCode( $languageCode ) {
		$GLOBALS['smwgContLang'] = $this->lang->fetch( $languageCode );
		return $GLOBALS['smwgContLang']->getNamespaces();
	}

	private function getNamespaceAliasesByLanguageCode( $languageCode ) {
		return $this->lang->fetch( $languageCode )->getNamespaceAliases();
	}

}