Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ArchivesToPurgeDistributedList.php « Tasks « CoreAdminHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ef5c7f533c9dbf8114783b453150cc2ed29a0f31 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
namespace Piwik\Plugins\CoreAdminHome\Tasks;

use Piwik\Concurrency\DistributedList;
use Piwik\Date;

/**
 * Distributed list that holds a list of year-month archive table identifiers (eg, 2015_01 or 2014_11). Each item in the
 * list is expected to identify a pair of archive tables that contain invalidated archives.
 *
 * The archiving purging scheduled task will read items in this list when executing the daily purge.
 *
 * This class is necessary in order to keep the archive purging scheduled task fast. W/o a way to keep track of
 * tables w/ invalid data, the task would have to iterate over every table, which is not desired for a task that
 * is executed daily.
 *
 * If users find other tables contain invalidated archives, they can use the core:purge-old-archive-data command
 * to manually purge them.
 */
class ArchivesToPurgeDistributedList extends DistributedList
{
    const OPTION_INVALIDATED_DATES_SITES_TO_PURGE = 'InvalidatedOldReports_DatesWebsiteIds';

    public function __construct()
    {
        parent::__construct(self::OPTION_INVALIDATED_DATES_SITES_TO_PURGE);
    }

    /**
     * @inheritdoc
     */
    public function setAll($yearMonths)
    {
        $yearMonths = array_unique($yearMonths, SORT_REGULAR);
        parent::setAll($yearMonths);
    }

    protected function getListOptionValue()
    {
        $result = parent::getListOptionValue();
        $this->convertOldDistributedList($result);
        return $result;
    }

    public function getAllAsDates()
    {
        $dates = array();
        foreach ($this->getAll() as $yearMonth) {
            try {
                $date = Date::factory(str_replace('_', '-', $yearMonth) . '-01');
            } catch (\Exception $ex) {
                continue; // invalid year month in distributed list
            }

            $dates[] = $date;
        }
        return $dates;
    }

    public function removeDate(Date $date)
    {
        $yearMonth = $date->toString('Y_m');
        $this->remove($yearMonth);
    }

    /**
     * Before 2.12.0 Piwik stored this list as an array mapping year months to arrays of site IDs. If this is
     * found in the DB, we convert the array to an array of year months to avoid errors and to make sure
     * the correct tables are still purged.
     */
    private function convertOldDistributedList(&$yearMonths)
    {
        foreach ($yearMonths as $key => $value) {
            if (preg_match("/^[0-9]{4}_[0-9]{2}$/", $key)) {
                unset($yearMonths[$key]);

                $yearMonths[] = $key;
            }
        }
    }
}