r64891 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64890‎ | r64891 | r64892 >
Date:18:20, 10 April 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.6 - added support for multiple seperators
Modified paths:
  • /trunk/extensions/Maps/Maps_CoordinateParser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/Maps_CoordinateParser.php
@@ -28,6 +28,7 @@
2929 class MapsCoordinateParser {
3030
3131 protected static $mSeperators = array( ',', ';' );
 32+ protected static $mSeperatorsRegex = false;
3233
3334 protected static $mI18nDirections = false; // Cache for localised direction labels
3435 protected static $mDirections; // Cache for English direction labels
@@ -38,11 +39,10 @@
3940 * will be stored in an array with keys 'lat' and 'lon'.
4041 *
4142 * @param string $coordinates The coordinates to be parsed.
42 - * @param string $separator Delimiter that seperates the latitude and longitude
4343 *
4444 * @return array or false
4545 */
46 - public static function parseCoordinates( $coordinates, $separator = ',' ) {
 46+ public static function parseCoordinates( $coordinates ) {
4747 // Normalize the coordinates string.
4848 $coordinates = self::normalizeCoordinates( $coordinates );
4949
@@ -58,10 +58,14 @@
5959 }
6060
6161 // Split the coodrinates string into a lat and lon part.
62 - $coordinates = explode( $separator, $coordinates );
 62+ foreach( self::$mSeperators as $seperator ){
 63+ $split = explode( $seperator, $coordinates );
 64+ if ( count( $split ) == 2 ) break;
 65+ }
 66+
6367 $coordinates = array(
64 - 'lat' => trim ( $coordinates[0] ),
65 - 'lon' => trim ( $coordinates[1] ),
 68+ 'lat' => trim ( $split[0] ),
 69+ 'lon' => trim ( $split[1] ),
6670 );
6771
6872 // Ensure the coordinates are in non-directional notation.
@@ -221,8 +225,9 @@
222226 * @return boolean
223227 */
224228 public static function areFloatCoordinates( $coordinates ) {
225 - return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?,(\s)?(-)?\d{1,3}(\.\d{1,20})?$/', $coordinates ) // Non-directional
226 - || preg_match( '/^\d{1,3}(\.\d{1,20})?(\s)?(N|S),(\s)?\d{1,3}(\.\d{1,20})?(\s)?(E|W)$/', $coordinates ); // Directional
 229+ $sep = self::getSeperatorsRegex();
 230+ return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep . '(\s)?(-)?\d{1,3}(\.\d{1,20})?$/', $coordinates ) // Non-directional
 231+ || preg_match( '/^\d{1,3}(\.\d{1,20})?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}(\.\d{1,20})?(\s)?(E|W)$/', $coordinates ); // Directional
227232 }
228233
229234 /**
@@ -233,8 +238,11 @@
234239 * @return boolean
235240 */
236241 public static function areDMSCoordinates( $coordinates ) {
237 - return preg_match( '/^(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?),(\s)?(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)$/', $coordinates ) // Non-directional
238 - || preg_match( '/^(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(N|S),(\s)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(E|W)$/', $coordinates ); // Directional
 242+ $sep = self::getSeperatorsRegex();
 243+ return preg_match( '/^(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)'
 244+ . $sep . '(\s)?(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)$/', $coordinates ) // Non-directional
 245+ || preg_match( '/^(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(N|S)'
 246+ . $sep . '(\s)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(E|W)$/', $coordinates ); // Directional
239247 }
240248
241249 /**
@@ -245,8 +253,9 @@
246254 * @return boolean
247255 */
248256 public static function areDDCoordinates( $coordinates ) {
249 - return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°,(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/', $coordinates ) // Non-directional
250 - || preg_match( '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S),(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)?(E|W)?$/', $coordinates ); // Directional
 257+ $sep = self::getSeperatorsRegex();
 258+ return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°' . $sep . '(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/', $coordinates ) // Non-directional
 259+ || preg_match( '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S)' . $sep . '(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)?(E|W)?$/', $coordinates ); // Directional
251260 }
252261
253262 /**
@@ -257,8 +266,9 @@
258267 * @return boolean
259268 */
260269 public static function areDMCoordinates( $coordinates ) {
261 - return preg_match( '/(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?,(\s)?(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?$/', $coordinates ) // Non-directional
262 - || preg_match( '/\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(N|S),(\s)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(E|W)?$/', $coordinates ); // Directional
 270+ $sep = self::getSeperatorsRegex();
 271+ return preg_match( '/(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?' . $sep . '(\s)?(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?$/', $coordinates ) // Non-directional
 272+ || preg_match( '/\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(E|W)?$/', $coordinates ); // Directional
263273 }
264274
265275 /**
@@ -425,4 +435,9 @@
426436 return $coordinate;
427437 }
428438
 439+ private static function getSeperatorsRegex() {
 440+ if ( !self::$mSeperatorsRegex ) self::$mSeperatorsRegex = '(' . implode( '|', self::$mSeperators ) . ')';
 441+ return self::$mSeperatorsRegex;
 442+ }
 443+
429444 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r64893Follow up to r64891jeroendedauw18:29, 10 April 2010

Status & tagging log