File: InvalidateReportData.php

package info (click to toggle)
matomo 5.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 95,068 kB
  • sloc: php: 289,425; xml: 127,249; javascript: 112,130; python: 202; sh: 178; makefile: 20; sql: 10
file content (485 lines) | stat: -rw-r--r-- 17,964 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Plugins\CoreAdminHome\Commands;

use Exception;
use Piwik\Archive\ArchiveInvalidator;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Container\StaticContainer;
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 Piwik\Log\LoggerInterface;

/**
 * Provides a simple interface for invalidating report data by date ranges, site IDs and periods.
 */
class InvalidateReportData extends ConsoleCommand
{
    public const ALL_OPTION_VALUE = 'all';

    /**
     * @var null|array<Segment>
     */
    private $allSegments = null;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var ArchiveInvalidator
     */
    private $invalidator;

    public function __construct()
    {
        parent::__construct();

        $this->logger = StaticContainer::get(LoggerInterface::class);
        $this->invalidator = StaticContainer::get(ArchiveInvalidator::class);
    }

    protected function configure()
    {
        $this->setName('core:invalidate-report-data');
        $this->setDescription('Invalidate archived report data by date range, site and period.');
        $this->addRequiredValueOption(
            'dates',
            null,
            'List of dates or date ranges to invalidate report data for, eg, 2015-01-03 or 2015-01-05,2015-02-12.',
            null,
            true
        );
        $this->addRequiredValueOption(
            'sites',
            null,
            'List of site IDs to invalidate report data for, eg, "1,2,3,4" or "all" for all sites.',
            self::ALL_OPTION_VALUE
        );
        $this->addRequiredValueOption(
            'periods',
            null,
            'List of period types to invalidate report data for. Can be one or more of the following values: day, '
            . 'week, month, year, range or "all" for all of them.',
            self::ALL_OPTION_VALUE
        );
        $this->addRequiredValueOption(
            'segment',
            null,
            '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).'
            . ' If no segment is provided, all segments (including all visits) will be invalidated. To specifically invalidate all visits --segment="" can be provided.',
            null,
            true
        );
        $this->addNoValueOption(
            'cascade',
            null,
            '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 '
            . 'invalidated as well. If --periods=month, then weeks and days will be invalidated. Note: if a period '
            . 'falls partly outside of a date range, then --cascade will also invalidate data for child periods '
            . 'outside the date range. For example, if --dates=2015-09-14,2015-09-15 & --periods=week, --cascade will'
            . ' also invalidate all days within 2015-09-13,2015-09-19, even those outside the date range.'
        );
        $this->addNoValueOption('dry-run', null, 'For tests. Runs the command w/o actually '
            . 'invalidating anything.');
        $this->addRequiredValueOption('plugin', null, 'To invalidate data for a specific plugin only.');
        $this->addNoValueOption(
            'ignore-log-deletion-limit',
            null,
            'Ignore the log purging limit when invalidating archives. If a date is older than the log purging threshold (which means '
            . 'there should be no log data for it), we normally skip invalidating it in order to prevent losing any report data. In some cases, '
            . 'however it is useful, if, for example, your site was imported from Google, and there is never any log data.'
        );
        $this->setHelp('Invalidate archived report data by date range, site and period. Invalidated archive data will '
            . 'be re-archived during the next core:archive run. If your log data has changed for some reason, this '
            . 'command can be used to make sure reports are generated using the new, changed log data.');
    }

    protected function doExecute(): int
    {
        $sites = $this->getSitesToInvalidateFor();
        $periodTypes = $this->getPeriodTypesToInvalidateFor();
        $dateRanges = $this->getDateRangesToInvalidateFor();
        $segments = $this->getSegmentsToInvalidateFor($sites);

        foreach ($periodTypes as $periodType) {
            if ($periodType === 'range') {
                $this->invalidateRangePeriods($dateRanges, $segments, $sites);
            } else {
                $this->invalidateNonRangePeriods($periodType, $dateRanges, $segments, $sites);
            }
        }

        return self::SUCCESS;
    }

    private function getSegmentsWithSitesToProcess(
        array $segments,
        array $sites,
        bool $ignoreInvalidSegments = false
    ): array {
        $segmentDetails = [];

        // check availability of provided segments for all sites
        foreach ($segments as $segmentStr) {
            // determine sites where current segment is available for
            $sitesToProcess = $this->getSitesForSegment($segmentStr, $sites);
            $segment = null;

            try {
                $segment = new Segment($segmentStr, $sitesToProcess);
            } catch (Exception $e) {
                if (!$ignoreInvalidSegments) {
                    throw $e;
                }
            }

            if (empty($sitesToProcess)) {
                // segment not available for any site
                $this->logger->info("Segment [$segmentStr] not available for any site, skipping it...");
                continue;
            }

            $sitesDiff = array_diff($sites, $sitesToProcess);

            if (count($sitesDiff)) {
                $this->logger->info(
                    "Segment [$segmentStr] not available for all sites, skipping this segment for sites [ "
                    . implode(', ', $sitesDiff) . " ]."
                );
            }

            $segmentDetails[$segmentStr] = [
                'segment' => $segment,
                'sites' => $sitesToProcess,
            ];
        }

        return $segmentDetails;
    }

