diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index fb07cb47..42b8ab4c 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -18,6 +18,7 @@ return [ Command\ShortUrl\DeleteShortUrlCommand::NAME => Command\ShortUrl\DeleteShortUrlCommand::class, Command\Visit\ProcessVisitsCommand::NAME => Command\Visit\ProcessVisitsCommand::class, + Command\Visit\UpdateDbCommand::NAME => Command\Visit\UpdateDbCommand::class, Command\Config\GenerateCharsetCommand::NAME => Command\Config\GenerateCharsetCommand::class, Command\Config\GenerateSecretCommand::NAME => Command\Config\GenerateSecretCommand::class, diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index b226933c..8a31cb02 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\CLI; +use Shlinkio\Shlink\Common\IpGeolocation\GeoLite2\DbUpdater; use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Shlinkio\Shlink\Common\Service\PreviewGenerator; use Shlinkio\Shlink\Core\Service; @@ -25,6 +26,7 @@ return [ Command\ShortUrl\DeleteShortUrlCommand::class => ConfigAbstractFactory::class, Command\Visit\ProcessVisitsCommand::class => ConfigAbstractFactory::class, + Command\Visit\UpdateDbCommand::class => ConfigAbstractFactory::class, Command\Config\GenerateCharsetCommand::class => ConfigAbstractFactory::class, Command\Config\GenerateSecretCommand::class => ConfigAbstractFactory::class, @@ -68,6 +70,7 @@ return [ IpLocationResolverInterface::class, 'translator', ], + Command\Visit\UpdateDbCommand::class => [DbUpdater::class, 'translator'], Command\Config\GenerateCharsetCommand::class => ['translator'], Command\Config\GenerateSecretCommand::class => ['translator'], diff --git a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php index 24e6e055..a5a33fcb 100644 --- a/module/CLI/src/Command/Visit/ProcessVisitsCommand.php +++ b/module/CLI/src/Command/Visit/ProcessVisitsCommand.php @@ -40,7 +40,7 @@ class ProcessVisitsCommand extends Command $this->visitService = $visitService; $this->ipLocationResolver = $ipLocationResolver; $this->translator = $translator; - parent::__construct(null); + parent::__construct(); } protected function configure(): void diff --git a/module/CLI/src/Command/Visit/UpdateDbCommand.php b/module/CLI/src/Command/Visit/UpdateDbCommand.php new file mode 100644 index 00000000..ca4963cf --- /dev/null +++ b/module/CLI/src/Command/Visit/UpdateDbCommand.php @@ -0,0 +1,61 @@ +geoLiteDbUpdater = $geoLiteDbUpdater; + $this->translator = $translator; + parent::__construct(); + } + + protected function configure(): void + { + $this + ->setName(self::NAME) + ->setDescription( + $this->translator->translate('Updates the GeoLite2 database file used to geolocate IP addresses') + ) + ->setHelp($this->translator->translate( + 'The GeoLite2 database is updated first Tuesday every month, so this command should be ideally run ' + . 'every first Wednesday' + )); + } + + protected function execute(InputInterface $input, OutputInterface $output): void + { + $io = new SymfonyStyle($input, $output); + + try { + $this->geoLiteDbUpdater->downloadFreshCopy(); + $io->success($this->translator->translate('GeoLite2 database properly updated')); + } catch (RuntimeException $e) { + $io->error($this->translator->translate('An error occurred while updating GeoLite2 database')); + if ($io->isVerbose()) { + $this->getApplication()->renderException($e, $output); + } + } + } +}