summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/ExpResourceMapper.php
blob: 954b048a79ddb1ad4b0a2a4abc16773fc3bf415b (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
<?php

namespace SMW\Exporter;

use RuntimeException;
use SMW\DataValueFactory;
use SMW\DIProperty;
use SMW\DIWikiPage;
use SMW\Exporter\Element\ExpNsResource;
use SMW\Exporter\Element\ExpResource;
use SMW\InMemoryPoolCache;
use SMW\Store;
use SMWDataItem as DataItem;
use SMWExporter as Exporter;
use Title;

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

	/**
	 * Identifies auxiliary data (helper values)
	 */
	const AUX_MARKER = 'aux';

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var DataValueFactory
	 */
	private $dataValueFactory;

	/**
	 * @var InMemoryPoolCache
	 */
	private $inMemoryPoolCache;

	/**
	 * @note Legacy setting expected to vanish with 3.0
	 *
	 * @var boolean
	 */
	private $bcAuxiliaryUse = true;

	/**
	 * @var boolean
	 */
	private $seekImportVocabulary = true;

	/**
	 * @since 2.2
	 *
	 * @param Store $store
	 */
	public function __construct( Store $store ) {
		$this->store = $store;
		$this->dataValueFactory = DataValueFactory::getInstance();
		$this->inMemoryPoolCache = InMemoryPoolCache::getInstance();
	}

	/**
	 * @since 2.3
	 */
	public function reset() {
		$this->inMemoryPoolCache->resetPoolCacheById( 'exporter.expresource.mapper' );
	}

	/**
	 * @since 2.2
	 *
	 * @param boolean $bcAuxiliaryUse
	 */
	public function setBCAuxiliaryUse( $bcAuxiliaryUse ) {
		$this->bcAuxiliaryUse = (bool)$bcAuxiliaryUse;
	}

	/**
	 * @since 2.2
	 *
	 * @param DIWikiPage $subject
	 */
	public function invalidateCache( DIWikiPage $subject ) {

		$hash = $subject->getHash();

		$poolCache = $this->inMemoryPoolCache->getPoolCacheById(
			'exporter.expresource.mapper'
		);

		foreach ( [ $hash, $hash . self::AUX_MARKER . $this->seekImportVocabulary ] as $key ) {
			$poolCache->delete( $key );
		}
	}

	/**
	 * Create an ExpElement for some internal resource, given by an
	 * DIProperty object.
	 *
	 * This code is only applied to user-defined properties, since the
	 * code for special properties in
	 * Exporter::getSpecialPropertyResource may require information
	 * about the namespace in which some special property is used.
	 *
	 * @note $useAuxiliaryModifier is to determine whether an auxiliary
	 * property resource is to store a helper value
	 * (see Exporter::getDataItemHelperExpElement) should be generated
	 *
	 * @param DIProperty $property
	 * @param boolean $useAuxiliaryModifier
	 * @param boolean $seekImportVocabulary
	 *
	 * @return ExpResource
	 * @throws RuntimeException
	 */
	public function mapPropertyToResourceElement( DIProperty $property, $useAuxiliaryModifier = false, $seekImportVocabulary = true ) {

		// We want the a canonical representation to ensure that resources
		// are language independent
		$this->seekImportVocabulary = $seekImportVocabulary;
		$diWikiPage = $property->getCanonicalDiWikiPage();

		if ( $diWikiPage === null ) {
			throw new RuntimeException( 'Only non-inverse, user-defined properties are permitted.' );
		}

		// No need for any aux properties besides those listed here
		if ( !$this->bcAuxiliaryUse && $property->findPropertyTypeID() !== '_dat' && $property->findPropertyTypeID() !== '_geo' ) {
			$useAuxiliaryModifier = false;
		}

		$expResource = $this->mapWikiPageToResourceElement( $diWikiPage, $useAuxiliaryModifier );
		$this->seekImportVocabulary = true;

		return $expResource;
	}

	/**
	 * Create an ExpElement for some internal resource, given by an
	 * DIWikiPage object. This is the one place in the code where URIs
	 * of wiki pages and user-defined properties are determined. A modifier
	 * can be given to make variants of a URI, typically done for
	 * auxiliary properties. In this case, the URI is modiied by appending
	 * "-23$modifier" where "-23" is the URI encoding of "#" (a symbol not
	 * occurring in MW titles).
	 *
	 * @param DIWikiPage $diWikiPage
	 * @param boolean $useAuxiliaryModifier
	 *
	 * @return ExpResource
	 */
	public function mapWikiPageToResourceElement( DIWikiPage $diWikiPage, $useAuxiliaryModifier = false ) {

		$modifier = $useAuxiliaryModifier ? self::AUX_MARKER : '';

		$hash = $diWikiPage->getHash() . $modifier . $this->seekImportVocabulary;

		$poolCache = $this->inMemoryPoolCache->getPoolCacheById( 'exporter.expresource.mapper' );

		if ( $poolCache->contains( $hash ) ) {
			return $poolCache->fetch( $hash );
		}

		if ( $diWikiPage->getSubobjectName() !== '' ) {
			$modifier = $diWikiPage->getSubobjectName();
		}

		$resource = $this->newExpNsResource(
			$diWikiPage,
			$modifier
		);

		$poolCache->save(
			$hash,
			$resource
		);

		return $resource;
	}

	private function newExpNsResource( $diWikiPage, $modifier ) {

		 $importDataItem = $this->findImportDataItem( $diWikiPage, $modifier );

		if ( $this->seekImportVocabulary && $importDataItem instanceof DataItem ) {
			list( $localName, $namespace, $namespaceId ) = $this->defineElementsForImportDataItem( $importDataItem );
		} else {
			list( $localName, $namespace, $namespaceId ) = $this->defineElementsForDiWikiPage( $diWikiPage, $modifier );
		}

		$resource = new ExpNsResource(
			$localName,
			$namespace,
			$namespaceId,
			$diWikiPage
		);

		$resource->isImported = $importDataItem instanceof DataItem;
		$dbKey = $diWikiPage->getDBkey();

		if ( $diWikiPage->getNamespace() === SMW_NS_PROPERTY && $dbKey !== '' && $dbKey{0} !== '-' ) {
			$resource->isUserDefined = DIProperty::newFromUserLabel( $diWikiPage->getDBkey() )->isUserDefined();
		}

		return $resource;
	}

	private function defineElementsForImportDataItem( DataItem $dataItem ) {

		$importValue = $this->dataValueFactory->newDataValueByItem(
			$dataItem,
			new DIProperty( '_IMPO' )
		);

		return [
			$importValue->getLocalName(),
			$importValue->getNS(),
			$importValue->getNSID()
		];
	}

	private function defineElementsForDiWikiPage( DIWikiPage $diWikiPage, $modifier ) {

		$localName = '';
		$hasFixedNamespace = false;

		if ( $diWikiPage->getNamespace() === NS_CATEGORY ) {
			$namespace = Exporter::getInstance()->getNamespaceUri( 'category' );
			$namespaceId = 'category';
			$localName = Escaper::encodeUri( $diWikiPage->getDBkey() );
			$hasFixedNamespace = true;
		}

		if ( $diWikiPage->getNamespace() === SMW_NS_PROPERTY ) {
			$namespace = Exporter::getInstance()->getNamespaceUri( 'property' );
			$namespaceId = 'property';
			$localName = Escaper::encodeUri( $diWikiPage->getDBkey() );
			$hasFixedNamespace = true;
		}

		if ( ( $localName === '' ) ||
		     ( in_array( $localName{0}, [ '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ] ) ) ||
		     ( $hasFixedNamespace && strpos( $localName, '/' ) !== false )
		     ) {
			$namespace = Exporter::getInstance()->getNamespaceUri( 'wiki' );
			$namespaceId = 'wiki';
			$localName = Escaper::encodePage( $diWikiPage );
		}

		if ( $hasFixedNamespace && strpos( $localName, '/' ) !== false ) {
			$namespace = Exporter::getInstance()->getNamespaceUri( 'wiki' );
			$namespaceId = 'wiki';
			$localName = Escaper::armorChars( Escaper::encodePage( $diWikiPage ) );
		}

		// "-23$modifier" where "-23" is the URI encoding of "#" (a symbol not
	 	// occurring in MW titles).
		if ( $modifier !== '' ) {
			$localName .=  '-23' . Escaper::encodeUri( $modifier );
		}

		return [
			$localName,
			$namespace,
			$namespaceId
		];
	}

	private function findImportDataItem( DIWikiPage $diWikiPage, $modifier ) {

		$importDataItems = null;

		// Only try to find an import vocab for a matchable entity
		if ( $this->seekImportVocabulary && $diWikiPage->getNamespace() === NS_CATEGORY || $diWikiPage->getNamespace() === SMW_NS_PROPERTY ) {
			$importDataItems = $this->store->getPropertyValues(
				$diWikiPage,
				new DIProperty( '_IMPO' )
			);
		}

		if ( $importDataItems !== null && $importDataItems !== [] ) {
			$importDataItems = current( $importDataItems );
		}

		return $importDataItems;
	}

}