1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use SMW\DataValueFactory; |
4
|
|
|
use SMW\ApplicationFactory; |
5
|
|
|
use SMW\Highlighter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Objects of this class represent properties in SMW. |
9
|
|
|
* |
10
|
|
|
* This class represents both normal (user-defined) properties and |
11
|
|
|
* predefined ("special") properties. Predefined properties may still |
12
|
|
|
* have a standard label (and associated wiki article) and they will |
13
|
|
|
* behave just like user-defined properties in most cases (e.g. when |
14
|
|
|
* asking for a printout text, a link to the according page is produced). |
15
|
|
|
* It is possible that predefined properties have no visible label at all, |
16
|
|
|
* if they are used only internally and never specified by or shown to |
17
|
|
|
* the user. Those will use their internal ID as DB key, and |
18
|
|
|
* empty texts for most printouts. All other proeprties use their |
19
|
|
|
* canonical DB key (even if they are predefined and have an id). |
20
|
|
|
* Functions are provided to check whether a property is visible or |
21
|
|
|
* user-defined, and to get the internal ID, if any. |
22
|
|
|
* |
23
|
|
|
* @note This datavalue is used only for representing properties and, |
24
|
|
|
* possibly objects/values, but never for subjects (pages as such). Hence |
25
|
|
|
* it does not provide a complete Title-like interface, or support for |
26
|
|
|
* things like sortkey. |
27
|
|
|
* |
28
|
|
|
* @author Markus Krötzsch |
29
|
|
|
* @ingroup SMWDataValues |
30
|
|
|
*/ |
31
|
|
|
class SMWPropertyValue extends SMWDataValue { |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Cache for wiki page value object associated to this property, or |
35
|
|
|
* null if no such page exists. Use getWikiPageValue() to get the data. |
36
|
|
|
* @var SMWWikiPageValue |
37
|
|
|
*/ |
38
|
|
|
protected $m_wikipage = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Cache for type value of this property, or null if not calculated yet. |
42
|
|
|
* @var SMWTypesValue |
43
|
|
|
*/ |
44
|
|
|
private $mPropTypeValue; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var DIProperty |
48
|
|
|
*/ |
49
|
|
|
private $inceptiveProperty = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @since 2.4 |
53
|
|
|
* |
54
|
169 |
|
* @param string $typeid |
55
|
169 |
|
*/ |
56
|
169 |
|
public function __construct( $typeid ) { |
57
|
|
|
parent::__construct( $typeid ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @since 2.4 |
62
|
|
|
* |
63
|
151 |
|
* @return boolean |
64
|
151 |
|
*/ |
65
|
|
|
public function isToFindPropertyRedirect() { |
66
|
|
|
return ( $this->getOptionValueFor( 'smwgDVFeatures' ) & SMW_DV_PROV_REDI ) != 0; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Static function for creating a new property object from a |
71
|
|
|
* propertyname (string) as a user might enter it. |
72
|
|
|
* @note The resulting property object might be invalid if |
73
|
|
|
* the provided name is not allowed. An object is returned |
74
|
|
|
* in any case. |
75
|
|
|
* |
76
|
|
|
* @param string $propertyName |
|
|
|
|
77
|
|
|
* |
78
|
90 |
|
* @return SMWPropertyValue |
79
|
90 |
|
*/ |
80
|
|
|
static public function makeUserProperty( $propertyLabel ) { |
|
|
|
|
81
|
|
|
return DataValueFactory::getInstance()->newPropertyValueByLabel( $propertyLabel ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Static function for creating a new property object from a property |
86
|
|
|
* identifier (string) as it might be used internally. This might be |
87
|
|
|
* the DB key version of some property title text or the id of a |
88
|
|
|
* predefined property (such as '_TYPE'). |
89
|
|
|
* @note This function strictly requires an internal identifier, i.e. |
90
|
|
|
* predefined properties must be referred to by their ID, and '-' is |
91
|
|
|
* not supported for indicating inverses. |
92
|
|
|
* @note The resulting property object might be invalid if |
93
|
|
|
* the provided name is not allowed. An object is returned |
94
|
|
|
* in any case. |
95
|
|
|
*/ |
96
|
|
|
static public function makeProperty( $propertyid ) { |
|
|
|
|
97
|
|
|
$diProperty = new SMWDIProperty( $propertyid ); |
98
|
|
|
$dvProperty = new SMWPropertyValue( '__pro' ); |
99
|
|
|
$dvProperty->setDataItem( $diProperty ); |
100
|
|
|
return $dvProperty; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* We use the internal wikipage object to store some of this objects data. |
105
|
|
|
* Clone it to make sure that data can be modified independently from the |
106
|
|
|
* original object's content. |
107
|
|
|
*/ |
108
|
|
|
public function __clone() { |
109
|
|
|
if ( !is_null( $this->m_wikipage ) ) { |
110
|
|
|
$this->m_wikipage = clone $this->m_wikipage; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @note If the inceptive property and the property referenced in dataItem |
116
|
|
|
* are not equal then the dataItem represents the end target to which the |
117
|
|
|
* inceptive property has been redirected. |
118
|
|
|
* |
119
|
|
|
* @since 2.4 |
120
|
|
|
* |
121
|
|
|
* @return DIProperty |
122
|
|
|
*/ |
123
|
|
|
public function getInceptiveProperty() { |
124
|
|
|
return $this->inceptiveProperty; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Extended parsing function to first check whether value refers to pre-defined |
129
|
|
|
* property, resolve aliases, and set internal property id accordingly. |
130
|
147 |
|
* @todo Accept/enforce property namespace. |
131
|
147 |
|
*/ |
132
|
147 |
|
protected function parseUserValue( $value ) { |
133
|
|
|
$this->mPropTypeValue = null; |
134
|
147 |
|
$this->m_wikipage = null; |
135
|
147 |
|
|
136
|
147 |
|
if ( $this->m_caption === false ) { // always use this as caption |
137
|
147 |
|
$this->m_caption = $value; |
138
|
147 |
|
} |
139
|
147 |
|
$propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) ); // slightly normalise label |
140
|
8 |
|
$inverse = false; |
141
|
|
|
if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse |
142
|
|
|
$propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) ); |
143
|
8 |
|
/// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false. |
144
|
8 |
|
/// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter. |
145
|
|
|
$inverse = true; |
146
|
|
|
} |
147
|
147 |
|
|
148
|
147 |
|
try { |
149
|
|
|
$this->m_dataitem = SMWDIProperty::newFromUserLabel( $propertyName, $inverse, $this->m_typeid ); |
|
|
|
|
150
|
|
|
} catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-" |
151
|
|
|
$this->addError( wfMessage( 'smw_noproperty', $value )->inContentLanguage()->text() ); |
152
|
|
|
$this->m_dataitem = new SMWDIProperty( 'ERROR', false ); // just to have something |
153
|
147 |
|
} |
154
|
|
|
|
155
|
147 |
|
$this->inceptiveProperty = $this->m_dataitem; |
|
|
|
|
156
|
147 |
|
|
157
|
147 |
|
if ( $this->isToFindPropertyRedirect() ) { |
158
|
147 |
|
$this->m_dataitem = $this->m_dataitem->getRedirectTarget(); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @see SMWDataValue::loadDataItem() |
164
|
|
|
* @param $dataitem SMWDataItem |
165
|
26 |
|
* @return boolean |
166
|
|
|
*/ |
167
|
26 |
|
protected function loadDataItem( SMWDataItem $dataItem ) { |
168
|
|
|
|
169
|
|
|
if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) { |
170
|
|
|
return false; |
171
|
26 |
|
} |
172
|
26 |
|
|
173
|
|
|
$this->inceptiveProperty = $dataItem; |
|
|
|
|
174
|
26 |
|
$this->m_dataitem = $dataItem; |
175
|
26 |
|
|
176
|
26 |
|
$this->mPropTypeValue = null; |
177
|
|
|
unset( $this->m_wikipage ); |
178
|
26 |
|
$this->m_caption = false; |
|
|
|
|
179
|
|
|
|
180
|
|
|
return true; |
181
|
69 |
|
} |
182
|
69 |
|
|
183
|
69 |
|
public function setCaption( $caption ) { |
184
|
69 |
|
parent::setCaption( $caption ); |
185
|
69 |
|
if ( $this->getWikiPageValue() instanceof SMWDataValue ) { // pass caption to embedded datavalue (used for printout) |
186
|
69 |
|
$this->m_wikipage->setCaption( $caption ); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function setOutputFormat( $formatstring ) { |
191
|
|
|
$this->m_outformat = $formatstring; |
192
|
|
|
if ( $this->m_wikipage instanceof SMWDataValue ) { |
193
|
|
|
$this->m_wikipage->setOutputFormat( $formatstring ); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setInverse( $isinverse ) { |
198
|
|
|
return $this->m_dataitem = new SMWDIProperty( $this->m_dataitem->getKey(), ( $isinverse == true ) ); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Return a wiki page value that can be used for displaying this |
203
|
|
|
* property, or null if no such wiki page exists (for predefined |
204
|
|
|
* properties without any label). |
205
|
72 |
|
* @return SMWWikiPageValue or null |
206
|
72 |
|
*/ |
207
|
|
|
public function getWikiPageValue() { |
208
|
72 |
|
if ( !isset( $this->m_wikipage ) ) { |
209
|
|
|
|
210
|
72 |
|
$diWikiPage = $this->m_dataitem->getDiWikiPage(); |
|
|
|
|
211
|
72 |
|
|
212
|
72 |
|
if ( $diWikiPage !== null ) { |
213
|
72 |
|
$this->m_wikipage = \SMW\DataValueFactory::getInstance()->newDataItemValue( $diWikiPage, null, $this->m_caption ); |
|
|
|
|
214
|
72 |
|
$this->m_wikipage->setOutputFormat( $this->m_outformat ); |
215
|
|
|
$this->addError( $this->m_wikipage->getErrors() ); |
216
|
|
|
} else { // should rarely happen ($value is only changed if the input $value really was a label for a predefined prop) |
217
|
72 |
|
$this->m_wikipage = null; |
218
|
72 |
|
} |
219
|
|
|
} |
220
|
|
|
return $this->m_wikipage; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Return TRUE if this is a property that can be displayed, and not a pre-defined |
225
|
|
|
* property that is used only internally and does not even have a user-readable name. |
226
|
39 |
|
* @note Every user defined property is necessarily visible. |
227
|
39 |
|
*/ |
228
|
|
|
public function isVisible() { |
229
|
|
|
return $this->isValid() && ( $this->m_dataitem->isUserDefined() || $this->m_dataitem->getLabel() !== '' ); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @since 2.2 |
234
|
|
|
* |
235
|
131 |
|
* @return boolean |
236
|
131 |
|
*/ |
237
|
|
|
public function canUse() { |
238
|
|
|
return $this->isValid() && $this->m_dataitem->isUnrestrictedForUse(); |
|
|
|
|
239
|
16 |
|
} |
240
|
|
|
|
241
|
16 |
|
public function getShortWikiText( $linked = null ) { |
242
|
16 |
|
|
243
|
16 |
|
if ( $this->isVisible() ) { |
244
|
|
|
$wikiPageValue = $this->getWikiPageValue(); |
245
|
|
|
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortWikiText( $linked ) ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return ''; |
249
|
1 |
|
} |
250
|
|
|
|
251
|
1 |
|
public function getShortHTMLText( $linked = null ) { |
252
|
1 |
|
|
253
|
1 |
|
if ( $this->isVisible() ) { |
254
|
|
|
$wikiPageValue = $this->getWikiPageValue(); |
255
|
|
|
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getShortHTMLText( $linked ), $linked ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return ''; |
259
|
8 |
|
} |
260
|
|
|
|
261
|
8 |
|
public function getLongWikiText( $linked = null ) { |
262
|
8 |
|
|
263
|
8 |
|
if ( $this->isVisible() ) { |
264
|
|
|
$wikiPageValue = $this->getWikiPageValue(); |
265
|
|
|
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongWikiText( $linked ) ); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return ''; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function getLongHTMLText( $linked = null ) { |
272
|
|
|
|
273
|
|
|
if ( $this->isVisible() ) { |
274
|
|
|
$wikiPageValue = $this->getWikiPageValue(); |
275
|
|
|
return is_null( $wikiPageValue ) ? '' : $this->highlightText( $wikiPageValue->getLongHTMLText( $linked ), $linked ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return ''; |
279
|
34 |
|
} |
280
|
34 |
|
|
281
|
|
|
public function getWikiValue() { |
282
|
|
|
return $this->isVisible() ? $this->m_dataitem->getLabel() : ''; |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* If this property was not user defined, return the internal ID string referring to |
287
|
|
|
* that property. Otherwise return FALSE; |
288
|
|
|
*/ |
289
|
|
|
public function getPropertyID() { |
290
|
|
|
return $this->m_dataitem->isUserDefined() ? false : $this->m_dataitem->getKey(); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Return an SMWTypesValue object representing the datatype of this |
295
|
|
|
* property. |
296
|
|
|
* @deprecated Types values are not a good way to exchange SMW type information. They are for input only. Use getPropertyTypeID() if you want the type id. This method will vanish in SMW 1.7. |
297
|
|
|
*/ |
298
|
|
|
public function getTypesValue() { |
299
|
|
|
$result = SMWTypesValue::newFromTypeId( $this->getPropertyTypeID() ); |
300
|
|
|
if ( !$this->isValid() ) { |
301
|
|
|
$result->addError( $this->getErrors() ); |
302
|
|
|
} |
303
|
|
|
return $result; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Convenience method to find the type id of this property. Most callers |
308
|
|
|
* should rather use SMWDIProperty::findPropertyTypeId() directly. Note |
309
|
|
|
* that this is not the same as getTypeID(), which returns the id of |
310
|
|
|
* this property datavalue. |
311
|
|
|
* |
312
|
4 |
|
* @return string |
313
|
4 |
|
*/ |
314
|
4 |
|
public function getPropertyTypeID() { |
315
|
|
|
if ( $this->isValid() ) { |
316
|
|
|
return $this->m_dataitem->findPropertyTypeId(); |
|
|
|
|
317
|
|
|
} else { |
318
|
|
|
return '__err'; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
22 |
|
* Create special highlighting for hinting at special properties. |
324
|
|
|
*/ |
325
|
22 |
|
protected function highlightText( $text, $linker = null ) { |
326
|
|
|
|
327
|
4 |
|
$propertySpecificationLookup = ApplicationFactory::getInstance()->getPropertySpecificationLookup(); |
328
|
4 |
|
|
329
|
|
|
if ( ( $content = $propertySpecificationLookup->getPropertyDescriptionFor( $this->m_dataitem, $linker ) ) !== '' ) { |
|
|
|
|
330
|
4 |
|
|
331
|
4 |
|
$highlighter = SMW\Highlighter::factory( SMW\Highlighter::TYPE_PROPERTY ); |
332
|
4 |
|
$highlighter->setContent( array ( |
333
|
|
|
'userDefined' => $this->m_dataitem->isUserDefined(), |
|
|
|
|
334
|
4 |
|
'caption' => $text, |
335
|
4 |
|
'content' => $content !== '' ? $content : wfMessage( 'smw_isspecprop' )->text() |
336
|
4 |
|
) ); |
337
|
4 |
|
|
338
|
4 |
|
return $highlighter->getHtml(); |
339
|
|
|
} |
340
|
4 |
|
|
341
|
|
|
return $text; |
342
|
|
|
} |
343
|
19 |
|
|
344
|
|
|
/** |
345
|
|
|
* A function for registering/overwriting predefined properties for SMW. Should be called from |
346
|
|
|
* within the hook 'smwInitProperties'. Ids should start with three underscores "___" to avoid |
347
|
|
|
* current and future confusion with SMW built-ins. |
348
|
|
|
* |
349
|
|
|
* @deprecated Use SMWDIProperty::registerProperty(). Will vanish before SMW 1.7. |
350
|
|
|
*/ |
351
|
|
|
static public function registerProperty( $id, $typeid, $label = false, $show = false ) { |
|
|
|
|
352
|
|
|
SMWDIProperty::registerProperty( $id, $typeid, $label, $show ); |
|
|
|
|
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Add a new alias label to an existing datatype id. Note that every ID should have a primary |
357
|
|
|
* label, either provided by SMW or registered with registerDatatype. This function should be |
358
|
|
|
* called from within the hook 'smwInitDatatypes'. |
359
|
|
|
* |
360
|
|
|
* @deprecated Use SMWDIProperty::registerPropertyAlias(). Will vanish before SMW 1.7. |
361
|
|
|
*/ |
362
|
|
|
static public function registerPropertyAlias( $id, $label ) { |
|
|
|
|
363
|
|
|
SMWDIProperty::registerPropertyAlias( $id, $label ); |
|
|
|
|
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* @see SMWDIProperty::isUserDefined() |
368
|
|
|
* |
369
|
|
|
* @deprecated since 1.6 |
370
|
|
|
*/ |
371
|
|
|
public function isUserDefined() { |
372
|
|
|
return $this->m_dataitem->isUserDefined(); |
|
|
|
|
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @see SMWDIProperty::isShown() |
377
|
|
|
* |
378
|
|
|
* @deprecated since 1.6 |
379
|
|
|
*/ |
380
|
|
|
public function isShown() { |
381
|
|
|
return $this->m_dataitem->isShown(); |
|
|
|
|
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @see SMWDIProperty::isInverse() |
386
|
|
|
* |
387
|
|
|
* @deprecated since 1.6 |
388
|
|
|
*/ |
389
|
|
|
public function isInverse() { |
390
|
|
|
return $this->m_dataitem->isInverse(); |
|
|
|
|
391
|
79 |
|
} |
392
|
79 |
|
|
393
|
|
|
/** |
394
|
|
|
* Return a DB-key-like string: for visible properties, it is the actual DB key, |
395
|
|
|
* for internal (invisible) properties, it is the property ID. The value agrees |
396
|
|
|
* with the first component of getDBkeys() and it can be used in its place. |
397
|
|
|
* @see SMWDIProperty::getKey() |
398
|
|
|
* |
399
|
|
|
* @deprecated since 1.6 |
400
|
|
|
*/ |
401
|
|
|
public function getDBkey() { |
402
|
|
|
return $this->m_dataitem->getKey(); |
|
|
|
|
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* @see SMWDIProperty::getLabel() |
407
|
|
|
* |
408
|
|
|
* @deprecated since 1.6 |
409
|
|
|
*/ |
410
|
|
|
public function getText() { |
411
|
|
|
return $this->m_dataitem->getLabel(); |
|
|
|
|
412
|
4 |
|
} |
413
|
4 |
|
|
414
|
|
|
} |
415
|
|
|
|
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.