Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Giehl <stefan@matomo.org>2020-02-22 04:05:46 +0300
committerGitHub <noreply@github.com>2020-02-22 04:05:46 +0300
commit9aff078592f879af269a9cfb143f92a0fedc2e46 (patch)
tree9551dc448db3e5eabaa1d571a02ebfc96c1e4337 /plugins
parent4c8fe7cf71e59bee91ff034c3d1f818a287acd14 (diff)
Try to determine the region iso code based on it's name if not available (#15592)
* Try to determine the region iso code based on it's name if not available * Adds test for region without code
Diffstat (limited to 'plugins')
-rw-r--r--plugins/GeoIp2/LocationProvider/GeoIp2/Php.php27
-rw-r--r--plugins/GeoIp2/tests/Integration/LocationProviderTest.php20
2 files changed, 46 insertions, 1 deletions
diff --git a/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php b/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php
index f2c145e35e..310a273dc4 100644
--- a/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php
+++ b/plugins/GeoIp2/LocationProvider/GeoIp2/Php.php
@@ -11,6 +11,7 @@ namespace Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use MaxMind\Db\Reader\InvalidDatabaseException;
+use Piwik\Common;
use Piwik\Log;
use Piwik\Piwik;
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
@@ -238,11 +239,35 @@ class Php extends GeoIp2
if (is_array($lookupResult->subdivisions) && count($lookupResult->subdivisions) > 0) {
$subdivisions = $lookupResult->subdivisions;
$subdivision = $this->determinSubdivision($subdivisions, $result[self::COUNTRY_CODE_KEY]);
- $result[self::REGION_CODE_KEY] = strtoupper($subdivision->isoCode);
+ $result[self::REGION_CODE_KEY] = strtoupper($subdivision->isoCode) ?: $this->determineRegionIsoCodeByNameAndCountryCode($subdivision->name, $result[self::COUNTRY_CODE_KEY]);
$result[self::REGION_NAME_KEY] = $subdivision->name;
}
}
+ /**
+ * Try to determine the ISO region code based on the region name and country code
+ *
+ * @param string $regionName
+ * @param string $countryCode
+ * @return string
+ */
+ protected function determineRegionIsoCodeByNameAndCountryCode($regionName, $countryCode)
+ {
+ $regionNames = self::getRegionNames();
+
+ if (empty($regionNames[$countryCode])) {
+ return '';
+ }
+
+ foreach ($regionNames[$countryCode] as $isoCode => $name) {
+ if (Common::mb_strtolower($name) === Common::mb_strtolower($regionName)) {
+ return $isoCode;
+ }
+ }
+
+ return '';
+ }
+
protected function determinSubdivision($subdivisions, $countryCode)
{
if (in_array($countryCode, ['GB'])) {
diff --git a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php
index 60c35c7438..b1231f1a57 100644
--- a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php
+++ b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php
@@ -34,6 +34,26 @@ class LocationProviderTest extends \PHPUnit_Framework_TestCase
], $result);
}
+ public function testGeoIP2CityWithoutRegionIsoCode()
+ {
+ // The IP 99.99.99.99 will only return a region name, based on that the region code should be determined
+ $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
+ $result = $locationProvider->getLocation(['ip' => '99.99.99.99']);
+
+ $this->assertEquals([
+ 'continent_name' => 'North America',
+ 'continent_code' => 'NA',
+ 'country_code' => 'US',
+ 'country_name' => 'United States',
+ 'city_name' => 'Englewood Cliffs',
+ 'lat' => 40.892,
+ 'long' => -73.947,
+ 'postal_code' => null,
+ 'region_code' => 'NJ',
+ 'region_name' => 'New Jersey',
+ ], $result);
+ }
+
public function testGeoIP2Country()
{
$locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-Country.mmdb'], 'isp' => []]);