Index: branches/SemanticMaps0.8/SM_Settings.php |
— | — | @@ -41,7 +41,7 @@ |
42 | 42 | # OpenLayers API |
43 | 43 | include_once $smgDir . 'includes/services/OpenLayers/SM_OpenLayers.php'; |
44 | 44 | # Yahoo! Maps API |
45 | | - include_once $smgDir . 'includes/services/YahooMaps/SM_YahooMaps.php'; |
| 45 | + //include_once $smgDir . 'includes/services/YahooMaps/SM_YahooMaps.php'; |
46 | 46 | |
47 | 47 | # Array of String. The default mapping service for each feature, which will be used when no valid service is provided by the user. |
48 | 48 | # Each service needs to be enabled, if not, the first one from the available services will be taken. |
— | — | @@ -89,4 +89,5 @@ |
90 | 90 | # Integer or string. The default width and height of maps in forms created by using Semantic Forms. |
91 | 91 | # These values only be used when the user does not provide them. |
92 | 92 | $smgFIWidth = 665; |
93 | | - $smgFIHeight = $egMapsMapHeight; |
\ No newline at end of file |
| 93 | + $smgFIHeight = $egMapsMapHeight; |
| 94 | + |
\ No newline at end of file |
Index: branches/SemanticMaps0.8/includes/queryprinters/SM_MapPrinter.old.php |
— | — | @@ -0,0 +1,512 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Abstract class that provides the common functionality for all map query printers. |
| 6 | + * |
| 7 | + * TODO: make use of SMQueryHandler |
| 8 | + * TODO: make use of new-style Validator parameter handling |
| 9 | + * |
| 10 | + * @file SM_MapPrinter.php |
| 11 | + * @ingroup SemanticMaps |
| 12 | + * |
| 13 | + * @author Jeroen De Dauw |
| 14 | + * @author Robert Buzink |
| 15 | + * @author Yaron Koren |
| 16 | + */ |
| 17 | +abstract class SMMapPrinter extends SMWResultPrinter implements iMappingFeature { |
| 18 | + |
| 19 | + /** |
| 20 | + * Returns the name of the service to get the correct mapping service object. |
| 21 | + * |
| 22 | + * @since 0.6.3 |
| 23 | + * |
| 24 | + * @return string |
| 25 | + */ |
| 26 | + protected abstract function getServiceName(); |
| 27 | + |
| 28 | + /** |
| 29 | + * @var iMappingService |
| 30 | + */ |
| 31 | + protected $service; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var array |
| 35 | + */ |
| 36 | + protected $locations = array(); |
| 37 | + |
| 38 | + /** |
| 39 | + * @var string |
| 40 | + */ |
| 41 | + protected $markerJs; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + protected $centreLat; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var string |
| 50 | + */ |
| 51 | + protected $centreLon; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var string |
| 55 | + */ |
| 56 | + protected $output = ''; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var array or false |
| 60 | + */ |
| 61 | + protected $specificParameters = false; |
| 62 | + |
| 63 | + /** |
| 64 | + * Indicates if the zoom paramter was set to it's default. |
| 65 | + * |
| 66 | + * @since 0.7 |
| 67 | + * |
| 68 | + * @var boolean |
| 69 | + */ |
| 70 | + protected $zoomDefaulted; |
| 71 | + |
| 72 | + /** |
| 73 | + * Constructor. |
| 74 | + * |
| 75 | + * @param $format String |
| 76 | + * @param $inline |
| 77 | + * @param $service iMappingService |
| 78 | + */ |
| 79 | + public function __construct( $format, $inline, /* iMappingService */ $service = null ) { |
| 80 | + // TODO: this is a hack since I can't find a way to pass along the service object here when the QP is created in SMW. |
| 81 | + if ( $service == null ) { |
| 82 | + $service = MapsMappingServices::getServiceInstance( $this->getServiceName() ); |
| 83 | + } |
| 84 | + |
| 85 | + $this->service = $service; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns the specific parameters by first checking if they have been initialized yet, |
| 90 | + * doing to work if this is not the case, and then returning them. |
| 91 | + * |
| 92 | + * @since 0.6.5 |
| 93 | + * |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + public final function getSpecificParameterInfo() { |
| 97 | + if ( $this->specificParameters === false ) { |
| 98 | + $this->specificParameters = array(); |
| 99 | + $this->initSpecificParamInfo( $this->specificParameters ); |
| 100 | + } |
| 101 | + |
| 102 | + return $this->specificParameters; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Initializes the specific parameters. |
| 107 | + * |
| 108 | + * Override this method to set parameters specific to a feature service comibination in |
| 109 | + * the inheriting class. |
| 110 | + * |
| 111 | + * @since 0.6.5 |
| 112 | + * |
| 113 | + * @param array $parameters |
| 114 | + */ |
| 115 | + protected function initSpecificParamInfo( array &$parameters ) { |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Builds up and returns the HTML for the map, with the queried coordinate data on it. |
| 120 | + * |
| 121 | + * @param SMWQueryResult $res |
| 122 | + * @param $outputmode |
| 123 | + * |
| 124 | + * @return array |
| 125 | + */ |
| 126 | + public final function getResultText( /* SMWQueryResult */ $res, $outputmode ) { |
| 127 | + if ( self::manageMapProperties( $this->m_params ) ) { |
| 128 | + $this->formatResultData( $res, $outputmode ); |
| 129 | + |
| 130 | + // Only create a map when there is at least one result. |
| 131 | + if ( count( $this->locations ) > 0 || $this->forceshow ) { |
| 132 | + $this->setZoom(); |
| 133 | + |
| 134 | + $this->setCentre(); |
| 135 | + |
| 136 | + $this->markerJs = $this->service->createMarkersJs( $this->locations ); |
| 137 | + |
| 138 | + $this->addSpecificMapHTML(); |
| 139 | + |
| 140 | + $dependencies = $this->service->getDependencyHtml(); |
| 141 | + $hash = md5( $dependencies ); |
| 142 | + SMWOutputs::requireHeadItem( $hash, $dependencies ); |
| 143 | + } |
| 144 | + else { |
| 145 | + // TODO: add warning when level high enough and append to error list? |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return array( $this->output, 'noparse' => true, 'isHTML' => true ); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Validates and corrects the provided map properties, and the sets them as class fields. |
| 154 | + * |
| 155 | + * @param array $mapProperties |
| 156 | + * |
| 157 | + * @return boolean Indicates whether the map should be shown or not. |
| 158 | + */ |
| 159 | + protected final function manageMapProperties( array $mapProperties ) { |
| 160 | + |
| 161 | + /* |
| 162 | + * Assembliy of the allowed parameters and their information. |
| 163 | + * The main parameters (the ones that are shared by everything) are overidden |
| 164 | + * by the feature parameters (the ones specific to a feature). The result is then |
| 165 | + * again overidden by the service parameters (the ones specific to the service), |
| 166 | + * and finally by the specific parameters (the ones specific to a service-feature combination). |
| 167 | + */ |
| 168 | + $parameterInfo = SMQueryPrinters::getParameterInfo(); |
| 169 | + $this->service->addParameterInfo( $parameterInfo ); |
| 170 | + |
| 171 | + // TODO |
| 172 | + $parameterInfo = array_merge_recursive( $parameterInfo, $this->getSpecificParameterInfo() ); |
| 173 | + |
| 174 | + $validator = new Validator( $this->getName(), false ); |
| 175 | + |
| 176 | + $validator->setParameters( $mapProperties, $parameterInfo ); |
| 177 | + |
| 178 | + $validator->validateParameters(); |
| 179 | + |
| 180 | + $fatalError = $validator->hasFatalError(); |
| 181 | + |
| 182 | + if ( $fatalError === false ) { |
| 183 | + $this->zoomDefaulted = $validator->getParameter( 'zoom' )->wasSetToDefault(); |
| 184 | + $this->setMapProperties( $validator->getParameterValues() ); |
| 185 | + } |
| 186 | + else { |
| 187 | + $this->output = '<span class="errorbox">' . |
| 188 | + htmlspecialchars( wfMsgExt( 'validator-fatal-error', 'parsemag', $fatalError->getMessage() ) ) . |
| 189 | + '</span>'; |
| 190 | + } |
| 191 | + |
| 192 | + return !$fatalError; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Sets the map properties as class fields. |
| 197 | + * |
| 198 | + * @param array $mapProperties |
| 199 | + */ |
| 200 | + private function setMapProperties( array $mapProperties ) { |
| 201 | + foreach ( $mapProperties as $paramName => $paramValue ) { |
| 202 | + if ( !property_exists( __CLASS__, $paramName ) ) { |
| 203 | + $this-> { $paramName } = $paramValue; |
| 204 | + } |
| 205 | + else { |
| 206 | + throw new Exception( 'Attempt to override a class field during map propertie assignment. Field name: ' . $paramName ); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Reads the parameters and gets the query printers output. |
| 213 | + * |
| 214 | + * @param SMWQueryResult $results |
| 215 | + * @param array $params |
| 216 | + * @param $outputmode |
| 217 | + * |
| 218 | + * @return array |
| 219 | + */ |
| 220 | + public final function getResult( /* SMWQueryResult */ $results, /* array */ $params, $outputmode ) { |
| 221 | + // Skip checks, results with 0 entries are normal. |
| 222 | + $this->readParameters( $params, $outputmode ); |
| 223 | + |
| 224 | + return $this->getResultText( $results, SMW_OUTPUT_HTML ); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Loops over the rows in the result and adds them via addResultRow. |
| 229 | + * |
| 230 | + * @param SMWQueryResult $res |
| 231 | + * @param $outputmode |
| 232 | + */ |
| 233 | + protected function formatResultData( SMWQueryResult $res, $outputmode ) { |
| 234 | + $this->addStaticLocations(); |
| 235 | + |
| 236 | + while ( ( $row = $res->getNext() ) !== false ) { |
| 237 | + $this->addResultRow( $outputmode, $row ); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Adds the static locations (specified via the staticlocations parameter) to the map. |
| 243 | + * |
| 244 | + * @since 0.7 |
| 245 | + */ |
| 246 | + protected function addStaticLocations() { |
| 247 | + global $wgTitle; |
| 248 | + |
| 249 | + // New parser object to render popup contents with. |
| 250 | + $parser = new Parser(); |
| 251 | + |
| 252 | + $this->title = $parser->parse( $this->title, $wgTitle, new ParserOptions() )->getText(); |
| 253 | + $this->label = $parser->parse( $this->label, $wgTitle, new ParserOptions() )->getText(); |
| 254 | + |
| 255 | + // Each $location is an array containg the coordinate set as first element, possibly followed by meta data. |
| 256 | + foreach ( $this->staticlocations as $location ) { |
| 257 | + $markerData = MapsCoordinateParser::parseCoordinates( array_shift( $location ) ); |
| 258 | + |
| 259 | + if ( !$markerData ) continue; |
| 260 | + |
| 261 | + $markerData = array( $markerData['lat'], $markerData['lon'] ); |
| 262 | + |
| 263 | + if ( count( $location ) > 0 ) { |
| 264 | + // Parse and add the point specific title if it's present. |
| 265 | + $markerData['title'] = $parser->parse( $location[0], $wgTitle, new ParserOptions() )->getText(); |
| 266 | + |
| 267 | + if ( count( $location ) > 1 ) { |
| 268 | + // Parse and add the point specific label if it's present. |
| 269 | + $markerData['label'] = $parser->parse( $location[1], $wgTitle, new ParserOptions() )->getText(); |
| 270 | + |
| 271 | + if ( count( $location ) > 2 ) { |
| 272 | + // Add the point specific icon if it's present. |
| 273 | + $markerData['icon'] = $location[2]; |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + // If there is no point specific icon, use the general icon parameter when available. |
| 279 | + if ( !array_key_exists( 'icon', $markerData ) ) { |
| 280 | + $markerData['icon'] = $this->icon; |
| 281 | + } |
| 282 | + |
| 283 | + if ( $markerData['icon'] != '' ) { |
| 284 | + $markerData['icon'] = MapsMapper::getImageUrl( $markerData['icon'] ); |
| 285 | + } |
| 286 | + |
| 287 | + // Temporary fix, will refactor away later |
| 288 | + // TODO |
| 289 | + $markerData = array_values( $markerData ); |
| 290 | + if ( count( $markerData ) < 5 ) { |
| 291 | + if ( count( $markerData ) < 4 ) { |
| 292 | + $markerData[] = ''; |
| 293 | + } |
| 294 | + $markerData[] = ''; |
| 295 | + } |
| 296 | + |
| 297 | + $this->locations[] = $markerData; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + /** |
| 302 | + * This function will loop through all properties (fields) of one record (row), |
| 303 | + * and add the location data, title, label and icon to the m_locations array. |
| 304 | + * |
| 305 | + * TODO: this doesn't qualify as a megamoth just yet, but some splitting up would be nice |
| 306 | + * |
| 307 | + * @param $outputmode |
| 308 | + * @param array $row The record you want to add data from |
| 309 | + */ |
| 310 | + protected function addResultRow( $outputmode, array $row ) { |
| 311 | + global $wgUser, $smgUseSpatialExtensions, $wgTitle; |
| 312 | + |
| 313 | + $skin = $wgUser->getSkin(); |
| 314 | + |
| 315 | + $title = ''; |
| 316 | + $titleForTemplate = ''; |
| 317 | + $text = ''; |
| 318 | + $lat = ''; |
| 319 | + $lon = ''; |
| 320 | + |
| 321 | + $coords = array(); |
| 322 | + $label = array(); |
| 323 | + |
| 324 | + // Loop throught all fields of the record. |
| 325 | + foreach ( $row as $i => $field ) { |
| 326 | + $pr = $field->getPrintRequest(); |
| 327 | + |
| 328 | + // Loop throught all the parts of the field value. |
| 329 | + while ( ( $object = $field->getNextObject() ) !== false ) { |
| 330 | + if ( $object->getTypeID() == '_wpg' && $i == 0 ) { |
| 331 | + if ( $this->showtitle ) $title = $object->getLongText( $outputmode, $skin ); |
| 332 | + if ( $this->template ) $titleForTemplate = $object->getLongText( $outputmode, NULL ); |
| 333 | + } |
| 334 | + |
| 335 | + if ( $object->getTypeID() != '_geo' && $i != 0 ) { |
| 336 | + if ( $this->template ) { |
| 337 | + if ( $object instanceof SMWWikiPageValue ) { |
| 338 | + $label[] = $object->getTitle()->getPrefixedText(); |
| 339 | + } else { |
| 340 | + $label[] = $object->getLongText( $outputmode, $skin ); |
| 341 | + } |
| 342 | + } |
| 343 | + else { |
| 344 | + $propertyName = $pr->getHTMLText( $skin ); |
| 345 | + if ( $propertyName != '' ) $propertyName .= ': '; |
| 346 | + $text .= $propertyName . $object->getLongText( $outputmode, $skin ) . '<br />'; |
| 347 | + } |
| 348 | + } |
| 349 | + |
| 350 | + if ( $pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_geo' ) { |
| 351 | + $coords[] = $object->getDBkeys(); |
| 352 | + } |
| 353 | + } |
| 354 | + } |
| 355 | + |
| 356 | + if ( $this->template ) { |
| 357 | + // New parser object to render the templates with. |
| 358 | + $parser = new Parser(); |
| 359 | + } |
| 360 | + |
| 361 | + foreach ( $coords as $coord ) { |
| 362 | + if ( count( $coord ) >= 2 ) { |
| 363 | + if ( $smgUseSpatialExtensions ) { |
| 364 | + // TODO |
| 365 | + } |
| 366 | + else { |
| 367 | + list( $lat, $lon ) = $coord; |
| 368 | + } |
| 369 | + |
| 370 | + if ( $lat != '' && $lon != '' ) { |
| 371 | + $icon = $this->getLocationIcon( $row ); |
| 372 | + |
| 373 | + if ( $this->template ) { |
| 374 | + $segments = array_merge( |
| 375 | + array( $this->template, 'title=' . $titleForTemplate, 'latitude=' . $lat, 'longitude=' . $lon ), |
| 376 | + $label |
| 377 | + ); |
| 378 | + |
| 379 | + $text = $parser->parse( '{{' . implode( '|', $segments ) . '}}', $wgTitle, new ParserOptions() )->getText(); |
| 380 | + } |
| 381 | + |
| 382 | + $this->locations[] = array( |
| 383 | + $lat, |
| 384 | + $lon, |
| 385 | + $title, |
| 386 | + $text, |
| 387 | + $icon |
| 388 | + ); |
| 389 | + } |
| 390 | + } |
| 391 | + } |
| 392 | + } |
| 393 | + |
| 394 | + /** |
| 395 | + * Get the icon for a row. |
| 396 | + * |
| 397 | + * @param array $row |
| 398 | + * |
| 399 | + * @return string |
| 400 | + */ |
| 401 | + private function getLocationIcon( array $row ) { |
| 402 | + $icon = ''; |
| 403 | + $legend_labels = array(); |
| 404 | + |
| 405 | + // Look for display_options field, which can be set by Semantic Compound Queries |
| 406 | + // the location of this field changed in SMW 1.5 |
| 407 | + $display_location = method_exists( $row[0], 'getResultSubject' ) ? $row[0]->getResultSubject() : $row[0]; |
| 408 | + |
| 409 | + if ( property_exists( $display_location, 'display_options' ) && is_array( $display_location->display_options ) ) { |
| 410 | + $display_options = $display_location->display_options; |
| 411 | + if ( array_key_exists( 'icon', $display_options ) ) { |
| 412 | + $icon = $display_options['icon']; |
| 413 | + |
| 414 | + // This is somewhat of a hack - if a legend label has been set, we're getting it for every point, instead of just once per icon |
| 415 | + if ( array_key_exists( 'legend label', $display_options ) ) { |
| 416 | + |
| 417 | + $legend_label = $display_options['legend label']; |
| 418 | + |
| 419 | + if ( ! array_key_exists( $icon, $legend_labels ) ) { |
| 420 | + $legend_labels[$icon] = $legend_label; |
| 421 | + } |
| 422 | + } |
| 423 | + } |
| 424 | + } // Icon can be set even for regular, non-compound queries If it is, though, we have to translate the name into a URL here |
| 425 | + elseif ( $this->icon != '' ) { |
| 426 | + $icon = MapsMapper::getImageUrl( $this->icon ); |
| 427 | + } |
| 428 | + |
| 429 | + return $icon; |
| 430 | + } |
| 431 | + |
| 432 | + /** |
| 433 | + * Sets the zoom level to the provided value, or when not set, to the default. |
| 434 | + */ |
| 435 | + protected function setZoom() { |
| 436 | + if ( $this->zoomDefaulted ) { |
| 437 | + if ( count( $this->locations ) > 1 ) { |
| 438 | + $this->zoom = 'null'; |
| 439 | + } |
| 440 | + } |
| 441 | + } |
| 442 | + |
| 443 | + /** |
| 444 | + * Sets the $centre_lat and $centre_lon fields. |
| 445 | + * Note: this needs to be done AFTRE the maker coordinates are set. |
| 446 | + */ |
| 447 | + protected function setCentre() { |
| 448 | + // If a centre value is set, use it. |
| 449 | + if ( $this->centre != '' ) { |
| 450 | + // Geocode and convert if required. |
| 451 | + $centre = MapsGeocoders::attemptToGeocode( $this->centre, $this->geoservice, $this->service->getName() ); |
| 452 | + |
| 453 | + if ( $centre ) { |
| 454 | + $this->centreLat = $centre['lat']; |
| 455 | + $this->centreLon = $centre['lon']; |
| 456 | + } |
| 457 | + else { |
| 458 | + $this->setCentreDefault(); |
| 459 | + } |
| 460 | + } |
| 461 | + else { |
| 462 | + $this->setCentreDefault(); |
| 463 | + } |
| 464 | + } |
| 465 | + |
| 466 | + /** |
| 467 | + * Figures out the default value for the centre. |
| 468 | + */ |
| 469 | + private function setCentreDefault() { |
| 470 | + if ( count( $this->locations ) > 1 ) { |
| 471 | + // If centre is not set, and there are multiple points, set the values to null, to be auto determined by the JS of the mapping API. |
| 472 | + $this->centreLat = 'null'; |
| 473 | + $this->centreLon = 'null'; |
| 474 | + } |
| 475 | + elseif ( count( $this->locations ) == 1 ) { |
| 476 | + // If centre is not set and there is exactelly one marker, use it's coordinates. |
| 477 | + $this->centreLat = Xml::escapeJsString( $this->locations[0][0] ); |
| 478 | + $this->centreLon = Xml::escapeJsString( $this->locations[0][1] ); |
| 479 | + } |
| 480 | + else { |
| 481 | + // If centre is not set and there are no results, centre on the default coordinates. |
| 482 | + global $egMapsDefaultMapCentre; |
| 483 | + // TODO |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + /** |
| 488 | + * Returns the internationalized name of the mapping service. |
| 489 | + * |
| 490 | + * @return string |
| 491 | + */ |
| 492 | + public final function getName() { |
| 493 | + return wfMsg( 'maps_' . $this->service->getName() ); |
| 494 | + } |
| 495 | + |
| 496 | + /** |
| 497 | + * Returns a list of parameter information, for usage by Special:Ask and others. |
| 498 | + * |
| 499 | + * @return array |
| 500 | + */ |
| 501 | + public function getParameters() { |
| 502 | + global $egMapsMapWidth, $egMapsMapHeight; |
| 503 | + |
| 504 | + $params = parent::exportFormatParameters(); |
| 505 | + |
| 506 | + $params[] = array( 'name' => 'zoom', 'type' => 'int', 'description' => wfMsg( 'semanticmaps_paramdesc_zoom' ) ); |
| 507 | + $params[] = array( 'name' => 'width', 'type' => 'int', 'description' => wfMsgExt( 'semanticmaps_paramdesc_width', 'parsemag', $egMapsMapWidth ) ); |
| 508 | + $params[] = array( 'name' => 'height', 'type' => 'int', 'description' => wfMsgExt( 'semanticmaps_paramdesc_height', 'parsemag', $egMapsMapHeight ) ); |
| 509 | + |
| 510 | + return $params; |
| 511 | + } |
| 512 | + |
| 513 | +} |
\ No newline at end of file |
Property changes on: branches/SemanticMaps0.8/includes/queryprinters/SM_MapPrinter.old.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 514 | + native |
Index: branches/SemanticMaps0.8/includes/queryprinters/SM_MapPrinter.php |
— | — | @@ -3,19 +3,27 @@ |
4 | 4 | /** |
5 | 5 | * Abstract class that provides the common functionality for all map query printers. |
6 | 6 | * |
7 | | - * TODO: make use of SMQueryHandler |
8 | | - * TODO: make use of new-style Validator parameter handling |
9 | | - * |
10 | 7 | * @file SM_MapPrinter.php |
11 | 8 | * @ingroup SemanticMaps |
12 | 9 | * |
13 | 10 | * @author Jeroen De Dauw |
14 | | - * @author Robert Buzink |
15 | | - * @author Yaron Koren |
16 | 11 | */ |
17 | | -abstract class SMMapPrinter extends SMWResultPrinter implements iMappingFeature { |
18 | | - |
| 12 | +abstract class SMMapPrinter extends SMWResultPrinter { |
| 13 | + |
19 | 14 | /** |
| 15 | + * Returns the HTML to display the map. |
| 16 | + * |
| 17 | + * @since 0.8 |
| 18 | + * |
| 19 | + * @param array $params |
| 20 | + * @param Parser $parser |
| 21 | + * @param string $mapName |
| 22 | + * |
| 23 | + * @return string |
| 24 | + */ |
| 25 | + protected abstract function getMapHTML( array $params, Parser $parser, $mapName ); |
| 26 | + |
| 27 | + /** |
20 | 28 | * Returns the name of the service to get the correct mapping service object. |
21 | 29 | * |
22 | 30 | * @since 0.6.3 |
— | — | @@ -27,48 +35,13 @@ |
28 | 36 | /** |
29 | 37 | * @var iMappingService |
30 | 38 | */ |
31 | | - protected $service; |
| 39 | + protected $service; |
32 | 40 | |
33 | | - /** |
34 | | - * @var array |
35 | | - */ |
36 | | - protected $locations = array(); |
| 41 | + protected $fatalErrorMsg = false; |
37 | 42 | |
38 | | - /** |
39 | | - * @var string |
40 | | - */ |
41 | | - protected $markerJs; |
| 43 | + protected $parameters; |
42 | 44 | |
43 | 45 | /** |
44 | | - * @var string |
45 | | - */ |
46 | | - protected $centreLat; |
47 | | - |
48 | | - /** |
49 | | - * @var string |
50 | | - */ |
51 | | - protected $centreLon; |
52 | | - |
53 | | - /** |
54 | | - * @var string |
55 | | - */ |
56 | | - protected $output = ''; |
57 | | - |
58 | | - /** |
59 | | - * @var array or false |
60 | | - */ |
61 | | - protected $specificParameters = false; |
62 | | - |
63 | | - /** |
64 | | - * Indicates if the zoom paramter was set to it's default. |
65 | | - * |
66 | | - * @since 0.7 |
67 | | - * |
68 | | - * @var boolean |
69 | | - */ |
70 | | - protected $zoomDefaulted; |
71 | | - |
72 | | - /** |
73 | 46 | * Constructor. |
74 | 47 | * |
75 | 48 | * @param $format String |
— | — | @@ -83,163 +56,100 @@ |
84 | 57 | |
85 | 58 | $this->service = $service; |
86 | 59 | } |
87 | | - |
88 | | - /** |
89 | | - * Returns the specific parameters by first checking if they have been initialized yet, |
90 | | - * doing to work if this is not the case, and then returning them. |
91 | | - * |
92 | | - * @since 0.6.5 |
93 | | - * |
94 | | - * @return array |
95 | | - */ |
96 | | - public final function getSpecificParameterInfo() { |
97 | | - if ( $this->specificParameters === false ) { |
98 | | - $this->specificParameters = array(); |
99 | | - $this->initSpecificParamInfo( $this->specificParameters ); |
100 | | - } |
101 | | - |
102 | | - return $this->specificParameters; |
103 | | - } |
104 | | - |
105 | | - /** |
106 | | - * Initializes the specific parameters. |
107 | | - * |
108 | | - * Override this method to set parameters specific to a feature service comibination in |
109 | | - * the inheriting class. |
110 | | - * |
111 | | - * @since 0.6.5 |
112 | | - * |
113 | | - * @param array $parameters |
114 | | - */ |
115 | | - protected function initSpecificParamInfo( array &$parameters ) { |
116 | | - } |
117 | | - |
118 | | - /** |
119 | | - * Builds up and returns the HTML for the map, with the queried coordinate data on it. |
120 | | - * |
121 | | - * @param SMWQueryResult $res |
122 | | - * @param $outputmode |
123 | | - * |
124 | | - * @return array |
125 | | - */ |
126 | | - public final function getResultText( /* SMWQueryResult */ $res, $outputmode ) { |
127 | | - if ( self::manageMapProperties( $this->m_params ) ) { |
128 | | - $this->formatResultData( $res, $outputmode ); |
129 | | - |
130 | | - // Only create a map when there is at least one result. |
131 | | - if ( count( $this->locations ) > 0 || $this->forceshow ) { |
132 | | - $this->setZoom(); |
133 | | - |
134 | | - $this->setCentre(); |
135 | | - |
136 | | - $this->markerJs = $this->service->createMarkersJs( $this->locations ); |
137 | | - |
138 | | - $this->addSpecificMapHTML(); |
139 | | - |
140 | | - $dependencies = $this->service->getDependencyHtml(); |
141 | | - $hash = md5( $dependencies ); |
142 | | - SMWOutputs::requireHeadItem( $hash, $dependencies ); |
143 | | - } |
144 | | - else { |
145 | | - // TODO: add warning when level high enough and append to error list? |
146 | | - } |
147 | | - } |
148 | 60 | |
149 | | - return array( $this->output, 'noparse' => true, 'isHTML' => true ); |
150 | | - } |
151 | | - |
152 | 61 | /** |
153 | | - * Validates and corrects the provided map properties, and the sets them as class fields. |
154 | | - * |
155 | | - * @param array $mapProperties |
156 | | - * |
157 | | - * @return boolean Indicates whether the map should be shown or not. |
| 62 | + * (non-PHPdoc) |
| 63 | + * @see SMWResultPrinter::readParameters() |
158 | 64 | */ |
159 | | - protected final function manageMapProperties( array $mapProperties ) { |
160 | | - |
161 | | - /* |
162 | | - * Assembliy of the allowed parameters and their information. |
163 | | - * The main parameters (the ones that are shared by everything) are overidden |
164 | | - * by the feature parameters (the ones specific to a feature). The result is then |
165 | | - * again overidden by the service parameters (the ones specific to the service), |
166 | | - * and finally by the specific parameters (the ones specific to a service-feature combination). |
167 | | - */ |
| 65 | + protected function readParameters( /* array */ $params, $outputmode ) { |
| 66 | + parent::readParameters( $params, $outputmode ); |
| 67 | + |
168 | 68 | $parameterInfo = SMQueryPrinters::getParameterInfo(); |
169 | 69 | $this->service->addParameterInfo( $parameterInfo ); |
170 | 70 | |
171 | | - // TODO |
172 | | - $parameterInfo = array_merge_recursive( $parameterInfo, $this->getSpecificParameterInfo() ); |
173 | | - |
174 | 71 | $validator = new Validator( $this->getName(), false ); |
175 | | - |
176 | 72 | $validator->setParameters( $mapProperties, $parameterInfo ); |
177 | | - |
178 | 73 | $validator->validateParameters(); |
179 | 74 | |
180 | 75 | $fatalError = $validator->hasFatalError(); |
181 | 76 | |
182 | 77 | if ( $fatalError === false ) { |
183 | | - $this->zoomDefaulted = $validator->getParameter( 'zoom' )->wasSetToDefault(); |
184 | | - $this->setMapProperties( $validator->getParameterValues() ); |
| 78 | + $this->parameters = $validator->getParameters( false ); |
185 | 79 | } |
186 | 80 | else { |
187 | | - $this->output = '<span class="errorbox">' . |
| 81 | + $this->fatalErrorMsg = |
| 82 | + '<span class="errorbox">' . |
188 | 83 | htmlspecialchars( wfMsgExt( 'validator-fatal-error', 'parsemag', $fatalError->getMessage() ) ) . |
189 | 84 | '</span>'; |
190 | | - } |
191 | | - |
192 | | - return !$fatalError; |
193 | | - } |
| 85 | + } |
| 86 | + } |
194 | 87 | |
195 | 88 | /** |
196 | | - * Sets the map properties as class fields. |
| 89 | + * Builds up and returns the HTML for the map, with the queried coordinate data on it. |
| 90 | + * |
| 91 | + * @param SMWQueryResult $res |
| 92 | + * @param $outputmode |
197 | 93 | * |
198 | | - * @param array $mapProperties |
| 94 | + * @return array |
199 | 95 | */ |
200 | | - private function setMapProperties( array $mapProperties ) { |
201 | | - foreach ( $mapProperties as $paramName => $paramValue ) { |
202 | | - if ( !property_exists( __CLASS__, $paramName ) ) { |
203 | | - $this-> { $paramName } = $paramValue; |
204 | | - } |
205 | | - else { |
206 | | - throw new Exception( 'Attempt to override a class field during map propertie assignment. Field name: ' . $paramName ); |
207 | | - } |
| 96 | + public final function getResultText( /* SMWQueryResult */ $res, $outputmode ) { |
| 97 | + if ( $this->fatalErrorMsg !== false ) { |
| 98 | + global $wgParser; |
| 99 | + $mapName = $this->service->getMapId(); |
| 100 | + |
| 101 | + $queryHandler = SMQueryHandler( $res, $outputmode, $this->parameters ); |
| 102 | + $locations = $queryHandler->getLocations(); |
| 103 | + |
| 104 | + return $this->getMapHTML( $this->parameters, $wgParser, $mapName ) . $this->getJSON( $this->parameters, $wgParser, $mapName ); |
208 | 105 | } |
| 106 | + else { |
| 107 | + return $this->fatalErrorMsg; |
| 108 | + } |
209 | 109 | } |
210 | 110 | |
211 | 111 | /** |
212 | | - * Reads the parameters and gets the query printers output. |
213 | | - * |
214 | | - * @param SMWQueryResult $results |
| 112 | + * Returns the JSON with the maps data. |
| 113 | + * |
| 114 | + * @since 0.8 |
| 115 | + * |
215 | 116 | * @param array $params |
216 | | - * @param $outputmode |
| 117 | + * @param Parser $parser |
| 118 | + * @param string $mapName |
217 | 119 | * |
218 | | - * @return array |
219 | | - */ |
220 | | - public final function getResult( /* SMWQueryResult */ $results, /* array */ $params, $outputmode ) { |
221 | | - // Skip checks, results with 0 entries are normal. |
222 | | - $this->readParameters( $params, $outputmode ); |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + protected function getJSON( array $params, Parser $parser, $mapName ) { |
| 123 | + $object = $this->getJSONObject( $params, $parser ); |
223 | 124 | |
224 | | - return $this->getResultText( $results, SMW_OUTPUT_HTML ); |
| 125 | + if ( $object === false ) { |
| 126 | + return ''; |
| 127 | + } |
| 128 | + |
| 129 | + return Html::inlineScript( |
| 130 | + MapsMapper::getBaseMapJSON( $this->service->getName() ) |
| 131 | + . "maps.{$this->service->getName()}.{$mapName}=" . json_encode( $object ) . ';' |
| 132 | + ); |
225 | 133 | } |
226 | 134 | |
227 | 135 | /** |
228 | | - * Loops over the rows in the result and adds them via addResultRow. |
| 136 | + * Returns a PHP object to encode to JSON with the map data. |
| 137 | + * |
| 138 | + * @since 0.8 |
| 139 | + * |
| 140 | + * @param array $params |
| 141 | + * @param Parser $parser |
229 | 142 | * |
230 | | - * @param SMWQueryResult $res |
231 | | - * @param $outputmode |
232 | | - */ |
233 | | - protected function formatResultData( SMWQueryResult $res, $outputmode ) { |
234 | | - $this->addStaticLocations(); |
235 | | - |
236 | | - while ( ( $row = $res->getNext() ) !== false ) { |
237 | | - $this->addResultRow( $outputmode, $row ); |
238 | | - } |
239 | | - } |
| 143 | + * @return mixed |
| 144 | + */ |
| 145 | + protected function getJSONObject( array $params, Parser $parser ) { |
| 146 | + return $params; |
| 147 | + } |
240 | 148 | |
241 | 149 | /** |
242 | 150 | * Adds the static locations (specified via the staticlocations parameter) to the map. |
243 | 151 | * |
| 152 | + * TODO |
| 153 | + * |
244 | 154 | * @since 0.7 |
245 | 155 | */ |
246 | 156 | protected function addStaticLocations() { |
— | — | @@ -295,195 +205,25 @@ |
296 | 206 | |
297 | 207 | $this->locations[] = $markerData; |
298 | 208 | } |
299 | | - } |
| 209 | + } |
300 | 210 | |
301 | 211 | /** |
302 | | - * This function will loop through all properties (fields) of one record (row), |
303 | | - * and add the location data, title, label and icon to the m_locations array. |
304 | | - * |
305 | | - * TODO: this doesn't qualify as a megamoth just yet, but some splitting up would be nice |
306 | | - * |
| 212 | + * Reads the parameters and gets the query printers output. |
| 213 | + * |
| 214 | + * @param SMWQueryResult $results |
| 215 | + * @param array $params |
307 | 216 | * @param $outputmode |
308 | | - * @param array $row The record you want to add data from |
| 217 | + * |
| 218 | + * @return array |
309 | 219 | */ |
310 | | - protected function addResultRow( $outputmode, array $row ) { |
311 | | - global $wgUser, $smgUseSpatialExtensions, $wgTitle; |
| 220 | + public final function getResult( /* SMWQueryResult */ $results, /* array */ $params, $outputmode ) { |
| 221 | + // Skip checks, results with 0 entries are normal. |
| 222 | + $this->readParameters( $params, $outputmode ); |
312 | 223 | |
313 | | - $skin = $wgUser->getSkin(); |
314 | | - |
315 | | - $title = ''; |
316 | | - $titleForTemplate = ''; |
317 | | - $text = ''; |
318 | | - $lat = ''; |
319 | | - $lon = ''; |
320 | | - |
321 | | - $coords = array(); |
322 | | - $label = array(); |
323 | | - |
324 | | - // Loop throught all fields of the record. |
325 | | - foreach ( $row as $i => $field ) { |
326 | | - $pr = $field->getPrintRequest(); |
327 | | - |
328 | | - // Loop throught all the parts of the field value. |
329 | | - while ( ( $object = $field->getNextObject() ) !== false ) { |
330 | | - if ( $object->getTypeID() == '_wpg' && $i == 0 ) { |
331 | | - if ( $this->showtitle ) $title = $object->getLongText( $outputmode, $skin ); |
332 | | - if ( $this->template ) $titleForTemplate = $object->getLongText( $outputmode, NULL ); |
333 | | - } |
334 | | - |
335 | | - if ( $object->getTypeID() != '_geo' && $i != 0 ) { |
336 | | - if ( $this->template ) { |
337 | | - if ( $object instanceof SMWWikiPageValue ) { |
338 | | - $label[] = $object->getTitle()->getPrefixedText(); |
339 | | - } else { |
340 | | - $label[] = $object->getLongText( $outputmode, $skin ); |
341 | | - } |
342 | | - } |
343 | | - else { |
344 | | - $propertyName = $pr->getHTMLText( $skin ); |
345 | | - if ( $propertyName != '' ) $propertyName .= ': '; |
346 | | - $text .= $propertyName . $object->getLongText( $outputmode, $skin ) . '<br />'; |
347 | | - } |
348 | | - } |
349 | | - |
350 | | - if ( $pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_geo' ) { |
351 | | - $coords[] = $object->getDBkeys(); |
352 | | - } |
353 | | - } |
354 | | - } |
355 | | - |
356 | | - if ( $this->template ) { |
357 | | - // New parser object to render the templates with. |
358 | | - $parser = new Parser(); |
359 | | - } |
360 | | - |
361 | | - foreach ( $coords as $coord ) { |
362 | | - if ( count( $coord ) >= 2 ) { |
363 | | - if ( $smgUseSpatialExtensions ) { |
364 | | - // TODO |
365 | | - } |
366 | | - else { |
367 | | - list( $lat, $lon ) = $coord; |
368 | | - } |
369 | | - |
370 | | - if ( $lat != '' && $lon != '' ) { |
371 | | - $icon = $this->getLocationIcon( $row ); |
372 | | - |
373 | | - if ( $this->template ) { |
374 | | - $segments = array_merge( |
375 | | - array( $this->template, 'title=' . $titleForTemplate, 'latitude=' . $lat, 'longitude=' . $lon ), |
376 | | - $label |
377 | | - ); |
378 | | - |
379 | | - $text = $parser->parse( '{{' . implode( '|', $segments ) . '}}', $wgTitle, new ParserOptions() )->getText(); |
380 | | - } |
381 | | - |
382 | | - $this->locations[] = array( |
383 | | - $lat, |
384 | | - $lon, |
385 | | - $title, |
386 | | - $text, |
387 | | - $icon |
388 | | - ); |
389 | | - } |
390 | | - } |
391 | | - } |
| 224 | + return $this->getResultText( $results, SMW_OUTPUT_HTML ); |
392 | 225 | } |
393 | 226 | |
394 | 227 | /** |
395 | | - * Get the icon for a row. |
396 | | - * |
397 | | - * @param array $row |
398 | | - * |
399 | | - * @return string |
400 | | - */ |
401 | | - private function getLocationIcon( array $row ) { |
402 | | - $icon = ''; |
403 | | - $legend_labels = array(); |
404 | | - |
405 | | - // Look for display_options field, which can be set by Semantic Compound Queries |
406 | | - // the location of this field changed in SMW 1.5 |
407 | | - $display_location = method_exists( $row[0], 'getResultSubject' ) ? $row[0]->getResultSubject() : $row[0]; |
408 | | - |
409 | | - if ( property_exists( $display_location, 'display_options' ) && is_array( $display_location->display_options ) ) { |
410 | | - $display_options = $display_location->display_options; |
411 | | - if ( array_key_exists( 'icon', $display_options ) ) { |
412 | | - $icon = $display_options['icon']; |
413 | | - |
414 | | - // This is somewhat of a hack - if a legend label has been set, we're getting it for every point, instead of just once per icon |
415 | | - if ( array_key_exists( 'legend label', $display_options ) ) { |
416 | | - |
417 | | - $legend_label = $display_options['legend label']; |
418 | | - |
419 | | - if ( ! array_key_exists( $icon, $legend_labels ) ) { |
420 | | - $legend_labels[$icon] = $legend_label; |
421 | | - } |
422 | | - } |
423 | | - } |
424 | | - } // Icon can be set even for regular, non-compound queries If it is, though, we have to translate the name into a URL here |
425 | | - elseif ( $this->icon != '' ) { |
426 | | - $icon = MapsMapper::getImageUrl( $this->icon ); |
427 | | - } |
428 | | - |
429 | | - return $icon; |
430 | | - } |
431 | | - |
432 | | - /** |
433 | | - * Sets the zoom level to the provided value, or when not set, to the default. |
434 | | - */ |
435 | | - protected function setZoom() { |
436 | | - if ( $this->zoomDefaulted ) { |
437 | | - if ( count( $this->locations ) > 1 ) { |
438 | | - $this->zoom = 'null'; |
439 | | - } |
440 | | - } |
441 | | - } |
442 | | - |
443 | | - /** |
444 | | - * Sets the $centre_lat and $centre_lon fields. |
445 | | - * Note: this needs to be done AFTRE the maker coordinates are set. |
446 | | - */ |
447 | | - protected function setCentre() { |
448 | | - // If a centre value is set, use it. |
449 | | - if ( $this->centre != '' ) { |
450 | | - // Geocode and convert if required. |
451 | | - $centre = MapsGeocoders::attemptToGeocode( $this->centre, $this->geoservice, $this->service->getName() ); |
452 | | - |
453 | | - if ( $centre ) { |
454 | | - $this->centreLat = $centre['lat']; |
455 | | - $this->centreLon = $centre['lon']; |
456 | | - } |
457 | | - else { |
458 | | - $this->setCentreDefault(); |
459 | | - } |
460 | | - } |
461 | | - else { |
462 | | - $this->setCentreDefault(); |
463 | | - } |
464 | | - } |
465 | | - |
466 | | - /** |
467 | | - * Figures out the default value for the centre. |
468 | | - */ |
469 | | - private function setCentreDefault() { |
470 | | - if ( count( $this->locations ) > 1 ) { |
471 | | - // If centre is not set, and there are multiple points, set the values to null, to be auto determined by the JS of the mapping API. |
472 | | - $this->centreLat = 'null'; |
473 | | - $this->centreLon = 'null'; |
474 | | - } |
475 | | - elseif ( count( $this->locations ) == 1 ) { |
476 | | - // If centre is not set and there is exactelly one marker, use it's coordinates. |
477 | | - $this->centreLat = Xml::escapeJsString( $this->locations[0][0] ); |
478 | | - $this->centreLon = Xml::escapeJsString( $this->locations[0][1] ); |
479 | | - } |
480 | | - else { |
481 | | - // If centre is not set and there are no results, centre on the default coordinates. |
482 | | - global $egMapsDefaultMapCentre; |
483 | | - // TODO |
484 | | - } |
485 | | - } |
486 | | - |
487 | | - /** |
488 | 228 | * Returns the internationalized name of the mapping service. |
489 | 229 | * |
490 | 230 | * @return string |
— | — | @@ -507,6 +247,6 @@ |
508 | 248 | $params[] = array( 'name' => 'height', 'type' => 'int', 'description' => wfMsgExt( 'semanticmaps_paramdesc_height', 'parsemag', $egMapsMapHeight ) ); |
509 | 249 | |
510 | 250 | return $params; |
511 | | - } |
| 251 | + } |
512 | 252 | |
513 | 253 | } |
\ No newline at end of file |