API
3 years ago
Access
3 years ago
Application
4 years ago
Archive
3 years ago
ArchiveProcessor
3 years ago
Archiver
5 years ago
AssetManager
3 years ago
Auth
3 years ago
Category
5 years ago
Changes
4 years ago
CliMulti
4 years ago
Columns
3 years ago
Concurrency
3 years ago
Config
4 years ago
Container
4 years ago
CronArchive
3 years ago
DataAccess
3 years ago
DataFiles
5 years ago
DataTable
3 years ago
Db
3 years ago
DeviceDetector
3 years ago
Email
5 years ago
Exception
3 years ago
Http
4 years ago
Intl
4 years ago
Mail
3 years ago
Measurable
5 years ago
Menu
3 years ago
Metrics
4 years ago
Notification
4 years ago
Period
4 years ago
Plugin
3 years ago
ProfessionalServices
4 years ago
Report
5 years ago
ReportRenderer
3 years ago
Scheduler
4 years ago
Segment
3 years ago
Session
4 years ago
Settings
3 years ago
Tracker
3 years ago
Translation
4 years ago
UpdateCheck
5 years ago
Updater
3 years ago
Updates
3 years ago
Validators
4 years ago
View
4 years ago
ViewDataTable
3 years ago
Visualization
4 years ago
Widget
5 years ago
.htaccess
6 years ago
Access.php
4 years ago
Archive.php
4 years ago
ArchiveProcessor.php
4 years ago
AssetManager.php
4 years ago
Auth.php
5 years ago
AuthResult.php
5 years ago
BaseFactory.php
5 years ago
Cache.php
5 years ago
CacheId.php
5 years ago
CliMulti.php
4 years ago
Common.php
3 years ago
Config.php
3 years ago
Console.php
3 years ago
Context.php
5 years ago
Cookie.php
3 years ago
CronArchive.php
3 years ago
DataArray.php
4 years ago
DataTable.php
3 years ago
Date.php
4 years ago
Db.php
4 years ago
DbHelper.php
3 years ago
Development.php
5 years ago
ErrorHandler.php
5 years ago
EventDispatcher.php
3 years ago
ExceptionHandler.php
3 years ago
FileIntegrity.php
5 years ago
Filechecks.php
4 years ago
Filesystem.php
3 years ago
FrontController.php
3 years ago
Http.php
3 years ago
IP.php
4 years ago
Log.php
4 years ago
LogDeleter.php
4 years ago
Mail.php
4 years ago
Metrics.php
3 years ago
NoAccessException.php
5 years ago
Nonce.php
3 years ago
Notification.php
5 years ago
NumberFormatter.php
4 years ago
Option.php
4 years ago
Period.php
5 years ago
Piwik.php
3 years ago
Plugin.php
4 years ago
Profiler.php
4 years ago
ProxyHeaders.php
5 years ago
ProxyHttp.php
3 years ago
QuickForm2.php
5 years ago
RankingQuery.php
3 years ago
ReportRenderer.php
4 years ago
Segment.php
3 years ago
Sequence.php
5 years ago
Session.php
3 years ago
SettingsPiwik.php
3 years ago
SettingsServer.php
5 years ago
Singleton.php
5 years ago
Site.php
3 years ago
SiteContentDetector.php
3 years ago
SupportedBrowser.php
3 years ago
TCPDF.php
5 years ago
Theme.php
5 years ago
Timer.php
5 years ago
Tracker.php
4 years ago
Twig.php
4 years ago
Unzip.php
5 years ago
UpdateCheck.php
5 years ago
Updater.php
4 years ago
UpdaterErrorException.php
5 years ago
Updates.php
5 years ago
Url.php
3 years ago
UrlHelper.php
3 years ago
Version.php
3 years ago
View.php
3 years ago
bootstrap.php
3 years ago
dispatch.php
5 years ago
testMinimumPhpVersion.php
3 years ago
LogDeleter.php
106 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | */ |
| 8 | |
| 9 | namespace Piwik; |
| 10 | |
| 11 | use Piwik\Container\StaticContainer; |
| 12 | use Piwik\DataAccess\RawLogDao; |
| 13 | use Piwik\Plugin\LogTablesProvider; |
| 14 | use Piwik\Plugins\PrivacyManager\Model\DataSubjects; |
| 15 | use Piwik\Plugins\SitesManager\Model; |
| 16 | |
| 17 | /** |
| 18 | * Service that deletes log entries. Methods in this class cascade, so deleting visits will delete visit actions, |
| 19 | * conversions and conversion items. |
| 20 | */ |
| 21 | class LogDeleter |
| 22 | { |
| 23 | /** |
| 24 | * @var RawLogDao |
| 25 | */ |
| 26 | private $rawLogDao; |
| 27 | |
| 28 | /** |
| 29 | * @var LogTablesProvider |
| 30 | */ |
| 31 | private $logTablesProvider; |
| 32 | |
| 33 | public function __construct(RawLogDao $rawLogDao, LogTablesProvider $logTablesProvider) |
| 34 | { |
| 35 | $this->rawLogDao = $rawLogDao; |
| 36 | $this->logTablesProvider = $logTablesProvider; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Deletes visits by ID. This method cascades, so conversions, conversion items and visit actions for |
| 41 | * the visits are also deleted. |
| 42 | * |
| 43 | * @param int[] $visitIds |
| 44 | * @return int The number of deleted visits. |
| 45 | */ |
| 46 | public function deleteVisits($visitIds) |
| 47 | { |
| 48 | $visitIds = array_map(function($visitid) { |
| 49 | return ['idvisit' => $visitid]; |
| 50 | }, $visitIds); |
| 51 | $dataSubjects = StaticContainer::get(DataSubjects::class); |
| 52 | $deleteCounts = $dataSubjects->deleteDataSubjectsWithoutInvalidatingArchives($visitIds); |
| 53 | return $deleteCounts['log_visit']; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Deletes visits within the specified date range and belonging to the specified site (if any). Visits are |
| 58 | * deleted in chunks, so only `$iterationStep` visits are deleted at a time. |
| 59 | * |
| 60 | * @param string|null $startDatetime A datetime string. Visits that occur at this time or after are deleted. If not supplied, |
| 61 | * visits from the beginning of time are deleted. |
| 62 | * @param string|null $endDatetime A datetime string. Visits that occur before this time are deleted. If not supplied, |
| 63 | * visits from the end of time are deleted. |
| 64 | * @param int|null $idSite The site to delete visits from. |
| 65 | * @param int $iterationStep The number of visits to delete at a single time. |
| 66 | * @param callable $afterChunkDeleted Callback executed after every chunk of visits are deleted. |
| 67 | * @return int The number of visits deleted. |
| 68 | */ |
| 69 | public function deleteVisitsFor($startDatetime, $endDatetime, $idSite = null, $iterationStep = 2000, $afterChunkDeleted = null) |
| 70 | { |
| 71 | $fields = array('idvisit'); |
| 72 | $conditions = array(); |
| 73 | |
| 74 | if (!empty($startDatetime)) { |
| 75 | $conditions[] = array('visit_last_action_time', '>=', $startDatetime); |
| 76 | } |
| 77 | |
| 78 | if (!empty($endDatetime)) { |
| 79 | $conditions[] = array('visit_last_action_time', '<', $endDatetime); |
| 80 | } |
| 81 | |
| 82 | if (!empty($idSite)) { |
| 83 | $conditions[] = array('idsite', '=', $idSite); |
| 84 | } elseif (!empty($startDatetime) || !empty($endDatetime)) { |
| 85 | // make sure to use index! |
| 86 | $sitesModel = new Model(); |
| 87 | $allIdSites = $sitesModel->getSitesId(); |
| 88 | $allIdSites = array_map('intval', $allIdSites); |
| 89 | $conditions[] = array('idsite', '', $allIdSites); |
| 90 | } |
| 91 | |
| 92 | $logsDeleted = 0; |
| 93 | $logPurger = $this; |
| 94 | $this->rawLogDao->forAllLogs('log_visit', $fields, $conditions, $iterationStep, function ($logs) use ($logPurger, &$logsDeleted, $afterChunkDeleted) { |
| 95 | $ids = array_map(function ($row) { return (int) (reset($row)); }, $logs); |
| 96 | sort($ids); |
| 97 | $logsDeleted += $logPurger->deleteVisits($ids); |
| 98 | if (!empty($afterChunkDeleted)) { |
| 99 | $afterChunkDeleted($logsDeleted); |
| 100 | } |
| 101 | }, $willDelete = true); |
| 102 | |
| 103 | return $logsDeleted; |
| 104 | } |
| 105 | } |
| 106 |