1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\DataValues\ValueFormatters; |
4
|
|
|
|
5
|
|
|
use SMW\DataValues\MonolingualTextValue; |
6
|
|
|
use SMW\DataValueFactory; |
7
|
|
|
use SMW\DIProperty; |
8
|
|
|
use SMWDataValue as DataValue; |
9
|
|
|
use RuntimeException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 2.4 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class MonolingualTextValueFormatter extends DataValueFormatter { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @since 2.4 |
21
|
|
|
* |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
|
|
public function isFormatterFor( DataValue $dataValue ) { |
25
|
|
|
return $dataValue instanceOf MonolingualTextValue; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @since 2.4 |
30
|
|
|
* |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
public function format( $type, $linker = null ) { |
34
|
|
|
|
35
|
|
|
if ( !$this->dataValue instanceOf MonolingualTextValue ) { |
36
|
|
|
throw new RuntimeException( "The formatter is missing a valid MonolingualTextValue object" ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ( |
40
|
|
|
$this->dataValue->getCaption() !== false && |
41
|
|
|
( $type === self::WIKI_SHORT || $type === self::HTML_SHORT ) ) { |
42
|
|
|
return $this->dataValue->getCaption(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->getOutputText( $type, $linker ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function getOutputText( $type, $linker = null ) { |
49
|
|
|
|
50
|
|
|
if ( !$this->dataValue->isValid() ) { |
51
|
|
|
return ( ( $type == self::WIKI_SHORT ) || ( $type == self::HTML_SHORT ) ) ? '' : $this->dataValue->getErrorText(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// For the inverse case, return the subject that contains the reference |
55
|
|
|
// for Foo annotated with [[Bar::abc@en]] -> [[-Bar::Foo]] |
|
|
|
|
56
|
|
|
if ( $this->dataValue->getProperty() !== null && $this->dataValue->getProperty()->isInverse() ) { |
57
|
|
|
|
58
|
|
|
$dataItems = $this->dataValue->getDataItem()->getSemanticData()->getPropertyValues( |
|
|
|
|
59
|
|
|
new DIProperty( $this->dataValue->getProperty()->getKey() ) |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
$dataItem = reset( $dataItems ); |
63
|
|
|
|
64
|
|
|
if ( !$dataItem ) { |
65
|
|
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $dataItem->getDBKey(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->doFormatFinalOutputFor( $type, $linker ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function doFormatFinalOutputFor( $type, $linker ) { |
75
|
|
|
|
76
|
|
|
$text = ''; |
77
|
|
|
$languagecode = ''; |
78
|
|
|
|
79
|
|
|
foreach ( $this->dataValue->getPropertyDataItems() as $property ) { |
|
|
|
|
80
|
|
|
|
81
|
|
|
// If we wanted to omit the language code display for some outputs then |
82
|
|
|
// this is the point to make it happen |
83
|
|
|
if ( ( $type == self::HTML_LONG || $type == self::WIKI_SHORT ) && $property->getKey() === '_LCODE' ) { |
|
|
|
|
84
|
|
|
//continue; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$dataItems = $this->dataValue->getDataItem()->getSemanticData()->getPropertyValues( $property ); |
|
|
|
|
88
|
|
|
|
89
|
|
|
// Should not happen but just in case |
90
|
|
|
if ( !$dataItems === array() ) { |
91
|
|
|
$this->dataValue->addError( wfMessage( 'smw-datavalue-monolingual-dataitem-missing' )->text() ); |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$dataItem = reset( $dataItems ); |
96
|
|
|
|
97
|
|
|
if ( $dataItem === false ) { |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$dataValue = DataValueFactory::getInstance()->newDataItemValue( |
102
|
|
|
$dataItem, |
103
|
|
|
$property |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
$result = $this->findValueOutputFor( |
107
|
|
|
$type, |
108
|
|
|
$dataValue, |
109
|
|
|
$linker |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
if ( $property->getKey() === '_LCODE' ) { |
113
|
|
|
$languagecode = ' ' . wfMessage( 'smw-datavalue-monolingual-lcode-parenthesis', $result )->text(); |
114
|
|
|
} else { |
115
|
|
|
$text = $result; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $text . $languagecode; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
private function findValueOutputFor( $type, $dataValue, $linker ) { |
123
|
|
|
switch ( $type ) { |
124
|
|
|
case self::VALUE: return $dataValue->getWikiValue(); |
125
|
|
|
case self::WIKI_SHORT: return $dataValue->getShortWikiText( $linker ); |
126
|
|
|
case self::HTML_SHORT: return $dataValue->getShortHTMLText( $linker ); |
127
|
|
|
case self::WIKI_LONG: return $dataValue->getShortWikiText( $linker ); |
128
|
|
|
case self::HTML_LONG: return $dataValue->getShortHTMLText( $linker ); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.