Index: trunk/extensions/SemanticMaps/includes/SM_DI_GeoCoord.php |
— | — | @@ -0,0 +1,66 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Implementation of dataitems that are geographic coordinates. |
| 6 | + * |
| 7 | + * @since 0.8 |
| 8 | + * |
| 9 | + * @file SM_DI_GeoCoord.php |
| 10 | + * @ingroup SemanticMaps |
| 11 | + * |
| 12 | + * @licence GNU GPL v3 |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
| 14 | + */ |
| 15 | +class SMDIGeoCoord extends SMWDataItem { |
| 16 | + |
| 17 | + protected $coordinateSet; |
| 18 | + protected $wikiValue; |
| 19 | + |
| 20 | + public function __construct( array $coords, $typeid = '_geo' ) { |
| 21 | + parent::__construct( $typeid ); |
| 22 | + |
| 23 | + $this->coordinateSet = $coords; |
| 24 | + |
| 25 | + //throw new SMWDataItemException( "Initialisation value '$number' is not a number." ); |
| 26 | + } |
| 27 | + |
| 28 | + public function getDIType() { |
| 29 | + return SMWDataItem::TYPE_GEO; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @since 0.8 |
| 34 | + * |
| 35 | + * @return array |
| 36 | + */ |
| 37 | + public function getCoordinateSet() { |
| 38 | + return $this->coordinateSet; |
| 39 | + } |
| 40 | + |
| 41 | + public function getSortKey() { |
| 42 | + return $this->coordinateSet['lat']; // TODO |
| 43 | + } |
| 44 | + |
| 45 | + public function getSerialization() { |
| 46 | + global $smgQPCoodFormat, $smgQPCoodDirectional; |
| 47 | + return MapsCoordinateParser::formatCoordinates( $this->coordinateSet, $smgQPCoodFormat, $smgQPCoodDirectional ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a data item from the provided serialization string and type |
| 52 | + * ID. |
| 53 | + * @note PHP can convert any string to some number, so we do not do |
| 54 | + * validation here (because this would require less efficient parsing). |
| 55 | + * @return SMWDINumber |
| 56 | + */ |
| 57 | + public static function doUnserialize( $serialization, $typeid ) { |
| 58 | + $parsedCoords = MapsCoordinateParser::parseCoordinates( $serialization ); |
| 59 | + |
| 60 | + if ( $parsedCoords === false || !is_array( $parsedCoords ) ) { |
| 61 | + throw new Exception( 'Unserialization of coordinates failed' ); |
| 62 | + } |
| 63 | + |
| 64 | + return new self( $parsedCoords, $typeid ); |
| 65 | + } |
| 66 | + |
| 67 | +} |
Index: trunk/extensions/SemanticMaps/includes/SM_GeoCoordsValue.php |
— | — | @@ -15,7 +15,6 @@ |
16 | 16 | */ |
17 | 17 | class SMGeoCoordsValue extends SMWDataValue { |
18 | 18 | |
19 | | - protected $coordinateSet; |
20 | 19 | protected $wikiValue; |
21 | 20 | |
22 | 21 | /** |
— | — | @@ -75,7 +74,7 @@ |
76 | 75 | if ( $value == '' ) { |
77 | 76 | $this->addError( wfMsg( 'smw_novalues' ) ); |
78 | 77 | } else { |
79 | | - SMWDataValue::prepareValue( $value, $comparator ); |
| 78 | + SMWDataValue::prepareValue( $value, $comparator ); |
80 | 79 | |
81 | 80 | $parts = explode( '(', $value ); |
82 | 81 | |
— | — | @@ -93,14 +92,15 @@ |
94 | 93 | |
95 | 94 | $parsedCoords = MapsCoordinateParser::parseCoordinates( $coordinates ); |
96 | 95 | if ( $parsedCoords ) { |
97 | | - $this->coordinateSet = $parsedCoords; |
98 | | - |
| 96 | + $this->m_dataitem = new SMDIGeoCoord( $parsedCoords, $this->m_typeid ); |
| 97 | + |
99 | 98 | if ( $this->m_caption === false && !$asQuery ) { |
100 | 99 | global $smgQPCoodFormat, $smgQPCoodDirectional; |
101 | 100 | $this->m_caption = MapsCoordinateParser::formatCoordinates( $parsedCoords, $smgQPCoodFormat, $smgQPCoodDirectional ); |
102 | 101 | } |
103 | 102 | } else { |
104 | 103 | $this->addError( wfMsgExt( 'maps_unrecognized_coords', array( 'parsemag' ), $coordinates, 1 ) ); |
| 104 | + $this->m_dataitem = new SMDIGeoCoord( array(0, 0), $this->m_typeid ); // make sure this is always set |
105 | 105 | } |
106 | 106 | } |
107 | 107 | |
— | — | @@ -127,17 +127,8 @@ |
128 | 128 | * @since 0.6 |
129 | 129 | */ |
130 | 130 | protected function parseDBkeys( $args ) { |
131 | | - global $smgQPCoodFormat, $smgQPCoodDirectional; |
132 | | - |
133 | | - $this->coordinateSet['lat'] = (float)$args[0]; |
134 | | - $this->coordinateSet['lon'] = (float)$args[1]; |
135 | | - |
136 | | - $this->m_caption = MapsCoordinateParser::formatCoordinates( |
137 | | - $this->coordinateSet, |
138 | | - $smgQPCoodFormat, |
139 | | - $smgQPCoodDirectional |
140 | | - ); |
141 | | - |
| 131 | + $this->setDataItem( new SMDIGeoCoord( array( (float)$args[0], (float)$args[1] ), $this->m_typeid ) ); |
| 132 | + $this->m_caption = $this->m_dataitem->getSerialization(); |
142 | 133 | $this->wikiValue = $this->m_caption; |
143 | 134 | } |
144 | 135 | |
— | — | @@ -148,10 +139,11 @@ |
149 | 140 | */ |
150 | 141 | public function getDBkeys() { |
151 | 142 | $this->unstub(); |
152 | | - |
| 143 | + $coordinateSet = $this->m_dataitem->getCoordinateSet(); |
| 144 | + |
153 | 145 | return array( |
154 | | - $this->coordinateSet['lat'], |
155 | | - $this->coordinateSet['lon'] |
| 146 | + $coordinateSet['lat'], |
| 147 | + $coordinateSet['lon'] |
156 | 148 | ); |
157 | 149 | } |
158 | 150 | |
— | — | @@ -174,10 +166,11 @@ |
175 | 167 | SMWOutputs::requireHeadItem( SMW_HEADER_TOOLTIP ); |
176 | 168 | |
177 | 169 | // TODO: fix lang keys so they include the space and coordinates. |
178 | | - |
| 170 | + $coordinateSet = $this->m_dataitem->getCoordinateSet(); |
| 171 | + |
179 | 172 | return '<span class="smwttinline">' . htmlspecialchars( $this->m_caption ) . '<span class="smwttcontent">' . |
180 | | - htmlspecialchars ( wfMsgForContent( 'maps-latitude' ) . ' ' . $this->coordinateSet['lat'] ) . '<br />' . |
181 | | - htmlspecialchars ( wfMsgForContent( 'maps-longitude' ) . ' ' . $this->coordinateSet['lon'] ) . |
| 173 | + htmlspecialchars ( wfMsgForContent( 'maps-latitude' ) . ' ' . $coordinateSet['lat'] ) . '<br />' . |
| 174 | + htmlspecialchars ( wfMsgForContent( 'maps-longitude' ) . ' ' . $coordinateSet['lon'] ) . |
182 | 175 | '</span></span>'; |
183 | 176 | } |
184 | 177 | else { |
— | — | @@ -205,7 +198,7 @@ |
206 | 199 | } |
207 | 200 | else { |
208 | 201 | global $smgQPCoodFormat, $smgQPCoodDirectional; |
209 | | - return MapsCoordinateParser::formatCoordinates( $this->coordinateSet, $smgQPCoodFormat, $smgQPCoodDirectional ); |
| 202 | + return MapsCoordinateParser::formatCoordinates( $this->m_dataitem->getCoordinateSet(), $smgQPCoodFormat, $smgQPCoodDirectional ); |
210 | 203 | } |
211 | 204 | } |
212 | 205 | |
— | — | @@ -237,7 +230,7 @@ |
238 | 231 | if ( $this->isValid() ) { |
239 | 232 | global $smgQPCoodFormat, $smgQPCoodDirectional; |
240 | 233 | $lit = new SMWExpLiteral( |
241 | | - MapsCoordinateParser::formatCoordinates( $this->coordinateSet, $smgQPCoodFormat, $smgQPCoodDirectional ), |
| 234 | + MapsCoordinateParser::formatCoordinates( $this->m_dataitem->getCoordinateSet(), $smgQPCoodFormat, $smgQPCoodDirectional ), |
242 | 235 | $this, |
243 | 236 | 'http://www.w3.org/2001/XMLSchema#string' |
244 | 237 | ); |
— | — | @@ -261,24 +254,16 @@ |
262 | 255 | * @return array |
263 | 256 | */ |
264 | 257 | protected function getServiceLinkParams() { |
| 258 | + $coordinateSet = $this->m_dataitem->getCoordinateSet(); |
265 | 259 | return array( |
266 | | - MapsCoordinateParser::formatCoordinates( $this->coordinateSet, 'float', false ), |
267 | | - MapsCoordinateParser::formatCoordinates( $this->coordinateSet, 'dms', true ), |
268 | | - $this->coordinateSet['lat'], |
269 | | - $this->coordinateSet['lon'] |
| 260 | + MapsCoordinateParser::formatCoordinates( $coordinateSet, 'float', false ), |
| 261 | + MapsCoordinateParser::formatCoordinates( $coordinateSet, 'dms', true ), |
| 262 | + $coordinateSet['lat'], |
| 263 | + $coordinateSet['lon'] |
270 | 264 | ); |
271 | 265 | } |
272 | 266 | |
273 | 267 | /** |
274 | | - * @since 0.6 |
275 | | - * |
276 | | - * @return array |
277 | | - */ |
278 | | - public function getCoordinateSet() { |
279 | | - return $this->coordinateSet; |
280 | | - } |
281 | | - |
282 | | - /** |
283 | 268 | * @see SMWDataValue::getValueIndex |
284 | 269 | * |
285 | 270 | * @since 0.6 |
Index: trunk/extensions/SemanticMaps/includes/SM_GeoCoordsHooks.php |
— | — | @@ -74,6 +74,7 @@ |
75 | 75 | */ |
76 | 76 | public static function initGeoCoordsType() { |
77 | 77 | SMWDataValueFactory::registerDatatype( '_geo', 'SMGeoCoordsValue', 'Geographic coordinate' ); |
| 78 | + SMWDataValueFactory::registerDataItem( '_geo', 'SMDIGeoCoord' ); |
78 | 79 | return true; |
79 | 80 | } |
80 | 81 | |
Index: trunk/extensions/SemanticMaps/SemanticMaps.php |
— | — | @@ -88,6 +88,9 @@ |
89 | 89 | |
90 | 90 | $wgAutoloadClasses['SMGeoCoordsHooks'] = $incDir . 'SM_GeoCoordsHooks.php'; |
91 | 91 | |
| 92 | +// Data items |
| 93 | +$wgAutoloadClasses['SMDIGeoCoord'] = $incDir . 'SM_DI_GeoCoord.php'; |
| 94 | + |
92 | 95 | // Data values |
93 | 96 | $wgAutoloadClasses['SMGeoCoordsValue'] = $incDir . 'SM_GeoCoordsValue.php'; |
94 | 97 | |