diff options
author | Stefan Giehl <stefan@matomo.org> | 2020-05-14 18:26:56 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 18:26:56 +0300 |
commit | dc4c4947d0f3d9e193be4191c963b55c6018d6a2 (patch) | |
tree | ee869886d568ff363c0d84b7092899b4405bf6d7 /plugins/GeoIp2 | |
parent | e2c98248f71a91d8b57c92ed72a4b8d68539584d (diff) |
Make it possible to automatically set up geoip2 in installation (#15939)
* make it possible to automatically set up geoip2 in installation
* reenable skipped installation ui tests
* updates expected UI files
* do download in hourly task
* trigger an async run of scheduled tasks to set up geoip2
* directly execute the task if possible and schedule the task only if not
* improve text
* updates UI test
Diffstat (limited to 'plugins/GeoIp2')
-rw-r--r-- | plugins/GeoIp2/GeoIP2AutoUpdater.php | 21 | ||||
-rw-r--r-- | plugins/GeoIp2/GeoIp2.php | 64 | ||||
-rw-r--r-- | plugins/GeoIp2/lang/en.json | 2 |
3 files changed, 83 insertions, 4 deletions
diff --git a/plugins/GeoIp2/GeoIP2AutoUpdater.php b/plugins/GeoIp2/GeoIP2AutoUpdater.php index c093bfcd6f..65903e94d7 100644 --- a/plugins/GeoIp2/GeoIP2AutoUpdater.php +++ b/plugins/GeoIp2/GeoIP2AutoUpdater.php @@ -15,13 +15,14 @@ use GeoIp2\Database\Reader; use Piwik\Common; use Piwik\Container\StaticContainer; use Piwik\Date; -use Piwik\Filesystem; use Piwik\Http; use Piwik\Log; use Piwik\Option; use Piwik\Piwik; use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2 AS LocationProviderGeoIp2; use Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2\Php; +use Piwik\Plugins\UserCountry\LocationProvider; +use Piwik\Scheduler\Schedule\Hourly; use Piwik\Scheduler\Scheduler; use Piwik\Scheduler\Task; use Piwik\Scheduler\Timetable; @@ -47,6 +48,8 @@ class GeoIP2AutoUpdater extends Task const LAST_RUN_TIME_OPTION_NAME = 'geoip2.updater_last_run_time'; + const AUTO_SETUP_OPTION_NAME = 'geoip2.autosetup'; + private static $urlOptions = array( 'loc' => self::LOC_URL_OPTION_NAME, 'isp' => self::ISP_URL_OPTION_NAME, @@ -82,6 +85,10 @@ class GeoIP2AutoUpdater extends Task break; } + if (Option::get(self::AUTO_SETUP_OPTION_NAME)) { + $schedulePeriod = new Hourly(); + } + parent::__construct($this, 'update', null, $schedulePeriod, Task::LOWEST_PRIORITY); } @@ -111,6 +118,15 @@ class GeoIP2AutoUpdater extends Task } $this->performRedundantDbChecks(); + + if (Option::get(self::AUTO_SETUP_OPTION_NAME)) { + Option::delete(self::AUTO_SETUP_OPTION_NAME); + LocationProvider::setCurrentProvider(Php::ID); + /** @var Scheduler $scheduler */ + $scheduler = StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler'); + // reschedule to ensure it's not run again in an hour + $scheduler->rescheduleTask(new GeoIP2AutoUpdater()); + } } /** @@ -262,7 +278,8 @@ class GeoIP2AutoUpdater extends Task $dbFilename = $php->detectDatabaseType($dbType) . '.mmdb'; } } else { - $ext = end(explode(basename($path), '.', 2)); + $parts = explode(basename($path), '.', 2); + $ext = end($parts); throw new Exception(Piwik::translate('GeoIp2_UnsupportedArchiveType', "'$ext'")); } diff --git a/plugins/GeoIp2/GeoIp2.php b/plugins/GeoIp2/GeoIp2.php index ffba09bd98..2f1c2e6748 100644 --- a/plugins/GeoIp2/GeoIp2.php +++ b/plugins/GeoIp2/GeoIp2.php @@ -8,7 +8,13 @@ */ namespace Piwik\Plugins\GeoIp2; +use Piwik\CliMulti; +use Piwik\Container\StaticContainer; +use Piwik\Option; +use Piwik\Piwik; +use Piwik\Plugins\Installation\FormDefaultSettings; use Piwik\Plugins\UserCountry\LocationProvider; +use Piwik\Scheduler\Scheduler; /** * @@ -18,8 +24,10 @@ class GeoIp2 extends \Piwik\Plugin public function registerEvents() { return array( - 'AssetManager.getJavaScriptFiles' => 'getJsFiles', - 'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys', + 'AssetManager.getJavaScriptFiles' => 'getJsFiles', + 'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys', + 'Installation.defaultSettingsForm.init' => 'installationFormInit', + 'Installation.defaultSettingsForm.submit' => 'installationFormSubmit', ); } @@ -50,4 +58,56 @@ class GeoIp2 extends \Piwik\Plugin $translationKeys[] = "General_Save"; $translationKeys[] = "General_Continue"; } + + /** + * Customize the Installation "default settings" form. + * + * @param FormDefaultSettings $form + */ + public function installationFormInit(FormDefaultSettings $form) + { + $form->addElement('checkbox', 'setup_geoip2', null, + [ + 'content' => '<div class="form-help">' . Piwik::translate('GeoIp2_AutomaticSetupDescription', ['<a rel="noreferrer noopener" href="https://db-ip.com/?refid=mtm">','</a>']) . '</div> ' . Piwik::translate('GeoIp2_AutomaticSetup') + ] + ); + + // default values + $form->addDataSource(new \HTML_QuickForm2_DataSource_Array([ + 'setup_geoip2' => true, + ])); + } + + /** + * Process the submit on the Installation "default settings" form. + * + * @param FormDefaultSettings $form + */ + public function installationFormSubmit(FormDefaultSettings $form) + { + $setupGeoIp2 = (bool) $form->getSubmitValue('setup_geoip2'); + + if ($setupGeoIp2) { + Option::set(GeoIP2AutoUpdater::AUTO_SETUP_OPTION_NAME, true); + GeoIP2AutoUpdater::setUpdaterOptions([ + 'loc' => \Piwik\Plugins\GeoIp2\LocationProvider\GeoIp2::getDbIpLiteUrl(), + 'period' => GeoIP2AutoUpdater::SCHEDULE_PERIOD_MONTHLY + ]); + + $cliMulti = new CliMulti(); + + // directly trigger the update task if possible + // otherwise ensure it will be run soonish as scheduled task + if ($cliMulti->supportsAsync()) { + $phpCli = new CliMulti\CliPhp(); + $command = sprintf('%s %s/console core:run-scheduled-tasks --force "Piwik\Plugins\GeoIp2\GeoIP2AutoUpdater.update" > /dev/null 2>&1 &', + $phpCli->findPhpBinary(), PIWIK_INCLUDE_PATH); + shell_exec($command); + } else { + /** @var Scheduler $scheduler */ + $scheduler = StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler'); + $scheduler->rescheduleTask(new GeoIP2AutoUpdater()); + } + } + } } diff --git a/plugins/GeoIp2/lang/en.json b/plugins/GeoIp2/lang/en.json index 158028266e..bee4e21e16 100644 --- a/plugins/GeoIp2/lang/en.json +++ b/plugins/GeoIp2/lang/en.json @@ -1,6 +1,8 @@ { "GeoIp2": { "AssumingNonApache": "Cannot find apache_get_modules function, assuming non-Apache webserver.", + "AutomaticSetup": "Automatically configure geolocation using a dbip database", + "AutomaticSetupDescription": "For a proper geolocation Matomo requires an external database. Using this option, Matomo will automatically be configured to download and use the latest dbip city level database. [%1$sView licensing terms%2$s]", "CannotFindGeoIPDatabaseInArchive": "No valid DBIP / GeoIP database could be found in tar archive %1$s!", "CannotFindGeoIPServerVar": "The %s variable is not set. Your server may not be configured correctly.", "CannotListContent": "Couldn't list content for %1$s: %2$s", |