r69685 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69684‎ | r69685 | r69686 >
Date:15:07, 21 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Fixed issues with distance parsing found with unit tests added in r69684
Modified paths:
  • /trunk/extensions/Maps/Includes/Maps_DistanceParser.php (modified) (history)
  • /trunk/extensions/Maps/Maps_Settings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Includes/Maps_DistanceParser.php
@@ -40,6 +40,8 @@
4141 return false;
4242 }
4343
 44+ $distance = self::normalizeDistance( $distance );
 45+
4446 $matches = array();
4547 preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance, $matches );
4648
@@ -90,6 +92,7 @@
9193 * @return boolean
9294 */
9395 public static function isDistance( $distance ) {
 96+ $distance = self::normalizeDistance( $distance );
9497 return preg_match( '/^(\d+)((\.|,)(\d+))?\s*(.*)?$/', $distance );
9598 }
9699
@@ -120,9 +123,10 @@
121124 global $egMapsDistanceUnit, $egMapsDistanceUnits;
122125
123126 // This ensures the value for $egMapsDistanceUnit is correct, and caches the result.
124 - if ( !self::$validatedDistanceUnit ) {
 127+ if ( self::$validatedDistanceUnit === false ) {
125128 if ( !array_key_exists( $egMapsDistanceUnit, $egMapsDistanceUnits ) ) {
126 - $egMapsDistanceUnit = $egMapsDistanceUnits[0];
 129+ $units = array_keys( $egMapsDistanceUnits );
 130+ $egMapsDistanceUnit = $units[0];
127131 }
128132
129133 self::$validatedDistanceUnit = true;
@@ -131,7 +135,7 @@
132136 if ( $unit == null || !array_key_exists( $unit, $egMapsDistanceUnits ) ) {
133137 $unit = $egMapsDistanceUnit;
134138 }
135 -
 139+
136140 return $unit;
137141 }
138142
@@ -147,4 +151,19 @@
148152 return array_keys( $egMapsDistanceUnits );
149153 }
150154
 155+ /**
 156+ * Normalizes a potential distance by removing spaces and truning comma's into dots.
 157+ *
 158+ * @since 0.6.5
 159+ *
 160+ * @param $distance String
 161+ *
 162+ * @return string
 163+ */
 164+ protected static function normalizeDistance( $distance ) {
 165+ $distance = str_replace( ' ', '', $distance );
 166+ $distance = str_replace( ',', '.', $distance );
 167+ return $distance;
 168+ }
 169+
151170 }
\ No newline at end of file
Index: trunk/extensions/Maps/Maps_Settings.php
@@ -170,7 +170,7 @@
171171 );
172172
173173 # String. The default unit for distances.
174 - $egMapsDistanceUnit = 'km';
 174+ $egMapsDistanceUnit = 'm';
175175
176176 # Integer. The default limit of fractal digits in a distance.
177177 $egMapsDistanceDecimals = 2;

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r69684Added unit testsjeroendedauw14:59, 21 July 2010

Status & tagging log