diff options
Diffstat (limited to 'plugins/CustomDimensions/Commands')
m--------- | plugins/CustomDimensions | 0 | ||||
-rw-r--r-- | plugins/CustomDimensions/Commands/AddCustomDimension.php | 115 | ||||
-rw-r--r-- | plugins/CustomDimensions/Commands/Info.php | 61 | ||||
-rw-r--r-- | plugins/CustomDimensions/Commands/RemoveCustomDimension.php | 144 |
4 files changed, 320 insertions, 0 deletions
diff --git a/plugins/CustomDimensions b/plugins/CustomDimensions deleted file mode 160000 -Subproject 318661a2fb1ef3b3e5d6d999ae8b9628cb5a113 diff --git a/plugins/CustomDimensions/Commands/AddCustomDimension.php b/plugins/CustomDimensions/Commands/AddCustomDimension.php new file mode 100644 index 0000000000..b30363e5a1 --- /dev/null +++ b/plugins/CustomDimensions/Commands/AddCustomDimension.php @@ -0,0 +1,115 @@ +<?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\CustomDimensions\Commands; + +use Piwik\Plugin\ConsoleCommand; +use Piwik\Plugins\CustomDimensions\CustomDimensions; +use Piwik\Plugins\CustomDimensions\Dao\LogTable; +use Piwik\Tracker\Cache; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + */ +class AddCustomDimension extends ConsoleCommand +{ + protected function configure() + { + $this->setName('customdimensions:add-custom-dimension'); + $this->setDescription('Add new Custom Dimension available.'); + $this->setHelp("Example: +./console customdimensions:add-custom-dimension --scope=action --count=10 +=> Will add 10 new Custom Dimensions in scope 'action'. +"); + + $description = sprintf('The scope of the Custom Dimension to add, either "%s" or "%s"', CustomDimensions::SCOPE_VISIT, CustomDimensions::SCOPE_ACTION); + $this->addOption('scope', null, InputOption::VALUE_REQUIRED, $description); + $this->addOption('count', null, InputOption::VALUE_REQUIRED, 'Define how many Custom Dimensions shall be added', '1'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $scope = $this->getScope($input); + $count = $this->getCount($input); + + $output->writeln(sprintf('Adding %d Custom Dimension(s) in scope %s.', $count, $scope)); + $output->writeln('<info>This causes schema changes in the database and may take a very long time.</info>'); + + $noInteraction = $input->getOption('no-interaction'); + if (!$noInteraction && !$this->confirmChange($output)) { + return; + } + + $output->writeln(''); + $output->writeln('Starting to add Custom Dimension(s)'); + $output->writeln(''); + + $tracking = new LogTable($scope); + $tracking->addManyCustomDimensions($count); + + if ($scope === CustomDimensions::SCOPE_VISIT) { + $tracking = new LogTable(CustomDimensions::SCOPE_CONVERSION); + $tracking->addManyCustomDimensions($count); + } + + Cache::clearCacheGeneral(); + + $numDimensionsAvailable = $tracking->getNumInstalledIndexes(); + + $this->writeSuccessMessage($output, array( + sprintf('Your Piwik is now configured for up to %d Custom Dimensions in scope %s.', $numDimensionsAvailable, $scope) + )); + } + + private function getScope(InputInterface $input) + { + $scope = $input->getOption('scope'); + + if (empty($scope) || !in_array($scope, CustomDimensions::getScopes())) { + // we also allow scope "conversion" in case on needs to repair something but we don't document as it would be rather confusing + $message = sprintf('The specified scope is invalid. Use either "--scope=%s" or "--scope=%s"', CustomDimensions::SCOPE_VISIT, CustomDimensions::SCOPE_ACTION); + throw new \InvalidArgumentException($message); + } + + return $scope; + } + + private function getCount(InputInterface $input) + { + $count = $input->getOption('count'); + + if (!is_numeric($count)) { + throw new \InvalidArgumentException('Option "count" must be a number'); + } + + $count = (int) $count; + + if ($count <= 0) { + throw new \InvalidArgumentException('Option "count" must be at least one'); + } + + return $count; + } + + private function confirmChange(OutputInterface $output) + { + $output->writeln(''); + + $dialog = $this->getHelperSet()->get('dialog'); + return $dialog->askConfirmation( + $output, + '<question>Are you sure you want to perform this action? (y/N)</question>', + false + ); + } + +} diff --git a/plugins/CustomDimensions/Commands/Info.php b/plugins/CustomDimensions/Commands/Info.php new file mode 100644 index 0000000000..a72e2f87c6 --- /dev/null +++ b/plugins/CustomDimensions/Commands/Info.php @@ -0,0 +1,61 @@ +<?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\CustomDimensions\Commands; + +use Piwik\Plugin\ConsoleCommand; +use Piwik\Plugins\CustomDimensions\CustomDimensions; +use Piwik\Plugins\CustomDimensions\Dao\LogTable; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + */ +class Info extends ConsoleCommand +{ + protected function configure() + { + $this->setName('customdimensions:info'); + $this->setDescription('Get information about currently installed Custom Dimensions'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + foreach (CustomDimensions::getScopes() as $scope) { + $tracking = new LogTable($scope); + $output->writeln(sprintf('%s Custom Dimensions available in scope "%s"', $tracking->getNumInstalledIndexes(), $scope)); + + if ($scope === CustomDimensions::SCOPE_CONVERSION) { + $output->writeln(sprintf('Custom Dimensions are automatically added via the scope "%s" and cannot be added manually', CustomDimensions::SCOPE_VISIT)); + } else { + $output->writeln(sprintf('To add a Custom Dimension execute "<comment>./console customdimensions:add-custom-dimension --scope=%s</comment>"', $scope)); + } + $output->writeln('Installed indexes are:'); + foreach ($tracking->getInstalledIndexes() as $index) { + $output->writeln(sprintf('%d to remove this Custom Dimension execute <comment>./console customdimensions:remove-custom-dimension --scope=%s --index=%d</comment>', $index, $scope, $index)); + } + $output->writeln(''); + } + + $visit = new LogTable(CustomDimensions::SCOPE_VISIT); + $numVisit = $visit->getNumInstalledIndexes(); + + $conversion = new LogTable(CustomDimensions::SCOPE_CONVERSION); + $numConversions = $conversion->getNumInstalledIndexes(); + + if ($numConversions < $numVisit) { + $output->writeln(''); + $output->writeln('<error>We found an error, Custom Dimensions in scope "conversion" are not correctly installed. Execute the following command to repair it:</error>'); + $output->writeln(sprintf('<comment>./console customdimensions:add-custom-dimension --scope=%s --count=%d</comment>', CustomDimensions::SCOPE_CONVERSION, $numVisit - $numConversions)); + } + } + +} diff --git a/plugins/CustomDimensions/Commands/RemoveCustomDimension.php b/plugins/CustomDimensions/Commands/RemoveCustomDimension.php new file mode 100644 index 0000000000..30fdfe7664 --- /dev/null +++ b/plugins/CustomDimensions/Commands/RemoveCustomDimension.php @@ -0,0 +1,144 @@ +<?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\CustomDimensions\Commands; + +use Piwik\Plugin\ConsoleCommand; +use Piwik\Plugins\CustomDimensions\CustomDimensions; +use Piwik\Plugins\CustomDimensions\Dao\Configuration; +use Piwik\Plugins\CustomDimensions\Dao\LogTable; +use Piwik\Tracker\Cache; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + */ +class RemoveCustomDimension extends ConsoleCommand +{ + protected function configure() + { + $this->setName('customdimensions:remove-custom-dimension'); + $this->setDescription('Removes an existing Custom Dimension'); + $this->setHelp("Example: +./console customdimensions:remove-custom-dimension --scope=action --index=4 +=> Will remove the Custom Dimension having the index 4 in scope action. +"); + + $description = sprintf('The scope of the Custom Dimension to remove, either "%s" or "%s"', CustomDimensions::SCOPE_VISIT, CustomDimensions::SCOPE_ACTION); + $this->addOption('scope', null, InputOption::VALUE_REQUIRED, $description); + $this->addOption('index', null, InputOption::VALUE_REQUIRED, 'Defines which specific Custom Dimension should be removed. To get a list of all available Custom Dimensions execute the command "./console customdimensions:info".'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $scope = $this->getScope($input); + + $tracking = new LogTable($scope); + $installedIndexes = $tracking->getInstalledIndexes(); + + $index = $this->getIndex($input, $installedIndexes); + + $output->writeln(sprintf('Remove Custom Dimension at index %d in scope %s.', $index, $scope)); + + $configuration = new Configuration(); + $configs = $configuration->getCustomDimensionsHavingIndex($scope, $index); + + $names = array(); + foreach ($configs as $config) { + $names[] = $config['name']; + } + + if (empty($names)) { + $output->writeln('This index is currently not used by any website'); + } else { + $output->writeln(sprintf('This index is used by %d websites and used for the following Custom Dimensions: "%s"', count($names), implode('", "', $names))); + } + + $output->writeln(''); + $output->writeln('<comment>This causes schema changes in the database and may take a very long time.</comment>'); + $output->writeln('<comment>Removing tracked Custom Dimension data cannot be undone unless you have a backup.</comment>'); + + $noInteraction = $input->getOption('no-interaction'); + if (!$noInteraction && !$this->confirmChange($output)) { + return; + } + + $output->writeln(''); + $output->writeln('Starting to remove this Custom Dimension.'); + $output->writeln(''); + + $tracking = new LogTable($scope); + $tracking->removeCustomDimension($index); + + $configuration->deleteConfigurationsForIndex($index, $scope); + + if ($scope === CustomDimensions::SCOPE_VISIT) { + $tracking = new LogTable(CustomDimensions::SCOPE_CONVERSION); + $tracking->removeCustomDimension($index); + } + + Cache::clearCacheGeneral(); + + $numDimensionsAvailable = $tracking->getNumInstalledIndexes(); + + $this->writeSuccessMessage($output, array( + sprintf('Your Piwik is now configured for up to %d Custom Dimensions in scope %s.', $numDimensionsAvailable, $scope) + )); + } + + private function getScope(InputInterface $input) + { + $scope = $input->getOption('scope'); + + if (empty($scope) || !in_array($scope, array(CustomDimensions::SCOPE_VISIT, CustomDimensions::SCOPE_ACTION))) { + $message = sprintf('The specified scope is invalid. Use either "--scope=%s" or "--scope=%s"', CustomDimensions::SCOPE_VISIT, CustomDimensions::SCOPE_ACTION); + throw new \InvalidArgumentException($message); + } + + return $scope; + } + + private function getIndex(InputInterface $input, $installedIndexes) + { + $index = $input->getOption('index'); + + $indexesHelp = 'Installed indexes are: ' . implode(', ', $installedIndexes); + + if (empty($index)) { + throw new \InvalidArgumentException('An option "index" must be specified. ' . $indexesHelp); + } + + if (!is_numeric($index)) { + throw new \InvalidArgumentException('Option "index" must be a number'); + } + + $index = (int) $index; + + if (!in_array($index, $installedIndexes)) { + throw new \InvalidArgumentException('Specified index is not installed. ' . $indexesHelp); + } + + return $index; + } + + private function confirmChange(OutputInterface $output) + { + $output->writeln(''); + + $dialog = $this->getHelperSet()->get('dialog'); + return $dialog->askConfirmation( + $output, + '<question>Are you sure you want to perform this action? (y/N)</question>', + false + ); + } + +} |