From a881f970997629ae7e22372988157c97c285aa0e Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Wed, 23 Dec 2009 01:24:17 +0000 Subject: [PATCH] files MDL-20635 Changed cleanup of files when a context is deleted from the moment it occurs to cron --- lib/accesslib.php | 5 ----- lib/file/file_storage.php | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 54386a05c5..ff0aceb381 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2319,11 +2319,6 @@ function delete_context($contextlevel, $instanceid) { blocks_delete_all_for_context($context->id); filter_delete_all_for_context($context->id); - // TODO: MDL-20635 Replace with a means to delete during a cron run - require_once($CFG->libdir.'/filelib.php'); - $fs = get_file_storage(); - $fs->delete_area_files($context->id); - return $result; } else { diff --git a/lib/file/file_storage.php b/lib/file/file_storage.php index be9b29ec36..6e73adab07 100644 --- a/lib/file/file_storage.php +++ b/lib/file/file_storage.php @@ -1033,11 +1033,25 @@ class file_storage { * Cron cleanup job. */ public function cron() { - global $CFG; + global $CFG, $DB; // remove trash pool files once a day // if you want to disable purging of trash put $CFG->fileslastcleanup=time(); into config.php if (empty($CFG->fileslastcleanup) or $CFG->fileslastcleanup < time() - 60*60*24) { require_once($CFG->libdir.'/filelib.php'); + // Delete files that are associated with a context that no longer exists. + mtrace('Cleaning up files from deleted contexts... ', ''); + $sql = "SELECT DISTINCT f.contextid + FROM {files} f + LEFT OUTER JOIN {context} c ON f.contextid = c.id + WHERE c.id IS NULL"; + if ($rs = $DB->get_recordset_sql($sql)) { + $fs = get_file_storage(); + foreach ($rs as $ctx) { + $fs->delete_area_files($ctx->contextid); + } + } + mtrace('done.'); + mtrace('Deleting trash files... ', ''); fulldelete($this->trashdir); set_config('fileslastcleanup', time()); -- 2.39.5