From 17b0efae11390b6aabc387e2c8a618ac81d28bfa Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 06:50:53 +0000 Subject: [PATCH] accesslib: Introducing cleanup_contexts() and use it in cron The newly minted cleanup_contexts() walks the context table matching contexts with the tables they are referencing and clearing any leftover contexts. --- admin/cron.php | 3 ++ lib/accesslib.php | 72 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/admin/cron.php b/admin/cron.php index d001e2e807..c9e7b57939 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -368,6 +368,9 @@ unset($authplugin); } + // Accesslib stuff + cleanup_contexts(); + if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) { // check we're not before our runtime diff --git a/lib/accesslib.php b/lib/accesslib.php index f99242300d..7321ffb83b 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1780,11 +1780,11 @@ function create_system_context() { } } /** - * Create a new context record for use by all roles-related stuff + * Remove a context record and any dependent entries * @param $level * @param $instanceid * - * @return true if properly deleted + * @return bool properly deleted */ function delete_context($contextlevel, $instanceid) { if ($context = get_context_instance($contextlevel, $instanceid)) { @@ -1797,6 +1797,74 @@ function delete_context($contextlevel, $instanceid) { return true; } +/** + * Remove stale context records + * + * @return bool + */ +function cleanup_contexts() { + global $CFG; + + $sql = " SELECT " . CONTEXT_COURSECAT . " AS level, + c.instanceid AS instanceid + FROM {$CFG->prefix}context c + LEFT OUTER JOIN {$CFG->prefix}course_categories AS t + ON c.instanceid = t.id + WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSECAT . " + UNION + SELECT " . CONTEXT_COURSE . " AS level, + c.instanceid AS instanceid + FROM {$CFG->prefix}context c + LEFT OUTER JOIN {$CFG->prefix}course AS t + ON c.instanceid = t.id + WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_COURSE . " + UNION + SELECT " . CONTEXT_MODULE . " AS level, + c.instanceid AS instanceid + FROM {$CFG->prefix}context c + LEFT OUTER JOIN {$CFG->prefix}course_modules AS t + ON c.instanceid = t.id + WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_MODULE . " + UNION + SELECT " . CONTEXT_USER . " AS level, + c.instanceid AS instanceid + FROM {$CFG->prefix}context c + LEFT OUTER JOIN {$CFG->prefix}user AS t + ON c.instanceid = t.id + WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_USER . " + UNION + SELECT " . CONTEXT_BLOCK . " AS level, + c.instanceid AS instanceid + FROM {$CFG->prefix}context c + LEFT OUTER JOIN {$CFG->prefix}block_instance AS t + ON c.instanceid = t.id + WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_BLOCK . " + UNION + SELECT " . CONTEXT_GROUP . " AS level, + c.instanceid AS instanceid + FROM {$CFG->prefix}context c + LEFT OUTER JOIN {$CFG->prefix}groups AS t + ON c.instanceid = t.id + WHERE t.id IS NULL AND c.contextlevel = " . CONTEXT_GROUP . " + "; + $rs = get_recordset_sql($sql); + if ($rs->RecordCount()) { + begin_sql(); + $tx = true; + while ($tx && $ctx = rs_fetch_next_record($rs)) { + $tx = $tx && delete_context($ctx->level, $ctx->instanceid); + } + rs_close($rs); + if ($tx) { + commit_sql(); + return true; + } + rollback_sql(); + return false; + } + return true; +} + /** * Validate that object with instanceid really exists in given context level. * -- 2.39.5