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