summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/PropertyRegistry.php
blob: ab5f521a0ab43f5c3a79e614a64a91537e9cb790 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php

namespace SMW;

/**
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 * @author Markus Krötzsch
 */
class PropertyRegistry {

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

	/**
	 * @var PropertyLabelFinder
	 */
	private $propertyLabelFinder = null;

	/**
	 * Array for assigning types to predefined properties. Each
	 * property is associated with an array with the following
	 * elements:
	 *
	 * * ID of datatype to be used for this property
	 *
	 * * Boolean, stating if this property is shown in Factbox, Browse, and
	 *   similar interfaces; (note that this is only relevant if the
	 *   property can be displayed at all, i.e. has a translated label in
	 *   the wiki language; invisible properties are never shown).
	 *
	 * @var array
	 */
	private $propertyList = [];

	/**
	 * @var string[]
	 */
	private $datatypeLabels = [];

	/**
	 * @var string[]
	 */
	private $propertyDescriptionMsgKeys = [];

	/**
	 * @var PropertyAliasFinder
	 */
	private $propertyAliasFinder;

	/**
	 * @var string[]
	 */
	private $dataTypePropertyExemptionList = [];

	/**
	 * @since 2.1
	 *
	 * @return PropertyRegistry
	 */
	public static function getInstance() {

		if ( self::$instance !== null ) {
			return self::$instance;
		}

		$applicationFactory = ApplicationFactory::getInstance();
		$lang = Localizer::getInstance()->getLang();

		$propertyAliasFinder = new PropertyAliasFinder(
			$applicationFactory->getCache(),
			$lang->getPropertyAliases(),
			$lang->getCanonicalPropertyAliases()
		);

		$settings = $applicationFactory->getSettings();

		self::$instance = new self(
			DataTypeRegistry::getInstance(),
			$applicationFactory->getPropertyLabelFinder(),
			$propertyAliasFinder,
			$settings->get( 'smwgDataTypePropertyExemptionList' )
		);

		self::$instance->initProperties(
			TypesRegistry::getPropertyList(
				$settings->isFlagSet( 'smwgCategoryFeatures', SMW_CAT_HIERARCHY )
			)
		);

		return self::$instance;
	}

	/**
	 * @since 2.1
	 *
	 * @param DataTypeRegistry $datatypeRegistry
	 * @param PropertyLabelFinder $propertyLabelFinder
	 * @param PropertyAliasFinder $propertyAliasFinder
	 * @param array $dataTypePropertyExemptionList
	 */
	public function __construct( DataTypeRegistry $datatypeRegistry, PropertyLabelFinder $propertyLabelFinder, PropertyAliasFinder $propertyAliasFinder, array $dataTypePropertyExemptionList = [] ) {

		$this->datatypeLabels = $datatypeRegistry->getKnownTypeLabels();
		$this->propertyLabelFinder = $propertyLabelFinder;
		$this->propertyAliasFinder = $propertyAliasFinder;

		// To get an index access
		$this->dataTypePropertyExemptionList = array_flip( $dataTypePropertyExemptionList );

		foreach ( $this->datatypeLabels as $id => $label ) {

			if ( isset( $this->dataTypePropertyExemptionList[$label] ) ) {
				continue;
			}

			$this->registerPropertyLabel( $id, $label );
		}

		foreach ( $datatypeRegistry->getKnownTypeAliases() as $alias => $id ) {

			if ( isset( $this->dataTypePropertyExemptionList[$alias] ) ) {
				continue;
			}

			$this->registerPropertyAlias( $id, $alias );
		}
	}

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

	/**
	 * @since 2.1
	 *
	 * @return array
	 */
	public function getPropertyList() {
		return $this->propertyList;
	}

	/**
	 * @since 2.1
	 *
	 * @return array
	 */
	public function getKnownPropertyAliases() {
		return $this->propertyAliasFinder->getKnownPropertyAliases();
	}

	/**
	 * A method for registering/overwriting predefined properties for SMW.
	 * It should be called from within the hook 'smwInitProperties' only.
	 * IDs should start with three underscores "___" to avoid current and
	 * future confusion with SMW built-ins.
	 *
	 * @param string $id
	 * @param string $valueType SMW type id
	 * @param string|bool $label user label or false (internal property)
	 * @param boolean $isVisible only used if label is given, see isShown()
	 * @param boolean $isAnnotable
	 */
	public function registerProperty( $id, $valueType, $label = false, $isVisible = false, $isAnnotable = true ) {

		$this->propertyList[$id] = [ $valueType, $isVisible, $isAnnotable ];

		if ( $label !== false ) {
			$this->registerPropertyLabel( $id, $label );
		}
	}

