summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/DataValues/TypesValue.php
blob: 97763490b67fe04e239facfe2a17766929a92d94 (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
<?php

namespace SMW\DataValues;

use SMW\DataTypeRegistry;
use SMW\Localizer;
use SMWDataItem as DataItem;
use SMWDataItemException as DataItemException;
use SMWDataValue as DataValue;
use SMWDIUri as DIUri;
use SpecialPageFactory;
use Title;

/**
 * This datavalue implements special processing suitable for defining types of
 * properties. Types behave largely like values of type SMWWikiPageValue
 * with three main differences. First, they actively check if a value is an
 * alias for another type, modifying the internal representation accordingly.
 * Second, they have a modified display for emphasizing if some type is defined
 * in SMW (built-in). Third, they use type ids for storing data (DB keys)
 * instead of using page titles.
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class TypesValue extends DataValue {

	/**
	 * DV identifier
	 */
	const TYPE_ID = '__typ';

	/**
	 * @var string
	 */
	private $typeLabel;

	/**
	 * @var string
	 */
	private $givenLabel;

	/**
	 * @var string
	 */
	private $m_typeId;

	/**
	 * @param string $typeid
	 */
	public function __construct( $typeid = '' ) {
		parent::__construct( self::TYPE_ID );
	}

	/**
	 * @since 1.6
	 *
	 * @param string $typeId
	 *
	 * @return TypesValue
	 */
	public static function newFromTypeId( $typeId ) {
		$result = new TypesValue( self::TYPE_ID );

		try {
			$dataItem = self::getTypeUriFromTypeId( $typeId );
		} catch ( DataItemException $e ) {
			$dataItem = self::getTypeUriFromTypeId( 'notype' );
		}

		$result->setDataItem( $dataItem );

		return $result;
	}

	/**
	 * @since 1.6
	 *
	 * @param string $typeId
	 *
	 * @return DIUri
	 */
	public static function getTypeUriFromTypeId( $typeId ) {
		return new DIUri( 'http', 'semantic-mediawiki.org/swivt/1.0', '', $typeId );
	}

	/**
	 * @see DataValue::getShortWikiText
	 *
	 * {@inheritDoc}
	 */
	public function getShortWikiText( $linker = null ) {

		if ( !$linker || $this->m_outformat === '-' || $this->m_caption === '' ) {
			return $this->m_caption;
		}

		$titleText = $this->makeSpecialPageTitleText();

		$contentLanguage = Localizer::getInstance()->getLanguage(
			$this->getOption( self::OPT_CONTENT_LANGUAGE )
		);

		$namespace = $contentLanguage->getNsText(
			NS_SPECIAL
		);

		return "[[$namespace:$titleText|{$this->m_caption}]]";
	}

	/**
	 * @see DataValue::getShortHTMLText
	 *
	 * {@inheritDoc}
	 */
	public function getShortHTMLText( $linker = null ) {

		if ( !$linker || $this->m_outformat === '-' || $this->m_caption === ''  ) {
			return htmlspecialchars( $this->m_caption );
		}

		$title = Title::makeTitle(
			NS_SPECIAL,
			$this->makeSpecialPageTitleText()
		);

		return $linker->link( $title, htmlspecialchars( $this->m_caption ) );
	}

	/**
	 * @see DataValue::getLongWikiText
	 *
	 * {@inheritDoc}
	 */
	public function getLongWikiText( $linker = null ) {

		if ( !$linker || $this->typeLabel === '' ) {
			return $this->typeLabel;
		}

		$titleText = $this->makeSpecialPageTitleText();

		$contentLanguage = Localizer::getInstance()->getLanguage(
			$this->getOption( self::OPT_CONTENT_LANGUAGE )
		);

		$namespace = $contentLanguage->getNsText(
			NS_SPECIAL
		);

		return "[[$namespace:$titleText|{$this->typeLabel}]]";
	}

	/**
	 * @see DataValue::getLongHTMLText
	 *
	 * {@inheritDoc}
	 */
	public function getLongHTMLText( $linker = null ) {

		if ( !$linker || $this->typeLabel === '' ) {
			return htmlspecialchars( $this->typeLabel );
		}

		$title = Title::makeTitle(
			NS_SPECIAL,
			$this->makeSpecialPageTitleText()
		);

		return $linker->link( $title, htmlspecialchars( $this->typeLabel ) );
	}

	/**
	 * @see DataValue::getWikiValue
	 *
	 * {@inheritDoc}
	 */
	public function getWikiValue() {
		return $this->typeLabel;
	}

	/**
	 * @see DataValue::loadDataItem
	 *
	 * {@inheritDoc}
	 */
	protected function parseUserValue( $value ) {

		if ( $this->m_caption === false ) {
			$this->m_caption = $value;
		}

		$valueParts = explode( ':', $value, 2 );
		$contentLanguage = $this->getOption( self::OPT_CONTENT_LANGUAGE );

		if ( $value !== '' && $value{0} === '_' ) {
			$this->m_typeId = $value;
		} else {
			$this->givenLabel = smwfNormalTitleText( $value );
			$this->m_typeId = DataTypeRegistry::getInstance()->findTypeByLabelAndLanguage( $this->givenLabel, $contentLanguage );
		}

		if ( $this->m_typeId === '' ) {
			$this->addErrorMsg( [ 'smw_unknowntype', $this->givenLabel ] );
			$this->typeLabel = $this->givenLabel;
		} else {
			$this->typeLabel = DataTypeRegistry::getInstance()->findTypeLabel( $this->m_typeId );
		}

		try {
			$this->m_dataitem = self::getTypeUriFromTypeId( $this->m_typeId );
		} catch ( DataItemException $e ) {
			$this->m_dataitem = self::getTypeUriFromTypeId( 'notype' );
			$this->addErrorMsg( [ 'smw-datavalue-type-invalid-typeuri', $this->m_typeId ] );
		}
	}

	/**
	 * @see DataValue::loadDataItem
	 *
	 * {@inheritDoc}
	 */
	protected function loadDataItem( DataItem $dataItem ) {

		if ( ( $dataItem instanceof DIUri ) && ( $dataItem->getScheme() == 'http' ) &&
			( $dataItem->getHierpart() == 'semantic-mediawiki.org/swivt/1.0' ) &&
			( $dataItem->getQuery() === '' ) ) {

			$this->m_typeId = $dataItem->getFragment();
			$this->typeLabel = DataTypeRegistry::getInstance()->findTypeLabel( $this->m_typeId );
			$this->m_caption = $this->givenLabel = $this->typeLabel;
			$this->m_dataitem = $dataItem;

			return true;
		}

		return false;
	}

	protected function makeSpecialPageTitleText() {
		return SpecialPageFactory::getLocalNameFor( 'Types', $this->typeLabel );
	}

}