|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maps\SemanticMW\DataValues; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\Geo\Parsers\LatLongParser; |
|
6
|
|
|
use DataValues\Geo\Values\LatLongValue; |
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Maps\MapsFactory; |
|
9
|
|
|
use MapsDistanceParser; |
|
10
|
|
|
use SMW\Query\Language\Description; |
|
11
|
|
|
use SMW\Query\Language\ThingDescription; |
|
12
|
|
|
use SMW\Query\QueryComparator; |
|
13
|
|
|
use SMWDataItem; |
|
14
|
|
|
use SMWDataValue; |
|
15
|
|
|
use SMWDIGeoCoord; |
|
16
|
|
|
use SMWOutputs; |
|
17
|
|
|
use ValueParsers\ParseException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @property SMWDIGeoCoord m_dataitem |
|
21
|
|
|
* |
|
22
|
|
|
* @licence GNU GPL v2+ |
|
23
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
24
|
|
|
* @author Markus Krötzsch |
|
25
|
|
|
*/ |
|
26
|
|
|
class CoordinateValue extends SMWDataValue { |
|
27
|
|
|
|
|
28
|
|
|
private $wikiValue; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Overwrite SMWDataValue::getQueryDescription() to be able to process |
|
32
|
|
|
* comparators between all values. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $value |
|
35
|
|
|
* |
|
36
|
|
|
* @return Description |
|
37
|
|
|
* @throws InvalidArgumentException |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getQueryDescription( $value ) { |
|
40
|
|
|
if ( !is_string( $value ) ) { |
|
41
|
|
|
throw new InvalidArgumentException( '$value needs to be a string' ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
list( $distance, $comparator ) = $this->parseUserValue( $value ); |
|
45
|
|
|
$distance = $this->parserDistance( $distance ); |
|
46
|
|
|
|
|
47
|
|
|
$this->setUserValue( $value ); |
|
48
|
|
|
|
|
49
|
|
|
switch ( true ) { |
|
50
|
|
|
case !$this->isValid(): |
|
51
|
|
|
return new ThingDescription(); |
|
52
|
|
|
case $distance !== false: |
|
53
|
|
|
return new \Maps\SemanticMW\ValueDescriptions\AreaDescription( |
|
54
|
|
|
$this->getDataItem(), |
|
55
|
|
|
$comparator, |
|
56
|
|
|
$distance |
|
57
|
|
|
); |
|
58
|
|
|
default: |
|
59
|
|
|
return new \Maps\SemanticMW\ValueDescriptions\CoordinateDescription( |
|
60
|
|
|
$this->getDataItem(), |
|
61
|
|
|
null, |
|
62
|
|
|
$comparator |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @see SMWDataValue::parseUserValue |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function parseUserValue( $value ) { |
|
71
|
|
|
if ( !is_string( $value ) ) { |
|
72
|
|
|
throw new InvalidArgumentException( '$value needs to be a string' ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->wikiValue = $value; |
|
76
|
|
|
|
|
77
|
|
|
$comparator = SMW_CMP_EQ; |
|
78
|
|
|
$distance = false; |
|
79
|
|
|
|
|
80
|
|
|
if ( $value === '' ) { |
|
81
|
|
|
$this->addError( wfMessage( 'smw_novalues' )->text() ); |
|
82
|
|
|
} else { |
|
83
|
|
|
$comparator = QueryComparator::getInstance()->extractComparatorFromString( $value ); |
|
84
|
|
|
|
|
85
|
|
|
list( $coordinates, $distance ) = $this->findValueParts( $value ); |
|
86
|
|
|
|
|
87
|
|
|
$this->tryParseAndSetDataItem( $coordinates ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return [ $distance, $comparator ]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function findValueParts( string $value ): array { |
|
94
|
|
|
$parts = explode( '(', $value ); |
|
95
|
|
|
|
|
96
|
|
|
$coordinates = trim( array_shift( $parts ) ); |
|
97
|
|
|
$distance = count( $parts ) > 0 ? trim( array_shift( $parts ) ) : false; |
|
98
|
|
|
|
|
99
|
|
|
return [ $coordinates, $distance ]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function tryParseAndSetDataItem( string $coordinates ) { |
|
103
|
|
|
$parser = new LatLongParser(); |
|
104
|
|
|
|
|
105
|
|
|
try { |
|
106
|
|
|
$value = $parser->parse( $coordinates ); |
|
107
|
|
|
$this->m_dataitem = new SMWDIGeoCoord( $value->getLatitude(), $value->getLongitude() ); |
|
108
|
|
|
} |
|
109
|
|
|
catch ( ParseException $parseException ) { |
|
110
|
|
|
$this->addError( wfMessage( 'maps_unrecognized_coords', $coordinates, 1 )->text() ); |
|
111
|
|
|
|
|
112
|
|
|
// Make sure this is always set |
|
113
|
|
|
// TODO: Why is this needed?! |
|
114
|
|
|
$this->m_dataitem = new SMWDIGeoCoord( [ 'lat' => 0, 'lon' => 0 ] ); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function parserDistance( $distance ) { |
|
119
|
|
|
if ( $distance !== false ) { |
|
120
|
|
|
$distance = substr( trim( $distance ), 0, -1 ); |
|
121
|
|
|
|
|
122
|
|
|
if ( !MapsDistanceParser::isDistance( $distance ) ) { |
|
123
|
|
|
$this->addError( wfMessage( 'semanticmaps-unrecognizeddistance', $distance )->text() ); |
|
124
|
|
|
$distance = false; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $distance; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @see SMWDataValue::getShortHTMLText |
|
133
|
|
|
* |
|
134
|
|
|
* @since 0.6 |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getShortHTMLText( $linker = null ) { |
|
137
|
|
|
return $this->getShortWikiText( $linker ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @see SMWDataValue::getShortWikiText |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getShortWikiText( $linked = null ) { |
|
144
|
|
|
if ( $this->isValid() ) { |
|
145
|
|
|
if ( $this->m_caption === false ) { |
|
146
|
|
|
return $this->getFormattedCoord( $this->m_dataitem ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->m_caption; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->getErrorText(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param SMWDIGeoCoord $dataItem |
|
157
|
|
|
* @param string|null $format |
|
158
|
|
|
* |
|
159
|
|
|
* @return string|null |
|
160
|
|
|
*/ |
|
161
|
|
|
private function getFormattedCoord( SMWDIGeoCoord $dataItem, string $format = null ) { |
|
162
|
|
|
return MapsFactory::globalInstance()->getCoordinateFormatter()->format( |
|
163
|
|
|
new LatLongValue( |
|
164
|
|
|
$dataItem->getLatitude(), |
|
165
|
|
|
$dataItem->getLongitude() |
|
166
|
|
|
), |
|
167
|
|
|
$format ?? $GLOBALS['smgQPCoodFormat'], |
|
168
|
|
|
$GLOBALS['smgQPCoodDirectional'] |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @see SMWDataValue::getLongHTMLText |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getLongHTMLText( $linker = null ) { |
|
176
|
|
|
return $this->getLongWikiText( $linker ); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @see SMWDataValue::getLongWikiText |
|
181
|
|
|
* |
|
182
|
|
|
* @since 0.6 |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getLongWikiText( $linked = null ) { |
|
185
|
|
|
if ( $this->isValid() ) { |
|
186
|
|
|
SMWOutputs::requireHeadItem( SMW_HEADER_TOOLTIP ); |
|
187
|
|
|
|
|
188
|
|
|
// TODO: fix lang keys so they include the space and coordinates. |
|
189
|
|
|
$coordinateSet = $this->m_dataitem->getCoordinateSet(); |
|
190
|
|
|
|
|
191
|
|
|
$text = $this->getFormattedCoord( $this->m_dataitem ); |
|
192
|
|
|
|
|
193
|
|
|
$lines = [ |
|
194
|
|
|
wfMessage( 'semanticmaps-latitude', $coordinateSet['lat'] )->inContentLanguage()->escaped(), |
|
195
|
|
|
wfMessage( 'semanticmaps-longitude', $coordinateSet['lon'] )->inContentLanguage()->escaped(), |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
if ( array_key_exists( 'alt', $coordinateSet ) ) { |
|
199
|
|
|
$lines[] = wfMessage( 'semanticmaps-altitude', $coordinateSet['alt'] )->inContentLanguage()->escaped(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return '<span class="smwttinline">' . htmlspecialchars( $text ) . '<span class="smwttcontent">' . |
|
203
|
|
|
implode( '<br />', $lines ) . |
|
204
|
|
|
'</span></span>'; |
|
205
|
|
|
} else { |
|
206
|
|
|
return $this->getErrorText(); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @see SMWDataValue::getWikiValue |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getWikiValue() { |
|
214
|
|
|
return $this->wikiValue; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @see SMWDataValue::setDataItem |
|
219
|
|
|
* |
|
220
|
|
|
* @param SMWDataItem $dataItem |
|
221
|
|
|
* |
|
222
|
|
|
* @return boolean |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function loadDataItem( SMWDataItem $dataItem ) { |
|
225
|
|
|
if ( $dataItem instanceof SMWDIGeoCoord ) { |
|
|
|
|
|
|
226
|
|
|
$formattedValue = $this->getFormattedCoord( $dataItem ); |
|
227
|
|
|
|
|
228
|
|
|
if ( $formattedValue !== null ) { |
|
229
|
|
|
$this->wikiValue = $formattedValue; |
|
230
|
|
|
$this->m_dataitem = $dataItem; |
|
231
|
|
|
return true; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return false; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Create links to mapping services based on a wiki-editable message. The parameters |
|
240
|
|
|
* available to the message are: |
|
241
|
|
|
* |
|
242
|
|
|
* $1: The location in non-directional float notation. |
|
243
|
|
|
* $2: The location in directional DMS notation. |
|
244
|
|
|
* $3: The latitude in non-directional float notation. |
|
245
|
|
|
* $4 The longitude in non-directional float notation. |
|
246
|
|
|
* |
|
247
|
|
|
* @return array |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function getServiceLinkParams() { |
|
250
|
|
|
$coordinateSet = $this->m_dataitem->getCoordinateSet(); |
|
251
|
|
|
return [ |
|
252
|
|
|
$this->getFormattedCoord( $this->m_dataitem, 'float' ), // TODO |
|
253
|
|
|
$this->getFormattedCoord( $this->m_dataitem, 'dms' ), // TODO |
|
254
|
|
|
$coordinateSet['lat'], |
|
255
|
|
|
$coordinateSet['lon'] |
|
256
|
|
|
]; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
} |
|
260
|
|
|
|