    private function invalidateNonRangePeriods(
        string $periodType,
        array $dateRangesToInvalidate,
        array $segments,
        array $sites
    ): void {
        $ignoreLogDeletionLimit = $this->getInput()->getOption('ignore-log-deletion-limit');
        $cascade = $this->getInput()->getOption('cascade');
        $dryRun = $this->getInput()->getOption('dry-run');
        $plugin = $this->getInput()->getOption('plugin');

        foreach ($dateRangesToInvalidate as $dateRange) {
            foreach ($segments as $segmentStr => $segmentData) {
                $sitesToProcess = $segmentData['sites'];
                $segment = $segmentData['segment'];

                $this->logger->info("Invalidating $periodType periods in $dateRange for site = [ "
                    . implode(', ', $sitesToProcess) . " ], segment = [ $segmentStr ]...");

                $dates = $this->getPeriodDates($periodType, $dateRange);

                if ($dryRun) {
                    $message = "[Dry-run] invalidating archives for site = [ " . implode(', ', $sitesToProcess)
                        . " ], dates = [ " . implode(', ', $dates) . " ], period = [ $periodType ], segment = [ "
                        . "$segmentStr ], cascade = [ " . (int)$cascade . " ]";
                    if (!empty($plugin)) {
                        $message .= ", plugin = [ $plugin ]";
                    }
                    $this->logger->info($message);
                } else {
                    $invalidationResult = $this->invalidator->markArchivesAsInvalidated(
                        $sitesToProcess,
                        $dates,
                        $periodType,
                        $segment,
                        $cascade,
                        false,
                        $plugin,
                        $ignoreLogDeletionLimit
                    );

                    if ($this->getOutput()->getVerbosity() > $this->getOutput()::VERBOSITY_NORMAL) {
                        foreach ($invalidationResult->makeOutputLogs() as $outputLog) {
                            $this->logger->info($outputLog);
                        }
                    }
                }
            }
        }
    }

    private function invalidateRangePeriods(array $dateRangesToInvalidate, array $segments, array $sites): void
    {
        $dryRun = $this->getInput()->getOption('dry-run');
        $plugin = $this->getInput()->getOption('plugin');
        $periods = trim($this->getInput()->getOption('periods'));
        $isUsingAllOption = $periods === self::ALL_OPTION_VALUE;
        $rangeDates = [];

        foreach ($dateRangesToInvalidate as $dateRange) {
            if (
                $isUsingAllOption
                && !Period::isMultiplePeriod($dateRange, 'day')
            ) {
                continue; // not a range, nothing to do... only when "all" option is used
            }

            $rangeDates[] = $this->getPeriodDates('range', $dateRange);
        }

        if (!empty($rangeDates)) {
            foreach ($segments as $segmentStr => $segmentData) {
                $sitesToProcess = $segmentData['sites'];
                $segment = $segmentData['segment'];

                if ($dryRun) {
                    $dateRangeStr = implode(';', $dateRangesToInvalidate);
                    $this->logger->info("Invalidating range periods overlapping $dateRangeStr for "
                        . "site = [ " . implode(', ', $sitesToProcess) . " ], segment = [ $segmentStr ]...");
                } else {
                    $this->invalidator->markArchivesOverlappingRangeAsInvalidated($sitesToProcess, $rangeDates, $segment, $plugin);
                }
            }
        }
    }

    /**
     * Returns all sites where the given segment is available for
     */
    private function getSitesForSegment(string $segmentStr, array $idSites): array
    {
        $sitesToProcess = [];

        foreach ($idSites as $idSite) {
            try {
                $segment = new Segment($segmentStr, $idSite);
                $sitesToProcess[] = $idSite;
            } catch (Exception $e) {
                continue;
            }
        }

        return $sitesToProcess;
    }

    /**
     * Parses, validates and returns the provided list of site ids
     */
    private function getSitesToInvalidateFor(): array
    {
        $sites = $this->getInput()->getOption('sites');

        $siteIds = Site::getIdSitesFromIdSitesString($sites);
        if (empty($siteIds)) {
            throw new \InvalidArgumentException("Invalid --sites value: '$sites'.");
        }

        $allSiteIds = SitesManagerAPI::getInstance()->getAllSitesId();
        foreach ($siteIds as $idSite) {
            if (!in_array($idSite, $allSiteIds)) {
                throw new \InvalidArgumentException("Invalid --sites value: '$sites', there are no sites with IDs = $idSite");
            }
        }

        return $siteIds;
    }

