setRestrictToPeriods(''); $this->periodIdsToLabels = array_flip(Piwik::$idPeriods); } /** * @param array $archive * @return string|false */ public function filterArchive($archive) { $segment = isset($archive['segment']) ? $archive['segment'] : ''; if ($this->disableSegmentsArchiving && !empty($segment) ) { return 'segment archiving disabled'; } if (!empty($this->segmentsToForce)) { if (!empty($this->segmentsToForce) && !in_array($segment, $this->segmentsToForce)) { return "segment '$segment' is not in --force-idsegments"; } } if (!empty($this->skipSegmentsForToday)) { $site = new Site($archive['idsite']); $period = Factory::build($this->periodIdsToLabels[$archive['period']], $archive['date1']); $segment = new Segment($segment, [$archive['idsite']]); if (Archive::shouldSkipArchiveIfSkippingSegmentArchiveForToday($site, $period, $segment)) { return "skipping segment archives for today"; } } if (!empty($this->restrictToDateRange) && ($this->restrictToDateRange[0]->isLater(Date::factory($archive['date2'])) || $this->restrictToDateRange[1]->isEarlier(Date::factory($archive['date1'])) ) ) { return "archive date range ({$archive['date1']},{$archive['date2']}) is not within --force-date-range"; } $periodLabel = $this->periodIdsToLabels[$archive['period']]; if (!empty($this->restrictToPeriods) && !in_array($periodLabel, $this->restrictToPeriods) ) { return "period is not specified in --force-periods"; } if (!empty($this->forceReport) && (empty($archive['plugin']) || empty($archive['report']) || $archive['plugin'] . '.' . $archive['report'] != $this->forceReport) ) { return "report is not the same as value specified in --force-report"; } return false; } public function logFilterInfo(LoggerInterface $logger) { $this->logForcedSegmentInfo($logger); $this->logForcedPeriodInfo($logger); $this->logSkipSegmentInfo($logger); } private function logForcedSegmentInfo(LoggerInterface $logger) { if (empty($this->segmentsToForce)) { return; } $logger->info("- Limiting segment archiving to following segments:"); foreach ($this->segmentsToForce as $segmentDefinition) { $logger->info(" * " . $segmentDefinition); } } private function logForcedPeriodInfo(LoggerInterface $logger) { if (!empty($this->restrictToPeriods)) { $logger->info("- Will only process the following periods: " . implode(", ", $this->restrictToPeriods) . " (--force-periods)"); } } /** * @return null */ public function getSegmentsToForce() { return $this->segmentsToForce; } /** * @param int[] $idSegments */ public function setSegmentsToForceFromSegmentIds($idSegments) { /** @var SegmentEditorModel $segmentEditorModel */ $segmentEditorModel = StaticContainer::get('Piwik\Plugins\SegmentEditor\Model'); $segments = $segmentEditorModel->getAllSegmentsAndIgnoreVisibility(); $segments = array_filter($segments, function ($segment) use ($idSegments) { return in_array($segment['idsegment'], $idSegments); }); $segments = array_map(function ($segment) { return $segment['definition']; }, $segments); $this->segmentsToForce = $segments; } /** * @return bool */ public function isDisableSegmentsArchiving() { return $this->disableSegmentsArchiving; } /** * @param bool $disableSegmentsArchiving */ public function setDisableSegmentsArchiving(bool $disableSegmentsArchiving) { $this->disableSegmentsArchiving = $disableSegmentsArchiving; } /** * @return false|string */ public function getRestrictToDateRange() { return $this->restrictToDateRange; } /** * @param false|string $restrictToDateRange */ public function setRestrictToDateRange($restrictToDateRange) { if (empty($restrictToDateRange)) { $this->restrictToDateRange = $restrictToDateRange; return; } try { $parts = explode(',', $restrictToDateRange); $parts = [ Date::factory($parts[0]), Date::factory($parts[1]), ]; } catch (\Exception $ex) { throw new \Exception('Invalid restrict to date range argument: ' . $restrictToDateRange); } $this->restrictToDateRange = $parts; } public function setSegmentsToForce(array $segments) { $this->segmentsToForce = $segments; } public function setSkipSegmentsForToday($skipSegmentsForToday) { $this->skipSegmentsForToday = $skipSegmentsForToday; } /** * @return bool */ public function isSkipSegmentsForToday(): bool { return $this->skipSegmentsForToday; } public function setForceReport($forceReport) { $this->forceReport = $forceReport; } /** * @return array */ private function getPeriodsToProcess() { return $this->restrictToPeriods; } /** * @return array */ private function getDefaultPeriodsToProcess() { return array('day', 'week', 'month', 'year', 'range'); } /** * @return string[] */ public function getRestrictToPeriods() { return $this->restrictToPeriods; } /** * @param string|string[] $restrictToPeriods */ public function setRestrictToPeriods($restrictToPeriods) { if (is_string($restrictToPeriods)) { $restrictToPeriods = explode(',', $restrictToPeriods); $restrictToPeriods = array_map('trim', $restrictToPeriods); } $this->restrictToPeriods = $restrictToPeriods ?: []; $this->restrictToPeriods = array_intersect($this->restrictToPeriods, $this->getDefaultPeriodsToProcess()); $this->restrictToPeriods = array_intersect($this->restrictToPeriods, PeriodFactory::getPeriodsEnabledForAPI()); } private function logSkipSegmentInfo(LoggerInterface $logger) { if ($this->skipSegmentsForToday) { $logger->info('Will skip segments archiving for today unless they were created recently'); } } }