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

namespace SMW\DataValues;

use SMW\DataValues\ValueFormatters\DataValueFormatter;
use SMWDataItem as DataItem;
use SMWDataValue as DataValue;
use SMWDIBlob as DIBlob;

/**
 * Implements a string/text based datavalue suitable for defining text properties.
 *
 * @license GNU GPL v2+
 * @since 1.6
 *
 * @author Nikolas Iwan
 * @author Markus Krötzsch
 */
class StringValue extends DataValue {

	/**
	 * DV text identifier
	 */
	const TYPE_ID = '_txt';

	/**
	 * DV identifier
	 */
	const TYPE_LEGACY_ID = '_str';

	/**
	 * DV code identifier
	 */
	const TYPE_COD_ID = '_cod';

	/**
	 * @var ValueFormatter
	 */
	private $valueFormatter;

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

		if ( $this->valueFormatter === null ) {
			$this->valueFormatter = $this->dataValueServiceFactory->getValueFormatter( $this );
		}

		return $this->valueFormatter->format( $this, [ DataValueFormatter::WIKI_SHORT, $linker ] );
	}

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

		if ( $this->valueFormatter === null ) {
			$this->valueFormatter = $this->dataValueServiceFactory->getValueFormatter( $this );
		}

		return $this->valueFormatter->format( $this, [ DataValueFormatter::HTML_SHORT, $linker ] );
	}

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

		if ( $this->valueFormatter === null ) {
			$this->valueFormatter = $this->dataValueServiceFactory->getValueFormatter( $this );
		}

		return $this->valueFormatter->format( $this, [ DataValueFormatter::WIKI_LONG, $linker ] );
	}

	/**
	 * @todo Rather parse input to obtain properly formatted HTML.
	 * @see DataValue::getLongHTMLText
	 *
	 * {@inheritDoc}
	 */
	public function getLongHTMLText( $linker = null ) {

		if ( $this->valueFormatter === null ) {
			$this->valueFormatter = $this->dataValueServiceFactory->getValueFormatter( $this );
		}

		return $this->valueFormatter->format( $this, [ DataValueFormatter::HTML_LONG, $linker ] );
	}

	/**
	 * @see DataValue::getWikiValue
	 *
	 * {@inheritDoc}
	 */
	public function getWikiValue() {

		if ( $this->valueFormatter === null ) {
			$this->valueFormatter = $this->dataValueServiceFactory->getValueFormatter( $this );
		}

		return $this->valueFormatter->format( $this, [ DataValueFormatter::VALUE, null ] );
	}

	/**
	 * @see DataValue::getInfolinks
	 *
	 * {@inheritDoc}
	 */
	public function getInfolinks() {

		if ( $this->m_typeid != '_cod' ) {
			return parent::getInfolinks();
		}

		return [];
	}

	/**
	 * @since 3.0
	 *
	 * @return integer
	 */
	public function getLength() {

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

		return mb_strlen( $this->m_dataitem->getString() );
	}

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

		if ( $value === '' ) {
			$this->addErrorMsg( 'smw_emptystring' );
		}

		$this->m_dataitem = new DIBlob( $value );
	}

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

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

		$this->m_caption = false;
		$this->m_dataitem = $dataItem;

		return true;
	}

	/**
	 * @see DataValue::getServiceLinkParams
	 *
	 * {@inheritDoc}
	 */
	protected function getServiceLinkParams() {

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

		// Create links to mapping services based on a wiki-editable message. The parameters
		// available to the message are:
		// $1: urlencoded string
		return [ rawurlencode( $this->m_dataitem->getString() ) ];
	}

}