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

LogDeleter.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc61ec9358540e3f6779f67514d53467127fb936 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?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;

use Piwik\DataAccess\RawLogDao;

/**
 * Service that deletes log entries. Methods in this class cascade, so deleting visits will delete visit actions,
 * conversions and conversion items.
 */
class LogDeleter
{
    /**
     * @var RawLogDao
     */
    private $rawLogDao;

    public function __construct(RawLogDao $rawLogDao)
    {
        $this->rawLogDao = $rawLogDao;
    }

    /**
     * Deletes visits by ID. This method cascades, so conversions, conversion items and visit actions for
     * the visits are also deleted.
     *
     * @param int[] $visitIds
     * @return int The number of deleted visits.
     */
    public function deleteVisits($visitIds)
    {
        $this->deleteConversions($visitIds);
        $this->rawLogDao->deleteVisitActionsForVisits($visitIds);

        return $this->rawLogDao->deleteVisits($visitIds);
    }

    /**
     * Deletes conversions by visit ID. This method cascades, so conversion items are also deleted.
     *
     * @param int[] $visitIds The list of visits to delete conversions for.
     * @return int The number rows deleted.
     */
    public function deleteConversions($visitIds)
    {
        $this->deleteConversionItems($visitIds);
        return $this->rawLogDao->deleteConversions($visitIds);
    }

    /**
     * Deletes conversion items by visit ID.
     *
     * @param int[] $visitIds The list of visits to delete conversions for.
     * @return int The number rows deleted.
     */
    public function deleteConversionItems($visitIds)
    {
        return $this->rawLogDao->deleteConversionItems($visitIds);
    }

    /**
     * Deletes visits within the specified date range and belonging to the specified site (if any). Visits are
     * deleted in chunks, so only `$iterationStep` visits are deleted at a time.
     *
     * @param string|null $startDatetime A datetime string. Visits that occur at this time or after are deleted. If not supplied,
     *                                   visits from the beginning of time are deleted.
     * @param string|null $endDatetime A datetime string. Visits that occur before this time are deleted. If not supplied,
     *                                 visits from the end of time are deleted.
     * @param int|null $idSite The site to delete visits from.
     * @param int $iterationStep The number of visits to delete at a single time.
     * @param callable $afterChunkDeleted Callback executed after every chunk of visits are deleted.
     * @return int The number of visits deleted.
     */
    public function deleteVisitsFor($startDatetime, $endDatetime, $idSite = null, $iterationStep = 1000, $afterChunkDeleted = null)
    {
        $fields = array('idvisit');
        $conditions = array();

        if (!empty($startDatetime)) {
            $conditions[] = array('visit_last_action_time', '>=', $startDatetime);
        }

        if (!empty($endDatetime)) {
            $conditions[] = array('visit_last_action_time', '<', $endDatetime);
        }

        if (!empty($idSite)) {
            $conditions[] = array('idsite', '=', $idSite);
        }

        $logsDeleted = 0;
        $logPurger = $this;
        $this->rawLogDao->forAllLogs('log_visit', $fields, $conditions, $iterationStep, function ($logs) use ($logPurger, &$logsDeleted, $afterChunkDeleted) {
            $ids = array_map(function ($row) { return reset($row); }, $logs);
            $logsDeleted += $logPurger->deleteVisits($ids);

            if (!empty($afterChunkDeleted)) {
                $afterChunkDeleted($logsDeleted);
            }
        });

        return $logsDeleted;
    }
}