1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This group contains all parts of SMW that relate to the processing of datavalues |
5
|
|
|
* of various types. |
6
|
|
|
* |
7
|
|
|
* @defgroup SMWDataValues SMWDataValues |
8
|
|
|
* @ingroup SMW |
9
|
|
|
*/ |
10
|
|
|
use SMW\ApplicationFactory; |
11
|
|
|
use SMW\DataValues\ValueFormatterRegistry; |
12
|
|
|
use SMW\DataValues\ValueValidatorRegistry; |
13
|
|
|
use SMW\Deserializers\DVDescriptionDeserializerRegistry; |
14
|
|
|
use SMW\Message; |
15
|
|
|
use SMW\Options; |
16
|
|
|
use SMW\Localizer; |
17
|
|
|
use SMW\Query\QueryComparator; |
18
|
|
|
use SMW\DataValues\InfoLinksProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Objects of this type represent all that is known about a certain user-provided |
22
|
|
|
* data value, especially its various representations as strings, tooltips, |
23
|
|
|
* numbers, etc. Objects can be created as "emtpy" containers of a certain type, |
24
|
|
|
* but are then usually filled with data to present one particular data value. |
25
|
|
|
* |
26
|
|
|
* Data values have two chief representation forms: the user-facing syntax and the |
27
|
|
|
* internal representation. In user syntax, every value is (necessarily) a single |
28
|
|
|
* string, however complex the value is. For example, a string such as "Help:editing" |
29
|
|
|
* may represent a wiki page called "Editing" in the namespace for "Help". The |
30
|
|
|
* internal representation may be any numerical array of strings and numbers. In the |
31
|
|
|
* example, it might be array("Editing",12), where 12 is the number used for identifying |
32
|
|
|
* the namespace "Help:". Of course, the internal representation could also use a single |
33
|
|
|
* string value, such as in array("Help:Editing"), but this might be less useful for |
34
|
|
|
* certain operations (e.g. filterng by namespace). Moreover, all values that are |
35
|
|
|
* restored from the database are given in the internal format, so it wise to choose a |
36
|
|
|
* format that allows for very fast and easy processing without unnecessary parsing. |
37
|
|
|
* |
38
|
|
|
* The main functions of data value objects are: |
39
|
|
|
* - setUserValue() which triggers parseUserValue() to process a user-level string. |
40
|
|
|
* |
41
|
|
|
* In addition, there are a number of get-functions that provide useful output versions |
42
|
|
|
* for displaying and serializing the value. |
43
|
|
|
* |
44
|
|
|
* @ingroup SMWDataValues |
45
|
|
|
* |
46
|
|
|
* @author Markus Krötzsch |
47
|
|
|
*/ |
48
|
|
|
abstract class SMWDataValue { |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Associated data item. This is the reference to the immutable object |
52
|
|
|
* that represents the current data content. All other data stored here |
53
|
|
|
* is only about presentation and parsing, but is not relevant to the |
54
|
|
|
* actual data that is represented (and stored later on). |
55
|
|
|
* |
56
|
|
|
* This variable must always be set to some data item, even if there |
57
|
|
|
* have been errors in initialising the data. |
58
|
|
|
* @var SMWDataItem |
59
|
|
|
*/ |
60
|
|
|
protected $m_dataitem; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The property for which this value is constructed or null if none |
64
|
|
|
* given. Property pages are used to make settings that affect parsing |
65
|
|
|
* and display, hence it is sometimes needed to know them. |
66
|
|
|
* |
67
|
|
|
* @var SMWDIProperty |
68
|
|
|
*/ |
69
|
|
|
protected $m_property = null; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Wiki page in the context of which the value is to be interpreted, or |
73
|
|
|
* null if not given (or not on a page). This information is used to |
74
|
|
|
* parse user values such as "#subsection" which only make sense when |
75
|
|
|
* used on a certain page. |
76
|
|
|
* |
77
|
|
|
* @var SMWDIWikiPage |
78
|
|
|
*/ |
79
|
|
|
protected $m_contextPage = null; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The text label to be used for output or false if none given. |
83
|
|
|
* @var string |
84
|
|
|
*/ |
85
|
|
|
protected $m_caption; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* The type id for this value object. |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
protected $m_typeid; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Output formatting string, false when not set. |
95
|
|
|
* @see setOutputFormat() |
96
|
|
|
* @var mixed |
97
|
|
|
*/ |
98
|
|
|
protected $m_outformat = false; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Array of error text messages. Private to allow us to track error insertion |
102
|
|
|
* (PHP's count() is too slow when called often) by using $mHasErrors. |
103
|
|
|
* @var array |
104
|
|
|
*/ |
105
|
|
|
private $mErrors = array(); |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Boolean indicating if there where any errors. |
109
|
|
|
* Should be modified accordingly when modifying $mErrors. |
110
|
|
|
* @var boolean |
111
|
|
|
*/ |
112
|
|
|
private $mHasErrors = false; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Extraneous services and object container |
116
|
|
|
* |
117
|
|
|
* @var array |
118
|
|
|
*/ |
119
|
|
|
private $extraneousFunctions = array(); |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @var Options |
123
|
|
|
*/ |
124
|
|
|
private $options; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var boolean |
128
|
|
|
*/ |
129
|
|
|
protected $approximateValue = false; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @var ValueConstraintValidator |
133
|
|
|
*/ |
134
|
|
|
private $valueConstraintValidator = null; |
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var InfoLinksProvider |
138
|
|
|
*/ |
139
|
|
|
private $infoLinksProvider = null; |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Constructor. |
143
|
|
|
* |
144
|
|
|
* @param string $typeid |
145
|
|
|
*/ |
146
|
|
|
public function __construct( $typeid ) { |
147
|
|
|
$this->m_typeid = $typeid; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
///// Set methods ///// |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Set the user value (and compute other representations if possible). |
154
|
|
|
* The given value is a string as supplied by some user. An alternative |
155
|
|
|
* label for printout might also be specified. |
156
|
|
|
* |
157
|
|
|
* The third argument was added in SMW 1.9 and should not be used from outside SMW. |
158
|
|
|
* |
159
|
|
|
* @param string $value |
160
|
|
|
* @param mixed $caption |
161
|
|
|
* @param boolean $approximateValue |
162
|
|
|
*/ |
163
|
206 |
|
public function setUserValue( $value, $caption = false, $approximateValue = false ) { |
164
|
206 |
|
|
165
|
206 |
|
$this->m_dataitem = null; |
166
|
|
|
$this->mErrors = array(); // clear errors |
167
|
|
|
$this->mHasErrors = false; |
168
|
|
|
$this->getInfoLinksProvider()->init(); |
169
|
|
|
$this->m_caption = is_string( $caption ) ? trim( $caption ) : false; |
|
|
|
|
170
|
|
|
$this->approximateValue = $approximateValue; |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
$this->parseUserValue( $value ); // may set caption if not set yet, depending on datavalue |
174
|
|
|
|
175
|
|
|
// The following checks for Strip markers generated by MediaWiki to handle special content, |
176
|
|
|
// from parser and extension tags e.g. <pre>,<nowiki>,<math>,<source>. |
177
|
|
|
// See https://en.wikipedia.org/wiki/Help:Strip_markers |
178
|
|
|
// In general, we are not prepared to handle such content properly, and we |
179
|
|
|
// also have no means of obtaining the user input at this point. Hence the assignment |
180
|
190 |
|
// just fails, even if parseUserValue() above might not have noticed this issue. |
181
|
|
|
// Note: \x07 was used in MediaWiki 1.11.0, \x7f is used now (backwards compatiblity, b/c) |
182
|
190 |
|
if ( ( strpos( $value, "\x7f" ) !== false ) || ( strpos( $value, "\x07" ) !== false ) ) { |
183
|
190 |
|
$this->addError( wfMessage( 'smw_parseerror' )->inContentLanguage()->text() ); |
184
|
190 |
|
} |
185
|
190 |
|
|
186
|
190 |
|
if ( $this->isValid() && !$approximateValue ) { |
187
|
190 |
|
$this->checkAllowedValues(); |
188
|
190 |
|
} |
189
|
190 |
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
190 |
|
/** |
193
|
|
|
* Set the actual data contained in this object. The method returns |
194
|
|
|
* true if this was successful (requiring the type of the dataitem |
195
|
|
|
* to match the data value). If false is returned, the data value is |
196
|
|
|
* left unchanged (the data item was rejected). |
197
|
|
|
* |
198
|
|
|
* @note Even if this function returns true, the data value object |
199
|
|
|
* might become invalid if the content of the data item caused errors |
200
|
|
|
* in spite of it being of the right basic type. False is only returned |
201
|
190 |
|
* if the data item is fundamentally incompatible with the data value. |
202
|
|
|
* |
203
|
|
|
* @param $dataitem SMWDataItem |
204
|
|
|
* @return boolean |
205
|
190 |
|
*/ |
206
|
189 |
|
public function setDataItem( SMWDataItem $dataItem ) { |
207
|
|
|
$this->getInfoLinksProvider()->init(); |
208
|
|
|
$this->m_dataitem = null; |
209
|
190 |
|
$this->mErrors = array(); |
210
|
|
|
$this->mHasErrors = $this->m_caption = false; |
|
|
|
|
211
|
|
|
return $this->loadDataItem( $dataItem ); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Specify the property to which this value refers. Property pages are |
216
|
|
|
* used to make settings that affect parsing and display, hence it is |
217
|
|
|
* sometimes needed to know them. |
218
|
|
|
* |
219
|
|
|
* @since 1.6 |
220
|
|
|
* |
221
|
|
|
* @param SMWDIProperty $property |
222
|
|
|
*/ |
223
|
|
|
public function setProperty( SMWDIProperty $property ) { |
224
|
|
|
$this->m_property = $property; |
225
|
147 |
|
} |
226
|
147 |
|
|
227
|
147 |
|
/** |
228
|
147 |
|
* Returns the property to which this value refers. |
229
|
147 |
|
* |
230
|
|
|
* @since 1.8 |
231
|
|
|
* |
232
|
|
|
* @return SMWDIProperty|null |
233
|
|
|
*/ |
234
|
|
|
public function getProperty() { |
235
|
|
|
return $this->m_property; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Specify the wiki page to which this value refers. This information is |
240
|
|
|
* used to parse user values such as "#subsection" which only make sense |
241
|
185 |
|
* when used on a certain page. |
242
|
185 |
|
* |
243
|
185 |
|
* @since 1.7 |
244
|
|
|
* |
245
|
|
|
* @param SMWDIWikiPage|null $contextPage |
246
|
|
|
*/ |
247
|
|
|
public function setContextPage( SMWDIWikiPage $contextPage = null ) { |
248
|
|
|
$this->m_contextPage = $contextPage; |
249
|
|
|
|
250
|
|
|
$this->setOption( |
251
|
|
|
'content.language', |
252
|
199 |
|
Localizer::getInstance()->getPreferredContentLanguage( $contextPage )->getCode() |
253
|
199 |
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @since 2.4 |
258
|
|
|
* |
259
|
|
|
* @return DIWikiPage|null |
260
|
|
|
*/ |
261
|
|
|
public function getContextPage() { |
262
|
|
|
return $this->m_contextPage; |
263
|
|
|
} |
264
|
|
|
|
265
|
181 |
|
/** |
266
|
181 |
|
* @since 2.4 |
267
|
|
|
* |
268
|
181 |
|
* @return Options $options |
269
|
181 |
|
*/ |
270
|
181 |
|
public function setOptions( Options $options ) { |
271
|
|
|
foreach ( $options->getOptions() as $key => $value ) { |
272
|
181 |
|
$this->setOption( $key, $value ); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @since 2.4 |
278
|
|
|
* |
279
|
174 |
|
* @return string $key |
280
|
174 |
|
* @param mxied $value |
281
|
|
|
*/ |
282
|
|
|
public function setOption( $key, $value ) { |
283
|
|
|
|
284
|
|
|
if ( $this->options === null ) { |
285
|
|
|
$this->options = new Options(); |
286
|
|
|
} |
287
|
|
|
|
288
|
203 |
|
$this->options->set( $key, $value ); |
289
|
203 |
|
} |
290
|
203 |
|
|
291
|
|
|
/** |
292
|
203 |
|
* @since 2.4 |
293
|
|
|
* |
294
|
|
|
* @param string $key |
295
|
|
|
* |
296
|
|
|
* @return mixed|false |
297
|
|
|
*/ |
298
|
|
|
public function getOptionValueFor( $key ) { |
299
|
|
|
|
300
|
203 |
|
if ( $this->options !== null && $this->options->has( $key ) ) { |
301
|
|
|
return $this->options->get( $key ); |
302
|
203 |
|
} |
303
|
203 |
|
|
304
|
|
|
return false; |
305
|
|
|
} |
306
|
203 |
|
|
307
|
203 |
|
/** |
308
|
|
|
* @since 2.4 |
309
|
|
|
* |
310
|
|
|
* @param integer $feature |
311
|
|
|
* |
312
|
|
|
* @return boolean |
313
|
|
|
*/ |
314
|
|
|
public function isEnabledFeature( $feature ) { |
315
|
|
|
return ( $this->getOptionValueFor( 'smwgDVFeatures' ) & $feature ) != 0; |
316
|
188 |
|
} |
317
|
|
|
|
318
|
188 |
|
/** |
319
|
188 |
|
* Change the caption (the text used for displaying this datavalue). The given |
320
|
|
|
* value must be a string. |
321
|
|
|
* |
322
|
61 |
|
* @param string $caption |
323
|
|
|
*/ |
324
|
|
|
public function setCaption( $caption ) { |
325
|
|
|
$this->m_caption = $caption; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @since 2.4 |
330
|
|
|
* |
331
|
|
|
* @param string $caption |
|
|
|
|
332
|
186 |
|
*/ |
333
|
186 |
|
public function getCaption() { |
334
|
|
|
return $this->m_caption; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Adds a single SMWInfolink object to the m_infolinks array. |
339
|
|
|
* |
340
|
|
|
* @param SMWInfolink $link |
341
|
|
|
*/ |
342
|
93 |
|
public function addInfolink( SMWInfolink $link ) { |
343
|
93 |
|
$this->getInfoLinksProvider()->addInfolink( $link ); |
344
|
93 |
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Define a particular output format. Output formats are user-supplied strings |
348
|
|
|
* that the datavalue may (or may not) use to customise its return value. For |
349
|
|
|
* example, quantities with units of measurement may interpret the string as |
350
|
|
|
* a desired output unit. In other cases, the output format might be built-in |
351
|
86 |
|
* and subject to internationalisation (which the datavalue has to implement). |
352
|
86 |
|
* In any case, an empty string resets the output format to the default. |
353
|
|
|
* |
354
|
|
|
* There is one predefined output format that all datavalues should respect: the |
355
|
|
|
* format '-' indicates "plain" output that is most useful for further processing |
356
|
|
|
* the value in a template. It should not use any wiki markup or beautification, |
357
|
|
|
* and it should also avoid localization to the current language. When users |
358
|
|
|
* explicitly specify an empty format string in a query, it is normalized to "-" |
359
|
|
|
* to avoid confusion. Note that empty format strings are not interpreted in |
360
|
|
|
* this way when directly passed to this function. |
361
|
|
|
* |
362
|
|
|
* @param string $formatString |
363
|
|
|
*/ |
364
|
|
|
public function setOutputFormat( $formatString ) { |
365
|
|
|
$this->m_outformat = $formatString; // just store it, subclasses may or may not use this |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* @since 2.4 |
370
|
|
|
* |
371
|
|
|
* @return string |
372
|
3 |
|
*/ |
373
|
3 |
|
public function getOutputFormat() { |
374
|
|
|
return $this->m_outformat; |
375
|
|
|
} |
376
|
|
|
|
377
|
3 |
|
/** |
378
|
3 |
|
* Add a new error string or array of such strings to the error list. |
379
|
|
|
* |
380
|
|
|
* @note Errors should not be escaped here in any way, in contradiction to what |
381
|
3 |
|
* the docs used to say here in 1.5 and before. Escaping should happen at the output. |
382
|
|
|
* |
383
|
|
|
* @param mixed $error A single string, or array of strings. |
384
|
|
|
*/ |
385
|
3 |
|
public function addError( $error ) { |
386
|
|
|
if ( is_array( $error ) ) { |
387
|
3 |
|
$this->mErrors = array_merge( $this->mErrors, $error ); |
388
|
3 |
|
$this->mHasErrors = $this->mHasErrors || ( count( $error ) > 0 ); |
389
|
|
|
} else { |
390
|
|
|
$this->mErrors[] = $error; |
391
|
1 |
|
$this->mHasErrors = true; |
392
|
1 |
|
} |
393
|
|
|
} |
394
|
1 |
|
|
395
|
|
|
/** |
396
|
|
|
* @since 2.4 |
397
|
|
|
* |
398
|
|
|
* @param $parameters |
399
|
|
|
* @param integer|null $type |
400
|
|
|
* @param integer|null $language |
401
|
|
|
*/ |
402
|
|
|
public function addErrorMsg( $parameters, $type = null, $language = null ) { |
403
|
|
|
$this->addError( Message::get( $parameters, $type, $language ) ); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @since 2.4 |
408
|
|
|
*/ |
409
|
|
|
public function clearErrors() { |
410
|
|
|
$this->mErrors = array(); |
411
|
1 |
|
$this->mHasErrors = false; |
412
|
1 |
|
} |
413
|
|
|
|
414
|
|
|
///// Abstract processing methods ///// |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Initialise the datavalue from the given value string. |
418
|
|
|
* The format of this strings might be any acceptable user input |
419
|
|
|
* and especially includes the output of getWikiValue(). |
420
|
|
|
* |
421
|
|
|
* @param string $value |
422
|
|
|
*/ |
423
|
|
|
abstract protected function parseUserValue( $value ); |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Set the actual data contained in this object. The method returns |
427
|
|
|
* true if this was successful (requiring the type of the dataitem |
428
|
|
|
* to match the data value). If false is returned, the data value is |
429
|
|
|
* left unchanged (the data item was rejected). |
430
|
|
|
* |
431
|
|
|
* @note Even if this function returns true, the data value object |
432
|
95 |
|
* might become invalid if the content of the data item caused errors |
433
|
95 |
|
* in spite of it being of the right basic type. False is only returned |
434
|
95 |
|
* if the data item is fundamentally incompatible with the data value. |
435
|
|
|
* |
436
|
|
|
* @since 1.6 |
437
|
|
|
* |
438
|
|
|
* @param SMWDataItem $dataItem |
439
|
|
|
* |
440
|
|
|
* @return boolean |
441
|
47 |
|
*/ |
442
|
47 |
|
abstract protected function loadDataItem( SMWDataItem $dataItem ); |
443
|
|
|
|
444
|
|
|
|
445
|
|
|
///// Query support ///// |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @see DataValueDescriptionDeserializer::deserialize |
449
|
|
|
* |
450
|
|
|
* @note Descriptions of values need to know their property to be able to |
451
|
|
|
* create a parsable wikitext version of a query condition again. Thus it |
452
|
|
|
* might be necessary to call setProperty() before using this method. |
453
|
117 |
|
* |
454
|
117 |
|
* @param string $value |
455
|
95 |
|
* |
456
|
95 |
|
* @return Description |
457
|
|
|
* @throws InvalidArgumentException |
458
|
32 |
|
*/ |
459
|
32 |
|
public function getQueryDescription( $value ) { |
460
|
|
|
|
461
|
117 |
|
$descriptionDeserializer = DVDescriptionDeserializerRegistry::getInstance()->getDescriptionDeserializerFor( $this ); |
462
|
|
|
$description = $descriptionDeserializer->deserialize( $value ); |
463
|
|
|
|
464
|
|
|
foreach ( $descriptionDeserializer->getErrors() as $error ) { |
465
|
|
|
$this->addError( $error ); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
return $description; |
469
|
|
|
} |
470
|
19 |
|
|
471
|
19 |
|
/** |
472
|
19 |
|
* Returns a DataValueFormatter that was matched and dispatched for the current |
473
|
|
|
* DV instance. |
474
|
|
|
* |
475
|
|
|
* @since 2.4 |
476
|
|
|
* |
477
|
|
|
* @return DataValueFormatter |
478
|
|
|
*/ |
479
|
|
|
public function getDataValueFormatter() { |
480
|
|
|
return ValueFormatterRegistry::getInstance()->getDataValueFormatterFor( $this ); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* @since 2.4 |
485
|
|
|
* |
486
|
|
|
* @return PropertySpecificationLookup |
487
|
|
|
*/ |
488
|
|
|
public function getPropertySpecificationLookup() { |
489
|
|
|
return ApplicationFactory::getInstance()->getPropertySpecificationLookup(); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @deprecated 2.3 |
494
|
|
|
* @see DescriptionDeserializer::prepareValue |
495
|
|
|
* |
496
|
|
|
* This method should no longer be used for direct public access, instead a |
497
|
|
|
* DataValue is expected to register a DescriptionDeserializer with |
498
|
|
|
* DVDescriptionDeserializerRegistry. |
499
|
|
|
* |
500
|
|
|
* FIXME as of 2.3, SMGeoCoordsValue still uses this method and requires |
501
|
|
|
* migration before 3.0 |
502
|
|
|
*/ |
503
|
|
|
static public function prepareValue( &$value, &$comparator ) { |
|
|
|
|
504
|
|
|
$comparator = QueryComparator::getInstance()->extractComparatorFromString( $value ); |
505
|
|
|
} |
506
|
|
|
|
507
|
|
|
///// Get methods ///// |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* Get the actual data contained in this object or null if the data is |
511
|
|
|
* not defined (due to errors or due to not being set at all). |
512
|
|
|
* @note Most implementations ensure that a data item is always set, |
513
|
|
|
* even if errors occurred, to avoid additional checks for not |
514
|
|
|
* accessing null. Hence, one must not assume that a non-null return |
515
|
|
|
* value here implies that isValid() returns true. |
516
|
|
|
* |
517
|
|
|
* @since 1.6 |
518
|
|
|
* |
519
|
|
|
* @return SMWDataItem|SMWDIError |
520
|
|
|
*/ |
521
|
|
|
public function getDataItem() { |
522
|
|
|
|
523
|
|
|
if ( $this->isValid() ) { |
524
|
|
|
return $this->m_dataitem; |
525
|
|
|
} |
526
|
|
|
|
527
|
94 |
|
return new SMWDIError( $this->mErrors ); |
528
|
|
|
} |
529
|
94 |
|
|
530
|
94 |
|
/** |
531
|
|
|
* @since 2.2 |
532
|
94 |
|
* |
533
|
1 |
|
* @return string |
534
|
|
|
*/ |
535
|
|
|
public function __toString() { |
536
|
94 |
|
return $this->getDataItem()->getSerialization(); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Returns a short textual representation for this data value. If the value |
541
|
|
|
* was initialised from a user supplied string, then this original string |
542
|
|
|
* should be reflected in this short version (i.e. no normalisation should |
543
|
|
|
* normally happen). There might, however, be additional parts such as code |
544
|
|
|
* for generating tooltips. The output is in wiki text. |
545
|
|
|
* |
546
|
|
|
* The parameter $linked controls linking of values such as titles and should |
547
|
116 |
|
* be non-NULL and non-false if this is desired. |
548
|
116 |
|
*/ |
549
|
|
|
abstract public function getShortWikiText( $linked = null ); |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Returns a short textual representation for this data value. If the value |
553
|
|
|
* was initialised from a user supplied string, then this original string |
554
|
|
|
* should be reflected in this short version (i.e. no normalisation should |
555
|
|
|
* normally happen). There might, however, be additional parts such as code |
556
|
175 |
|
* for generating tooltips. The output is in HTML text. |
557
|
175 |
|
* |
558
|
|
|
* The parameter $linker controls linking of values such as titles and should |
559
|
|
|
* be some Linker object (or NULL for no linking). |
560
|
|
|
*/ |
561
|
|
|
abstract public function getShortHTMLText( $linker = null ); |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* Return the long textual description of the value, as printed for |
565
|
|
|
* example in the factbox. If errors occurred, return the error message |
566
|
|
|
* The result always is a wiki-source string. |
567
|
|
|
* |
568
|
|
|
* The parameter $linked controls linking of values such as titles and should |
569
|
|
|
* be non-NULL and non-false if this is desired. |
570
|
|
|
*/ |
571
|
|
|
abstract public function getLongWikiText( $linked = null ); |
572
|
|
|
|
573
|
|
|
/** |
574
|
|
|
* Return the long textual description of the value, as printed for |
575
|
|
|
* example in the factbox. If errors occurred, return the error message |
576
|
|
|
* The result always is an HTML string. |
577
|
|
|
* |
578
|
|
|
* The parameter $linker controls linking of values such as titles and should |
579
|
|
|
* be some Linker object (or NULL for no linking). |
580
|
|
|
*/ |
581
|
|
|
abstract public function getLongHTMLText( $linker = null ); |
582
|
|
|
|
583
|
|
|
/** |
584
|
|
|
* Returns a short textual representation for this data value. If the value |
585
|
|
|
* was initialised from a user supplied string, then this original string |
586
|
|
|
* should be reflected in this short version (i.e. no normalisation should |
587
|
|
|
* normally happen). There might, however, be additional parts such as code |
588
|
|
|
* for generating tooltips. The output is in the specified format. |
589
|
192 |
|
* |
590
|
|
|
* The parameter $linker controls linking of values such as titles and should |
591
|
192 |
|
* be some Linker object (for HTML output), or NULL for no linking. |
592
|
192 |
|
*/ |
593
|
|
|
public function getShortText( $outputformat, $linker = null ) { |
594
|
|
|
switch ( $outputformat ) { |
595
|
1 |
|
case SMW_OUTPUT_WIKI: |
596
|
|
|
return $this->getShortWikiText( $linker ); |
597
|
|
|
case SMW_OUTPUT_HTML: |
598
|
|
|
case SMW_OUTPUT_FILE: |
599
|
|
|
default: |
600
|
|
|
return $this->getShortHTMLText( $linker ); |
601
|
|
|
} |
602
|
|
|
} |
603
|
|
|
|
604
|
|
|
/** |
605
|
|
|
* Return the long textual description of the value, as printed for |
606
|
|
|
* example in the factbox. If errors occurred, return the error message. |
607
|
|
|
* The output is in the specified format. |
608
|
|
|
* |
609
|
|
|
* The parameter $linker controls linking of values such as titles and should |
610
|
|
|
* be some Linker object (for HTML output), or NULL for no linking. |
611
|
|
|
*/ |
612
|
|
|
public function getLongText( $outputformat, $linker = null ) { |
613
|
|
|
switch ( $outputformat ) { |
614
|
|
|
case SMW_OUTPUT_WIKI: |
615
|
|
|
return $this->getLongWikiText( $linker ); |
616
|
|
|
case SMW_OUTPUT_HTML: |
617
|
|
|
case SMW_OUTPUT_FILE: |
618
|
|
|
default: |
619
|
|
|
return $this->getLongHTMLText( $linker ); |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* Return text serialisation of info links. Ensures more uniform layout |
625
|
|
|
* throughout wiki (Factbox, Property pages, ...). |
626
|
|
|
* |
627
|
|
|
* @param integer $outputformat Element of the SMW_OUTPUT_ enum |
628
|
|
|
* @param $linker |
629
|
|
|
* |
630
|
|
|
* @return string |
631
|
|
|
*/ |
632
|
|
|
public function getInfolinkText( $outputformat, $linker = null ) { |
633
|
|
|
return $this->getInfoLinksProvider()->getInfolinkText( $outputformat, $linker ); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Return the plain wiki version of the value, or |
638
|
|
|
* FALSE if no such version is available. The returned |
639
|
|
|
* string suffices to reobtain the same DataValue |
640
|
|
|
* when passing it as an input string to setUserValue(). |
641
|
|
|
*/ |
642
|
|
|
abstract public function getWikiValue(); |
643
|
|
|
|
644
|
|
|
/** |
645
|
|
|
* Return a short string that unambiguously specify the type of this |
646
|
|
|
* value. This value will globally be used to identify the type of a |
647
|
|
|
* value (in spite of the class it actually belongs to, which can still |
648
|
|
|
* implement various types). |
649
|
|
|
*/ |
650
|
|
|
public function getTypeID() { |
651
|
|
|
return $this->m_typeid; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
/** |
655
|
|
|
* @since 2.1 |
656
|
|
|
*/ |
657
|
|
|
public function disableServiceLinks() { |
658
|
|
|
$this->getInfoLinksProvider()->disableServiceLinks(); |
659
|
|
|
} |
660
|
|
|
|
661
|
48 |
|
/** |
662
|
|
|
* Return an array of SMWLink objects that provide additional resources |
663
|
48 |
|
* for the given value. Captions can contain some HTML markup which is |
664
|
48 |
|
* admissible for wiki text, but no more. Result might have no entries |
665
|
|
|
* but is always an array. |
666
|
|
|
*/ |
667
|
|
|
public function getInfolinks() { |
668
|
|
|
|
669
|
|
|
$this->getInfoLinksProvider()->setServiceLinkParameters( |
670
|
|
|
$this->getServiceLinkParams() |
|
|
|
|
671
|
|
|
); |
672
|
|
|
|
673
|
|
|
return $this->getInfoLinksProvider()->createInfoLinks(); |
674
|
|
|
} |
675
|
|
|
|
676
|
|
|
/** |
677
|
|
|
* Overwritten by callers to supply an array of parameters that can be used for |
678
|
|
|
* creating servicelinks. The number and content of values in the parameter array |
679
|
|
|
* may vary, depending on the concrete datatype. |
680
|
|
|
*/ |
681
|
|
|
protected function getServiceLinkParams() { |
682
|
|
|
return false; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
/** |
686
|
|
|
* Return a string that identifies the value of the object, and that can |
687
|
|
|
* be used to compare different value objects. |
688
|
|
|
* Possibly overwritten by subclasses (e.g. to ensure that returned |
689
|
|
|
* value is normalized first) |
690
|
|
|
* |
691
|
|
|
* @return string |
692
|
|
|
*/ |
693
|
|
|
public function getHash() { |
694
|
|
|
return $this->isValid() ? $this->m_dataitem->getHash() : implode( "\t", $this->mErrors ); |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
/** |
698
|
|
|
* Convenience method that checks if the value that is used to sort |
699
|
|
|
* data of this type is numeric. This only works if the value is set. |
700
|
4 |
|
* |
701
|
4 |
|
* @return boolean |
702
|
4 |
|
*/ |
703
|
4 |
|
public function isNumeric() { |
704
|
|
|
if ( isset( $this->m_dataitem ) ) { |
705
|
|
|
return is_numeric( $this->m_dataitem->getSortKey() ); |
706
|
4 |
|
} else { |
707
|
1 |
|
return false; |
708
|
1 |
|
} |
709
|
1 |
|
} |
710
|
|
|
|
711
|
1 |
|
/** |
712
|
|
|
* Return true if a value was defined and understood by the given type, |
713
|
1 |
|
* and false if parsing errors occurred or no value was given. |
714
|
|
|
* |
715
|
|
|
* @return boolean |
716
|
1 |
|
*/ |
717
|
|
|
public function isValid() { |
718
|
3 |
|
return !$this->mHasErrors && isset( $this->m_dataitem ); |
719
|
3 |
|
} |
720
|
3 |
|
|
721
|
3 |
|
/** |
722
|
3 |
|
* Whether a datavalue can be used or not (can be made more restrictive then |
723
|
|
|
* isValid). |
724
|
3 |
|
* |
725
|
|
|
* @note Validity defines a processable state without any technical restrictions |
726
|
|
|
* while usability is determined by its accessibility to a context |
727
|
3 |
|
* (permission, convention etc.) |
728
|
|
|
* |
729
|
|
|
* @since 2.2 |
730
|
4 |
|
* |
731
|
|
|
* @return boolean |
732
|
|
|
*/ |
733
|
|
|
public function canUse() { |
734
|
|
|
return true; |
735
|
4 |
|
} |
736
|
|
|
|
737
|
|
|
/** |
738
|
|
|
* @note Normally set by the DataValueFactory, or during tests |
739
|
|
|
* |
740
|
|
|
* @since 2.3 |
741
|
|
|
* |
742
|
|
|
* @param array |
743
|
|
|
*/ |
744
|
|
|
public function setExtraneousFunctions( array $extraneousFunctions ) { |
745
|
|
|
$this->extraneousFunctions = $extraneousFunctions; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* @since 2.3 |
750
|
|
|
* |
751
|
|
|
* @param string $name |
752
|
129 |
|
* @param array $parameters |
753
|
129 |
|
* |
754
|
|
|
* @return mixed |
755
|
|
|
* @throws RuntimeException |
756
|
|
|
*/ |
757
|
|
|
public function getExtraneousFunctionFor( $name, array $parameters = array() ) { |
758
|
|
|
|
759
|
|
|
if ( isset( $this->extraneousFunctions[$name] ) && is_callable( $this->extraneousFunctions[$name] ) ) { |
760
|
1 |
|
return call_user_func_array( $this->extraneousFunctions[$name], $parameters ); |
761
|
1 |
|
} |
762
|
1 |
|
|
763
|
|
|
throw new RuntimeException( "$name is not registered as extraneous function." ); |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* Return a string that displays all error messages as a tooltip, or |
768
|
|
|
* an empty string if no errors happened. |
769
|
|
|
* |
770
|
4 |
|
* @return string |
771
|
4 |
|
*/ |
772
|
4 |
|
public function getErrorText() { |
773
|
4 |
|
return smwfEncodeMessages( $this->mErrors ); |
774
|
4 |
|
} |
775
|
4 |
|
|
776
|
|
|
/** |
777
|
|
|
* Return an array of error messages, or an empty array |
778
|
4 |
|
* if no errors occurred. |
779
|
3 |
|
* |
780
|
|
|
* @return array |
781
|
|
|
*/ |
782
|
|
|
public function getErrors() { |
783
|
4 |
|
return $this->mErrors; |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Check if property is range restricted and, if so, whether the current value is allowed. |
788
|
|
|
* Creates an error if the value is illegal. |
789
|
|
|
*/ |
790
|
|
|
protected function checkAllowedValues() { |
791
|
3 |
|
ValueValidatorRegistry::getInstance()->getConstraintValueValidator()->validate( $this ); |
|
|
|
|
792
|
3 |
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* @since 2.4 |
796
|
|
|
* |
797
|
|
|
* @param string $value |
798
|
|
|
* |
799
|
|
|
* @return string |
800
|
|
|
*/ |
801
|
|
|
protected function convertDoubleWidth( $value ) { |
802
|
|
|
return Localizer::convertDoubleWidth( $value ); |
803
|
|
|
} |
804
|
|
|
|
805
|
|
|
private function getInfoLinksProvider() { |
806
|
|
|
|
807
|
|
|
if ( $this->infoLinksProvider === null ) { |
808
|
|
|
$this->infoLinksProvider = new InfoLinksProvider( $this ); |
809
|
|
|
} |
810
|
|
|
|
811
|
|
|
return $this->infoLinksProvider; |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
} |
815
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.