[MediaWiki-commits] [Gerrit] Moved coordinate-related static functions from CargoStore to... - change (mediawiki...Cargo)
Yaron Koren has submitted this change and it was merged. Change subject: Moved coordinate-related static functions from CargoStore to CargoUtils .. Moved coordinate-related static functions from CargoStore to CargoUtils Change-Id: I22cb47f4ea89c145b09f3643045ec1a1f3999340 --- M CargoSQLQuery.php M CargoUtils.php M parserfunctions/CargoDisplayMap.php M parserfunctions/CargoStore.php 4 files changed, 124 insertions(+), 124 deletions(-) Approvals: Yaron Koren: Checked; Looks good to me, approved diff --git a/CargoSQLQuery.php b/CargoSQLQuery.php index 5734dc9..6044228 100644 --- a/CargoSQLQuery.php +++ b/CargoSQLQuery.php @@ -881,7 +881,7 @@ $latDistance = $distanceInKM / 111; // Convert the latitude string to a latitude number - code is - // copied from CargoStore::parseCoordinatesString(). + // copied from CargoUtils::parseCoordinatesString(). $latIsNegative = false; if ( strpos( $latString, 'S' ) > 0 ) { $latIsNegative = true; @@ -890,7 +890,7 @@ if ( is_numeric( $latString ) ) { $latNum = floatval( $latString ); } else { - $latNum = CargoStore::coordinatePartToNumber( $latString ); + $latNum = CargoUtils::coordinatePartToNumber( $latString ); } if ( $latIsNegative ) { $latNum *= -1; diff --git a/CargoUtils.php b/CargoUtils.php index 3966fe5..1d6ed2c 100644 --- a/CargoUtils.php +++ b/CargoUtils.php @@ -537,4 +537,123 @@ } } + /** +* Parses one half of a set of coordinates into a number. +* +* Copied from Miga, also written by Yaron Koren +* (https://github.com/yaronkoren/miga/blob/master/MDVCoordinates.js) +* - though that one is in Javascript. +*/ + public static function coordinatePartToNumber( $coordinateStr ) { + $degreesSymbols = array( "\x{00B0}", "d" ); + $minutesSymbols = array( "'", "\x{2032}", "\x{00B4}" ); + $secondsSymbols = array( '"', "\x{2033}", "\x{00B4}\x{00B4}" ); + + $numDegrees = null; + $numMinutes = null; + $numSeconds = null; + + foreach ( $degreesSymbols as $degreesSymbol ) { + $pattern = '/([\d\.]+)' . $degreesSymbol . '/u'; + if ( preg_match( $pattern, $coordinateStr, $matches ) ) { + $numDegrees = floatval( $matches[1] ); + break; + } + } + if ( $numDegrees == null ) { + throw new MWException( "Error: could not parse degrees in \"$coordinateStr\"." ); + } + + foreach ( $minutesSymbols as $minutesSymbol ) { + $pattern = '/([\d\.]+)' . $minutesSymbol . '/u'; + if ( preg_match( $pattern, $coordinateStr, $matches ) ) { + $numMinutes = floatval( $matches[1] ); + break; + } + } + if ( $numMinutes == null ) { + // This might not be an error - the number of minutes + // might just not have been set. + $numMinutes = 0; + } + + foreach ( $secondsSymbols as $secondsSymbol ) { + $pattern = '/(\d+)' . $secondsSymbol . '/u'; + if ( preg_match( $pattern, $coordinateStr, $matches ) ) { + $numSeconds = floatval( $matches[1] ); + break; + } + } + if ( $numSeconds == null ) { + // This might not be an error - the number of seconds + // might just not have been set. + $numSeconds = 0; + } + + return ( $numDegrees + ( $numMinutes / 60 ) + ( $numSeconds / 3600 ) ); + } + + /** +* Parses a coordinate string in (hopefully) any standard format. +* +* Copied from Miga, also written by Yaron Koren +* (https://github.com/yaronkoren/miga/blob/master/MDVCoordinates.js) +* - though that one is in Javascript. +*/ + public static function parseCoordinatesString( $coordinatesString ) { + $coordinatesString = trim( $coordinatesString ); + if ( $coordinatesString == null ) { + return; + } + + // This is safe to do, right? + $coordinatesString = str_replace( array( '[', ']' ), '', $coordinatesString ); + // See if they're sepa
[MediaWiki-commits] [Gerrit] Moved coordinate-related static functions from CargoStore to... - change (mediawiki...Cargo)
Yaron Koren has uploaded a new change for review. https://gerrit.wikimedia.org/r/227731 Change subject: Moved coordinate-related static functions from CargoStore to CargoUtils .. Moved coordinate-related static functions from CargoStore to CargoUtils Change-Id: I22cb47f4ea89c145b09f3643045ec1a1f3999340 --- M CargoSQLQuery.php M CargoUtils.php M parserfunctions/CargoDisplayMap.php M parserfunctions/CargoStore.php 4 files changed, 124 insertions(+), 124 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo refs/changes/31/227731/1 diff --git a/CargoSQLQuery.php b/CargoSQLQuery.php index 5734dc9..6044228 100644 --- a/CargoSQLQuery.php +++ b/CargoSQLQuery.php @@ -881,7 +881,7 @@ $latDistance = $distanceInKM / 111; // Convert the latitude string to a latitude number - code is - // copied from CargoStore::parseCoordinatesString(). + // copied from CargoUtils::parseCoordinatesString(). $latIsNegative = false; if ( strpos( $latString, 'S' ) > 0 ) { $latIsNegative = true; @@ -890,7 +890,7 @@ if ( is_numeric( $latString ) ) { $latNum = floatval( $latString ); } else { - $latNum = CargoStore::coordinatePartToNumber( $latString ); + $latNum = CargoUtils::coordinatePartToNumber( $latString ); } if ( $latIsNegative ) { $latNum *= -1; diff --git a/CargoUtils.php b/CargoUtils.php index 3966fe5..1d6ed2c 100644 --- a/CargoUtils.php +++ b/CargoUtils.php @@ -537,4 +537,123 @@ } } + /** +* Parses one half of a set of coordinates into a number. +* +* Copied from Miga, also written by Yaron Koren +* (https://github.com/yaronkoren/miga/blob/master/MDVCoordinates.js) +* - though that one is in Javascript. +*/ + public static function coordinatePartToNumber( $coordinateStr ) { + $degreesSymbols = array( "\x{00B0}", "d" ); + $minutesSymbols = array( "'", "\x{2032}", "\x{00B4}" ); + $secondsSymbols = array( '"', "\x{2033}", "\x{00B4}\x{00B4}" ); + + $numDegrees = null; + $numMinutes = null; + $numSeconds = null; + + foreach ( $degreesSymbols as $degreesSymbol ) { + $pattern = '/([\d\.]+)' . $degreesSymbol . '/u'; + if ( preg_match( $pattern, $coordinateStr, $matches ) ) { + $numDegrees = floatval( $matches[1] ); + break; + } + } + if ( $numDegrees == null ) { + throw new MWException( "Error: could not parse degrees in \"$coordinateStr\"." ); + } + + foreach ( $minutesSymbols as $minutesSymbol ) { + $pattern = '/([\d\.]+)' . $minutesSymbol . '/u'; + if ( preg_match( $pattern, $coordinateStr, $matches ) ) { + $numMinutes = floatval( $matches[1] ); + break; + } + } + if ( $numMinutes == null ) { + // This might not be an error - the number of minutes + // might just not have been set. + $numMinutes = 0; + } + + foreach ( $secondsSymbols as $secondsSymbol ) { + $pattern = '/(\d+)' . $secondsSymbol . '/u'; + if ( preg_match( $pattern, $coordinateStr, $matches ) ) { + $numSeconds = floatval( $matches[1] ); + break; + } + } + if ( $numSeconds == null ) { + // This might not be an error - the number of seconds + // might just not have been set. + $numSeconds = 0; + } + + return ( $numDegrees + ( $numMinutes / 60 ) + ( $numSeconds / 3600 ) ); + } + + /** +* Parses a coordinate string in (hopefully) any standard format. +* +* Copied from Miga, also written by Yaron Koren +* (https://github.com/yaronkoren/miga/blob/master/MDVCoordinates.js) +* - though that one is in Javascript. +*/ + public static function parseCoordinatesString( $coordinatesString ) { + $coordinatesString = trim( $coordinatesString ); + if ( $coordinatesString == null ) { + return; + } + + // This is safe to do, right? + $coordinatesString = str_replace( array( '[', ']' )