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

namespace SMW\DataValues;

use SMW\Message;
use SMWDataItem as DataItem;
use SMWDataValue as DataValue;
use SMWDIBlob as DIBlob;

/**
 * This datavalue implements datavalues used by special property '_IMPO' used
 * for assigning imported vocabulary to some page of the wiki. It looks up a
 * MediaWiki message to find out whether a user-supplied vocabulary name can be
 * imported in the wiki, and whether its declaration is correct (to the extent
 * that this can be checked).
 *
 * @author Fabian Howahl
 * @author Markus Krötzsch
 */
class ImportValue extends DataValue {

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

	/**
	 * Fixed Mediawiki import prefix
	 */
	const IMPORT_PREFIX = 'Smw_import_';

	/**
	 * Type string assigned by the import declaration
	 *
	 * @var string
	 */
	private $termType = '';

	/**
	 * String provided by user which is used to look up data on Mediawiki:*-Page
	 *
	 * @var string
	 */
	private $qname = '';

	/**
	 * URI of namespace (without local name)
	 *
	 * @var string
	 */
	private $uri = '';

	/**
	 * Namespace id (e.g. "foaf")
	 *
	 * @var string
	 */
	private $namespace = '';

	/**
	 * Local name (e.g. "knows")
	 *
	 * @var string
	 */
	private $term = '';

	/**
	 * Wiki name of the vocab (e.g. "Friend of a Friend"), might contain wiki markup
	 *
	 * @var string
	 */
	private $declarativeName = '';

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

	/**
	 * @see DataValue::parseUserValue
	 *
	 * @param string $value
	 */
	protected function parseUserValue( $value ) {
		$this->qname = $value;

		$importValueParser = $this->dataValueServiceFactory->getValueParser(
			$this
		);

		list( $this->namespace, $this->term, $this->uri, $this->declarativeName, $this->termType ) = $importValueParser->parse(
			$value
		);

		if ( $importValueParser->getErrors() !== [] ) {

			foreach ( $importValueParser->getErrors() as $message ) {
				$this->addErrorMsg( $message );
			}

			$this->m_dataitem = new DIBlob( 'ERROR' );
			return;
		}

		// Encoded string for DB storage
		$this->m_dataitem = new DIBlob(
			$this->namespace . ' ' .
			$this->term . ' ' .
			$this->uri . ' ' .
			$this->termType
		);

		// check whether caption is set, otherwise assign link statement to caption
		if ( $this->m_caption === false ) {
			$this->m_caption = $this->createCaption( $this->namespace, $this->qname, $this->uri, $this->declarativeName );
		}
	}

	/**
	 * @see SMWDataValue::loadDataItem
	 *
	 * @param DataItem $dataitem
	 *
	 * @return boolean
	 */
	protected function loadDataItem( DataItem $dataItem ) {

		if ( !$dataItem instanceof DIBlob ) {
			return false;
		}

		$this->m_dataitem = $dataItem;
		$parts = explode( ' ', $dataItem->getString(), 4 );

		if ( count( $parts ) != 4 ) {
			$this->addErrorMsg( [ 'smw-datavalue-import-invalid-format', $dataItem->getString() ] );
		} else {
			$this->namespace = $parts[0];
			$this->term = $parts[1];
			$this->uri = $parts[2];
			$this->termType = $parts[3];
			$this->qname = $this->namespace . ':' . $this->term;
			$this->declarativeName = '';
			$this->m_caption = $this->createCaption( $this->namespace, $this->qname, $this->uri, $this->declarativeName );
		}

		return true;
	}

	/**
	 * @see DataValue::getShortWikiText
	 */
	public function getShortWikiText( $linked = null ) {
		return $this->m_caption;
	}

	/**
	 * @see DataValue::getShortHTMLText
	 */
	public function getShortHTMLText( $linker = null ) {
		return htmlspecialchars( $this->qname );
	}

	/**
	 * @see DataValue::getLongWikiText
	 */
	public function getLongWikiText( $linked = null ) {

		if ( !$this->isValid() ) {
			return $this->getErrorText();
		}

		return "[[MediaWiki:" . self::IMPORT_PREFIX . $this->namespace . "|" . $this->qname . "]]";
	}

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

		if ( !$this->isValid() ) {
			return $this->getErrorText();
		}

		return htmlspecialchars( $this->qname );
	}

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

	public function getNS() {
		return $this->uri;
	}

	public function getNSID() {
		return $this->namespace;
	}

	public function getLocalName() {
		return $this->term;
	}

	/**
	 * @since 2.2
	 *
	 * @return string
	 */
	public function getTermType() {
		return $this->termType;
	}

	/**
	 * @since 2.2
	 *
	 * @return string
	 */
	public function getImportReference() {
		return $this->namespace . ':' . $this->term . '|' . $this->uri;
	}

	private function createCaption( $namespace, $qname, $uri, $declarativeName ) {
		return "[[MediaWiki:" . self::IMPORT_PREFIX . $namespace . "|" . $qname . "]] " .  Message::get( [ 'parentheses', "[$uri $namespace] | " . $declarativeName ], Message::PARSE );
	}

}