r65275 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65274‎ | r65275 | r65276 >
Date:12:52, 19 April 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r65108 - Fixed smart geocoding in display_map and display_point(s)
Modified paths:
  • /trunk/extensions/Maps/Geocoders/Maps_Geocoder.php (modified) (history)
  • /trunk/extensions/Maps/Maps.i18n.magic.php (modified) (history)
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/Maps_CoordinateParser.php (modified) (history)
  • /trunk/extensions/Maps/Maps_MapFeature.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Mapper.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/Geocode/Maps_GeocodeFunctions.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Maps.i18n.magic.php
@@ -9,6 +9,8 @@
1010 'geocode' => array( 0, 'geocode' ),
1111 'geocodelat' => array( 0, 'geocodelat' ),
1212 'geocodelon' => array( 0, 'geocodelon' ),
 13+ 'geodistance' => array( 0, 'geodistance' ),
 14+ 'finddestination' => array( 0, 'finddestination' ),
1315 );
1416
1517 $magicWords['ar'] = array(
Index: trunk/extensions/Maps/ParserFunctions/GeoFunctions/Maps_GeoFunctions.php
@@ -77,7 +77,7 @@
7878 $doCalculation = $parameters !== false;
7979
8080 if ( $doCalculation ) {
81 - $canGeocode = self::geocoderIsAvailable();
 81+ $canGeocode = MapsMapper::geocoderIsAvailable();
8282
8383 if ( $canGeocode ) {
8484 $start = MapsGeocoder::attemptToGeocode( $parameters['location1'] );
@@ -195,7 +195,7 @@
196196 $doCalculation = $parameters !== false;
197197
198198 if ( $doCalculation ) {
199 - $canGeocode = self::geocoderIsAvailable();
 199+ $canGeocode = MapsMapper::geocoderIsAvailable();
200200
201201 if ( $canGeocode ) {
202202 $location = MapsGeocoder::attemptToGeocode( $parameters['location'] );
@@ -298,15 +298,5 @@
299299 );
300300 }
301301
302 - /**
303 - * Returns a boolean indicating if MapsGeocoder is available.
304 - *
305 - * @return Boolean
306 - */
307 - private static function geocoderIsAvailable() {
308 - global $wgAutoloadClasses;
309 - return array_key_exists( 'MapsGeocoder', $wgAutoloadClasses );
310 - }
311 -
312302 }
313303
Index: trunk/extensions/Maps/ParserFunctions/Geocode/Maps_GeocodeFunctions.php
@@ -145,7 +145,7 @@
146146 * @return string
147147 */
148148 public static function renderGeocoderLat( Parser &$parser, $address, $service = '', $mappingService = '' ) {
149 - if ( self::geocoderIsAvailable() ) $geovalues = MapsGeocoder::geocode( $address, $service, $mappingService );
 149+ if ( MapsMapper::geocoderIsAvailable() ) $geovalues = MapsGeocoder::geocode( $address, $service, $mappingService );
150150 return $geovalues ? $geovalues['lat'] : '';
151151 }
152152
@@ -161,11 +161,8 @@
162162 * @return string
163163 */
164164 public static function renderGeocoderLon( Parser &$parser, $address, $service = '', $mappingService = '' ) {
165 - if ( self::geocoderIsAvailable() ) $geovalues = MapsGeocoder::geocode( $address, $service, $mappingService );
 165+ if ( MapsMapper::geocoderIsAvailable() ) $geovalues = MapsGeocoder::geocode( $address, $service, $mappingService );
166166 return $geovalues ? $geovalues['lon'] : '';
167167 }
168168
169 -}
170 -
171 -
172 -
 169+}
