Skip to content

Commit

Permalink
MDL-36789 Quick course cache integrity check for deleted modules
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Aug 15, 2013
1 parent 838d78a commit 676fa06
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/modinfolib.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function get_section_info($sectionnumber, $strictness = IGNORE_MISSING) {
* @param int $userid User ID
*/
public function __construct($course, $userid) {
global $CFG, $DB;
global $CFG, $DB, $COURSE, $SITE;

// Check modinfo field is set. If not, build and load it.
if (empty($course->modinfo) || empty($course->sectioncache)) {
Expand Down Expand Up @@ -288,8 +288,28 @@ public function __construct($course, $userid) {
}

// If we haven't already preloaded contexts for the course, do it now
// Modules are also cached here as long as it's the first time this course has been preloaded.
context_helper::preload_course($course->id);

// Quick integrity check: as a result of race conditions modinfo may not be regenerated after the change.
// It is especially dangerous if modinfo contains the deleted course module, as it results in fatal error.
// We can check it very cheap by validating the existence of module context.
if ($course->id == $COURSE->id || $course->id == $SITE->id) {
// Only verify current course (or frontpage) as pages with many courses may not have module contexts cached.
// (Uncached modules will result in a very slow verification).
foreach ($info as $mod) {
if (!context_module::instance($mod->cm, IGNORE_MISSING)) {
debugging('Course cache integrity check failed: course module with id '. $mod->cm.
' does not have context. Rebuilding cache for course '. $course->id);
rebuild_course_cache($course->id);
$this->course = $DB->get_record('course', array('id' => $course->id), '*', MUST_EXIST);
$info = unserialize($this->course->modinfo);
$sectioncache = unserialize($this->course->sectioncache);
break;
}
}
}

// Loop through each piece of module data, constructing it
$modexists = array();
foreach ($info as $mod) {
Expand Down

0 comments on commit 676fa06

Please sign in to comment.