summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/PropertyAliasFinder.php
blob: d43c057d0a936a62cf6b1647a506922ff4f33a57 (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
<?php

namespace SMW;

use Onoi\Cache\Cache;

/**
 * @license GNU GPL v2
 * @since 2.4
 *
 * @author mwjames
 */
class PropertyAliasFinder {

	/**
	 * Identifies the cache namespace
	 */
	const CACHE_NAMESPACE = 'smw:property:alias';

	/**
	 * Identifies the cache TTL (one week)
	 */
	const CACHE_TTL = 604800;

	/**
	 * @var Cache
	 */
	private $cache;

	/**
	 * Array with entries "property alias" => "property id"
	 *
	 * @var string[]
	 */
	private $propertyAliases = [];

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

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

	/**
	 * @since 2.4
	 *
	 * @param Cache $cache
	 * @param array $propertyAliases
	 * @param array $canonicalPropertyAliases
	 */
	public function __construct( Cache $cache, array $propertyAliases = [], array $canonicalPropertyAliases = [] ) {
		$this->cache = $cache;
		$this->canonicalPropertyAliases = $canonicalPropertyAliases;

		foreach ( $propertyAliases as $alias => $id ) {
			$this->registerAliasByFixedLabel( $id, $alias );
		}
	}

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

	/**
	 * @since 2.4
	 *
	 * @return array
	 */
	public function getKnownPropertyAliasesWithMsgKey() {
		return $this->propertyAliasesByMsgKey;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $languageCode
	 *
	 * @return array
	 */
	public function getKnownPropertyAliasesByLanguageCode( $languageCode = 'en' ) {

		$key = smwfCacheKey(
			self::CACHE_NAMESPACE,
			[
				$languageCode,
				$this->propertyAliasesByMsgKey
			]
		);

		if ( ( $propertyAliases = $this->cache->fetch( $key ) ) !== false ) {
			return $propertyAliases;
		}

		$propertyAliases = [];

		foreach ( $this->propertyAliasesByMsgKey as $msgKey => $id ) {
			$propertyAliases[Message::get( $msgKey, Message::TEXT, $languageCode )] = $id;
		}

		$this->cache->save( $key, $propertyAliases, self::CACHE_TTL );

		return $propertyAliases;
	}

	/**
	 * Add a new alias label to an existing property ID. Note that every ID
	 * should have a primary label.
	 *
	 * @param string $id string
	 * @param string $label
	 */
	public function registerAliasByFixedLabel( $id, $label ) {

		// Prevent an extension to register an already known
		// label
		if ( isset( $this->canonicalPropertyAliases[$label] ) && $this->canonicalPropertyAliases[$label] !== $id ) {
			return;
		}

		// Indicates an untranslated MW message key
		if ( $label !== '' && $label{0} === '<' ) {
			return null;
		}

		$this->propertyAliases[$label] = $id;
	}

	/**
	 * Register an alias using a message key to allow fetching localized
	 * labels dynamically.
	 *
	 * @since 2.4
	 *
	 * @param string $id
	 * @param string $msgKey
	 */
	public function registerAliasByMsgKey( $id, $msgKey ) {
		$this->propertyAliasesByMsgKey[$msgKey] = $id;
	}

	/**
	 * @since 2.4
	 *
	 * @param string $id
	 *
	 * @return string|boolean
	 */
	public function findCanonicalPropertyAliasById( $id ) {
		return array_search( $id, $this->canonicalPropertyAliases );
	}

	/**
	 * @since 2.4
	 *
	 * @param string $id
	 *
	 * @return string|boolean
	 */
	public function findPropertyAliasById( $id ) {
		return array_search( $id, $this->propertyAliases );
	}

	/**
	 * 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 $alias
	 *
	 * @return string|boolean
	 */
	public function findPropertyIdByAlias( $alias ) {

		if ( isset( $this->propertyAliases[$alias] ) ) {
			return $this->propertyAliases[$alias];
		} elseif ( isset( $this->canonicalPropertyAliases[$alias] ) ) {
			return $this->canonicalPropertyAliases[$alias];
		}

		return false;
	}

}