	/**
	 * Add a new alias label to an existing property ID. Note that every ID
	 * should have a primary label, either provided by SMW or registered
	 * with registerProperty().
	 *
	 * @param $id string id of a property
	 * @param $label string alias label for the property
	 *
	 * @note Always use registerProperty() for the first label. No property
	 * that has used "false" for a label on registration should have an
	 * alias.
	 */
	public function registerPropertyAlias( $id, $label ) {
		$this->propertyAliasFinder->registerAliasByFixedLabel( $id, $label );
	}

	/**
	 * Register an alias using a message key to allow fetching localized
	 * labels dynamically (for when the user language is changed etc).
	 *
	 * @since 2.4
	 *
	 * @param string $id
	 * @param string $msgKey
	 */
	public function registerPropertyAliasByMsgKey( $id, $msgKey ) {
		$this->propertyAliasFinder->registerAliasByMsgKey( $id, $msgKey );
	}

	/**
	 * Register a description message key for allowing it to be displayed in a
	 * localized context.
	 *
	 * @since 2.5
	 *
	 * @param string $id
	 * @param string $msgKey
	 */
	public function registerPropertyDescriptionByMsgKey( $id, $msgKey ) {
		$this->propertyDescriptionMsgKeys[$id] = $msgKey;
	}

	/**
	 * @deprecated since 3.0, use PropertyRegistry::registerPropertyDescriptionByMsgKey
	 */
	public function registerPropertyDescriptionMsgKeyById( $id, $msgKey ) {
		$this->registerPropertyDescriptionByMsgKey( $id, $msgKey );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $id
	 *
	 * @return string
	 */
	public function findPropertyDescriptionMsgKeyById( $id ) {
		return isset( $this->propertyDescriptionMsgKeys[$id] ) ? $this->propertyDescriptionMsgKeys[$id] : '';
	}

	/**
	 * Get the translated user label for a given internal property ID.
	 * Returns empty string for properties without a translation (these are
	 * usually internal, generated by SMW but not shown to the user).
	 *
	 * @note An empty string is returned for incomplete translation (language
	 * bug) or deliberately invisible property
	 *
	 * @since 2.1
	 *
	 * @param string $id
	 *
	 * @return string
	 */
	public function findPropertyLabelById( $id ) {

		// This is a hack but there is no other good way to make it work without
		// open a whole new can of worms
		// '__' indicates predefined properties of extensions that contain alias
		// and translated labels and if available we want the translated label
		if ( ( substr( $id, 0, 2 ) === '__' ) &&
			( $label = $this->propertyAliasFinder->findPropertyAliasById( $id ) ) ) {
			return $label;
		}

		// core has dedicated files per language so the label is available over
		// the invoked language
		return $this->propertyLabelFinder->findPropertyLabelById( $id );
	}

	/**
	 * @since 2.4
	 *
	 * @param string $id
	 *
	 * @return string
	 */
	public function findCanonicalPropertyLabelById( $id ) {
		return $this->propertyLabelFinder->findCanonicalPropertyLabelById( $id );
	}

	/**
	 * @since 2.5
	 *
	 * @param string $id
	 * @param string $languageCode
	 *
	 * @return string
	 */
	public function findPropertyLabelFromIdByLanguageCode( $id, $languageCode = '' ) {
		return $this->propertyLabelFinder->findPropertyLabelFromIdByLanguageCode( $id, $languageCode );
	}

	/**
	 * @deprecated since 2.1 use findPropertyLabelById instead
	 */
	public function findPropertyLabel( $id ) {
		return $this->findPropertyLabelById( $id );
	}

	/**
	 * Get the type ID of a predefined property, or '' if the property
	 * is not predefined.
	 * The function is guaranteed to return a type ID for keys of
	 * properties where isUserDefined() returns false.
	 *
	 * @param string $id
	 *
	 * @return string
	 */
	public function getPropertyValueTypeById( $id ) {

		if ( $this->isRegistered( $id ) ) {
			return $this->propertyList[$id][0];
		}

		return '';
	}

	/**
	 * @deprecated since 3.0, use PropertyRegistry::getPropertyValueTypeById instead
	 */
	public function getPropertyTypeId( $id ) {
		return $this->getPropertyValueTypeById( $id );
	}

	/**
	 * @deprecated since 2.1 use getPropertyValueTypeById instead
	 */
	public function getPredefinedPropertyTypeId( $id ) {
		return $this->getPropertyValueTypeById( $id );
	}

	/**
	 * Find and return the ID for the pre-defined property of the given
	 * local label. If the label does not belong to a pre-defined property,
	 * return false.
	 *
	 * @param string $label normalized property label
	 * @param boolean $useAlias determining whether to check if the label is an alias
	 *
	 * @return mixed string property ID or false
	 */
	public function findPropertyIdByLabel( $label, $useAlias = true ) {

		$id = $this->propertyLabelFinder->searchPropertyIdByLabel( $label );

		if ( $id !== false ) {
			return $id;
		} elseif ( $useAlias && $this->propertyAliasFinder->findPropertyIdByAlias( $label ) ) {
			return $this->propertyAliasFinder->findPropertyIdByAlias( $label );
		}

		return false;
	}

	/**
	 * @since 2.4
	 *
	 * @param string $label
	 * @param string $languageCode
	 *
	 * @return mixed string property ID or false
	 */
	public function findPropertyIdFromLabelByLanguageCode( $label, $languageCode = '' ) {

		$languageCode = mb_strtolower( trim( $languageCode ) );

		// Match the canonical form
		if ( $languageCode === '' ) {
			return $this->findPropertyIdByLabel( $label );
		}

		$lang = Localizer::getInstance()->getLang(
			$languageCode
		);

		// Language dep. stored as aliases
		$aliases = $lang->getPropertyLabels() + $lang->getDatatypeLabels();

		if ( ( $id = array_search( $label, $aliases ) ) !== false && !isset( $this->dataTypePropertyExemptionList[$label] ) ) {
			return $id;
		}

		// Those are mostly from extension that register a msgKey as no dedicated
		// lang. file exists; maybe this should be cached somehow?
		foreach ( $this->propertyAliasFinder->getKnownPropertyAliasesByLanguageCode( $languageCode ) as $alias => $id ) {
			if ( $label === $alias ) {
				return $id;
			}
		}

		return false;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $id
	 * @param string|null $languageCode
	 *
	 * @return string
	 */
	public function findPreferredPropertyLabelFromIdByLanguageCode( $id, $languageCode = '' ) {

		if ( $languageCode === false || $languageCode === '' ) {
			$languageCode = Localizer::getInstance()->getUserLanguage()->getCode();
		}

		return $this->propertyLabelFinder->findPreferredPropertyLabelByLanguageCode( $id, $languageCode );
	}

	/**
	 * @deprecated since 2.1 use findPropertyIdByLabel instead
	 */
	public function findPropertyId( $label, $useAlias = true ) {
		return $this->findPropertyIdByLabel( $label, $useAlias );
	}

	/**
	 * @deprecated since 3.0 use isRegistered instead
	 */
	public function isKnownPropertyId( $id ) {
		return $this->isRegistered( $id );
	}

	/**
	 * @since 2.1
	 *
	 * @param  string $id
	 *
	 * @return boolean
	 */
	public function isRegistered( $id ) {
		return isset( $this->propertyList[$id] ) || array_key_exists( $id, $this->propertyList );
	}

	/**
	 * @since 2.1
	 *
	 * @param  string $id
	 *
	 * @return boolean
	 */
	public function isVisible( $id ) {
		return $this->isRegistered( $id ) ? $this->propertyList[$id][1] : false;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $id
	 *
	 * @return boolean
	 */
	public function isAnnotable( $id ) {
		return $this->isRegistered( $id ) ? $this->propertyList[$id][2] : false;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $id
	 *
	 * @return boolean
	 */
	public function isDeclarative( $id ) {

		if ( !$this->isRegistered( $id ) ) {
			return false;
		}

		return isset( $this->propertyList[$id][3] ) ? $this->propertyList[$id][3] : false;
	}

	/**
	 * @note All ids must start with underscores. The translation for each ID,
	 * if any, is defined in the language files. Properties without translation
	 * cannot be entered by or displayed to users, whatever their "show" value
	 * below.
	 */
	protected function initProperties( array $propertyList ) {

		$this->propertyList = $propertyList;

		foreach ( $this->datatypeLabels as $id => $label ) {
			$this->propertyList[$id] = [ $id, true, true, false ];
		}

		// @deprecated since 2.1
		\Hooks::run( 'smwInitProperties' );

		\Hooks::run( 'SMW::Property::initProperties', [ $this ] );
	}

	private function registerPropertyLabel( $id, $label, $asCanonical = true ) {
		$this->propertyLabelFinder->registerPropertyLabel( $id, $label, $asCanonical );
	}

}