]> git.mjollnir.org Git - moodle.git/commitdiff
files MDL-20635 Changed cleanup of files when a context is deleted from the moment...
authorSam Hemelryk <sam@moodle.com>
Wed, 23 Dec 2009 01:24:17 +0000 (01:24 +0000)
committerSam Hemelryk <sam@moodle.com>
Wed, 23 Dec 2009 01:24:17 +0000 (01:24 +0000)
lib/accesslib.php
lib/file/file_storage.php

index 54386a05c524e57d1d9ee8f45f03c84e809264d3..ff0aceb381c9585b4473a5548bb99e4327f66492 100755 (executable)
@@ -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 {
 
index be9b29ec36ff5c9fa6e01c9cdc4e34aa097e6a6b..6e73adab07b6bde0ddb6ec838cd049f2ac5bf9c3 100644 (file)
@@ -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());