Skip to content

Commit

Permalink
Allow customization of GeoIP2 database location using DI (matomo-org#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl authored and tsteur committed Mar 15, 2019
1 parent a99baa9 commit 3474bf2
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 21 deletions.
5 changes: 2 additions & 3 deletions plugins/GeoIp2/LocationProvider/GeoIp2.php
Expand Up @@ -9,6 +9,7 @@
namespace Piwik\Plugins\GeoIp2\LocationProvider;

use Exception;
use Piwik\Container\StaticContainer;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\UserCountry\LocationProvider;
Expand All @@ -23,8 +24,6 @@ abstract class GeoIp2 extends LocationProvider
const TEST_IP = '194.57.91.215';
const SWITCH_TO_ISO_REGIONS_OPTION_NAME = 'usercountry.switchtoisoregions';

public static $geoIPDatabaseDir = 'misc';

/**
* Cached region name array. Data is from geoipregionvars.php.
*
Expand Down Expand Up @@ -125,7 +124,7 @@ public static function getPathToGeoIpDatabase($possibleFileNames)
*/
public static function getPathForGeoIpDatabase($filename)
{
return PIWIK_INCLUDE_PATH . '/' . self::$geoIPDatabaseDir . '/' . $filename;
return StaticContainer::get('path.geoip2') . $filename;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions plugins/GeoIp2/config/config.php
@@ -0,0 +1,5 @@
<?php

return [
'path.geoip2' => DI\string('{path.root}/misc/'),
];
5 changes: 5 additions & 0 deletions plugins/GeoIp2/config/test.php
@@ -0,0 +1,5 @@
<?php

return [
'path.geoip2' => DI\string('{path.root}/tests/lib/geoip-files/'),
];
10 changes: 0 additions & 10 deletions plugins/GeoIp2/tests/Integration/LocationProviderTest.php
Expand Up @@ -17,8 +17,6 @@ class LocationProviderTest extends \PHPUnit_Framework_TestCase
{
public function testGeoIP2City()
{
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';

$locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => []]);
$result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

Expand All @@ -38,8 +36,6 @@ public function testGeoIP2City()

public function testGeoIP2Country()
{
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';

$locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-Country.mmdb'], 'isp' => []]);
$result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

Expand All @@ -53,8 +49,6 @@ public function testGeoIP2Country()

public function testGeoIP2ASN()
{
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';

$locationProvider = new GeoIp2\Php(['loc' => [], 'isp' => ['GeoLite2-ASN.mmdb']]);
$result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

Expand All @@ -66,8 +60,6 @@ public function testGeoIP2ASN()

public function testGeoIP2ISP()
{
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';

$locationProvider = new GeoIp2\Php(['loc' => [], 'isp' => ['GeoIP2-ISP.mmdb']]);
$result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

Expand All @@ -79,8 +71,6 @@ public function testGeoIP2ISP()

public function testGeoIP2CityAndISP()
{
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';

$locationProvider = new GeoIp2\Php(['loc' => ['GeoIP2-City.mmdb'], 'isp' => ['GeoIP2-ISP.mmdb']]);
$result = $locationProvider->getLocation(['ip' => '194.57.91.215']);

Expand Down
Expand Up @@ -38,7 +38,6 @@ public function setUp()
self::$idSite = Fixture::createWebsite('2016-01-01');
Fixture::createSuperUser(true);

GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';
LocationProvider::$providers = null;
LocationProvider::setCurrentProvider('geoip2php');
}
Expand Down
1 change: 0 additions & 1 deletion plugins/UserCountry/tests/Integration/APITest.php
Expand Up @@ -42,7 +42,6 @@ public function setUp()

public function test_setLocationProvider()
{
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';
$locationProvider = GeoIp2\Php::ID;
$this->api->setLocationProvider($locationProvider);
$this->assertEquals($locationProvider, Common::getCurrentLocationProviderId());
Expand Down
2 changes: 0 additions & 2 deletions tests/PHPUnit/Fixtures/ManySitesImportedLogs.php
Expand Up @@ -37,7 +37,6 @@ public function setUp()
$this->setUpWebsitesAndGoals();

LocationProvider::$providers = null;
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';
LocationProvider::setCurrentProvider('geoip2php');

self::createSuperUser();
Expand All @@ -49,7 +48,6 @@ public function setUp()
public function tearDown()
{
LocationProvider::$providers = null;
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';
ManyVisitsWithGeoIP::unsetLocationProvider();
}

Expand Down
1 change: 0 additions & 1 deletion tests/PHPUnit/Fixtures/ManyVisitsWithGeoIP.php
Expand Up @@ -245,7 +245,6 @@ private function trackOtherVisits()
public function setLocationProvider($file)
{
GeoIp2::$dbNames['loc'] = array($file);
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';
LocationProvider::$providers = null;
LocationProvider::setCurrentProvider(self::GEOIP_IMPL_TO_TEST);

Expand Down
1 change: 0 additions & 1 deletion tests/PHPUnit/Fixtures/UITestFixture.php
Expand Up @@ -79,7 +79,6 @@ public function setUp()
);

// for proper geolocation
GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';
LocationProvider::setCurrentProvider(GeoIp2\Php::ID);
IPAnonymizer::deactivate();

Expand Down
2 changes: 0 additions & 2 deletions tests/PHPUnit/proxy/piwik.php
Expand Up @@ -33,8 +33,6 @@

Environment::setGlobalEnvironmentManipulator(new TestingEnvironmentManipulator(new TestingEnvironmentVariables(), $globalObservers));

\Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2::$geoIPDatabaseDir = 'tests/lib/geoip-files';

include PIWIK_INCLUDE_PATH . '/matomo.php';
} catch (Exception $ex) {
echo "Unexpected error during tracking: " . $ex->getMessage() . "\n" . $ex->getTraceAsString() . "\n";
Expand Down

0 comments on commit 3474bf2

Please sign in to comment.