These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | use SMW\ApplicationFactory; |
||
4 | use SMW\DataValues\ValueFormatters\DataValueFormatter; |
||
5 | use SMW\DataValueFactory; |
||
6 | use SMW\DIProperty; |
||
7 | use SMW\Highlighter; |
||
8 | use SMW\Message; |
||
9 | |||
10 | /** |
||
11 | * Objects of this class represent properties in SMW. |
||
12 | * |
||
13 | * This class represents both normal (user-defined) properties and |
||
14 | * predefined ("special") properties. Predefined properties may still |
||
15 | * have a standard label (and associated wiki article) and they will |
||
16 | * behave just like user-defined properties in most cases (e.g. when |
||
17 | * asking for a printout text, a link to the according page is produced). |
||
18 | * It is possible that predefined properties have no visible label at all, |
||
19 | * if they are used only internally and never specified by or shown to |
||
20 | * the user. Those will use their internal ID as DB key, and |
||
21 | * empty texts for most printouts. All other proeprties use their |
||
22 | * canonical DB key (even if they are predefined and have an id). |
||
23 | * Functions are provided to check whether a property is visible or |
||
24 | * user-defined, and to get the internal ID, if any. |
||
25 | * |
||
26 | * @note This datavalue is used only for representing properties and, |
||
27 | * possibly objects/values, but never for subjects (pages as such). Hence |
||
28 | * it does not provide a complete Title-like interface, or support for |
||
29 | * things like sortkey. |
||
30 | * |
||
31 | * @author Markus Krötzsch |
||
32 | * @ingroup SMWDataValues |
||
33 | */ |
||
34 | class SMWPropertyValue extends SMWDataValue { |
||
35 | |||
36 | /** |
||
37 | * Avoid the display of a tooltip |
||
38 | */ |
||
39 | const OPT_NO_HIGHLIGHT = 'no.highlight'; |
||
40 | |||
41 | /** |
||
42 | * Cache for wiki page value object associated to this property, or |
||
43 | * null if no such page exists. Use getWikiPageValue() to get the data. |
||
44 | * @var SMWWikiPageValue |
||
45 | */ |
||
46 | protected $m_wikipage = null; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $linkAttributes = array(); |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $preferredLabel = ''; |
||
57 | |||
58 | /** |
||
59 | * Cache for type value of this property, or null if not calculated yet. |
||
60 | * @var SMWTypesValue |
||
61 | */ |
||
62 | private $mPropTypeValue; |
||
63 | |||
64 | /** |
||
65 | * @var DIProperty |
||
66 | */ |
||
67 | private $inceptiveProperty = null; |
||
68 | |||
69 | /** |
||
70 | * @since 2.4 |
||
71 | * |
||
72 | * @param string $typeid |
||
73 | */ |
||
74 | 227 | public function __construct( $typeid = '__pro' ) { |
|
75 | 227 | parent::__construct( $typeid ); |
|
76 | 227 | } |
|
77 | |||
78 | /** |
||
79 | * Static function for creating a new property object from a |
||
80 | * propertyname (string) as a user might enter it. |
||
81 | * @note The resulting property object might be invalid if |
||
82 | * the provided name is not allowed. An object is returned |
||
83 | * in any case. |
||
84 | * |
||
85 | * @param string $propertyName |
||
86 | * |
||
87 | * @return SMWPropertyValue |
||
88 | */ |
||
89 | 137 | static public function makeUserProperty( $propertyLabel ) { |
|
90 | 137 | return DataValueFactory::getInstance()->newPropertyValueByLabel( $propertyLabel ); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Static function for creating a new property object from a property |
||
95 | * identifier (string) as it might be used internally. This might be |
||
96 | * the DB key version of some property title text or the id of a |
||
97 | * predefined property (such as '_TYPE'). |
||
98 | * @note This function strictly requires an internal identifier, i.e. |
||
99 | * predefined properties must be referred to by their ID, and '-' is |
||
100 | * not supported for indicating inverses. |
||
101 | * @note The resulting property object might be invalid if |
||
102 | * the provided name is not allowed. An object is returned |
||
103 | * in any case. |
||
104 | */ |
||
105 | static public function makeProperty( $propertyid ) { |
||
106 | $diProperty = new DIProperty( $propertyid ); |
||
107 | $dvProperty = new SMWPropertyValue( '__pro' ); |
||
108 | $dvProperty->setDataItem( $diProperty ); |
||
109 | return $dvProperty; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * We use the internal wikipage object to store some of this objects data. |
||
114 | * Clone it to make sure that data can be modified independently from the |
||
115 | * original object's content. |
||
116 | */ |
||
117 | 1 | public function __clone() { |
|
118 | 1 | if ( !is_null( $this->m_wikipage ) ) { |
|
119 | $this->m_wikipage = clone $this->m_wikipage; |
||
120 | } |
||
121 | 1 | } |
|
122 | |||
123 | /** |
||
124 | * @note If the inceptive property and the property referenced in dataItem |
||
125 | * are not equal then the dataItem represents the end target to which the |
||
126 | * inceptive property has been redirected. |
||
127 | * |
||
128 | * @since 2.4 |
||
129 | * |
||
130 | * @return DIProperty |
||
131 | */ |
||
132 | 1 | public function getInceptiveProperty() { |
|
133 | 1 | return $this->inceptiveProperty; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Extended parsing function to first check whether value refers to pre-defined |
||
138 | * property, resolve aliases, and set internal property id accordingly. |
||
139 | * @todo Accept/enforce property namespace. |
||
140 | */ |
||
141 | 208 | protected function parseUserValue( $value ) { |
|
142 | 208 | $this->mPropTypeValue = null; |
|
143 | 208 | $this->m_wikipage = null; |
|
144 | |||
145 | 208 | list( $propertyName, $inverse ) = $this->doNormalizeUserValue( |
|
146 | $value |
||
147 | ); |
||
148 | |||
149 | 208 | $contentLanguage = $this->getOptionBy( self::OPT_CONTENT_LANGUAGE ); |
|
150 | |||
151 | try { |
||
152 | 208 | $this->m_dataitem = DIProperty::newFromUserLabel( $propertyName, $inverse, $contentLanguage ); |
|
153 | 3 | } catch ( SMWDataItemException $e ) { // happens, e.g., when trying to sort queries by property "-" |
|
154 | 3 | $this->addErrorMsg( array( 'smw_noproperty', $value ) ); |
|
155 | 3 | $this->m_dataitem = new DIProperty( 'ERROR', false ); // just to have something |
|
156 | } |
||
157 | |||
158 | // @see the SMW_DV_PROV_DTITLE explanation |
||
159 | 208 | if ( $this->isEnabledFeature( SMW_DV_PROV_DTITLE ) ) { |
|
160 | $dataItem = ApplicationFactory::getInstance()->getPropertySpecificationLookup()->getPropertyFromDisplayTitle( |
||
161 | $value |
||
162 | ); |
||
163 | |||
164 | $this->m_dataitem = $dataItem ? $dataItem : $this->m_dataitem; |
||
165 | } |
||
166 | |||
167 | // Copy the original DI to ensure we can compare it against a possible redirect |
||
168 | 208 | $this->inceptiveProperty = $this->m_dataitem; |
|
169 | |||
170 | 208 | if ( $this->isEnabledFeature( SMW_DV_PROV_REDI ) ) { |
|
171 | 200 | $this->m_dataitem = $this->m_dataitem->getRedirectTarget(); |
|
172 | } |
||
173 | |||
174 | // If no external caption has been invoked then fetch a preferred label |
||
175 | 208 | if ( $this->m_caption === false || $this->m_caption === '' ) { |
|
176 | 208 | $this->preferredLabel = $this->m_dataitem->getPreferredLabel( $this->getOptionBy( self::OPT_USER_LANGUAGE ) ); |
|
177 | } |
||
178 | |||
179 | // Use the preferred label as first choice for a caption, if available |
||
180 | 208 | if ( $this->preferredLabel !== '' ) { |
|
181 | 166 | $this->m_caption = $this->preferredLabel; |
|
182 | 188 | } elseif ( $this->m_caption === false ) { |
|
183 | 188 | $this->m_caption = $value; |
|
184 | } |
||
185 | 208 | } |
|
186 | |||
187 | /** |
||
188 | * @see SMWDataValue::loadDataItem() |
||
189 | * @param $dataitem SMWDataItem |
||
190 | * @return boolean |
||
191 | */ |
||
192 | 32 | protected function loadDataItem( SMWDataItem $dataItem ) { |
|
193 | |||
194 | 32 | if ( $dataItem->getDIType() !== SMWDataItem::TYPE_PROPERTY ) { |
|
195 | return false; |
||
196 | } |
||
197 | |||
198 | 32 | $this->inceptiveProperty = $dataItem; |
|
199 | 32 | $this->m_dataitem = $dataItem; |
|
200 | 32 | $this->preferredLabel = $this->m_dataitem->getPreferredLabel(); |
|
201 | |||
202 | 32 | $this->mPropTypeValue = null; |
|
203 | 32 | unset( $this->m_wikipage ); |
|
204 | 32 | $this->m_caption = false; |
|
205 | 32 | $this->linkAttributes = array(); |
|
206 | |||
207 | 32 | if ( $this->preferredLabel !== '' ) { |
|
208 | 8 | $this->m_caption = $this->preferredLabel; |
|
209 | } |
||
210 | |||
211 | 32 | return true; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * @since 2.5 |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 81 | public function getPreferredLabel() { |
|
220 | 81 | return $this->preferredLabel; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * @since 2.4 |
||
225 | * |
||
226 | * @param array $linkAttributes |
||
227 | */ |
||
228 | 1 | public function setLinkAttributes( array $linkAttributes ) { |
|
229 | 1 | $this->linkAttributes = $linkAttributes; |
|
230 | |||
231 | 1 | if ( $this->getWikiPageValue() instanceof SMWDataValue ) { |
|
232 | 1 | $this->m_wikipage->setLinkAttributes( $linkAttributes ); |
|
233 | } |
||
234 | 1 | } |
|
235 | |||
236 | 109 | public function setCaption( $caption ) { |
|
237 | 109 | parent::setCaption( $caption ); |
|
238 | 109 | if ( $this->getWikiPageValue() instanceof SMWDataValue ) { // pass caption to embedded datavalue (used for printout) |
|
239 | 109 | $this->m_wikipage->setCaption( $caption ); |
|
240 | } |
||
241 | 109 | } |
|
242 | |||
243 | public function setOutputFormat( $formatstring ) { |
||
244 | $this->m_outformat = $formatstring; |
||
245 | if ( $this->getWikiPageValue() instanceof SMWDataValue ) { |
||
246 | $this->m_wikipage->setOutputFormat( $formatstring ); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | public function setInverse( $isinverse ) { |
||
251 | return $this->m_dataitem = new DIProperty( $this->m_dataitem->getKey(), ( $isinverse == true ) ); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Return a wiki page value that can be used for displaying this |
||
256 | * property, or null if no such wiki page exists (for predefined |
||
257 | * properties without any label). |
||
258 | * @return SMWWikiPageValue or null |
||
259 | */ |
||
260 | 115 | public function getWikiPageValue() { |
|
261 | |||
262 | 115 | if ( isset( $this->m_wikipage ) ) { |
|
263 | 64 | return $this->m_wikipage; |
|
264 | } |
||
265 | |||
266 | 115 | $diWikiPage = $this->m_dataitem->getCanonicalDiWikiPage(); |
|
267 | |||
268 | 115 | if ( $diWikiPage !== null ) { |
|
269 | 115 | $this->m_wikipage = DataValueFactory::getInstance()->newDataValueByItem( $diWikiPage, null, $this->m_caption ); |
|
270 | 115 | $this->m_wikipage->setOutputFormat( $this->m_outformat ); |
|
271 | 115 | $this->m_wikipage->setLinkAttributes( $this->linkAttributes ); |
|
272 | 115 | $this->m_wikipage->setOptions( $this->getOptions() ); |
|
273 | 115 | $this->addError( $this->m_wikipage->getErrors() ); |
|
274 | } else { // should rarely happen ($value is only changed if the input $value really was a label for a predefined prop) |
||
275 | $this->m_wikipage = null; |
||
276 | } |
||
277 | |||
278 | 115 | return $this->m_wikipage; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Return TRUE if this is a property that can be displayed, and not a pre-defined |
||
283 | * property that is used only internally and does not even have a user-readable name. |
||
284 | * @note Every user defined property is necessarily visible. |
||
285 | */ |
||
286 | 100 | public function isVisible() { |
|
287 | 100 | return $this->isValid() && ( $this->m_dataitem->isUserDefined() || $this->m_dataitem->getCanonicalLabel() !== '' ); |
|
288 | } |
||
289 | |||
290 | /** |
||
291 | * @since 2.2 |
||
292 | * |
||
293 | * @return boolean |
||
294 | */ |
||
295 | 186 | public function canUse() { |
|
296 | 186 | return $this->isValid() && $this->m_dataitem->isUnrestricted(); |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * @see DataValue::getShortWikiText |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 45 | public function getShortWikiText( $linker = null ) { |
|
305 | 45 | return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_SHORT, $linker ); |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * @see DataValue::getShortHTMLText |
||
310 | * |
||
311 | * @return string |
||
312 | */ |
||
313 | 9 | public function getShortHTMLText( $linker = null ) { |
|
314 | 9 | return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_SHORT, $linker ); |
|
315 | } |
||
316 | |||
317 | /** |
||
318 | * @see DataValue::getLongWikiText |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 12 | public function getLongWikiText( $linker = null ) { |
|
323 | 12 | return $this->getDataValueFormatter()->format( DataValueFormatter::WIKI_LONG, $linker ); |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * @see DataValue::getLongHTMLText |
||
328 | * |
||
329 | * @return string |
||
330 | */ |
||
331 | public function getLongHTMLText( $linker = null ) { |
||
332 | return $this->getDataValueFormatter()->format( DataValueFormatter::HTML_LONG, $linker ); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @see DataValue::getWikiValue |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | 71 | public function getWikiValue() { |
|
341 | 71 | return $this->getDataValueFormatter()->format( DataValueFormatter::VALUE ); |
|
342 | } |
||
343 | |||
344 | /** |
||
345 | * If this property was not user defined, return the internal ID string referring to |
||
346 | * that property. Otherwise return FALSE; |
||
347 | */ |
||
348 | public function getPropertyID() { |
||
349 | return $this->m_dataitem->isUserDefined() ? false : $this->m_dataitem->getKey(); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * Return an SMWTypesValue object representing the datatype of this |
||
354 | * property. |
||
355 | * @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. |
||
356 | */ |
||
357 | public function getTypesValue() { |
||
358 | $result = SMWTypesValue::newFromTypeId( $this->getPropertyTypeID() ); |
||
359 | if ( !$this->isValid() ) { |
||
360 | $result->addError( $this->getErrors() ); |
||
361 | } |
||
362 | return $result; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Convenience method to find the type id of this property. Most callers |
||
367 | * should rather use DIProperty::findPropertyTypeId() directly. Note |
||
368 | * that this is not the same as getTypeID(), which returns the id of |
||
369 | * this property datavalue. |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | 4 | public function getPropertyTypeID() { |
|
374 | 4 | if ( $this->isValid() ) { |
|
375 | 4 | return $this->m_dataitem->findPropertyTypeId(); |
|
376 | } else { |
||
377 | return '__err'; |
||
378 | } |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * A function for registering/overwriting predefined properties for SMW. Should be called from |
||
383 | * within the hook 'smwInitProperties'. Ids should start with three underscores "___" to avoid |
||
384 | * current and future confusion with SMW built-ins. |
||
385 | * |
||
386 | * @deprecated Use DIProperty::registerProperty(). Will vanish before SMW 1.7. |
||
387 | */ |
||
388 | static public function registerProperty( $id, $typeid, $label = false, $show = false ) { |
||
389 | DIProperty::registerProperty( $id, $typeid, $label, $show ); |
||
0 ignored issues
–
show
|
|||
390 | } |
||
391 | |||
392 | /** |
||
393 | * Add a new alias label to an existing datatype id. Note that every ID should have a primary |
||
394 | * label, either provided by SMW or registered with registerDatatype. This function should be |
||
395 | * called from within the hook 'smwInitDatatypes'. |
||
396 | * |
||
397 | * @deprecated Use DIProperty::registerPropertyAlias(). Will vanish before SMW 1.7. |
||
398 | */ |
||
399 | static public function registerPropertyAlias( $id, $label ) { |
||
400 | DIProperty::registerPropertyAlias( $id, $label ); |
||
0 ignored issues
–
show
The method
SMW\DIProperty::registerPropertyAlias() has been deprecated with message: since 2.1, use PropertyRegistry::registerPropertyAlias
This method has been deprecated. The supplier of the class has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.
Loading history...
|
|||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @see DIProperty::isUserDefined() |
||
405 | * |
||
406 | * @deprecated since 1.6 |
||
407 | */ |
||
408 | public function isUserDefined() { |
||
409 | return $this->m_dataitem->isUserDefined(); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * @see DIProperty::isShown() |
||
414 | * |
||
415 | * @deprecated since 1.6 |
||
416 | */ |
||
417 | public function isShown() { |
||
418 | return $this->m_dataitem->isShown(); |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @see DIProperty::isInverse() |
||
423 | * |
||
424 | * @deprecated since 1.6 |
||
425 | */ |
||
426 | public function isInverse() { |
||
427 | return $this->m_dataitem->isInverse(); |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * Return a DB-key-like string: for visible properties, it is the actual DB key, |
||
432 | * for internal (invisible) properties, it is the property ID. The value agrees |
||
433 | * with the first component of getDBkeys() and it can be used in its place. |
||
434 | * @see DIProperty::getKey() |
||
435 | * |
||
436 | * @deprecated since 1.6 |
||
437 | */ |
||
438 | public function getDBkey() { |
||
439 | return $this->m_dataitem->getKey(); |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * @see DIProperty::getLabel() |
||
444 | * |
||
445 | * @deprecated since 1.6 |
||
446 | */ |
||
447 | public function getText() { |
||
448 | return $this->m_dataitem->getLabel(); |
||
449 | } |
||
450 | |||
451 | 208 | private function doNormalizeUserValue( $value ) { |
|
452 | |||
453 | if ( |
||
454 | 208 | ( $pos = strpos( $value, '#' ) ) !== false && strlen( $value ) > 1 || /* #1567 */ |
|
455 | 208 | ( $pos = strpos( $value, '[' ) ) !== false ) /* #1638 */ { |
|
456 | 2 | $this->addErrorMsg( array( 'smw-datavalue-property-invalid-name', $value, substr( $value, $pos, 1 ) ) ); |
|
457 | 2 | $this->m_dataitem = new DIProperty( 'ERROR', false ); |
|
458 | } |
||
459 | |||
460 | // #1727 <Foo> or <Foo-<Bar> are not permitted but |
||
461 | // Foo-<Bar will be converted to Foo- |
||
462 | 208 | $value = strip_tags( htmlspecialchars_decode( $value ) ); |
|
463 | 208 | $inverse = false; |
|
464 | |||
465 | // Enforce upper case for the first character on annotations that are used |
||
466 | // within the property namespace in order to avoid confusion when |
||
467 | // $wgCapitalLinks setting is disabled |
||
468 | 208 | if ( $this->getContextPage() !== null && $this->getContextPage()->getNamespace() === SMW_NS_PROPERTY ) { |
|
469 | // ucfirst is not utf-8 safe hence the reliance on mb_strtoupper |
||
470 | 150 | $value = mb_strtoupper( mb_substr( $value, 0, 1 ) ) . mb_substr( $value, 1 ); |
|
471 | } |
||
472 | |||
473 | // slightly normalise label |
||
474 | 208 | $propertyName = smwfNormalTitleText( ltrim( rtrim( $value, ' ]' ), ' [' ) ); |
|
475 | |||
476 | 208 | if ( ( $propertyName !== '' ) && ( $propertyName { 0 } == '-' ) ) { // property refers to an inverse |
|
477 | 11 | $propertyName = smwfNormalTitleText( (string)substr( $value, 1 ) ); |
|
478 | /// NOTE The cast is necessary at least in PHP 5.3.3 to get string '' instead of boolean false. |
||
479 | /// NOTE It is necessary to normalize again here, since normalization may uppercase the first letter. |
||
480 | 11 | $inverse = true; |
|
481 | } |
||
482 | |||
483 | 208 | return array( $propertyName, $inverse ); |
|
484 | } |
||
485 | |||
486 | } |
||
487 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.