\ No newline at end of file
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php
@@ -31,7 +31,7 @@
3232 * and will load the required classes.
3333 */
3434 public static function initialize() {
35 - global $egMapsDir, $IP, $wgAutoloadClasses, $egMapsFeatures, $egMapsServices;
 35+ global $egMapsDir, $egMapsFeatures;
3636
3737 include_once $egMapsDir . 'ParserFunctions/Maps_iDisplayFunction.php';
3838
@@ -44,7 +44,6 @@
4545 }
4646
4747 private static function initializeParams() {
48 - global $egMapsAvailableServices, $egMapsDefaultServices, $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
4948 }
5049
5150 /**
@@ -110,7 +109,12 @@
111110 // Call the function according to the map service to get the HTML output.
112111 $output = $mapClass->displayMap( $parser, $parameters ) . $manager->getErrorList();
113112 } else {
114 - // TODO: add errors to output depending on validator fatal level
 113+ // TODO: Get failiures
 114+ if ( $egValidatorFatalLevel == Validator_ERRORS_WARN ) {
 115+ $output .= htmlspecialchars( wfMsg( '' ) );
 116+ } elseif ( $egValidatorFatalLevel > Validator_ERRORS_WARN ) {
 117+ $output .= htmlspecialchars( wfMsg( '' ) );
 118+ }
115119 }
116120
117121 // Return the result.
@@ -131,8 +135,8 @@
132136 public static function inParamAliases( $name, $mainParamName, array $paramInfo = array(), $compareMainName = true ) {
133137 $equals = $compareMainName && $mainParamName == $name;
134138
135 - if ( array_key_exists( $mainParamName, $paramInfo ) ) {
136 - $equals = $equals || in_array( $name, $paramInfo[$mainParamName] );
 139+ if ( !$equals && array_key_exists( $mainParamName, $paramInfo ) ) {
 140+ $equals = in_array( $name, $paramInfo[$mainParamName] );
137141 }
138142
139143 return $equals;
Index: trunk/extensions/Maps/Maps.php
@@ -33,7 +33,7 @@
3434 echo '<b>Warning:</b> You need to have <a href="http://www.mediawiki.org/wiki/Extension:Validator">Validator</a> installed in order to use <a href="http://www.mediawiki.org/wiki/Extension:Maps">Maps</a>.';
3535 }
3636 else {
37 - define( 'Maps_VERSION', '0.6 a13' );
 37+ define( 'Maps_VERSION', '0.6 a14' );
3838
3939 // The different coordinate notations.
4040 define( 'Maps_COORDS_FLOAT', 'float' );
Index: trunk/extensions/Maps/Maps_Mapper.php
@@ -23,6 +23,20 @@
2424 Validator::addOutputFormat( 'mapdimension', array( __CLASS__, 'setMapDimension' ) );
2525 Validator::addValidationFunction( 'is_map_dimension', array( __CLASS__, 'isMapDimension' ) );
2626
 27+ // Take care of maybe not having the geocoder available here.
 28+ // This is done by hooking a MapsGeocoder function that can handle addresses and coordinates,
 29+ // or one of MapsCoordinateParser, which only accepts coordinates.
 30+ if ( self::geocoderIsAvailable() ) {
 31+ $locationValidationFunction = array( 'MapsGeocoder', 'attemptToGeocodeToString' );
 32+ $locationFormattingFunction = array( __CLASS__, 'attemptToGeocodeToString' );
 33+ } else {
 34+ $locationValidationFunction = array( 'MapsCoordinateParser', 'areCoordinates' );
 35+ $locationFormattingFunction = array( __CLASS__, 'parseAndFormat' );
 36+ }
 37+
 38+ Validator::addValidationFunction( 'is_location', $locationValidationFunction );
 39+ Validator::addOutputFormat( 'format_coordinates', $locationFormattingFunction );
 40+
2741 self::$mainParams = array(
2842 'zoom' => array(
2943 'type' => 'integer',
@@ -189,4 +203,22 @@
190204 self::isMapDimension( $value, $dimension, true, $default );
191205 }
192206
 207+ /**
 208+ * Returns a boolean indicating if MapsGeocoder is available.
 209+ *
 210+ * @return Boolean
 211+ */
 212+ public static function geocoderIsAvailable() {
 213+ global $wgAutoloadClasses;
 214+ return array_key_exists( 'MapsGeocoder', $wgAutoloadClasses );
 215+ }
 216+
 217+ public static function attemptToGeocodeToString( &$value ) {
 218+ $value = MapsGeocoder::attemptToGeocodeToString( $value );
 219+ }
 220+
 221+ public static function parseAndFormat( &$value ) {
 222+ $value = MapsCoordinateParser::parseAndFormat( $value );
 223+ }
 224+
