From e43871cf0997ab90a1f7929aac0a89ccd38d9018 Mon Sep 17 00:00:00 2001 From: diosmosis Date: Mon, 16 Mar 2015 12:20:39 -0700 Subject: Correct floating point comparison for latitude/longitude in VisitorGeolocator when determining visit fields to update. --- plugins/UserCountry/VisitorGeolocator.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'plugins') diff --git a/plugins/UserCountry/VisitorGeolocator.php b/plugins/UserCountry/VisitorGeolocator.php index 78446408c2..b6105349d8 100644 --- a/plugins/UserCountry/VisitorGeolocator.php +++ b/plugins/UserCountry/VisitorGeolocator.php @@ -36,6 +36,8 @@ require_once PIWIK_INCLUDE_PATH . "/plugins/UserCountry/LocationProvider.php"; */ class VisitorGeolocator { + const LAT_LONG_COMPARE_EPSILON = 0.0001; + /** * @var string[] */ @@ -190,7 +192,7 @@ class VisitorGeolocator $locationPropertyValue = $location[$locationKey]; $existingPropertyValue = $row[$column]; - if ($locationPropertyValue != $existingPropertyValue) { + if (!$this->areLocationPropertiesEqual($locationKey, $locationPropertyValue, $existingPropertyValue)) { $valuesToUpdate[$column] = $locationPropertyValue; } } @@ -213,6 +215,19 @@ class VisitorGeolocator return $this->backupProvider; } + private function areLocationPropertiesEqual($locationKey, $locationPropertyValue, $existingPropertyValue) + { + if (($locationKey == LocationProvider::LATITUDE_KEY + || $locationKey == LocationProvider::LONGITUDE_KEY) + && $existingPropertyValue != 0 + ) { + // floating point comparison + return abs(($locationPropertyValue - $existingPropertyValue) / $existingPropertyValue) < self::LAT_LONG_COMPARE_EPSILON; + } else { + return $locationPropertyValue == $existingPropertyValue; + } + } + private function getDefaultProvider() { return LocationProvider::getProviderById(DefaultProvider::ID); -- cgit v1.2.3