r64202 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64201‎ | r64202 | r64203 >
Date:00:36, 26 March 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.5.6 - rewriting coordinate validation and parsing
Modified paths:
  • /trunk/extensions/Maps/Geocoders/Maps_GeocodeUtils.php (deleted) (history)
  • /trunk/extensions/Maps/Geocoders/Maps_Geocoder.php (modified) (history)
  • /trunk/extensions/Maps/Geocoders/Maps_Geocoders.php (modified) (history)
  • /trunk/extensions/Maps/Maps.php (modified) (history)
  • /trunk/extensions/Maps/Maps_CoordinateParser.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Utils.php (modified) (history)
  • /trunk/extensions/Maps/OpenStreetMap/Maps_OSMCgiBin.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_BaseMap.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_BasePointMap.php (modified) (history)
  • /trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/ParserFunctions/DisplayPoint/Maps_BasePointMap.php
@@ -174,7 +174,7 @@
175175 }
176176 }
177177 else { // If a centre value is set, geocode when needed and use it.
178 - $this->centre = MapsGeocodeUtils::attemptToGeocode( $this->centre, $this->geoservice, $this->serviceName );
 178+ $this->centre = MapsGeocoder::attemptToGeocode( $this->centre, $this->geoservice, $this->serviceName );
179179
180180 // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde.
181181 if ( $this->centre ) {
Index: trunk/extensions/Maps/ParserFunctions/DisplayMap/Maps_BaseMap.php
@@ -73,7 +73,7 @@
7474 $this->centre_lon = $egMapsMapLon;
7575 }
7676 else { // If a centre value is set, geocode when needed and use it.
77 - $this->coordinates = MapsGeocodeUtils::attemptToGeocode( $this->coordinates, $this->geoservice, $this->serviceName );
 77+ $this->coordinates = MapsGeocoder::attemptToGeocode( $this->coordinates, $this->geoservice, $this->serviceName );
7878
7979 // If the centre is not false, it will be a valid coordinate, which can be used to set the latitude and longitutde.
8080 if ( $this->coordinates ) {
Index: trunk/extensions/Maps/ParserFunctions/Maps_ParserFunctions.php
@@ -165,7 +165,7 @@
166166 $coordinates = explode( $delimeter, $coordList );
167167
168168 foreach ( $coordinates as $coordinate ) {
169 - if ( MapsGeocodeUtils::isCoordinate( $coordinate ) ) {
 169+ if ( MapsCoordinateParser::areCoordinates( $coordinate ) ) {
170170 $validCoordinates[] = $coordinate;
171171 }
172172 else {
@@ -226,7 +226,7 @@
227227 $args[0] = trim( $args[0] );
228228
229229 if ( strlen( $args[0] ) > 0 ) {
230 - $coords = MapsGeocodeUtils::attemptToGeocode( $args[0], $geoservice, $service, $isDefault );
 230+ $coords = MapsGeocoder::attemptToGeocode( $args[0], $geoservice, $service, $isDefault );
231231
232232 if ( $coords ) {
233233 $args[0] = $coords;
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.5.5' );
 37+ define( 'Maps_VERSION', '0.5.6' );
3838
3939 $egMapsScriptPath = ( isset( $wgExtensionAssetsPath ) && $wgExtensionAssetsPath ? $wgExtensionAssetsPath : $wgScriptPath . '/extensions' ) . '/Maps';
4040 $egMapsDir = dirname( __FILE__ ) . '/';
@@ -51,6 +51,7 @@
5252 $wgHooks['AdminLinks'][] = 'efMapsAddToAdminLinks';
5353
5454 // Autoload the general classes
 55+ $wgAutoloadClasses['MapsCoordinateParser'] = $egMapsDir . 'Maps_CoordinateParser.php';
5556 $wgAutoloadClasses['MapsMapFeature'] = $egMapsDir . 'Maps_MapFeature.php';
5657 $wgAutoloadClasses['MapsMapper'] = $egMapsDir . 'Maps_Mapper.php';
5758 $wgAutoloadClasses['MapsUtils'] = $egMapsDir . 'Maps_Utils.php';
Index: trunk/extensions/Maps/OpenStreetMap/Maps_OSMCgiBin.php
@@ -101,19 +101,26 @@
102102
103103 $center = array( $this->lon, $this->lat );
104104 if ( $this->options['sphericalMercator'] ) {
105 - // Calculate bounds within a spherical mercator projection if that is what the scale is based on
 105+ // Calculate bounds within a spherical mercator projection if that is what the scale is based on.
106106 $mercatorCenter = MapsUtils::forwardMercator( $center );
107 - $mbounds = array(
108 - $mercatorCenter[0] - $w_deg / 2,
109 - $mercatorCenter[1] - $h_deg / 2,
110 - $mercatorCenter[0] + $w_deg / 2,
111 - $mercatorCenter[1] + $h_deg / 2
 107+
 108+ $this->bounds = MapsUtils::inverseMercator(
 109+ array(
 110+ $mercatorCenter[0] - $w_deg / 2,
 111+ $mercatorCenter[1] - $h_deg / 2,
 112+ $mercatorCenter[0] + $w_deg / 2,
 113+ $mercatorCenter[1] + $h_deg / 2
 114+ )
112115 );
113 - $this->bounds = MapsUtils::inverseMercator( $mbounds );
114116 }
115117 else {
116118 // Calculate bounds within WGS84
117 - $this->bounds = array( $center[0] - $w_deg / 2, $center[1] - $h_deg / 2, $center[0] + $w_deg / 2, $center[1] + $h_deg / 2 );
 119+ $this->bounds = array(
 120+ $center[0] - $w_deg / 2,
 121+ $center[1] - $h_deg / 2,
 122+ $center[0] + $w_deg / 2,
 123+ $center[1] + $h_deg / 2
 124+ );
118125 }
119126 }
120127
Index: trunk/extensions/Maps/Maps_Utils.php
@@ -45,10 +45,10 @@
4646 * @param $deg_coord
4747 * @return unknown_type
4848 */
49 - private static function convertCoord( $deg_coord = "" ) {
 49+ private static function convertCoord( $deg_coord = '' ) {
5050 if ( preg_match ( '/°/', $deg_coord ) ) {
5151 if ( preg_match ( '/"/', $deg_coord ) ) {
52 - return MapsUtils::degree2Decimal ( $deg_coord );
 52+ return MapsUtils::DMSToDecimal ( $deg_coord );
5353 } else {
5454 return MapsUtils::decDegree2Decimal ( $deg_coord );
5555 }
@@ -58,23 +58,29 @@
5959
6060 /**
6161 *
 62+ *
6263 * @param $deg_coord
6364 * @return unknown_type
6465 */
65 - private static function degree2Decimal( $deg_coord = "" ) {
66 - $dpos = strpos ( $deg_coord, '°' );
67 - $mpos = strpos ( $deg_coord, '.' );
68 - $spos = strpos ( $deg_coord, '"' );
69 - $mlen = ( ( $mpos - $dpos ) - 1 );
70 - $slen = ( ( $spos - $mpos ) - 1 );
71 - $direction = substr ( strrev ( $deg_coord ), 0, 1 );
72 - $degrees = substr ( $deg_coord, 0, $dpos );
73 - $minutes = substr ( $deg_coord, $dpos + 1, $mlen );
74 - $seconds = substr ( $deg_coord, $mpos + 1, $slen );
 66+ private static function DMSToDecimal( $dmsCoordinates = '' ) {
 67+ $degreePosition = strpos( $dmsCoordinates, '°' );
 68+ $minutePosition = strpos( $dmsCoordinates, '.' );
 69+ $secondPosition = strpos( $dmsCoordinates, '"' );
 70+
 71+ $minuteLength = $minutePosition - $degreePosition - 1;
 72+ $secondLength = $secondPosition - $minutePosition - 1;
 73+
 74+ $direction = substr ( strrev ( $dmsCoordinates ), 0, 1 );
 75+
 76+ $degrees = substr ( $dmsCoordinates, 0, $dpos );
 77+ $minutes = substr ( $dmsCoordinates, $dpos + 1, $mlen );
 78+ $seconds = substr ( $dmsCoordinates, $mpos + 1, $slen );
 79+
7580 $seconds = ( $seconds / 60 );
7681 $minutes = ( $minutes + $seconds );
7782 $minutes = ( $minutes / 60 );
7883 $decimal = ( $degrees + $minutes );
 84+
7985 // South latitudes and West longitudes need to return a negative result
8086 if ( $direction == "S" || $direction == "W" ) {
8187 $decimal *= - 1;
@@ -150,7 +156,7 @@
151157 /**
152158 * Convert from WGS84 to spherical mercator.
153159 */
154 - public static function forwardMercator( $lonlat ) {
 160+ public static function forwardMercator( array $lonlat ) {
155161 for ( $i = 0; $i < count( $lonlat ); $i += 2 ) {
156162 /* lon */
157163 $lonlat[$i] = $lonlat[$i] * ( 2 * M_PI * 6378137 / 2.0 ) / 180.0;
@@ -165,7 +171,7 @@
166172 /**
167173 * Convert from spherical mercator to WGS84.
168174 */
169 - public static function inverseMercator( $lonlat ) {
 175+ public static function inverseMercator( array $lonlat ) {
170176 for ( $i = 0; $i < count( $lonlat ); $i += 2 ) {
171177 /* lon */
172178 $lonlat[$i] = $lonlat[$i] / ( ( 2 * M_PI * 6378137 / 2.0 ) / 180.0 );
Index: trunk/extensions/Maps/Maps_CoordinateParser.php
@@ -74,12 +74,10 @@
7575
7676 $coordinates = self::resolveAngles( $coordinates );
7777
78 - $coordinates = array(
 78+ return array(
7979 'lat' => self::parseCoordinate( $coordinates['lat'], $coordsType ),
8080 'lon' => self::parseCoordinate( $coordinates['lon'], $coordsType ),
8181 );
82 -
83 - return $floatCoordinates;
8482 }
8583
8684 /**
@@ -109,6 +107,25 @@
110108 }
111109 }
112110
 111+ /**
 112+ * Returns a boolean indicating if the provided value is a valid set of coordinate.
 113+ *
 114+ * @param string $coordsOrAddress
 115+ *
 116+ * @return boolean
 117+ */
 118+ public static function areCoordinates( $coordsOrAddress ) {
 119+ return self::getCoordinatesType( $coordsOrAddress ) !== false;
 120+ }
 121+
 122+ /**
 123+ * Returns the coordinate parsed in the given notation.
 124+ *
 125+ * @param string $coordinate
 126+ * @param coordinate type $coordType
 127+ *
 128+ * @return string
 129+ */
113130 private static function parseCoordinate( $coordinate, $coordType ) {
114131 switch ( $coordType ) {
115132 case COORDS_FLOAT:
Index: trunk/extensions/Maps/Geocoders/Maps_GeocodeUtils.php
@@ -1,72 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * MapsGeocodeUtils holds static functions to geocode values when needed.
6 - *
7 - * @file Maps_GeocodeUtils.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 -final class MapsGeocodeUtils {
18 - /**
19 - * This function first determines wether the provided string is a pair or coordinates
20 - * or an address. If it's the later, an attempt to geocode will be made. The function will
21 - * return the coordinates or false, in case a geocoding attempt was made but failed.
22 - *
23 - * @param $coordsOrAddress
24 - * @param $geoservice
25 - * @param $service
26 - *
27 - * @return string or boolean
28 - */
29 - public static function attemptToGeocode( $coordsOrAddress, $geoservice, $service, $checkForCoords = true ) {
30 - if ( $checkForCoords ) {
31 - if ( MapsGeocodeUtils::isCoordinate( $coordsOrAddress ) ) {
32 - $coords = $coordsOrAddress;
33 - }
34 - else {
35 - $coords = MapsGeocoder::geocodeToString( $coordsOrAddress, $geoservice, $service );
36 - }
37 - }
38 - else {
39 - $coords = MapsGeocoder::geocodeToString( $coordsOrAddress, $geoservice, $service );
40 - }
41 -
42 - return $coords;
43 - }
44 -
45 - /**
46 - * Returns a boolean indication if a provided value is a valid coordinate.
47 - *
48 - * @param string $coordsOrAddress
49 - *
50 - * @return boolean
51 - */
52 - public static function isCoordinate( $coordsOrAddress ) {
53 - $coordRegexes = array(
54 - '/^(-)?\d{1,3}(\.\d{1,20})?,(\s)?(-)?\d{1,3}(\.\d{1,20})?$/', // Floats
55 - '/^(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(N|S)(\s)?(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(E|W)$/', // DMS
56 - '/^(-)?\d{1,3}(|\.\d{1,20})°,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/', // DD
57 - '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S),(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)(E|W)?$/', // DD (directional)
58 - '/(-)?\d{1,3}°\d{1,3}(\.\d{1,20}\')?,(\s)?(-)?\d{1,3}°\d{1,3}(\.\d{1,20}\')?$/', // DM
59 - );
60 -
61 - $isCoordinate = false;
62 -
63 - foreach ( $coordRegexes as $coordRegex ) {
64 - if ( preg_match( $coordRegex, trim( $coordsOrAddress ) ) ) {
65 - $isCoordinate = true;
66 - continue;
67 - }
68 - }
69 -
70 - return $isCoordinate;
71 - }
72 -
73 -}
\ No newline at end of file
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoders.php
@@ -31,7 +31,6 @@
3232
3333 $wgAutoloadClasses['MapsBaseGeocoder'] = $egMapsDir . 'Geocoders/Maps_BaseGeocoder.php';
3434 $wgAutoloadClasses['MapsGeocoder'] = $egMapsDir . 'Geocoders/Maps_Geocoder.php';
35 - $wgAutoloadClasses['MapsGeocodeUtils'] = $egMapsDir . 'Geocoders/Maps_GeocodeUtils.php';
3635 }
3736
3837 }
\ No newline at end of file
Index: trunk/extensions/Maps/Geocoders/Maps_Geocoder.php
@@ -32,6 +32,26 @@
3333 private static $mGeocoderCache = array();
3434
3535 /**
 36+ * This function first determines wether the provided string is a pair or coordinates
 37+ * or an address. If it's the later, an attempt to geocode will be made. The function will
 38+ * return the coordinates or false, in case a geocoding attempt was made but failed.
 39+ *
 40+ * @param string $coordsOrAddress
 41+ * @param string $geoservice
 42+ * @param string $service
 43+ * @param boolean $checkForCoords
 44+ *
 45+ * @return string or boolean
 46+ */
 47+ public static function attemptToGeocode( $coordsOrAddress, $geoservice, $service, $checkForCoords = true ) {
 48+ if ( $checkForCoords && MapsCoordinateParser::areCoordinates( $coordsOrAddress ) ) {
 49+ return $coordsOrAddress;
 50+ } else {
 51+ return self::geocodeToString( $coordsOrAddress, $geoservice, $service );
 52+ }
 53+ }
 54+
 55+ /**
3656 * Geocodes an address with the provided geocoding service and returns the result
3757 * as a string with the optionally provided format, or false when the geocoding failed.
3858 *

Status & tagging log