diff options
author | dizzy <diosmosis@users.noreply.github.com> | 2021-03-02 09:53:35 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-02 09:53:35 +0300 |
commit | 5b07a224820003cd6de727fff179528852cbf3a7 (patch) | |
tree | bc06ee253a05d60a3af4c4d1f9930781b8c36c90 /plugins | |
parent | 6e0302642443bb36fa932c4f2d9074b52374c86b (diff) |
In core:invalidate-report-data match segments by ID, name or definition ... (#16994)
* starting on change
* In core:invalidate-report-data match segments by ID, name or definition and warn when a definition does not match any known segment.
* fix test
* remove --yes option since it can break bc
* add test
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/CoreAdminHome/Commands/InvalidateReportData.php | 65 | ||||
-rw-r--r-- | plugins/CoreAdminHome/tests/Integration/Commands/InvalidateReportDataTest.php | 32 |
2 files changed, 91 insertions, 6 deletions
diff --git a/plugins/CoreAdminHome/Commands/InvalidateReportData.php b/plugins/CoreAdminHome/Commands/InvalidateReportData.php index b3bb2d115b..3e112c8c8c 100644 --- a/plugins/CoreAdminHome/Commands/InvalidateReportData.php +++ b/plugins/CoreAdminHome/Commands/InvalidateReportData.php @@ -13,14 +13,19 @@ use Piwik\Date; use Piwik\Period; use Piwik\Period\Range; use Piwik\Piwik; +use Piwik\Plugins\SegmentEditor\API; use Piwik\Segment; use Piwik\Plugin\ConsoleCommand; use Piwik\Plugins\SitesManager\API as SitesManagerAPI; use Piwik\Site; use Piwik\Period\Factory as PeriodFactory; +use Psr\Log\LoggerInterface; +use Symfony\Component\Console\Helper\QuestionHelper; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Console\Question\Question; /** * Provides a simple interface for invalidating report data by date ranges, site IDs and periods. @@ -29,6 +34,8 @@ class InvalidateReportData extends ConsoleCommand { const ALL_OPTION_VALUE = 'all'; + private $allSegments = null; + protected function configure() { $this->setName('core:invalidate-report-data'); @@ -43,7 +50,8 @@ class InvalidateReportData extends ConsoleCommand . 'week, month, year or "all" for all of them.', self::ALL_OPTION_VALUE); $this->addOption('segment', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, - 'List of segments to invalidate report data for.'); + 'List of segments to invalidate report data for. This can be the segment string itself, the segment name from the UI or the ID of the segment.' + . ' If specifying the segment definition, make sure it is encoded properly (it should be the same as the segment parameter in the URL.'); $this->addOption('cascade', null, InputOption::VALUE_NONE, 'If supplied, invalidation will cascade, invalidating child period types even if they aren\'t specified in' . ' --periods. For example, if --periods=week, --cascade will cause the days within those weeks to be ' @@ -70,7 +78,7 @@ class InvalidateReportData extends ConsoleCommand $sites = $this->getSitesToInvalidateFor($input); $periodTypes = $this->getPeriodTypesToInvalidateFor($input); $dateRanges = $this->getDateRangesToInvalidateFor($input); - $segments = $this->getSegmentsToInvalidateFor($input, $sites); + $segments = $this->getSegmentsToInvalidateFor($input, $sites, $output); foreach ($periodTypes as $periodType) { if ($periodType === 'range') { @@ -214,7 +222,7 @@ class InvalidateReportData extends ConsoleCommand return $result; } - private function getSegmentsToInvalidateFor(InputInterface $input, $idSites) + private function getSegmentsToInvalidateFor(InputInterface $input, $idSites, OutputInterface $output) { $segments = $input->getOption('segment'); $segments = array_map('trim', $segments); @@ -225,9 +233,56 @@ class InvalidateReportData extends ConsoleCommand } $result = array(); - foreach ($segments as $segmentString) { - $result[] = new Segment($segmentString, $idSites); + foreach ($segments as $segmentOptionValue) { + $segmentDefinition = $this->findSegment($segmentOptionValue, $idSites, $input, $output); + if (empty($segmentDefinition)) { + continue; + } + + $result[] = new Segment($segmentDefinition, $idSites); } return $result; } + + private function findSegment($segmentOptionValue, $idSites, InputInterface $input, OutputInterface $output) + { + $logger = StaticContainer::get(LoggerInterface::class); + + $allSegments = $this->getAllSegments(); + foreach ($allSegments as $segment) { + if (!empty($segment['enable_only_idsite']) + && !in_array($segment['enable_only_idsite'], $idSites) + ) { + continue; + } + + if ($segmentOptionValue == $segment['idsegment']) { + $logger->debug("Matching '$segmentOptionValue' by idsegment with segment {segment}.", ['segment' => json_encode($segment)]); + return $segment['definition']; + } + + if (strtolower($segmentOptionValue) == strtolower($segment['name'])) { + $logger->debug("Matching '$segmentOptionValue' by name with segment {segment}.", ['segment' => json_encode($segment)]); + return $segment['definition']; + } + + if ($segment['definition'] == $segmentOptionValue + || $segment['definition'] == urldecode($segmentOptionValue) + ) { + $logger->debug("Matching '{value}' by definition with segment {segment}.", ['value' => $segmentOptionValue, 'segment' => json_encode($segment)]); + return $segment['definition']; + } + } + + $logger->warning("'$segmentOptionValue' did not match any stored segment, but invalidating it anyway."); + return $segmentOptionValue; + } + + private function getAllSegments() + { + if ($this->allSegments === null) { + $this->allSegments = API::getInstance()->getAll(); + } + return $this->allSegments; + } } diff --git a/plugins/CoreAdminHome/tests/Integration/Commands/InvalidateReportDataTest.php b/plugins/CoreAdminHome/tests/Integration/Commands/InvalidateReportDataTest.php index 8a4f9e83b3..53abf9999b 100644 --- a/plugins/CoreAdminHome/tests/Integration/Commands/InvalidateReportDataTest.php +++ b/plugins/CoreAdminHome/tests/Integration/Commands/InvalidateReportDataTest.php @@ -8,6 +8,8 @@ namespace Piwik\Plugins\CoreAdminHome\tests\Integration\Commands; +use Piwik\ArchiveProcessor\Rules; +use Piwik\Plugins\SegmentEditor\API; use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase; @@ -21,9 +23,11 @@ class InvalidateReportDataTest extends ConsoleCommandTestCase { parent::setUpBeforeClass(); + $idSite = Fixture::createWebsite('2012-01-01 00:00:00'); Fixture::createWebsite('2012-01-01 00:00:00'); Fixture::createWebsite('2012-01-01 00:00:00'); - Fixture::createWebsite('2012-01-01 00:00:00'); + + API::getInstance()->add('test segment', 'browserCode==IE', $idSite); } /** @@ -384,6 +388,32 @@ class InvalidateReportDataTest extends ConsoleCommandTestCase '[Dry-run] invalidating archives for site = [ 1 ], dates = [ 2015-05-04 ], period = [ day ], segment = [ ], cascade = [ 0 ], plugin = [ ExamplePlugin ]', ], ], + + // match segment by id + [ + ['2015-05-04'], + 'day', + '1', + false, + [1], + null, + [ + '[Dry-run] invalidating archives for site = [ 1 ], dates = [ 2015-05-04 ], period = [ day ], segment = [ browserCode==IE ], cascade = [ 0 ]', + ], + ], + + // match segment by name + [ + ['2015-05-04'], + 'day', + '1', + false, + ['test segment'], + null, + [ + '[Dry-run] invalidating archives for site = [ 1 ], dates = [ 2015-05-04 ], period = [ day ], segment = [ browserCode==IE ], cascade = [ 0 ]', + ], + ], ); } } |