    /**
     * Parses, validates and returns the provided list of period types
     * If 'all' is provided, this will be converted to a list of all period types
     */
    private function getPeriodTypesToInvalidateFor(): array
    {
        $periods = $this->getInput()->getOption('periods');
        if (empty($periods)) {
            throw new \InvalidArgumentException("The --periods argument is required.");
        }

        if ($periods == self::ALL_OPTION_VALUE) {
            $result = array_keys(Piwik::$idPeriods);
            return $result;
        }

        $periods = explode(',', $periods);
        $periods = array_map('trim', $periods);

        foreach ($periods as $periodIdentifier) {
            if (!isset(Piwik::$idPeriods[$periodIdentifier])) {
                throw new \InvalidArgumentException("Invalid period type '$periodIdentifier' supplied in --periods.");
            }
        }

        return $periods;
    }

    /**
     * Returns the list of provided dates / date ranges
     */
    private function getDateRangesToInvalidateFor(): array
    {
        $dateRanges = $this->getInput()->getOption('dates');
        if (empty($dateRanges)) {
            throw new \InvalidArgumentException("The --dates option is required.");
        }

        return is_array($dateRanges) ? $dateRanges : [$dateRanges];
    }

    private function getPeriodDates($periodType, $dateRange)
    {
        if (!isset(Piwik::$idPeriods[$periodType])) {
            throw new \InvalidArgumentException("Invalid period type '$periodType'.");
        }

        try {
            $period = PeriodFactory::build($periodType, $dateRange);
        } catch (\Exception $ex) {
            throw new \InvalidArgumentException("Invalid date or date range specifier '$dateRange'", $code = 0, $ex);
        }

        $result = [];
        if ($periodType === 'range') {
            $result[] = $period->getDateStart();
            $result[] = $period->getDateEnd();
        } elseif ($period instanceof Range) {
            foreach ($period->getSubperiods() as $subperiod) {
                $result[] = $subperiod->getDateStart();
            }
        } else {
            $result[] = $period->getDateStart();
        }
        return $result;
    }

    /**
     * Parses, validates and returns the list of segments that can be invalidated for the provided sites
     * If no segment is provided, a list of all segments available for the provided sites will be returned (including all visits segment)
     *
     * @param array<int> $idSites
     *
     * @return array<string>
     */
    private function getSegmentsToInvalidateFor(array $idSites): array
    {
        $input = $this->getInput();
        $segments = $input->getOption('segment');
        $segments = array_map('trim', $segments);
        $segments = array_unique($segments);

        if (empty($segments)) {
            $this->logger->debug("No segment provided. Invalidating all stored segments.");
            $segments = Rules::getSegmentsToProcess($idSites);
            array_unshift($segments, "");
            return $this->getSegmentsWithSitesToProcess($segments, $idSites, true);
        }

        $result = [];

        foreach ($segments as $segmentOptionValue) {
            if ($segmentOptionValue === "") {
                $result[] = "";
                continue;
            }

            $segmentDefinition = $this->findSegment($segmentOptionValue, $idSites);

            if (empty($segmentDefinition)) {
                continue;
            }

            $result[] = $segmentDefinition;
        }

        return $this->getSegmentsWithSitesToProcess($result, $idSites);
    }

    /**
     * @param array<int> $idSites
     */
    private function findSegment(string $segmentOptionValue, array $idSites)
    {
        $allSegments = $this->getAllSegments($idSites);

        foreach ($allSegments as $segment) {
            if (
                !empty($segment['enable_only_idsite'])
                && !in_array($segment['enable_only_idsite'], $idSites)
            ) {
                continue;
            }

            if ($segmentOptionValue == $segment['idsegment']) {
                $this->logger->debug("Matching '$segmentOptionValue' by idsegment with segment {segment}.", ['segment' => json_encode($segment)]);
                return $segment['definition'];
            }

            if (strtolower($segmentOptionValue) == strtolower($segment['name'])) {
                $this->logger->debug("Matching '$segmentOptionValue' by name with segment {segment}.", ['segment' => json_encode($segment)]);
                return $segment['definition'];
            }

            if (
                $segment['definition'] == $segmentOptionValue
                || $segment['definition'] == urldecode($segmentOptionValue)
            ) {
                $this->logger->debug("Matching '{value}' by definition with segment {segment}.", ['value' => $segmentOptionValue, 'segment' => json_encode($segment)]);
                return $segment['definition'];
            }
        }

        $this->logger->warning("'$segmentOptionValue' did not match any stored segment, but invalidating it anyway.");
        return $segmentOptionValue;
    }

    /**
     * @param array<int> $idSites
     *
     * @return array<Segment>
     */
    private function getAllSegments(array $idSites): array
    {
        if ($this->allSegments === null) {
            $segmentsByDefinition = [];

            if ([] === $idSites) {
                $idSites = [null];
            }

            foreach ($idSites as $idSite) {
                $siteSegments = API::getInstance()->getAll($idSite);

                $siteSegmentsByDefinition = array_combine(
                    array_column($siteSegments, 'definition'),
                    $siteSegments
                );

                $segmentsByDefinition = array_merge(
                    $segmentsByDefinition,
                    $siteSegmentsByDefinition
                );
            }

            $this->allSegments = array_values($segmentsByDefinition);
        }

        return $this->allSegments;
    }
}