#!/usr/bin/php5 -q
<?php
/***************************************************************************
-* Copyright (C) 2003-2009 Polytechnique.org *
+* Copyright (C) 2003-2014 Polytechnique.org *
* http://opensource.polytechnique.org/ *
* *
* This program is free software; you can redistribute it and/or modify *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
***************************************************************************/
-require('./connect.db.inc.php');
+require './connect.db.inc.php';
/**
* Discards from the logger database session details for users.
* no matter what.
*/
function discardExpiredSessions($userPerms, $retentionPeriod, $minimalBacklog) {
- XDB::execute(
- "DELETE #logger#.s
- FROM #logger#.sessions AS s
- JOIN (SELECT u.user_id,
- (SELECT us.start
- FROM #logger#.sessions AS us
- WHERE us.uid = u.user_id
- ORDER BY us.start DESC
- LIMIT {?}, 1) AS no_discard_limit
- FROM #x4dat#.auth_user_md5 AS u
- WHERE u.perms = {?}
- ORDER BY u.user_id ASC) AS ut ON (ut.user_id = s.uid)
- WHERE s.start < DATE_SUB(NOW(), INTERVAL {?} MONTH)
- AND s.start < ut.no_discard_limit",
- $minimalBacklog - 1, $userPerms, $retentionPeriod);
+ $begin = time();
+ switch ($userPerms) {
+ case 'user':
+ $state = 'active';
+ $isAdmin = 0;
+ break;
+ case 'admin':
+ $state = 'active';
+ $isAdmin = 1;
+ break;
+ case 'disabled':
+ $state = 'disabled';
+ $isAdmin = 0;
+ break;
+ default:
+ return;
+ }
- $affectedRows = XDB::affectedRows();
- echo "Users with permission '$userPerms': removed $affectedRows sessions.\n";
+ list($low, $high) = XDB::fetchOneRow(
+ "SELECT MIN(uid), MAX(uid)
+ FROM #x5dat#.accounts
+ WHERE state = {?} AND is_admin = {?}",
+ $state, $isAdmin);
+
+ $batchSize = 100;
+ $nbBatches = 0;
+ $affectedRows = 0;
+
+ // Run in batches.
+ for ($lowUID = $low; $lowUID <= $high; $lowUID += $batchSize) {
+
+ // Slight optimization for last loop: adjust to exactly what's necessary.
+ $highUID = min($high + 1, $lowUID + $batchSize);
+
+ XDB::execute(
+ "DELETE s
+ FROM log_sessions AS s
+ JOIN (SELECT a.uid,
+ (SELECT us.start
+ FROM log_sessions AS us
+ WHERE us.uid = a.uid AND (us.suid IS NULL OR us.suid = 0)
+ ORDER BY us.start DESC
+ LIMIT {?}, 1) AS no_discard_limit
+ FROM #x5dat#.accounts AS a
+ WHERE a.state = {?} AND a.is_admin = {?}
+ AND a.uid >= {?} 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 #logger#.sessions AS s
- LEFT JOIN #x4dat#.auth_user_md5 AS u ON (u.user_id = s.uid)
- WHERE u.user_id IS NULL");
+ 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())) {
- echo "Orphaned sessions: found $count orphaned sessions. Please fix that.\n";
+ $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 #logger#.e
- FROM #logger#.events AS e
- LEFT JOIN #logger#.sessions AS s ON (s.id = e.session)
+ "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();
- echo "Orphaned events: removed $affectedRows events.\n";
+ $duration = time() - $begin;
+ echo "Orphaned events: removed $affectedRows events in $duration seconds.\n";
}
// Remove expired sessions.
purgeOrphanedEvents();
// Many rows have been removed from the two logger tables. Let's optimize them.
-XDB::execute("OPTIMIZE TABLE #logger#.events");
-XDB::execute("OPTIMIZE TABLE #logger#.sessions");
+XDB::execute("OPTIMIZE TABLE log_events");
+XDB::execute("OPTIMIZE TABLE log_sessions");
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
?>