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>2022-01-26 22:50:47 +0300
committerGitHub <noreply@github.com>2022-01-26 22:50:47 +0300
commit2ba0f7a67cb4aa336ef312e2850de1f325adcb69 (patch)
tree62bbc9cd0b444607009d73008c33f4b58d97a120 /plugins
parenta8e12af576ac4c5c33975e63865f02370a00c6d9 (diff)
Allow disabling the usage of the default geolocation provider as fallback (#18634)
* Allow disabling the usage of the default geolocation provider as fallback * Improve naming * add changelog entry * Introduce a disabled provider and setting to disable the default provider * fix tests * update changelog
Diffstat (limited to 'plugins')
-rw-r--r--plugins/GeoIp2/GeoIp2.php2
-rw-r--r--plugins/GeoIp2/tests/Integration/LocationProviderTest.php35
-rw-r--r--plugins/UserCountry/Controller.php3
-rw-r--r--plugins/UserCountry/LocationProvider.php18
-rw-r--r--plugins/UserCountry/LocationProvider/DefaultProvider.php18
-rw-r--r--plugins/UserCountry/LocationProvider/DisabledProvider.php98
-rw-r--r--plugins/UserCountry/VisitorGeolocator.php10
-rw-r--r--plugins/UserCountry/lang/en.json1
-rw-r--r--plugins/UserCountry/templates/adminIndex.twig5
-rw-r--r--plugins/UserCountry/tests/Integration/LocationProviderTest.php6
-rw-r--r--plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2.png4
-rw-r--r--plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2_no_provider.png4
-rw-r--r--plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_no_providers.png4
13 files changed, 188 insertions, 20 deletions
diff --git a/plugins/GeoIp2/GeoIp2.php b/plugins/GeoIp2/GeoIp2.php
index 03ccd58b1c..3f9363a3f2 100644
--- a/plugins/GeoIp2/GeoIp2.php
+++ b/plugins/GeoIp2/GeoIp2.php
@@ -40,7 +40,7 @@ class GeoIp2 extends \Piwik\Plugin
{
// switch to default provider if GeoIP2 provider was in use
if (LocationProvider::getCurrentProvider() instanceof \Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2) {
- LocationProvider::setCurrentProvider(LocationProvider\DefaultProvider::ID);
+ LocationProvider::setCurrentProvider(LocationProvider::getDefaultProviderId());
}
}
diff --git a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php
index 938325c5bd..ea1fc20927 100644
--- a/plugins/GeoIp2/tests/Integration/LocationProviderTest.php
+++ b/plugins/GeoIp2/tests/Integration/LocationProviderTest.php
@@ -8,7 +8,11 @@
*/
namespace Piwik\Plugins\GeoIp2\tests\Integration;
+use Piwik\Config;
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
+use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
+use Piwik\Plugins\UserCountry\VisitorGeolocator;
+use Piwik\Tests\Framework\Fixture;
/**
* @group GeoIp2
@@ -109,4 +113,35 @@ class LocationProviderTest extends \PHPUnit\Framework\TestCase
'org' => 'Innocraft'
], $result);
}
+
+ public function testGeoIP2NoResultFallback()
+ {
+ Fixture::loadAllTranslations();
+ $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
+ $geolocator = new VisitorGeolocator($locationProvider, new DefaultProvider());
+
+ $result = $geolocator->getLocation(['ip' => '221.0.0.9', 'lang' => 'de-ch'], false);
+
+ $this->assertEquals([
+ 'country_code' => 'ch',
+ 'country_name' => 'Switzerland',
+ 'continent_code' => 'eur',
+ 'continent_name' => 'Europe',
+ ], $result);
+ }
+
+ public function testGeoIP2NoResultFallbackDisabled()
+ {
+ Fixture::loadAllTranslations();
+ Config::getInstance()->Tracker['enable_default_location_provider'] = 0;
+
+ $locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
+ $geolocator = new VisitorGeolocator($locationProvider);
+
+ $result = $geolocator->getLocation(['ip' => '221.0.0.9', 'lang' => 'de-ch'], false);
+
+ $this->assertEquals([
+ 'country_code' => 'xx',
+ ], $result);
+ }
} \ No newline at end of file
diff --git a/plugins/UserCountry/Controller.php b/plugins/UserCountry/Controller.php
index c3d84522aa..1001108a64 100644
--- a/plugins/UserCountry/Controller.php
+++ b/plugins/UserCountry/Controller.php
@@ -16,6 +16,7 @@ use Piwik\Piwik;
use Piwik\Plugin\Manager;
use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
+use Piwik\Plugins\UserCountry\LocationProvider\DisabledProvider;
use Piwik\SettingsPiwik;
use Piwik\View;
@@ -78,7 +79,7 @@ class Controller extends \Piwik\Plugin\ControllerAdmin
// check if there is a working provider (that isn't the default one)
$isThereWorkingProvider = false;
foreach ($allProviderInfo as $id => $provider) {
- if ($id != DefaultProvider::ID
+ if ($id != DefaultProvider::ID && $id != DisabledProvider::ID
&& $provider['status'] == LocationProvider::INSTALLED
) {
$isThereWorkingProvider = true;
diff --git a/plugins/UserCountry/LocationProvider.php b/plugins/UserCountry/LocationProvider.php
index cbc2117b33..812f2bfc58 100644
--- a/plugins/UserCountry/LocationProvider.php
+++ b/plugins/UserCountry/LocationProvider.php
@@ -16,7 +16,9 @@ use Piwik\Piwik;
use Piwik\Plugin;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
use Piwik\Plugin\Manager as PluginManager;
+use Piwik\Plugins\UserCountry\LocationProvider\DisabledProvider;
use Piwik\Tracker\Cache;
+use Piwik\Tracker\TrackerConfig;
/**
* @see plugins/UserCountry/functions.php
@@ -340,7 +342,7 @@ abstract class LocationProvider
} catch (\Exception $e) {
$optionValue = false;
}
- return $optionValue === false ? DefaultProvider::ID : $optionValue;
+ return $optionValue === false ? self::getDefaultProviderId() : $optionValue;
}
/**
@@ -378,6 +380,20 @@ abstract class LocationProvider
}
/**
+ * Returns the default provider id to use
+ *
+ * @return string
+ */
+ public static function getDefaultProviderId()
+ {
+ if (!!TrackerConfig::getConfigValue('enable_default_location_provider')) {
+ return DefaultProvider::ID;
+ }
+
+ return DisabledProvider::ID;
+ }
+
+ /**
* Returns a provider instance by ID or false if the ID is invalid or unavailable.
*
* @param string $providerId
diff --git a/plugins/UserCountry/LocationProvider/DefaultProvider.php b/plugins/UserCountry/LocationProvider/DefaultProvider.php
index 2abfe0098a..e5f311fed6 100644
--- a/plugins/UserCountry/LocationProvider/DefaultProvider.php
+++ b/plugins/UserCountry/LocationProvider/DefaultProvider.php
@@ -12,6 +12,7 @@ use Piwik\Common;
use Piwik\Config;
use Piwik\Piwik;
use Piwik\Plugins\UserCountry\LocationProvider;
+use Piwik\Tracker\TrackerConfig;
/**
* The default LocationProvider, this LocationProvider guesses a visitor's country
@@ -47,13 +48,22 @@ class DefaultProvider extends LocationProvider
/**
* Returns whether this location provider is available.
*
- * This implementation is always available.
- *
- * @return bool always true
+ * @return bool
*/
public function isAvailable()
{
- return true;
+ return !!TrackerConfig::getConfigValue('enable_default_location_provider');
+ }
+
+
+ /**
+ * Returns whether this location provider is visible.
+ *
+ * @return bool
+ */
+ public function isVisible()
+ {
+ return !!TrackerConfig::getConfigValue('enable_default_location_provider');
}
/**
diff --git a/plugins/UserCountry/LocationProvider/DisabledProvider.php b/plugins/UserCountry/LocationProvider/DisabledProvider.php
new file mode 100644
index 0000000000..d5444a2c02
--- /dev/null
+++ b/plugins/UserCountry/LocationProvider/DisabledProvider.php
@@ -0,0 +1,98 @@
+<?php
+/**
+ * Matomo - free/libre analytics platform
+ *
+ * @link https://matomo.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+namespace Piwik\Plugins\UserCountry\LocationProvider;
+
+use Piwik\Common;
+use Piwik\Config;
+use Piwik\Piwik;
+use Piwik\Plugins\UserCountry\LocationProvider;
+
+/**
+ * The disabled LocationProvider, this LocationProvider always returns an empty result set.
+ *
+ */
+class DisabledProvider extends LocationProvider
+{
+ const ID = 'disabled';
+ const TITLE = 'General_Disabled';
+
+ /**
+ * Guesses a visitor's location using a visitor's browser language.
+ *
+ * @param array $info Contains 'ip' & 'lang' keys.
+ * @return false.
+ */
+ public function getLocation($info)
+ {
+ return false;
+ }
+
+ /**
+ * Returns whether this location provider is available.
+ *
+ * This implementation is always available.
+ *
+ * @return bool always true
+ */
+ public function isAvailable()
+ {
+ return true;
+ }
+
+ /**
+ * Returns whether this location provider is working correctly.
+ *
+ * This implementation is always working correctly.
+ *
+ * @return bool always true
+ */
+ public function isWorking()
+ {
+ return true;
+ }
+
+ /**
+ * Returns an array describing the types of location information this provider will
+ * return.
+ *
+ * @return array
+ */
+ public function getSupportedLocationInfo()
+ {
+ return [];
+ }
+
+ /**
+ * Returns information about this location provider. Contains an id, title & description:
+ *
+ * array(
+ * 'id' => 'default',
+ * 'title' => '...',
+ * 'description' => '...'
+ * );
+ *
+ * @return array
+ */
+ public function getInfo()
+ {
+ $desc = Piwik::translate('UserCountry_DisabledLocationProvider');
+ return array('id' => self::ID, 'title' => self::TITLE, 'description' => $desc, 'order' => 0);
+ }
+
+ public function getUsageWarning(): ?string
+ {
+ $comment = Piwik::translate('UserCountry_DefaultLocationProviderDesc1') . ' ';
+ $comment .= Piwik::translate('UserCountry_DefaultLocationProviderDesc2', array(
+ '<a href="https://matomo.org/docs/geo-locate/" rel="noreferrer noopener" target="_blank">', '', '', '</a>'
+ ));
+
+ return $comment;
+ }
+}
+
diff --git a/plugins/UserCountry/VisitorGeolocator.php b/plugins/UserCountry/VisitorGeolocator.php
index 23395dea17..b5725d5188 100644
--- a/plugins/UserCountry/VisitorGeolocator.php
+++ b/plugins/UserCountry/VisitorGeolocator.php
@@ -11,10 +11,13 @@ namespace Piwik\Plugins\UserCountry;
use Matomo\Cache\Cache;
use Matomo\Cache\Transient;
use Piwik\Common;
+use Piwik\Config\GeneralConfig;
use Piwik\Container\StaticContainer;
use Piwik\DataAccess\RawLogDao;
use Matomo\Network\IPUtils;
use Piwik\Plugins\UserCountry\LocationProvider\DefaultProvider;
+use Piwik\Plugins\UserCountry\LocationProvider\DisabledProvider;
+use Piwik\Tracker\TrackerConfig;
use Piwik\Tracker\Visit;
use Psr\Log\LoggerInterface;
@@ -90,7 +93,7 @@ class VisitorGeolocator
$provider = LocationProvider::getProviderById(Common::getCurrentLocationProviderId());
if (empty($provider)) {
- Common::printDebug("GEO: no current location provider sent, falling back to default '" . DefaultProvider::ID . "' one.");
+ Common::printDebug("GEO: no current location provider sent, falling back to '" . LocationProvider::getDefaultProviderId() . "' one.");
$provider = $this->getDefaultProvider();
}
@@ -118,7 +121,8 @@ class VisitorGeolocator
$providerId = $this->provider->getId();
Common::printDebug("GEO: couldn't find a location with Geo Module '$providerId'");
- if ($providerId != $this->backupProvider->getId()) {
+ // Only use the default provider as fallback if the configured one isn't "disabled"
+ if ($providerId != DisabledProvider::ID && $providerId != $this->backupProvider->getId()) {
Common::printDebug("Using default provider as fallback...");
$location = $this->getLocationObject($this->backupProvider, $userInfo);
@@ -299,7 +303,7 @@ class VisitorGeolocator
private function getDefaultProvider()
{
- return LocationProvider::getProviderById(DefaultProvider::ID);
+ return LocationProvider::getProviderById(LocationProvider::getDefaultProviderId());
}
public static function getDefaultLocationCache()
diff --git a/plugins/UserCountry/lang/en.json b/plugins/UserCountry/lang/en.json
index 2c52d95574..deed27baf9 100644
--- a/plugins/UserCountry/lang/en.json
+++ b/plugins/UserCountry/lang/en.json
@@ -16,6 +16,7 @@
"DefaultLocationProviderDesc1": "The default location provider guesses a visitor's country based on the language they use.",
"DefaultLocationProviderDesc2": "This is not very accurate, so %1$swe recommend installing and using %2$sa geolocation database%3$s.%4$s",
"DefaultLocationProviderExplanation": "You are using the default location provider, which means Matomo will guess the visitor's location based on the language they use. %1$sRead this%2$s to learn how to setup more accurate geolocation.",
+ "DisabledLocationProvider": "Disables the geolocation.",
"DistinctCountries": "%s distinct countries",
"FromDifferentCities": "different cities",
"GeoIPDocumentationSuffix": "In order to see data for this report, you must setup GeoIP in the Geolocation admin tab. The commercial %1$sMaxmind%2$s GeoIP databases are more accurate than the free ones. To see how accurate they are, click %3$shere%4$s.",
diff --git a/plugins/UserCountry/templates/adminIndex.twig b/plugins/UserCountry/templates/adminIndex.twig
index b83aa07333..20cc6deede 100644
--- a/plugins/UserCountry/templates/adminIndex.twig
+++ b/plugins/UserCountry/templates/adminIndex.twig
@@ -89,7 +89,10 @@
{% endfor %}
- {% if locationProviders|length == 1 %}
+ {% if locationProviders|filter(provider => (
+ provider.id != constant("Piwik\\Plugins\\UserCountry\\LocationProvider\\DefaultProvider::ID") and
+ provider.id != constant("Piwik\\Plugins\\UserCountry\\LocationProvider\\DisabledProvider::ID")
+ ))|length == 0 %}
<div piwik-notification
noclear="true"
context="warning">
diff --git a/plugins/UserCountry/tests/Integration/LocationProviderTest.php b/plugins/UserCountry/tests/Integration/LocationProviderTest.php
index 262317653f..ee9397cf16 100644
--- a/plugins/UserCountry/tests/Integration/LocationProviderTest.php
+++ b/plugins/UserCountry/tests/Integration/LocationProviderTest.php
@@ -23,9 +23,9 @@ class LocationProviderTest extends IntegrationTestCase
{
$allProviders = LocationProvider::getAllProviderInfo();
- // We currently have 3 Providers shipped with core
- $this->assertSame(3, count($allProviders));
- $this->assertEquals(['default', 'geoip2php', 'geoip2server'], array_keys($allProviders));
+ // We currently have 4 Providers shipped with core
+ $this->assertSame(4, count($allProviders));
+ $this->assertEquals(['disabled', 'default', 'geoip2php', 'geoip2server'], array_keys($allProviders));
}
public function testGetAllProviderInfoWithDuplicateOrder()
diff --git a/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2.png b/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2.png
index b3626a974c..3efae3951b 100644
--- a/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2.png
+++ b/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2.png
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:64cc7b555c783a0365eb8249701cabf6d1650fc0eeebac8f562dfcf9bd70fba9
-size 247298
+oid sha256:61efd451e6bd350c71beb3ea6da0e40e0f4ee9a1add39252ae39b0b1e530f976
+size 260278
diff --git a/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2_no_provider.png b/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2_no_provider.png
index 532e60ab82..a236cc76d8 100644
--- a/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2_no_provider.png
+++ b/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_geoip2_no_provider.png
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:33c3b0f2924e79a33fcdca6f541b6c259d1255817d294643fd64513fe566668f
-size 256721
+oid sha256:1a15ce0840e134885bc87ec6fa97a2e15425d2412faf06b7f79279328e3708b8
+size 269720
diff --git a/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_no_providers.png b/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_no_providers.png
index 04e05d4a2b..86d911fa77 100644
--- a/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_no_providers.png
+++ b/plugins/UserCountry/tests/UI/expected-screenshots/UserCountry_admin_no_providers.png
@@ -1,3 +1,3 @@
version https://git-lfs.github.com/spec/v1
-oid sha256:b9bfdbf2b4fa503fefb46fffbe75d641dcff47d6597079f6fba34ba0f102a13a
-size 67371
+oid sha256:219dba7d0c5fabfa578cad517c226bd263111ddb72348a15ed022d9289ee1d3b
+size 80454