logDeleter = $logDeleter; $this->rawLogDao = $rawLogDao; } /** * Purges old data from the following tables: * - log_visit * - log_link_visit_action * - log_conversion * - log_conversion_item * - log_action * * @param int $deleteLogsOlderThan The number of days after which log entries are considered old. * Visits and related data whose age is greater than this number * will be purged. * @param bool $deleteUnusedLogActions Whether to delete unused log actions or not */ public function purgeData($deleteLogsOlderThan, $deleteUnusedLogActions) { $dateUpperLimit = Date::factory("today")->subDay($deleteLogsOlderThan); $transactionLevel = new TransactionLevel(Db::get()); $transactionLevel->setUncommitted(); $this->logDeleter->deleteVisitsFor($start = null, $dateUpperLimit->getDatetime()); $transactionLevel->restorePreviousStatus(); $logTables = self::getDeleteTableLogTables(); // delete unused actions from the log_action table (but only if we can lock tables) if ($deleteUnusedLogActions) { if (Db::isLockPrivilegeGranted()) { $this->rawLogDao->deleteUnusedLogActions(); } else { $logMessage = get_class($this) . ": LOCK TABLES privilege not granted; skipping unused actions purge"; Log::warning($logMessage); } } /** * Triggered when a plugin is supposed to delete log/raw data that is older than a certain amount of days. * * **Example** * * public function deleteLogsOlderThan($dateUpperLimit, $deleteLogsOlderThan) * { * Db::query('DELETE FROM mytable WHERE creation_date < ' . $dateUpperLimit->getDateTime()); * } * * @param \Piwik\Date $dateUpperLimit A date where visits that occur before this time should be deleted. * @param int $deleteLogsOlderThan The number of days after which log entries are considered old. * Visits and related data whose age is greater than this number will be purged. */ Piwik::postEvent('PrivacyManager.deleteLogsOlderThan', array($dateUpperLimit, $deleteLogsOlderThan)); // optimize table overhead after deletion Db::optimizeTables($logTables); } /** * Returns an array describing what data would be purged if purging were invoked. * * This function returns an array that maps table names with the number of rows * that will be deleted. * * @param int $deleteLogsOlderThan The number of days after which log entries are considered old. * Visits and related data whose age is greater than this number * will be purged. * @return array * * TODO: purge estimate uses max idvisit w/ time, but purge does not, so estimate may be less accurate. * to be more accurate, it should use the same strategy as purgeData(), but this could be very slow. */ public function getPurgeEstimate($deleteLogsOlderThan) { $result = array(); // deal w/ log tables that will be purged $maxIdVisit = $this->getDeleteIdVisitOffset($deleteLogsOlderThan); if (!empty($maxIdVisit)) { foreach (self::getDeleteTableLogTables() as $table) { // getting an estimate for log_action is not supported since it can take too long if ($table != Common::prefixTable('log_action')) { $rowCount = $this->getLogTableDeleteCount($table, $maxIdVisit); if ($rowCount > 0) { $result[$table] = $rowCount; } } } } return $result; } /** * get highest idVisit to delete rows from * @return string */ private function getDeleteIdVisitOffset($deleteLogsOlderThan) { $logVisit = Common::prefixTable("log_visit"); // get max idvisit $maxIdVisit = Db::fetchOne("SELECT MAX(idvisit) FROM $logVisit"); if (empty($maxIdVisit)) { return false; } // select highest idvisit to delete from $dateStart = Date::factory("today")->subDay($deleteLogsOlderThan); $sql = "SELECT idvisit FROM $logVisit WHERE '" . $dateStart->toString('Y-m-d H:i:s') . "' > visit_last_action_time AND idvisit <= ? AND idvisit > ? ORDER BY idvisit DESC LIMIT 1"; return Db::segmentedFetchFirst($sql, $maxIdVisit, 0, -self::$selectSegmentSize); } private function getLogTableDeleteCount($table, $maxIdVisit) { $sql = "SELECT COUNT(*) FROM $table WHERE idvisit <= ?"; return (int) Db::fetchOne($sql, array($maxIdVisit)); } // let's hardcode, since these are not dynamically created tables public static function getDeleteTableLogTables() { $provider = StaticContainer::get('Piwik\Plugin\LogTablesProvider'); $result = array(); foreach ($provider->getAllLogTables() as $logTable) { if ($logTable->getColumnToJoinOnIdVisit()) { $result[] = Common::prefixTable($logTable->getName()); } } if (Db::isLockPrivilegeGranted()) { $result[] = Common::prefixTable('log_action'); } return $result; } }