Index: trunk/extensions/Maps/Features/DisplayPoint/Maps_DisplayPoint.php |
— | — | @@ -0,0 +1,71 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * File holding the registration and handling functions for the display_point parser function.
|
| 6 | + *
|
| 7 | + * @file Maps_DisplayPoint.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + */
|
| 12 | +
|
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) {
|
| 14 | + die( 'Not an entry point.' );
|
| 15 | +}
|
| 16 | +
|
| 17 | +$wgAutoloadClasses['MapsDisplayPoint'] = __FILE__;
|
| 18 | +$wgAutoloadClasses['MapsBasePointMap'] = dirname( __FILE__ ) . '/Maps_BasePointMap.php';
|
| 19 | +
|
| 20 | +if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
|
| 21 | + $wgHooks['LanguageGetMagic'][] = 'efMapsDisplayPointMagic';
|
| 22 | +}
|
| 23 | +$wgHooks['ParserFirstCallInit'][] = 'efMapsRegisterDisplayPoint';
|
| 24 | +
|
| 25 | +$egMapsFeatures['pf'][] = 'MapsDisplayPoint::initialize';
|
| 26 | +
|
| 27 | +/**
|
| 28 | + * Adds the magic words for the parser functions.
|
| 29 | + */
|
| 30 | +function efMapsDisplayPointMagic( &$magicWords, $langCode ) {
|
| 31 | + // The display_address(es) aliases are for backward compatibility only, and will be removed eventually.
|
| 32 | + $magicWords['display_point'] = array( 0, 'display_point', 'display_points' );
|
| 33 | +
|
| 34 | + return true; // Unless we return true, other parser functions won't get loaded.
|
| 35 | +}
|
| 36 | +
|
| 37 | +/**
|
| 38 | + * Adds the parser function hooks
|
| 39 | + */
|
| 40 | +function efMapsRegisterDisplayPoint( &$wgParser ) {
|
| 41 | + // Hooks to enable the '#display_point' and '#display_points' parser functions.
|
| 42 | + $wgParser->setFunctionHook( 'display_point', array( 'MapsDisplayPoint', 'displayPointRender' ) );
|
| 43 | +
|
| 44 | + return true;
|
| 45 | +}
|
| 46 | +
|
| 47 | +/**
|
| 48 | + * Class containing the rendering functions for the display_point parser function.
|
| 49 | + *
|
| 50 | + * @author Jeroen De Dauw
|
| 51 | + *
|
| 52 | + */
|
| 53 | +final class MapsDisplayPoint {
|
| 54 | +
|
| 55 | + public static $parameters = array();
|
| 56 | +
|
| 57 | + public static function initialize() {
|
| 58 | + }
|
| 59 | +
|
| 60 | + /**
|
| 61 | + * Returns the output for a display_point call.
|
| 62 | + *
|
| 63 | + * @param unknown_type $parser
|
| 64 | + *
|
| 65 | + * @return array
|
| 66 | + */
|
| 67 | + public static function displayPointRender( &$parser ) {
|
| 68 | + $args = func_get_args();
|
| 69 | + return MapsParserFunctions::getMapHtml( $parser, $args, 'display_point' );
|
| 70 | + }
|
| 71 | +
|
| 72 | +} |
\ No newline at end of file |
Index: trunk/extensions/Maps/Features/DisplayPoint/Maps_BasePointMap.php |
— | — | @@ -0,0 +1,244 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * File holding class MapsBasePointMap.
|
| 6 | + *
|
| 7 | + * @file Maps_BasePointMap.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + */
|
| 12 | +
|
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) {
|
| 14 | + die( 'Not an entry point.' );
|
| 15 | +}
|
| 16 | +
|
| 17 | +/**
|
| 18 | + * Abstract class MapsBasePointMap provides the scafolding for classes handling display_point(s)
|
| 19 | + * calls for a spesific mapping service. It inherits from MapsMapFeature and therefore forces
|
| 20 | + * inheriting classes to implement sereveral methods.
|
| 21 | + *
|
| 22 | + * @ingroup Maps
|
| 23 | + *
|
| 24 | + * @author Jeroen De Dauw
|
| 25 | + */
|
| 26 | +abstract class MapsBasePointMap implements iMapParserFunction {
|
| 27 | +
|
| 28 | + public $serviceName;
|
| 29 | +
|
| 30 | + protected $centreLat, $centreLon;
|
| 31 | +
|
| 32 | + protected $output = '';
|
| 33 | +
|
| 34 | + protected $spesificParameters = false;
|
| 35 | + protected $featureParameters = false;
|
| 36 | +
|
| 37 | + private $markerData = array();
|
| 38 | + protected $markerString;
|
| 39 | +
|
| 40 | + /**
|
| 41 | + * Sets the map properties as class fields.
|
| 42 | + *
|
| 43 | + * @param array $mapProperties
|
| 44 | + */
|
| 45 | + protected function setMapProperties( array $mapProperties ) {
|
| 46 | + foreach ( $mapProperties as $paramName => $paramValue ) {
|
| 47 | + if ( !property_exists( __CLASS__, $paramName ) ) {
|
| 48 | + $this-> { $paramName } = $paramValue;
|
| 49 | + }
|
| 50 | + else {
|
| 51 | + // If this happens in any way, it could be a big vunerability, so throw an exception.
|
| 52 | + throw new Exception( 'Attempt to override a class field during map property assignment. Field name: ' . $paramName );
|
| 53 | + }
|
| 54 | + }
|
| 55 | + }
|
| 56 | +
|
| 57 | + /**
|
| 58 | + * @return array
|
| 59 | + */
|
| 60 | + public function getSpecificParameterInfo() {
|
| 61 | + return array();
|
| 62 | + }
|
| 63 | +
|
| 64 | + /**
|
| 65 | + * @return array
|
| 66 | + */
|
| 67 | + public function getFeatureParameters() {
|
| 68 | + global $egMapsDefaultServices, $egMapsDefaultTitle, $egMapsDefaultLabel;
|
| 69 | +
|
| 70 | + return array(
|
| 71 | + 'service' => array(
|
| 72 | + 'default' => $egMapsDefaultServices['display_point']
|
| 73 | + ),
|
| 74 | + 'centre' => array(
|
| 75 | + 'aliases' => array( 'center' ),
|
| 76 | + ),
|
| 77 | + 'title' => array(
|
| 78 | + 'default' => $egMapsDefaultTitle
|
| 79 | + ),
|
| 80 | + 'label' => array(
|
| 81 | + 'default' => $egMapsDefaultLabel
|
| 82 | + ),
|
| 83 | + 'icon' => array(
|
| 84 | + 'criteria' => array(
|
| 85 | + 'not_empty' => array()
|
| 86 | + )
|
| 87 | + ),
|
| 88 | + 'coordinates' => array(
|
| 89 | + 'required' => true,
|
| 90 | + 'type' => array( 'string', 'list', ';' ),
|
| 91 | + 'aliases' => array( 'coords', 'location', 'locations' ),
|
| 92 | + 'criteria' => array(
|
| 93 | + 'are_locations' => array()
|
| 94 | + ),
|
| 95 | + 'output-type' => 'coordinateSets',
|
| 96 | + ),
|
| 97 | + );
|
| 98 | + }
|
| 99 | +
|
| 100 | + /**
|
| 101 | + * Handles the request from the parser hook by doing the work that's common for all
|
| 102 | + * mapping services, calling the specific methods and finally returning the resulting output.
|
| 103 | + *
|
| 104 | + * @param Parser $parser
|
| 105 | + * @param array $params
|
| 106 | + *
|
| 107 | + * @return html
|
| 108 | + */
|
| 109 | + public final function getMapHtml( Parser &$parser, array $params ) {
|
| 110 | + $this->featureParameters = MapsDisplayPoint::$parameters;
|
| 111 | +
|
| 112 | + $this->doMapServiceLoad();
|
| 113 | +
|
| 114 | + $this->setMapProperties( $params );
|
| 115 | +
|
| 116 | + $this->setMarkerData( $parser );
|
| 117 | +
|
| 118 | + $this->createMarkerString();
|
| 119 | +
|
| 120 | + $this->setCentre();
|
| 121 | +
|
| 122 | + if ( count( $this->markerData ) <= 1 && $this->zoom == 'null' ) {
|
| 123 | + $this->zoom = $this->getDefaultZoom();
|
| 124 | + }
|
| 125 | +
|
| 126 | + $this->addSpecificMapHTML( $parser );
|
| 127 | +
|
| 128 | + return $this->output;
|
| 129 | + }
|
| 130 | +
|
| 131 | + /**
|
| 132 | + * Fills the $markerData array with the locations and their meta data.
|
| 133 | + *
|
| 134 | + * @param unknown_type $parser
|
| 135 | + */
|
| 136 | + private function setMarkerData( $parser ) {
|
| 137 | + $this->title = Xml::escapeJsString( $parser->recursiveTagParse( $this->title ) );
|
| 138 | + $this->label = Xml::escapeJsString( $parser->recursiveTagParse( $this->label ) );
|
| 139 | +
|
| 140 | + foreach ( $this->coordinates as $coordinates ) {
|
| 141 | + $args = explode( '~', $coordinates );
|
| 142 | +
|
| 143 | + $markerData = MapsCoordinateParser::parseCoordinates( $args[0] );
|
| 144 | +
|
| 145 | + if ( !$markerData ) continue;
|
| 146 | +
|
| 147 | + if ( count( $args ) > 1 ) {
|
| 148 | + // Parse and add the point specific title if it's present.
|
| 149 | + $markerData['title'] = $parser->recursiveTagParse( $args[1] );
|
| 150 | +
|
| 151 | + if ( count( $args ) > 2 ) {
|
| 152 | + // Parse and add the point specific label if it's present.
|
| 153 | + $markerData['label'] = $parser->recursiveTagParse( $args[2] );
|
| 154 | +
|
| 155 | + if ( count( $args ) > 3 ) {
|
| 156 | + // Add the point specific icon if it's present.
|
| 157 | + $markerData['icon'] = $args[3];
|
| 158 | + }
|
| 159 | + }
|
| 160 | + }
|
| 161 | +
|
| 162 | + // If there is no point specific icon, use the general icon parameter when available.
|
| 163 | + if ( ! array_key_exists( 'icon', $markerData ) && strlen( $this->icon ) > 0 ) $markerData['icon'] = $this->icon;
|
| 164 | +
|
| 165 | + // Get the url for the icon when there is one, else set the icon to an empty string.
|
| 166 | + if ( array_key_exists( 'icon', $markerData ) ) {
|
| 167 | + $icon_image_page = new ImagePage( Title::newFromText( $markerData['icon'] ) );
|
| 168 | + $markerData['icon'] = $icon_image_page->getDisplayedFile()->getURL();
|
| 169 | + }
|
| 170 | + else {
|
| 171 | + $markerData['icon'] = '';
|
| 172 | + }
|
| 173 | +
|
| 174 | + $this->markerData[] = $markerData;
|
| 175 | + }
|
| 176 | + }
|
| 177 | +
|
| 178 | + /**
|
| 179 | + * Creates a JS string with the marker data. Takes care of escaping the used values.
|
| 180 | + */
|
| 181 | + private function createMarkerString() {
|
| 182 | + $markerItems = array();
|
| 183 | +
|
| 184 | + foreach ( $this->markerData as $markerData ) {
|
| 185 | + $title = array_key_exists( 'title', $markerData ) ? Xml::escapeJsString( $markerData['title'] ) : $this->title;
|
| 186 | + $label = array_key_exists( 'label', $markerData ) ? Xml::escapeJsString( $markerData['label'] ) : $this->label;
|
| 187 | +
|
| 188 | + $markerData['lon'] = Xml::escapeJsString( $markerData['lon'] );
|
| 189 | + $markerData['lat'] = Xml::escapeJsString( $markerData['lat'] );
|
| 190 | + $markerData['icon'] = Xml::escapeJsString( $markerData['icon'] );
|
| 191 | +
|
| 192 | + $markerItems[] = str_replace( array( 'lon', 'lat', 'title', 'label', 'icon' ),
|
| 193 | + array( $markerData['lon'], $markerData['lat'], $title, $label, $markerData['icon'] ),
|
| 194 | + $this->markerStringFormat
|
| 195 | + );
|
| 196 | + }
|
| 197 | +
|
| 198 | + $this->markerString = implode( ',', $markerItems );
|
| 199 | + }
|
| 200 | +
|
| 201 | + /**
|
| 202 | + * Sets the $centre_lat and $centre_lon fields.
|
| 203 | + * Note: this needs to be done AFTRE the maker coordinates are set.
|
| 204 | + */
|
| 205 | + private function setCentre() {
|
| 206 | + if ( empty( $this->centre ) ) {
|
| 207 | + if ( count( $this->markerData ) == 1 ) {
|
| 208 | + // If centre is not set and there is exactelly one marker, use it's coordinates.
|
| 209 | + $this->centreLat = Xml::escapeJsString( $this->markerData[0]['lat'] );
|
| 210 | + $this->centreLon = Xml::escapeJsString( $this->markerData[0]['lon'] );
|
| 211 | + }
|
| 212 | + elseif ( count( $this->markerData ) > 1 ) {
|
| 213 | + // If centre is not set and there are multiple markers, set the values to null,
|
| 214 | + // to be auto determined by the JS of the mapping API.
|
| 215 | + $this->centreLat = 'null';
|
| 216 | + $this->centreLon = 'null';
|
| 217 | + }
|
| 218 | + else {
|
| 219 | + // If centre is not set and there are no markers, use the default latitude and longitutde.
|
| 220 | + $this->setCentreDefaults();
|
| 221 | + }
|
| 222 | + }
|
| 223 | + else { // If a centre value is set, geocode when needed and use it.
|
| 224 | + $this->centre = MapsGeocoder::attemptToGeocode( $this->centre, $this->geoservice, $this->serviceName );
|
| 225 | +
|
| 226 | + // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde.
|
| 227 | + if ( $this->centre ) {
|
| 228 | + $this->centreLat = Xml::escapeJsString( $this->centre['lat'] );
|
| 229 | + $this->centreLon = Xml::escapeJsString( $this->centre['lon'] );
|
| 230 | + }
|
| 231 | + else { // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used.
|
| 232 | + $this->setCentreDefaults();
|
| 233 | + }
|
| 234 | + }
|
| 235 | + }
|
| 236 | +
|
| 237 | + /**
|
| 238 | + * Sets the centre latitude and longitutde to the defaults.
|
| 239 | + */
|
| 240 | + private function setCentreDefaults() {
|
| 241 | + global $egMapsMapLat, $egMapsMapLon;
|
| 242 | + $this->centreLat = $egMapsMapLat;
|
| 243 | + $this->centreLon = $egMapsMapLon;
|
| 244 | + }
|
| 245 | +}
|
Index: trunk/extensions/Maps/Features/Maps_iMapParserFunction.php |
— | — | @@ -0,0 +1,36 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * File holding interface iMapParserFunction.
|
| 6 | + *
|
| 7 | + * @file Maps_iMapParserFunction.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + *
|
| 12 | + * TODO: revise this interface
|
| 13 | + */
|
| 14 | +
|
| 15 | +if ( !defined( 'MEDIAWIKI' ) ) {
|
| 16 | + die( 'Not an entry point.' );
|
| 17 | +}
|
| 18 | +
|
| 19 | +/**
|
| 20 | + * Interface that should be implemented by all mapping feature classes.
|
| 21 | + *
|
| 22 | + * @author Jeroen De Dauw
|
| 23 | + */
|
| 24 | +interface iMapParserFunction {
|
| 25 | + function getMapHtml( Parser &$parser, array $params );
|
| 26 | +
|
| 27 | + /**
|
| 28 | + * Map service specific map count and loading of dependencies.
|
| 29 | + */
|
| 30 | + function doMapServiceLoad();
|
| 31 | +
|
| 32 | + /**
|
| 33 | + * Adds the HTML specific to the mapping service to the output.
|
| 34 | + */
|
| 35 | + function addSpecificMapHTML( Parser $parser );
|
| 36 | +}
|
| 37 | +
|
Index: trunk/extensions/Maps/Features/DisplayMap/Maps_DisplayMap.php |
— | — | @@ -0,0 +1,70 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * File holding the registration and handling functions for the display_map parser function.
|
| 6 | + *
|
| 7 | + * @file Maps_DisplayMap.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + */
|
| 12 | +
|
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) {
|
| 14 | + die( 'Not an entry point.' );
|
| 15 | +}
|
| 16 | +
|
| 17 | +$wgAutoloadClasses['MapsDisplayMap'] = __FILE__;
|
| 18 | +$wgAutoloadClasses['MapsBaseMap'] = dirname( __FILE__ ) . '/Maps_BaseMap.php';
|
| 19 | +
|
| 20 | +if ( version_compare( $wgVersion, '1.16alpha', '<' ) ) {
|
| 21 | + $wgHooks['LanguageGetMagic'][] = 'efMapsDisplayMapMagic';
|
| 22 | +}
|
| 23 | +$wgHooks['ParserFirstCallInit'][] = 'efMapsRegisterDisplayMap';
|
| 24 | +
|
| 25 | +$egMapsFeatures['pf'][] = 'MapsDisplayMap::initialize';
|
| 26 | +
|
| 27 | +/**
|
| 28 | + * Adds the magic words for the parser functions.
|
| 29 | + */
|
| 30 | +function efMapsDisplayMapMagic( &$magicWords, $langCode ) {
|
| 31 | + $magicWords['display_map'] = array( 0, 'display_map' );
|
| 32 | +
|
| 33 | + return true; // Unless we return true, other parser functions won't get loaded.
|
| 34 | +}
|
| 35 | +
|
| 36 | +/**
|
| 37 | + * Adds the parser function hooks
|
| 38 | + */
|
| 39 | +function efMapsRegisterDisplayMap( &$wgParser ) {
|
| 40 | + // A hook to enable the '#display_map' parser function.
|
| 41 | + $wgParser->setFunctionHook( 'display_map', array( 'MapsDisplayMap', 'displayMapRender' ) );
|
| 42 | +
|
| 43 | + return true;
|
| 44 | +}
|
| 45 | +
|
| 46 | +/**
|
| 47 | + * Class containing the rendering functions for the display_map parser function.
|
| 48 | + *
|
| 49 | + * @author Jeroen De Dauw
|
| 50 | + *
|
| 51 | + */
|
| 52 | +final class MapsDisplayMap {
|
| 53 | +
|
| 54 | + public static $parameters = array();
|
| 55 | +
|
| 56 | + public static function initialize() {
|
| 57 | + }
|
| 58 | +
|
| 59 | + /**
|
| 60 | + * Returns the output for a display_map call.
|
| 61 | + *
|
| 62 | + * @param unknown_type $parser
|
| 63 | + *
|
| 64 | + * @return array
|
| 65 | + */
|
| 66 | + public static function displayMapRender( &$parser ) {
|
| 67 | + $args = func_get_args();
|
| 68 | + return MapsParserFunctions::getMapHtml( $parser, $args, 'display_map' );
|
| 69 | + }
|
| 70 | +
|
| 71 | +} |
\ No newline at end of file |
Index: trunk/extensions/Maps/Features/DisplayMap/Maps_BaseMap.php |
— | — | @@ -0,0 +1,134 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * File holding class MapsBaseMap.
|
| 6 | + *
|
| 7 | + * @file Maps_BaseMap.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + */
|
| 12 | +
|
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) {
|
| 14 | + die( 'Not an entry point.' );
|
| 15 | +}
|
| 16 | +
|
| 17 | +/**
|
| 18 | + * Abstract class MapsBaseMap provides the scafolding for classes handling display_map
|
| 19 | + * calls for a spesific mapping service. It inherits from MapsMapFeature and therefore
|
| 20 | + * forces inheriting classes to implement sereveral methods.
|
| 21 | + *
|
| 22 | + * @ingroup Maps
|
| 23 | + *
|
| 24 | + * @author Jeroen De Dauw
|
| 25 | + */
|
| 26 | +abstract class MapsBaseMap implements iMapParserFunction {
|
| 27 | +
|
| 28 | + public $serviceName;
|
| 29 | +
|
| 30 | + protected $centreLat, $centreLon;
|
| 31 | +
|
| 32 | + protected $output = '';
|
| 33 | +
|
| 34 | + protected $spesificParameters = false;
|
| 35 | + protected $featureParameters = false;
|
| 36 | +
|
| 37 | + /**
|
| 38 | + * Sets the map properties as class fields.
|
| 39 | + *
|
| 40 | + * @param array $mapProperties
|
| 41 | + */
|
| 42 | + protected function setMapProperties( array $mapProperties ) {
|
| 43 | + foreach ( $mapProperties as $paramName => $paramValue ) {
|
| 44 | + if ( !property_exists( __CLASS__, $paramName ) ) {
|
| 45 | + $this-> { $paramName } = $paramValue;
|
| 46 | + }
|
| 47 | + else {
|
| 48 | + // If this happens in any way, it could be a big vunerability, so throw an exception.
|
| 49 | + throw new Exception( 'Attempt to override a class field during map property assignment. Field name: ' . $paramName );
|
| 50 | + }
|
| 51 | + }
|
| 52 | + }
|
| 53 | +
|
| 54 | + /**
|
| 55 | + * @return array
|
| 56 | + */
|
| 57 | + public function getSpecificParameterInfo() {
|
| 58 | + return array();
|
| 59 | + }
|
| 60 | +
|
| 61 | + /**
|
| 62 | + * @return array
|
| 63 | + */
|
| 64 | + public function getFeatureParameters() {
|
| 65 | + global $egMapsDefaultServices;
|
| 66 | +
|
| 67 | + return array(
|
| 68 | + 'service' => array(
|
| 69 | + 'default' => $egMapsDefaultServices['display_map']
|
| 70 | + ),
|
| 71 | + 'coordinates' => array(
|
| 72 | + 'required' => true,
|
| 73 | + 'aliases' => array( 'coords', 'location', 'locations' ),
|
| 74 | + 'criteria' => array(
|
| 75 | + 'is_location' => array()
|
| 76 | + ),
|
| 77 | + 'output-type' => 'coordinateSet',
|
| 78 | + ),
|
| 79 | + );
|
| 80 | + }
|
| 81 | +
|
| 82 | + /**
|
| 83 | + * Handles the request from the parser hook by doing the work that's common for all
|
| 84 | + * mapping services, calling the specific methods and finally returning the resulting output.
|
| 85 | + *
|
| 86 | + * @param unknown_type $parser
|
| 87 | + * @param array $params
|
| 88 | + *
|
| 89 | + * @return html
|
| 90 | + */
|
| 91 | + public final function getMapHtml( Parser &$parser, array $params ) {
|
| 92 | + $this->featureParameters = MapsDisplayMap::$parameters;
|
| 93 | +
|
| 94 | + $this->doMapServiceLoad();
|
| 95 | +
|
| 96 | + $this->setMapProperties( $params );
|
| 97 | +
|
| 98 | + $this->setCentre();
|
| 99 | +
|
| 100 | + if ( $this->zoom == 'null' ) {
|
| 101 | + $htis->zoom = $this->getDefaultZoom();
|
| 102 | + }
|
| 103 | +
|
| 104 | + $this->addSpecificMapHTML( $parser );
|
| 105 | +
|
| 106 | + return $this->output;
|
| 107 | + }
|
| 108 | +
|
| 109 | + /**
|
| 110 | + * Sets the $centre_lat and $centre_lon fields.
|
| 111 | + */
|
| 112 | + private function setCentre() {
|
| 113 | + if ( empty( $this->coordinates ) ) { // If centre is not set, use the default latitude and longitutde.
|
| 114 | + global $egMapsMapLat, $egMapsMapLon;
|
| 115 | + $this->centreLat = $egMapsMapLat;
|
| 116 | + $this->centreLon = $egMapsMapLon;
|
| 117 | + }
|
| 118 | + else { // If a centre value is set, geocode when needed and use it.
|
| 119 | + $this->coordinates = MapsGeocoder::attemptToGeocode( $this->coordinates, $this->geoservice, $this->serviceName );
|
| 120 | +
|
| 121 | + // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde.
|
| 122 | + if ( $this->coordinates ) {
|
| 123 | + $this->centreLat = Xml::escapeJsString( $this->coordinates['lat'] );
|
| 124 | + $this->centreLon = Xml::escapeJsString( $this->coordinates['lon'] );
|
| 125 | + }
|
| 126 | + else { // If it's false, the coordinate was invalid, or geocoding failed. Either way, the default's should be used.
|
| 127 | + // TODO: Some warning this failed would be nice here.
|
| 128 | + global $egMapsMapLat, $egMapsMapLon;
|
| 129 | + $this->centreLat = $egMapsMapLat;
|
| 130 | + $this->centreLon = $egMapsMapLon;
|
| 131 | + }
|
| 132 | + }
|
| 133 | + }
|
| 134 | +
|
| 135 | +}
|
Index: trunk/extensions/Maps/Features/Maps_ParserFunctions.php |
— | — | @@ -0,0 +1,136 @@ |
| 2 | +<?php
|
| 3 | +
|
| 4 | +/**
|
| 5 | + * Initialization file for parser function functionality in the Maps extension
|
| 6 | + *
|
| 7 | + * @file Maps_ParserFunctions.php
|
| 8 | + * @ingroup Maps
|
| 9 | + *
|
| 10 | + * @author Jeroen De Dauw
|
| 11 | + */
|
| 12 | +
|
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) {
|
| 14 | + die( 'Not an entry point.' );
|
| 15 | +}
|
| 16 | +
|
| 17 | +$wgAutoloadClasses['MapsParserFunctions'] = __FILE__;
|
| 18 | +
|
| 19 | +$wgHooks['MappingFeatureLoad'][] = 'MapsParserFunctions::initialize';
|
| 20 | +
|
| 21 | +/**
|
| 22 | + * A class that holds handlers for the mapping parser functions.
|
| 23 | + *
|
| 24 | + * @author Jeroen De Dauw
|
| 25 | + */
|
| 26 | +final class MapsParserFunctions {
|
| 27 | +
|
| 28 | + /**
|
| 29 | + * Initialize the parser functions feature. This function handles the parser function hook,
|
| 30 | + * and will load the required classes.
|
| 31 | + */
|
| 32 | + public static function initialize() {
|
| 33 | + global $egMapsDir, $egMapsFeatures;
|
| 34 | +
|
| 35 | + include_once dirname( __FILE__ ) . '/Maps_iMapParserFunction.php';
|
| 36 | +
|
| 37 | + // This runs a small hook that enables parser functions to run initialization code.
|
| 38 | + foreach ( $egMapsFeatures['pf'] as $hook ) {
|
| 39 | + call_user_func( $hook );
|
| 40 | + }
|
| 41 | +
|
| 42 | + return true;
|
| 43 | + }
|
| 44 | +
|
| 45 | + /**
|
| 46 | + * Returns the output for the call to the specified parser function.
|
| 47 | + *
|
| 48 | + * @param Parser $parser
|
| 49 | + * @param array $params
|
| 50 | + * @param string $parserFunction
|
| 51 | + *
|
| 52 | + * @return array
|
| 53 | + */
|
| 54 | + public static function getMapHtml( Parser &$parser, array $args, $parserFunction ) {
|
| 55 | + global $wgLang, $egValidatorErrorLevel, $egValidatorFatalLevel, $egMapsServices;
|
| 56 | +
|
| 57 | + array_shift( $args ); // We already know the $parser.
|
| 58 | +
|
| 59 | + $parameters = array();
|
| 60 | + $setService = false;
|
| 61 | +
|
| 62 | + foreach( $args as $arg ) {
|
| 63 | + $split = explode( '=', $arg );
|
| 64 | + $name = strtolower( trim( array_shift( $split ) ) );
|
| 65 | + if ( count( $split ) > 1 && self::inParamAliases( $name, 'service', MapsMapper::getCommonParameters() ) ) {
|
| 66 | + if ( !$setService ) {
|
| 67 | + $service = implode( '=', $split );
|
| 68 | + $parameters = 'service=' . $service;
|
| 69 | + $setService = true;
|
| 70 | + }
|
| 71 | + } else {
|
| 72 | + $parameters[] = $arg;
|
| 73 | + }
|
| 74 | + }
|
| 75 | +
|
| 76 | + $service = MapsMapper::getValidService( $setService ? $service : '', $parserFunction );
|
| 77 | +
|
| 78 | + $mapClass = new $egMapsServices[$service]['features'][$parserFunction]();
|
| 79 | +
|
| 80 | + $manager = new ValidatorManager();
|
| 81 | +
|
| 82 | + /*
|
| 83 | + * Assembliy of the allowed parameters and their information.
|
| 84 | + * The main parameters (the ones that are shared by everything) are overidden
|
| 85 | + * by the feature parameters (the ones spesific to a feature). The result is then
|
| 86 | + * again overidden by the service parameters (the ones spesific to the service),
|
| 87 | + * and finally by the spesific parameters (the ones spesific to a service-feature combination).
|
| 88 | + */
|
| 89 | + $parameterInfo = array_merge_recursive( MapsMapper::getCommonParameters(), $mapClass->getFeatureParameters() );
|
| 90 | + $parameterInfo = array_merge_recursive( $parameterInfo, $egMapsServices[$service]['parameters'] );
|
| 91 | + $parameterInfo = array_merge_recursive( $parameterInfo, $mapClass->getSpecificParameterInfo() );
|
| 92 | +
|
| 93 | + $parameters = $manager->manageParameters(
|
| 94 | + $parameters,
|
| 95 | + $parameterInfo,
|
| 96 | + array( 'coordinates' )
|
| 97 | + );
|
| 98 | +
|
| 99 | + $displayMap = $parameters !== false;
|
| 100 | +
|
| 101 | + if ( $displayMap ) {
|
| 102 | + // Call the function according to the map service to get the HTML output.
|
| 103 | + $output = $mapClass->getMapHtml( $parser, $parameters ) . $manager->getErrorList();
|
| 104 | + } else {
|
| 105 | + // TODO: Get failiures
|
| 106 | + if ( $egValidatorFatalLevel == Validator_ERRORS_WARN ) {
|
| 107 | + $output .= htmlspecialchars( wfMsg( '' ) );
|
| 108 | + } elseif ( $egValidatorFatalLevel > Validator_ERRORS_WARN ) {
|
| 109 | + $output .= htmlspecialchars( wfMsg( '' ) );
|
| 110 | + }
|
| 111 | + }
|
| 112 | +
|
| 113 | + // Return the result.
|
| 114 | + return array( $output, 'noparse' => true, 'isHTML' => true );
|
| 115 | + }
|
| 116 | +
|
| 117 | + /**
|
| 118 | + * Gets if a provided name is present in the aliases array of a parameter
|
| 119 | + * name in the $mainParams array.
|
| 120 | + *
|
| 121 | + * @param string $name The name you want to check for.
|
| 122 | + * @param string $mainParamName The main parameter name.
|
| 123 | + * @param array $paramInfo Contains meta data, including aliases, of the possible parameters.
|
| 124 | + * @param boolean $compareMainName Boolean indicating wether the main name should also be compared.
|
| 125 | + *
|
| 126 | + * @return boolean
|
| 127 | + */
|
| 128 | + public static function inParamAliases( $name, $mainParamName, array $paramInfo = array(), $compareMainName = true ) {
|
| 129 | + $equals = $compareMainName && $mainParamName == $name;
|
| 130 | +
|
| 131 | + if ( !$equals && array_key_exists( $mainParamName, $paramInfo ) ) {
|
| 132 | + $equals = in_array( $name, $paramInfo[$mainParamName] );
|
| 133 | + }
|
| 134 | +
|
| 135 | + return $equals;
|
| 136 | + }
|
| 137 | +} |
\ No newline at end of file |