193225 }
Index: trunk/extensions/Maps/Maps_MapFeature.php
@@ -92,14 +92,18 @@
9393 global $egMapsAvailableServices, $egMapsAvailableGeoServices, $egMapsDefaultGeoService;
9494
9595 return array(
 96+ 'coordinates' => array(
 97+ 'aliases' => array( 'coords', 'location', 'locations' ),
 98+ 'criteria' => array(
 99+ 'is_location' => array()
 100+ ),
 101+ 'output-type' => 'format_coordinates',
 102+ ),
96103 'service' => array(
97104 'criteria' => array(
98105 'in_array' => $egMapsAvailableServices
99106 ),
100107 ),
101 - 'coordinates' => array(
102 - 'aliases' => array( 'coords', 'location', 'locations' ),
103 - ),
104108 'geoservice' => array(
105109 'criteria' => array(
106110 'in_array' => $egMapsAvailableGeoServices
Index: trunk/extensions/Maps/Maps_CoordinateParser.php
@@ -141,6 +141,24 @@
142142 * @return string
143143 */
144144 public static function formatCoordinates( array $coordinates, $targetFormat = Maps_COORDS_FLOAT, $directional = false, $separator = ', ' ) {
 145+ return implode( $separator, self::formatToArray( $coordinates, $targetFormat, $directional ) );
 146+ }
 147+
 148+ /**
 149+ * Turns a given coordinate set into a single string that gets formatted
 150+ * depending on the $targetType and $directional parameters.
 151+ *
 152+ * they will be parsed to the given notation, which defaults to
 153+ * non-directional floats
 154+ *
 155+ * @param array $coordinates The set of coordinates that needs to be formatted. Either an associative
 156+ * array with lat and lon keys, or a numbered aray with lat on index 0, and lon on index 1.
 157+ * @param coordinate type $targetFormat The notation to which they should be formatted. Defaults to floats.
 158+ * @param boolean $directional Indicates if the target notation should be directional. Defaults to false.
 159+ *
 160+ * @return array
 161+ */
 162+ public static function formatToArray( array $coordinates, $targetFormat = Maps_COORDS_FLOAT, $directional = false ) {
145163 if ( !array_key_exists( 'lat', $coordinates ) || !array_key_exists( 'lon', $coordinates ) ) {
146164 list( $coordinates['lat'], $coordinates['lon'] ) = $coordinates;
147165 }
@@ -150,9 +168,7 @@
151169 'lon' => self::formatCoordinate( $coordinates['lon'], $targetFormat ),
152170 );
153171
154 - $coordinates = self::setAngles( $coordinates, $directional );
155 -
156 - return implode( $separator, $coordinates );
 172+ return self::setAngles( $coordinates, $directional );
157173 }
158174
159175 /**
@@ -443,4 +459,21 @@
444460 return self::$mSeperatorsRegex;
445461 }
446462
 463+ /**
 464+ *
 465+ * @param unknown_type $coordinates
 466+ * @param unknown_type $targetFormat
 467+ * @param unknown_type $directional
 468+ *
 469+ * return
 470+ */
 471+ public static function parseAndFormat( $coordinates, $targetFormat = Maps_COORDS_FLOAT, $directional = false ) {
 472+ $parsedCoords = self::parseCoordinates( $coordinates );
 473+ if ( $parsedCoords ) {
 474+ return self::formatCoordinates( $parsedCoords );
 475+ } else {
 476+ return false;
 477+ }
 478+ }
 479+
447480 }
\ No newline at end of file
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoder.php
@@ -33,6 +33,7 @@
3434 * @param string $geoservice
3535 * @param string $mappingService
3636 * @param boolean $checkForCoords
 37+ * @param boolean $checkForCoords
3738 *
3839 * @return array or false
3940 */
@@ -55,10 +56,11 @@
5657 * @param string $coordsOrAddress
5758 * @param string $service
5859 * @param string $mappingService
 60+ * @param boolean $checkForCoords
5961 * @param coordinate type $targetFormat The notation to which they should be formatted. Defaults to floats.
6062 * @param boolean $directional Indicates if the target notation should be directional. Defaults to false.
6163 *
62 - * @return formatted coordinate string or false
 64+ * @return formatted coordinates string or false
6365 */
6466 public static function attemptToGeocodeToString( $coordsOrAddress, $service = '', $mappingService = false, $checkForCoords = true, $targetFormat = Maps_COORDS_FLOAT, $directional = false ) {
6567 $geoValues = self::attemptToGeocode( $coordsOrAddress, $service, $mappingService, $checkForCoords );
@@ -97,6 +99,22 @@
98100 }
99101
100102 /**
 103+ * Does the same as Geocode, but also formats the result into a string.
 104+ *
 105+ * @param string $coordsOrAddress
 106+ * @param string $service
 107+ * @param string $mappingService
 108+ * @param coordinate type $targetFormat The notation to which they should be formatted. Defaults to floats.
 109+ * @param boolean $directional Indicates if the target notation should be directional. Defaults to false.
 110+ *
 111+ * @return formatted coordinates string or false
 112+ */
 113+ public static function geocodeToString( $address, $service = '', $mappingService = false, $targetFormat = Maps_COORDS_FLOAT, $directional = false ) {
 114+ $coordinates = self::geocode( $address, $service, $mappingService );
 115+ return $coordinates ? MapsCoordinateParser::formatCoordinates( $coordinates, $targetFormat, $directional ) : false;
 116+ }
 117+
 118+ /**
101119 * Makes sure that the geo service is one of the available ones.
102120 * Also enforces licencing restrictions when no geocoding service is explicitly provided.
103121 *
@@ -120,16 +138,13 @@
121139 }
122140
123141 // If no overrides where applied, use the default mapping service.
124 - if ( strlen( $service ) < 1 ) $service = $egMapsDefaultGeoService;
 142+ if ( $service == '' ) $service = $egMapsDefaultGeoService;
125143 }
126144 else {
127145 // If a service is provided, but is not supported, use the default.
128 - if ( ! array_key_exists( $service, $egMapsAvailableGeoServices ) ) $service = $egMapsDefaultGeoService;
 146+ if ( !array_key_exists( $service, $egMapsAvailableGeoServices ) ) $service = $egMapsDefaultGeoService;
129147 }
130148
131149 return $service;
132150 }
133 -}
134 -
135 -
136 -
 151+}
\ No newline at end of file

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r65108Changes for 0.6 - doing big rewrite of the maping classes and MapsParserFunct...jeroendedauw06:33, 16 April 2010

Status & tagging log