#!/usr/bin/php5 -q = {?} AND a.uid < {?} ORDER BY a.uid ASC) AS ut ON (ut.uid = s.uid) WHERE s.start < DATE_SUB(NOW(), INTERVAL {?} MONTH) AND s.start < ut.no_discard_limit", $minimalBacklog - 1, $state, $isAdmin, $lowUID, $highUID, $retentionPeriod); $nbBatches += 1; $affectedRows += XDB::affectedRows(); } $duration = time() - $begin; echo "Users with permission '$userPerms': removed $affectedRows sessions in $duration seconds ($nbBatches batches).\n"; } /** * Checks for sessions without a valid associated user id. */ function checkOrphanedSessions() { $begin = time(); $res = XDB::query( "SELECT COUNT(*) FROM log_sessions AS s LEFT JOIN #x5dat#.accounts AS a ON (a.uid = s.uid) WHERE a.uid IS NULL"); if (($count = $res->fetchOneCell())) { $duration = time() - $begin; echo "Orphaned sessions: found $count orphaned sessions in $duration seconds. Please fix that.\n"; } } /** * Purges session events without a valid session. */ function purgeOrphanedEvents() { $begin = time(); XDB::execute( "DELETE e FROM log_events AS e LEFT JOIN log_sessions AS s ON (s.id = e.session) WHERE s.id IS NULL"); $affectedRows = XDB::affectedRows(); $duration = time() - $begin; echo "Orphaned events: removed $affectedRows events in $duration seconds.\n"; } // Remove expired sessions. // For normal user, we only keep 12 months of data (and at least the last two sessions). // For administrator, we also keep data for 12 months, but with a backlog of at least 20 sessions. // For disabled users, we keep data for 5 years, and with a backlog of at least 2 sessions. // For other users, no data are discarded. discardExpiredSessions('user', 12, 2); discardExpiredSessions('admin', 12, 20); discardExpiredSessions('disabled', 60, 2); // Purge orphaned entries; events are purged automatically, sessions require explicit // action from the administrator. checkOrphanedSessions(); purgeOrphanedEvents(); // Many rows have been removed from the two logger tables. Let's optimize them. XDB::execute("OPTIMIZE TABLE log_events"); XDB::execute("OPTIMIZE TABLE log_sessions"); // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: ?>