MDL-17458 upgrade logging implemented + a lot more refactoring + exceptions implemented in install/upgrade code + lang pack cleanup + some more improvements

This commit is contained in:
skodak 2009-01-31 20:07:32 +00:00
parent 9c421fab3b
commit 795a08adb7
17 changed files with 628 additions and 743 deletions

View File

@ -1119,8 +1119,6 @@ if ( file_exists(dirname(dirname(__FILE__)) . '/config.php')) {
// create default course category
$cat = get_course_category();
} else {
print_error('cannotsetupsite', 'error');
}
}

View File

@ -130,7 +130,6 @@
}
$strdatabasesetup = get_string("databasesetup");
$strdatabasesuccess = get_string("databasesuccess");
$navigation = build_navigation(array(array('name'=>$strdatabasesetup, 'link'=>null, 'type'=>'misc')));
print_header($strdatabasesetup, $strdatabasesetup, $navigation,
@ -147,31 +146,31 @@
}
}
upgrade_started(true); // does not store ugprade runnign flag
print_heading('coresystem');
$DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
upgrade_started(); // we want the flag to be stored in config table ;-)
try {
print_upgrade_part_start('moodle', true); // does not store upgrade running flag
/// set all core default records and default settings
require_once("$CFG->libdir/db/install.php");
xmldb_main_install();
$DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
upgrade_started(); // we want the flag to be stored in config table ;-)
/// store version
upgrade_main_savepoint(true, $version, false);
/// set all core default records and default settings
require_once("$CFG->libdir/db/install.php");
xmldb_main_install();
/// Continue with the instalation
/// store version
upgrade_main_savepoint(true, $version, false);
// Install core event handlers
events_update_definition('moodle');
/// Continue with the instalation
events_update_definition('moodle');
message_update_providers('moodle');
message_update_providers('message');
// Install core message providers
message_update_providers('moodle');
message_update_providers('message');
/// Write default settings unconditionlly
admin_apply_default_settings(NULL, true);
// Write default settings unconditionally
admin_apply_default_settings(NULL, true);
notify($strdatabasesuccess, 'notifysuccess');
print_upgrade_separator();
print_upgrade_part_end(null, true);
} catch (exception $ex) {
upgrade_handle_exception($ex);
}
}
@ -197,16 +196,9 @@
$CFG->debug = DEBUG_MINIMAL;
error_reporting($CFG->debug);
// logo ut in case we are upgrading from pre 1.9 version in order to prevent
// weird session/role problems caused by incorrect data in USER and SESSION
if ($CFG->version < 2007101500) {
require_logout();
}
if (empty($confirmupgrade)) {
$navigation = build_navigation(array(array('name'=>$strdatabasechecking, 'link'=>null, 'type'=>'misc')));
print_header($strdatabasechecking, $stradministration, $navigation,
"", "", false, "&nbsp;", "&nbsp;");
print_header($strdatabasechecking, $stradministration, $navigation, "", "", false, "&nbsp;", "&nbsp;");
notice_yesno(get_string('upgradesure', 'admin', $a->newversion), 'index.php?confirmupgrade=1', 'index.php');
print_footer('none');
@ -260,42 +252,36 @@
die();
} else {
/// return to original debugging level
$CFG->debug = $origdebug;
error_reporting($CFG->debug);
upgrade_started();
/// Upgrade current language pack if we can
if (empty($CFG->skiplangupgrade)) {
upgrade_language_pack();
}
print_heading($strdatabasechecking);
/// Launch the old main upgrade (if exists)
$status = xmldb_main_upgrade($CFG->version);
/// If successful, continue upgrading roles and setting everything properly
if ($status) {
/// Launch main upgrade
try {
print_upgrade_part_start('moodle', false);
$result = xmldb_main_upgrade($CFG->version);
if ($version > $CFG->version) {
// store version if not already there
upgrade_main_savepoint($result, $version, false);
}
// perform all other component upgrade routines
update_capabilities('moodle');
// Update core events
events_update_definition('moodle');
// Update core message providers
message_update_providers('moodle');
message_update_providers('message');
set_config("version", $version);
remove_dir($CFG->dataroot . '/cache', true); // flush cache
notify(get_string("databasesuccess"), "green");
print_upgrade_separator();
/// Main upgrade not success
} else {
notify('Main Upgrade failed! See lib/db/upgrade.php');
print_continue('index.php?confirmupgrade=1&amp;confirmrelease=1&amp;confirmplugincheck=1');
print_footer('none');
die;
print_upgrade_part_end('moodle', false);
} catch (Exception $ex) {
upgrade_handle_exception($ex);
}
}
} else if ($version < $CFG->version) {
@ -312,19 +298,31 @@
/// upgrade all plugins types
$plugintypes = get_plugin_types();
foreach ($plugintypes as $type=>$location) {
upgrade_plugins($type, $location);
try {
foreach ($plugintypes as $type=>$location) {
upgrade_plugins($type, $location, 'print_upgrade_part_start', 'print_upgrade_part_end');
}
} catch (Exception $ex) {
upgrade_handle_exception($ex);
}
/// Check for changes to RPC functions
if ($CFG->mnet_dispatcher_mode != 'off') {
require_once("$CFG->dirroot/$CFG->admin/mnet/adminlib.php");
upgrade_RPC_functions(); // Return here afterwards
try {
require_once("$CFG->dirroot/$CFG->admin/mnet/adminlib.php");
upgrade_RPC_functions(); // Return here afterwards
} catch (Exception $ex) {
upgrade_handle_exception($ex);
}
}
/// Check for local database customisations
require_once("$CFG->dirroot/lib/locallib.php");
upgrade_local_db(); // Return here afterwards
try {
require_once("$CFG->dirroot/lib/locallib.php");
upgrade_local_db('print_upgrade_part_start', 'print_upgrade_part_end');
} catch (Exception $ex) {
upgrade_handle_exception($ex);
}
/// indicate that this site is fully configured except the admin password
if (empty($CFG->rolesactive)) {

View File

@ -103,14 +103,10 @@
$aclrecord->mnet_host_id = $user->mnethostid;
$aclrecord->username = $user->username;
$aclrecord->accessctrl = $accessctrl;
if (!$DB->insert_record('mnet_sso_access_control', $aclrecord)) {
print_error('dbnotinsert', 'debug', '', 'the MNET access control list');
}
$DB->insert_record('mnet_sso_access_control', $aclrecord);
} else {
$aclrecord->accessctrl = $accessctrl;
if (!$DB->update_record('mnet_sso_access_control', $aclrecord)) {
print_error('dbnotupdate', 'debug', '', 'the MNET access control list');
}
$DB->update_record('mnet_sso_access_control', $aclrecord);
}
$mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
notify("MNET access control list updated: username '$user->username' from host '"

View File

@ -107,13 +107,6 @@ class block_base {
$this->block_base();
}
/**
* Function that can be overridden to do extra setup after
* the database install. (Called once per block, not per instance!)
*/
function after_install() {
}
/**
* Function that can be overridden to do extra cleanup before
* the database tables are deleted. (Called once per block, not per instance!)

19
blocks/upgrade.txt Normal file
View File

@ -0,0 +1,19 @@
This files describes API changes in /blocks/* - activity modules,
information provided here is intended especially for developers.
=== 2.0 ===
required changes in code:
* use new DML syntax everywhere
* use new DDL syntax in db/upgrade.php
* replace defaults.php by settings.php and db/install.php
* replace STATEMENTS section in db/install.xml by db/install.php
* move post instalation code from install() method into db/install.php
* completely rewrite file handling
* rewrite backup/restore
optional - no changes needed in older code:
*
TODO: add links to docs

View File

@ -795,6 +795,7 @@ $string['uploadpicture_userskipped'] = 'Skipping user $a (already has a picture)
$string['uploadpicture_userupdated'] = 'Picture updated for user $a.';
$string['uploadpicture_cannotsave'] = 'Cannot save picture for user $a. Check original picture file.';
$string['updatetimezones'] = 'Update timezones';
$string['upgradeerror'] = 'Unknown error upgrading $a->plugin to version $a->version, can not continue.';
$string['upgradeforumread'] = 'A new feature has been added in Moodle 1.5 to track read/unread forum posts.<br />To use this functionality you need to <a href=\"$a\">update your tables</a>.';
$string['upgradeforumreadinfo'] = 'A new feature has been added in Moodle 1.5 to track read/unread forum posts. To use this functionality you need to update your tables with all the tracking information for existing posts. Depending on the size of your site this can take a long time (hours) and can be quite taxing on the database, so it\'s best to do it during a quiet period. However, your site will continue functioning during this upgrade and users won\'t be affected. Once you start this process you should let it finish (keep your browser window open). However, if you stop the process by closing the window: don\'t worry, you can start over.<br /><br />Do you want to start the upgrading process now?';
$string['upgradelogs'] = 'For full functionality, your old logs need to be upgraded. <a href=\"$a\">More information</a>';

View File

@ -5,23 +5,12 @@
$string['authpluginnotfound'] = 'Authentication plugin $a not found.';
$string['blocknotexist'] = '$a block doesn\'t exist';
$string['cannotbenull'] = '$a cannot be null!';
$string['cannotcreateadminuser'] = 'SERIOUS ERROR: Could not create admin user record !!!';
$string['cannotdowngrade'] = 'Cannot downgrade from $a->oldversion to $a->newversion.';
$string['cannotdowngrade'] = 'Cannot downgrade $a->plugin from $a->oldversion to $a->newversion.';
$string['cannotinitpage'] = 'Cannot fully initialize page: invalid $a->name id $a->id';
$string['cannotupgradecapabilities'] = 'Had trouble upgrading the core capabilities for the Roles System';
$string['cannotupdateversion'] = 'Upgrade failed! (Could not update version in config table)';
$string['cannotupdaterelease'] = 'ERROR: Could not update release version in database!!';
$string['cannotsetupsite'] = 'Serious Error! Could not set up the site!';
$string['cannotsetuptable'] = '$a tables could NOT be set up successfully!';
$string['cannotfindadmin'] = 'Could not find an admin user!';
$string['cannotupgradedbcustom'] = 'Upgrade of local database customisations failed! (Could not update version in config table)';
$string['codingerror'] = 'Coding error detected, it must be fixed by a programmer: $a';
$string['configmoodle'] = 'Moodle has not been configured yet. You need to edit config.php first.';
$string['dbnotinsert'] = 'Database error - Cannot insert ($a)';
$string['dbnotupdate'] = 'Database error - Cannot update ($a)';
$string['dbnotsupport'] = 'Error: Your database ($a) is not yet fully supported by Moodle or install.xml is not present. See the lib/db directory.';
$string['dbnotsetup'] = 'Error: Main databases NOT set up successfully';
$string['doesnotworkwitholdversion'] = 'This script does not work with this old version of Moodle';
$string['erroroccur'] = 'An error has occurred during this process';
$string['fixsetting'] = 'Please fix your settings in config.php: <p>You have:</p> <p>\$CFG->dirroot = \"$a->current\";</p> <p>but it should be:</p> <p>\$CFG->dirroot = \"$a->found\"</p>';
$string['invalideventdata'] = 'Incorrect eventadata submitted: $a';
@ -32,8 +21,6 @@ $string['morethanonerecordinfetch'] = 'Found more than one record in fetch() !';
$string['noadminrole'] = 'No admin role could be found';
$string['noactivityname'] = 'Page object derived from page_generic_activity but did not define $this->activityname';
$string['noblocks'] = 'No blocks installed!';
$string['noblockbase'] = 'Class block_base is not defined or file not found for /blocks/moodleblock.class.php';
$string['nocaps'] = 'Error: no capabilitites defined!';
$string['nocate'] = 'No categories!';
$string['notables'] = 'No Tables!';
$string['nopageclass'] = 'Imported $a but found no page classes';
@ -45,7 +32,6 @@ $string['phpvaron'] = 'The PHP server variable \'$a->name\' is not turned On - $
$string['sessionmissing'] = '$a object missing from session';
$string['siteisnotdefined'] = 'Site is not defined!';
$string['sqlrelyonobsoletetable'] = 'This SQL relies on obsolete table(s): $a! Your code must be fixed by a developer.';
$string['upgradefail'] = 'Upgrade failed! see: $a';
$string['withoutversion'] = 'Main version.php file is missing, not readable or broken.';
$string['xmlizeunavailable'] = 'xmlize functions are not available';

View File

@ -127,12 +127,10 @@ $string['cannotsetupblock'] = 'Blocks tables could NOT be set up successfully!';
$string['cannotsetupcategory'] = 'Serious error! Could not set up a default course category!';
$string['cannotsetupcapforplugin'] = 'Could not set up the capabilities for $a';
$string['cannotsetupcapformod'] = 'Could not set up the capabilities for $a';
$string['cannotsetupsite'] = 'Serious error! Could not set up the site!';
$string['cannotshowhidecoursesincategory'] = 'Cannot show/hide the courses in category $a.';
$string['cannotunzipfile'] = 'Cannot unzip file';
$string['cannotupgradeblock'] = 'Upgrade of blocks system failed! (Could not update version in config table.)';
$string['cannotupgradecaps'] = 'Had difficulties upgrading the core capabilities for the Roles system';
$string['cannotupdateblock'] = 'Could not update block $a record in block table!';
$string['cannotupdatecategory'] = 'Could not update the category ($a)';
$string['cannotupdatecoursemodule'] = 'Could not update the course module';
$string['cannotupdatecomment'] = 'Could not update this comment';
@ -202,6 +200,7 @@ $string['ddlxmlfileerror'] = 'XML database file errors found';
$string['dmlreadexception'] = 'Error reading from database';
$string['dmlwriteexception'] = 'Error writing to database';
$string['destinationcmnotexit'] = 'The destination course module does not exist';
$string['detectedbrokenplugin'] = 'Plugin \"$a\" is defective, can not continue, sorry.';
$string['downloadedfilecheckfailed'] = 'Downloaded file check failed';
$string['duplicateusername'] = 'Duplicate username - skipping record';
$string['duplicateparaminsql'] = 'ERROR: duplicate parameter name in query';
@ -331,7 +330,6 @@ $string['moduledoesnotexist'] = 'This module does not exist';
$string['moduleinstancedoesnotexist'] = 'The instance of this module does not exist';
$string['moduledisable'] = 'This module ($a) has been disabled for this particular course';
$string['modulemissingcode'] = 'Module $a is missing the code needed to perform this function';
$string['modulerequirementsnotmet'] = 'Module \"$a->modulename\" ($a->moduleversion) could not be installed. It requires a newer version of Moodle (currently you are using $a->currentmoodle, you need $a->requiremoodle).';
$string['mustbeteacher'] = 'You must be a teacher to look at this page';
$string['multiplerestorenotallow'] = 'Multiple restore execution not allowed!';
$string['mustbeloggedin'] = 'You must be logged in to do this';
@ -364,7 +362,6 @@ $string['nopermissiontolock'] = 'No permission to lock!';
$string['nopermissiontomkdir'] = 'Cannot create folder. The site administrator needs to fix the file permissions';
$string['nopermissiontoshow'] = 'No permission to see this!';
$string['nopermissiontounlock'] = 'No permission to unlock!';
$string['nopermissiontoupdateblock'] = 'No permission to update $a!';
$string['nopermissiontoviewpage'] = 'You are not allowed to look at this page';
$string['nopermissiontoviewletergrade'] = 'Missing permission to view letter grades';
$string['nopermissiontomanagegroup'] = 'You do not have the required permissions to manage groups';
@ -472,8 +469,6 @@ $string['usernotupdatederror'] = 'User not updated - error';
$string['usernotupdatednotexists'] = 'User not updated - does not exist';
$string['userselectortoomany'] = 'user_selector got more than one selected user, even though multiselect is false.';
$string['updatersserror'] = 'There was an error trying to update RSS feed with id: $a';
$string['upgradeversionfail'] = 'Upgrade of backup system failed! (Could not update version in config table.)';
$string['upgradefail'] = 'Upgrade failed! $a';
$string['upgraderunning'] = 'Site is being upgraded, please retry later.';
$string['younotteacher'] = 'You are not a teacher!';
$string['wrongcall'] = 'This script is called wrongly';

View File

@ -3099,9 +3099,7 @@ function get_cached_capabilities($component='moodle') {
*/
function get_default_capabilities($legacyrole) {
global $DB;
if (!$allcaps = $DB->get_records('capabilities')) {
print_error('nocaps', 'debug');
}
$allcaps = $DB->get_records('capabilities');
$alldefs = array();
$defaults = array();
$components = array();

View File

@ -95,7 +95,7 @@ function get_plugin_types() {
return array('mod' => 'mod',
'qtype' => 'question/type',
'blocks' => 'blocks',
'block' => 'blocks',
'auth' => 'auth',
'enrol' => 'enrol',
'format' => 'course/format',
@ -105,11 +105,12 @@ function get_plugin_types() {
'message' => 'message/output',
'coursereport' => 'course/report',
'report' => $CFG->admin.'/report',
'quizreport' => 'mod/quiz/report',
'portfolio' => 'portfolio/type',
'trackerexport' => 'tracker/export',
'trackerimport' => 'tracker/import',
'trackerreport' => 'tracker/report',
// following types a very ugly hacks - we should not make exceptions like this - all plugins should be equal;
// these plugins may cause problems such as when wanting to uninstall them
'quizreport' => 'mod/quiz/report',
'assignment_type' => 'mod/assignment/type',
);
}

View File

@ -18,70 +18,6 @@ define('BLOCKS_PINNED_BOTH',2);
require_once($CFG->libdir.'/pagelib.php');
require_once($CFG->dirroot.'/course/lib.php'); // needed to solve all those: Call to undefined function: print_recent_activity() when adding Recent Activity
// Returns false if this block is incompatible with the current version of Moodle.
function block_is_compatible($blockname) {
global $CFG;
$file = @file($CFG->dirroot.'/blocks/'.$blockname.'/block_'.$blockname.'.php'); // ignore errors when file does not exist
if(empty($file)) {
return NULL;
}
foreach($file as $line) {
// If you find MoodleBlock (appearing in the class declaration) it's not compatible
if(strpos($line, 'MoodleBlock')) {
return false;
}
// But if we find a { it means the class declaration is over, so it's compatible
else if(strpos($line, '{')) {
return true;
}
}
return NULL;
}
// Returns the case-sensitive name of the class' constructor function. This includes both
// PHP5- and PHP4-style constructors. If no appropriate constructor can be found, returns NULL.
// If there is no such class, returns boolean false.
function get_class_constructor($classname) {
// Caching
static $constructors = array();
if(!class_exists($classname)) {
return false;
}
// Tests indicate this doesn't hurt even in PHP5.
$classname = strtolower($classname);
// Return cached value, if exists
if(isset($constructors[$classname])) {
return $constructors[$classname];
}
// Get a list of methods. After examining several different ways of
// doing the check, (is_callable, method_exists, function_exists etc)
// it seems that this is the most reliable one.
$methods = get_class_methods($classname);
// PHP5 constructor?
if(phpversion() >= '5') {
if(in_array('__construct', $methods)) {
return $constructors[$classname] = '__construct';
}
}
// If we have PHP5 but no magic constructor, we have to lowercase the methods
$methods = array_map('strtolower', $methods);
if(in_array($classname, $methods)) {
return $constructors[$classname] = $classname;
}
return $constructors[$classname] = NULL;
}
//This function retrieves a method-defined property of a class WITHOUT instantiating an object
function block_method_result($blockname, $method, $param = NULL) {
if(!block_load_class($blockname)) {
@ -1069,227 +1005,4 @@ function blocks_repopulate_page($page) {
return true;
}
//This function finds all available blocks and install them
//into blocks table or do all the upgrade process if newer
function upgrade_blocks_plugins() {
global $CFG, $interactive, $DB, $SITE;
require_once($CFG->dirroot .'/blocks/moodleblock.class.php');
$updated_blocks = false;
$blocktitles = array();
$invalidblocks = array();
$validblocks = array();
$notices = array();
//Count the number of blocks in db
$blockcount = $DB->count_records('block');
//If there isn't records. This is the first install, so I remember it
$first_install = ($blockcount == 0);
if (!$blocks = get_list_of_plugins('blocks') ) {
print_error('noblocks', 'debug');
}
if (!class_exists('block_base')) {
print_error('noblockbase');
}
foreach ($blocks as $blockname) {
if ($blockname == 'NEWBLOCK') { // Someone has unzipped the template, ignore it
continue;
}
if(!block_is_compatible($blockname)) {
// This is an old-style block
//$notices[] = 'Block '. $blockname .' is not compatible with the current version of Mooodle and needs to be updated by a programmer.';
$invalidblocks[] = $blockname;
continue;
}
$fullblock = $CFG->dirroot .'/blocks/'. $blockname;
if ( is_readable($fullblock.'/block_'.$blockname.'.php')) {
include_once($fullblock.'/block_'.$blockname.'.php');
} else {
$notices[] = 'Block '. $blockname .': '. $fullblock .'/block_'. $blockname .'.php was not readable';
continue;
}
$newupgrade = false;
if ( @is_dir($fullblock .'/db/')) {
if ( @is_readable($fullblock .'/db/upgrade.php')) {
include_once($fullblock .'/db/upgrade.php'); // defines new upgrading function
$newupgrade = true;
}
}
$classname = 'block_'.$blockname;
if(!class_exists($classname)) {
$notices[] = 'Block '. $blockname .': '. $classname .' not implemented';
continue;
}
// Here is the place to see if the block implements a constructor (old style),
// an init() function (new style) or nothing at all (error time).
$constructor = get_class_constructor($classname);
if(empty($constructor)) {
// No constructor
$notices[] = 'Block '. $blockname .': class does not have a constructor';
$invalidblocks[] = $blockname;
continue;
}
$block = new stdClass; // This may be used to update the db below
$blockobj = new $classname; // This is what we 'll be testing
// Inherits from block_base?
if(!is_subclass_of($blockobj, 'block_base')) {
$notices[] = 'Block '. $blockname .': class does not inherit from block_base';
continue;
}
// OK, it's as we all hoped. For further tests, the object will do them itself.
if(!$blockobj->_self_test()) {
$notices[] = 'Block '. $blockname .': self test failed';
continue;
}
$block->version = $blockobj->get_version();
if (!isset($block->version)) {
$notices[] = 'Block '. $blockname .': has no version support. It must be updated by a programmer.';
continue;
}
$block->name = $blockname; // The name MUST match the directory
$blocktitle = $blockobj->get_title();
if ($currblock = $DB->get_record('block', array('name'=>$block->name))) {
if ($currblock->version == $block->version) {
// do nothing
} else if ($currblock->version < $block->version) {
$updated_blocks = true;
upgrade_started();
print_heading('New version of '.$blocktitle.' ('.$block->name.') exists');
@set_time_limit(0); // To allow slow databases to complete the long SQL
/// Run de old and new upgrade functions for the module
$newupgrade_function = 'xmldb_block_' . $block->name .'_upgrade';
/// Then, the new function if exists and the old one was ok
$newupgrade_status = true;
if ($newupgrade && function_exists($newupgrade_function)) {
$newupgrade_status = $newupgrade_function($currblock->version, $block);
} else if ($newupgrade) {
notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
$fullblock . '/db/upgrade.php');
}
/// Now analyze upgrade results
if ($newupgrade_status) { // No upgrading failed
// Set the block cron on upgrade
$block->cron = !empty($blockobj->cron) ? $blockobj->cron : 0;
// OK so far, now update the block record
$block->id = $currblock->id;
if (!$DB->update_record('block', $block)) {
print_error('cannotupdateblock', '', '', $block->name);
}
$component = 'block/'.$block->name;
if (!update_capabilities($component)) {
print_error('nopermissiontoupdateblock', '', '', $block->name);
}
// Update events
events_update_definition($component);
// Update message providers
require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions
message_update_providers($component);
notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess');
} else {
notify('Upgrading block '. $block->name .' from '. $currblock->version .' to '. $block->version .' FAILED!');
}
print_upgrade_separator();
} else {
print_error('cannotdowngrade', 'debug', '', (object)array('oldversion'=>$currblock->version, 'newversion'=>$block->version));
}
} else { // block not installed yet, so install it
// If it allows multiples, start with it enabled
if ($blockobj->instance_allow_multiple()) {
$block->multiple = 1;
}
// Set the block cron on install
$block->cron = !empty($blockobj->cron) ? $blockobj->cron : 0;
// [pj] Normally this would be inline in the if, but we need to
// check for NULL (necessary for 4.0.5 <= PHP < 4.2.0)
$conflictblock = array_search($blocktitle, $blocktitles);
if ($conflictblock !== false && $conflictblock !== NULL) {
// Duplicate block titles are not allowed, they confuse people
// AND PHP's associative arrays ;)
print_error('blocknameconflict', '', '', (object)array('name'=>$block->name, 'conflict'=>$conflictblock));
}
$updated_blocks = true;
upgrade_started();
print_heading($block->name);
@set_time_limit(0); // To allow slow databases to complete the long SQL
/// Both old .sql files and new install.xml are supported
/// but we priorize install.xml (XMLDB) if present
if (file_exists($fullblock . '/db/install.xml')) {
$DB->get_manager()->install_from_xmldb_file($fullblock . '/db/install.xml'); //New method
}
$block->id = $DB->insert_record('block', $block);
$blockobj->after_install();
$component = 'block/'.$block->name;
update_capabilities($component);
// Update events
events_update_definition($component);
// Update message providers
require_once($CFG->libdir .'/messagelib.php'); // Messagelib functions
message_update_providers($component);
notify(get_string('blocksuccess', '', $blocktitle), 'notifysuccess');
print_upgrade_separator();
}
$blocktitles[$block->name] = $blocktitle;
}
if(!empty($notices)) {
foreach($notices as $notice) {
notify($notice);
}
}
// Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
if ($first_install) {
require_once($CFG->dirroot.'/'.$CFG->admin.'/pagelib.php');
//Iterate over each course - there should be only site course here now
if ($courses = $DB->get_records('course')) {
foreach ($courses as $course) {
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
blocks_repopulate_page($page);
}
}
page_map_class(PAGE_ADMIN, 'page_admin');
$page = page_create_object(PAGE_ADMIN, 0); // there must be some id number
blocks_repopulate_page($page);
}
return $updated_blocks;
}
?>

View File

@ -30,6 +30,38 @@ function xmldb_main_upgrade($oldversion) {
///upgrade supported only from 1.9.x ///
////////////////////////////////////////
if ($result && $oldversion < 2008030600) {
//NOTE: this table was added much later, that is why this step is repeated later in this file
/// Define table upgrade_log to be created
$table = new xmldb_table('upgrade_log');
/// Adding fields to table upgrade_log
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null);
$table->add_field('type', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, null, null);
$table->add_field('plugin', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
$table->add_field('version', XMLDB_TYPE_CHAR, '100', null, null, null, null, null, null);
$table->add_field('info', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null);
$table->add_field('details', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
$table->add_field('backtrace', XMLDB_TYPE_TEXT, 'small', null, null, null, null, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, null);
/// Adding keys to table upgrade_log
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('userid', XMLDB_KEY_FOREIGN, array('userid'), 'user', array('id'));
/// Adding indexes to table upgrade_log
$table->add_index('timemodified', XMLDB_INDEX_NOTUNIQUE, array('timemodified'));
$table->add_index('type-timemodified', XMLDB_INDEX_NOTUNIQUE, array('type', 'timemodified'));
/// Create table for upgrade_log
$dbman->create_table($table);
/// Main savepoint reached
upgrade_main_savepoint($result, 2008030600);
}
if ($result && $oldversion < 2008030700) {
upgrade_set_timeout(60*20); // this may take a while
@ -1373,6 +1405,7 @@ function xmldb_main_upgrade($oldversion) {
}
if ($result && $oldversion < 2009012901) {
// NOTE: this table may already exist, see beginning of this file ;-)
/// Define table upgrade_log to be created
$table = new xmldb_table('upgrade_log');

View File

@ -105,65 +105,6 @@
* or delete_course() from moodlelib are called.
*/
/**
* This function checks to see whether local database customisations are up-to-date
* by comparing $CFG->local_version to the variable $local_version defined in
* local/version.php. If not, it looks for a function called 'xmldb_local_upgrade'
* in a file called 'local/db/upgrade.php', and if it's there calls it with the
* appropiate $oldversion parameter. Then it updates $CFG->local_version.
*
* @uses $CFG
* @return bool true if upgraded anything
*/
function upgrade_local_db() {
global $CFG, $DB;
// if we don't have code version, just return false
if (!file_exists($CFG->dirroot.'/local/version.php')) {
return false;
}
$local_version = null;
require($CFG->dirroot.'/local/version.php'); // Get code versions
if (empty($CFG->local_version)) { // install
upgrade_started();
if (file_exists($CFG->dirroot.'/local/db/install.php')) {
require_once($CFG->dirroot.'/local/db/install.php');
xmldb_local_install();
}
set_config('local_version', $local_version);
notify(get_string('databaseupgradelocal', '', $local_version), 'notifysuccess');
print_upgrade_separator();
/// Capabilities
update_capabilities('local');
return true;
} else if ($local_version > $CFG->local_version) { // upgrade!
upgrade_started();
if (file_exists($CFG->dirroot.'/local/db/upgrade.php')) {
require_once($CFG->dirroot.'/local/db/upgrade.php');
xmldb_local_upgrade($CFG->local_version);
}
set_config('local_version', $local_version);
notify(get_string('databaseupgradelocal', '', $local_version), 'notifysuccess');
print_upgrade_separator();
/// Capabilities
update_capabilities('local');
return true;
} else if ($local_version < $CFG->local_version) {
notify('WARNING!!! The local version you are using is OLDER than the version that made these databases!');
}
return false;
}
/**
* Notify local code that a course is being deleted.
* Look for a function local_delete_course() in a file called

View File

@ -77,7 +77,7 @@ function default_exception_handler($ex) {
}
} else {
if (!isset($CFG->theme) or !isset($CFG->stylesheets)) {
_print_early_error('generalexceptionmessage', 'error', $ex->getMessage());
_print_early_error('generalexceptionmessage', 'error', $ex->getMessage(), $backtrace);
} else {
_print_normal_error('generalexceptionmessage', 'error', $ex->getMessage(), '', $backtrace);
}

View File

@ -10,7 +10,49 @@
define('UPGRADE_LOG_NORMAL', 0);
define('UPGRADE_LOG_NOTICE', 1);
define('UPGRADE_LOG_ERROR', 2);
define('UPGRADE_LOG_ERROR', 2);
/**
* Exception indicating unknown error during upgrade.
*/
class upgrade_exception extends moodle_exception {
function __construct($plugin, $version) {
global $CFG;
$a = (object)array('plugin'=>$plugin, 'version'=>$version);
parent::__construct('upgradeerror', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a);
}
}
/**
* Exception indicating downgrade error during upgrade.
*/
class downgrade_exception extends moodle_exception {
function __construct($plugin, $oldversion, $newversion) {
global $CFG;
$plugin = is_null($plugin) ? 'moodle' : $plugin;
$a = (object)array('plugin'=>$plugin, 'oldversion'=>$oldversion, 'newversion'=>$newversion);
parent::__construct('cannotdowngrade', 'debug', "$CFG->wwwroot/$CFG->admin/index.php", $a);
}
}
class upgrade_requires_exception extends moodle_exception {
function __construct($plugin, $pluginversion, $currentmoodle, $requiremoodle) {
global $CFG;
$a = new object();
$a->pluginname = $plugin;
$a->pluginversion = $pluginversion;
$a->currentmoodle = $currentmoodle;
$a->requiremoodle = $currentmoodle;
parent::__construct('pluginrequirementsnotmet', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a);
}
}
class plugin_defective_exception extends moodle_exception {
function __construct($plugin, $details) {
global $CFG;
parent::__construct('detectedbrokenplugin', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $plugin, $details);
}
}
/**
* Insert or update log display entry. Entry may already exist.
@ -58,16 +100,18 @@ function update_log_display_entry($module, $action, $mtable, $field) {
function upgrade_main_savepoint($result, $version, $allowabort=true) {
global $CFG;
if ($result) {
if ($CFG->version >= $version) {
// something really wrong is going on in main upgrade script
print_error('cannotdowngrade', 'debug', '', (object)array('oldversion'=>$CFG->version, 'newversion'=>$version));
}
set_config('version', $version);
} else {
error("Upgrade savepoint: Error during main upgrade to version $version"); // TODO: localise
if (!$result) {
throw new upgrade_exception('moodle core', $version);
}
if ($CFG->version >= $version) {
// something really wrong is going on in main upgrade script
throw new downgrade_exception(null, $CFG->version, $version);
}
set_config('version', $version);
upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
// reset upgrade timeout to default
upgrade_set_timeout();
@ -91,20 +135,21 @@ function upgrade_main_savepoint($result, $version, $allowabort=true) {
function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
global $DB;
if (!$result) {
throw new upgrade_exception("mod/$modname", $version);
}
if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
print_error('modulenotexist', 'debug', '', $modname);
}
if ($result) {
if ($module->version >= $version) {
// something really wrong is going on in upgrade script
print_error('cannotdowngrade', 'debug', '', (object)array('oldversion'=>$module->version, 'newversion'=>$version));
}
$module->version = $version;
$DB->update_record('modules', $module);
} else {
error("Upgrade savepoint: Error during mod upgrade to version $version"); // TODO: localise
if ($module->version >= $version) {
// something really wrong is going on in upgrade script
throw new downgrade_exception("mod/$modname", $module->version, $version);
}
$module->version = $version;
$DB->update_record('modules', $module);
upgrade_log(UPGRADE_LOG_NORMAL, "mod/$modname", 'Upgrade savepoint reached');
// reset upgrade timeout to default
upgrade_set_timeout();
@ -126,23 +171,24 @@ function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
* @param bool $allowabort allow user to abort script execution here
* @return void
*/
function upgrade_blocks_savepoint($result, $version, $blockname, $allowabort=true) {
function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
global $DB;
if (!$result) {
throw new upgrade_exception("blocks/$blockname", $version);
}
if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
print_error('blocknotexist', 'debug', '', $blockname);
}
if ($result) {
if ($block->version >= $version) {
// something really wrong is going on in upgrade script
print_error('cannotdowngrade', 'debug', '', (object)array('oldversion'=>$block->version, 'newversion'=>$version));
}
$block->version = $version;
$DB->update_record('block', $block);
} else {
error("Upgrade savepoint: Error during mod upgrade to version $version"); // TODO: localise
if ($block->version >= $version) {
// something really wrong is going on in upgrade script
throw new downgrade_exception("blocks/$blockname", $block->version, $version);
}
$block->version = $version;
$DB->update_record('block', $block);
upgrade_log(UPGRADE_LOG_NORMAL, "blocks/$blockname", 'Upgrade savepoint reached');
// reset upgrade timeout to default
upgrade_set_timeout();
@ -166,21 +212,21 @@ function upgrade_blocks_savepoint($result, $version, $blockname, $allowabort=tru
* @return void
*/
function upgrade_plugin_savepoint($result, $version, $type, $dir, $allowabort=true) {
if ($result) {
$fullname = $type . '_' . $dir;
$installedversion = get_config($fullname, 'version');
if ($installedversion >= $version) {
// Something really wrong is going on in the upgrade script
$a = new stdClass;
$a->oldversion = $installedversion;
$a->newversion = $version;
print_error('cannotdowngrade', 'debug', '', $a);
}
set_config('version', $version, $fullname);
} else {
error("Upgrade savepoint: Error during mod upgrade to version $version"); // TODO: localise
if (!$result) {
throw new upgrade_exception("$type/$dir", $version);
}
$fullname = $type.'_'.$dir;
$component = $type.'/'.$dir;
$installedversion = get_config($fullname, 'version');
if ($installedversion >= $version) {
// Something really wrong is going on in the upgrade script
throw new downgrade_exception($component, $installedversion, $version);
}
set_config('version', $version, $fullname);
upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
// Reset upgrade timeout to default
upgrade_set_timeout();
@ -199,155 +245,109 @@ function upgrade_plugin_savepoint($result, $version, $type, $dir, $allowabort=tr
* @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
* @param string $return The url to prompt the user to continue to
*/
function upgrade_plugins($type, $dir) {
function upgrade_plugins($type, $dir, $startcallback, $endcallback) {
global $CFG, $DB;
/// special cases
if ($type === 'mod') {
return upgrade_activity_modules();
} else if ($type === 'blocks') {
return upgrade_blocks_plugins();
return upgrade_plugins_modules($startcallback, $endcallback);
} else if ($type === 'block') {
return upgrade_plugins_blocks($startcallback, $endcallback);
}
$plugs = get_list_of_plugins($dir);
$updated_plugins = false;
$strpluginsetup = get_string('pluginsetup');
foreach ($plugs as $plug) {
$fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
$fullplug = $CFG->dirroot.'/'.$dir.'/'.$plug;
$component = $type.'/'.$plug; // standardised plugin name
unset($plugin);
if (is_readable($fullplug .'/version.php')) {
include($fullplug .'/version.php'); // defines $plugin with version etc
} else {
continue; // Nothing to do.
}
$newupgrade = false;
if (is_readable($fullplug . '/db/upgrade.php')) {
include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function
$newupgrade = true;
}
if (!isset($plugin)) {
if (!is_readable($fullplug.'/version.php')) {
continue;
}
if (!empty($plugin->requires)) {
if ($plugin->requires > $CFG->version) {
$info = new object();
$info->pluginname = $plug;
$info->pluginversion = $plugin->version;
$info->currentmoodle = $CFG->version;
$info->requiremoodle = $plugin->requires;
upgrade_started();
notify(get_string('pluginrequirementsnotmet', 'error', $info));
$updated_plugins = true;
continue;
}
$plugin = new object();
require($fullplug.'/version.php'); // defines $plugin with version etc
if (empty($plugin->version)) {
throw new plugin_defective_exception($component, 'Missing version value in version.php');
}
$plugin->name = $plug; // The name MUST match the directory
$plugin->name = $plug; // The name MUST match the directory
$plugin->fullname = $type.'_'.$plug; // The name MUST match the directory
if (!empty($plugin->requires)) {
if ($plugin->requires > $CFG->version) {
throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
}
}
$installedversion = get_config($plugin->fullname, 'version');
if ($installedversion === false) {
set_config('version', 0, $plugin->fullname);
}
if (empty($installedversion)) { // new installation
$startcallback($component, true);
if ($installedversion == $plugin->version) {
// do nothing
} else if ($installedversion < $plugin->version) {
$updated_plugins = true;
upgrade_started();
print_heading($dir.'/'. $plugin->name .' plugin needs upgrading');
@set_time_limit(0); // To allow slow databases to complete the long SQL
if ($installedversion == 0) { // It's a new install of this plugin
/// Both old .sql files and new install.xml are supported
/// but we priorize install.xml (XMLDB) if present
if (file_exists($fullplug . '/db/install.xml')) {
$DB->get_manager()->install_from_xmldb_file($fullplug . '/db/install.xml'); //New method
}
$status = true;
/// Continue with the instalation, roles and other stuff
if ($status) {
/// OK so far, now update the plugins record
set_config('version', $plugin->version, $plugin->fullname);
/// Install capabilities
update_capabilities($type.'/'.$plug);
/// Install events
events_update_definition($type.'/'.$plug);
/// Install message providers
message_update_providers($type.'/'.$plug);
/// Run local install function if there is one
if (is_readable($fullplug . '/lib.php')) {
include_once($fullplug . '/lib.php');
$installfunction = $plugin->name.'_install';
if (function_exists($installfunction)) {
if (! $installfunction() ) {
notify('Encountered a problem running install function for '.$module->name.'!');
}
}
}
notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
} else {
notify('Installing '. $plugin->name .' FAILED!');
}
} else { // Upgrade existing install
/// Run the upgrade function for the plugin.
$newupgrade_function = 'xmldb_' .$plugin->fullname .'_upgrade';
$newupgrade_status = true;
if ($newupgrade && function_exists($newupgrade_function)) {
$newupgrade_status = $newupgrade_function($installedversion);
} else if ($newupgrade) {
notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
$fullplug . '/db/upgrade.php');
}
/// Now analyze upgrade results
if ($newupgrade_status) { // No upgrading failed
/// OK so far, now update the plugins record
set_config('version', $plugin->version, $plugin->fullname);
update_capabilities($type.'/'.$plug);
/// Update events
events_update_definition($type.'/'.$plug);
/// Update message providers
message_update_providers($type.'/'.$plug);
notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
} else {
notify('Upgrading '. $plugin->name .' from '. $installedversion .' to '. $plugin->version .' FAILED!');
}
/// Install tables if defined
if (file_exists($fullplug.'/db/install.xml')) {
$DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
}
print_upgrade_separator();
} else {
print_error('cannotdowngrade', 'debug', '', (object)array('oldversion'=>$installedversion, 'newversion'=>$plugin->version));
/// execute post install file
if (file_exists($fullplug.'/db/install.php')) {
require_once($fullplug.'/db/install.php');
$post_install_function = 'xmldb_'.$plugin->fullname.'_install';;
$post_install_function();
}
/// store version
upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
/// Install various components
update_capabilities($component);
events_update_definition($component);
message_update_providers($component);
$endcallback($component, true);
} else if ($installedversion < $plugin->version) { // upgrade
/// Run the upgrade function for the plugin.
$startcallback($component, false);
if (is_readable($fullplug.'/db/upgrade.php')) {
require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
$newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
$result = $newupgrade_function($installedversion);
} else {
$result = true;
}
$installedversion = get_config($plugin->fullname, 'version');
if ($installedversion < $plugin->version) {
// store version if not already there
upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
}
/// Upgrade various components
update_capabilities($component);
events_update_definition($component);
message_update_providers($component);
$endcallback($component, false);
} else if ($installedversion > $plugin->version) {
throw new downgrade_exception($component, $installedversion, $plugin->version);
}
}
return $updated_plugins;
}
/**
* Find and check all modules and load them up or upgrade them if necessary
*/
function upgrade_activity_modules() {
function upgrade_plugins_modules($startcallback, $endcallback) {
global $CFG, $DB;
if (!$mods = get_list_of_plugins('mod') ) {
print_error('nomodules', 'debug');
}
$strmodulesetup = get_string('modulesetup');
$mods = get_list_of_plugins('mod');
foreach ($mods as $mod) {
@ -355,99 +355,35 @@ function upgrade_activity_modules() {
continue;
}
$fullmod = $CFG->dirroot .'/mod/'. $mod;
$fullmod = $CFG->dirroot.'/mod/'.$mod;
$component = 'mod/'.$mod;
unset($module);
if (is_readable($fullmod .'/version.php')) {
require($fullmod .'/version.php'); // defines $module with version etc
} else {
error('Module '. $mod .': '. $fullmod .'/version.php was not readable'); // TODO: localise
if (!is_readable($fullmod.'/version.php')) {
throw new plugin_defective_exception($component, 'Missing version.php');
}
$newupgrade = false;
if ( is_readable($fullmod . '/db/upgrade.php')) {
include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function
$newupgrade = true;
}
$module = new object();
require($fullmod .'/version.php'); // defines $module with version etc
if (!isset($module)) {
continue;
if (empty($module->version)) {
throw new plugin_defective_exception($component, 'Missing version value in version.php');
}
if (!empty($module->requires)) {
if ($module->requires > $CFG->version) {
$info = new object();
$info->modulename = $mod;
$info->moduleversion = $module->version;
$info->currentmoodle = $CFG->version;
$info->requiremoodle = $module->requires;
upgrade_started();
notify(get_string('modulerequirementsnotmet', 'error', $info));
continue;
throw new upgrade_requires_exception($component, $module->version, $CFG->version, $module->requires);
}
}
$module->name = $mod; // The name MUST match the directory
include_once($fullmod.'/lib.php'); // defines upgrading and/or installing functions
$currmodule = $DB->get_record('modules', array('name'=>$module->name));
if ($currmodule = $DB->get_record('modules', array('name'=>$module->name))) {
if ($currmodule->version == $module->version) {
// do nothing
} else if ($currmodule->version < $module->version) {
/// If versions say that we need to upgrade but no upgrade files are available, notify and continue
if (!$newupgrade) {
notify('Upgrade file ' . $mod . ': ' . $fullmod . '/db/upgrade.php is not readable');
continue;
}
upgrade_started();
if (empty($currmodule->version)) {
$startcallback($component, true);
print_heading($module->name .' module needs upgrading');
/// Run de old and new upgrade functions for the module
$newupgrade_function = 'xmldb_' . $module->name . '_upgrade';
/// Then, the new function if exists and the old one was ok
$newupgrade_status = true;
if ($newupgrade && function_exists($newupgrade_function)) {
$newupgrade_status = $newupgrade_function($currmodule->version, $module);
} else if ($newupgrade) {
notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
$mod . ': ' . $fullmod . '/db/upgrade.php');
}
/// Now analyze upgrade results
if ($newupgrade_status) { // No upgrading failed
// OK so far, now update the modules record
$module->id = $currmodule->id;
$DB->update_record('modules', $module);
remove_dir($CFG->dataroot . '/cache', true); // flush cache
notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
print_upgrade_separator();
} else {
notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
}
/// Update the capabilities table?
update_capabilities('mod/'.$module->name);
/// Update events
events_update_definition('mod/'.$module->name);
/// Update message providers
message_update_providers('mod/'.$module->name);
} else {
print_error('cannotdowngrade', 'debug', '', (object)array('oldversion'=>$currmodule->version, 'newversion'=>$module->version));
}
} else { // module not installed yet, so install it
upgrade_started();
print_heading($module->name);
/// Execute install.xml (XMLDB) - must be present
$DB->get_manager()->install_from_xmldb_file($fullmod . '/db/install.xml'); //New method
/// Execute install.xml (XMLDB) - must be present in all modules
$DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
/// Post installation hook - optional
if (file_exists("$fullmod/db/install.php")) {
@ -459,50 +395,246 @@ function upgrade_activity_modules() {
/// Continue with the installation, roles and other stuff
$module->id = $DB->insert_record('modules', $module);
/// Capabilities
update_capabilities('mod/'.$module->name);
/// Install various components
update_capabilities($component);
events_update_definition($component);
message_update_providers($component);
/// Events
events_update_definition('mod/'.$module->name);
$endcallback($component, true);
/// Message providers
message_update_providers('mod/'.$module->name);
} else if ($currmodule->version < $module->version) {
/// If versions say that we need to upgrade but no upgrade files are available, notify and continue
$startcallback($component, false);
notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
print_upgrade_separator();
if (is_readable($fullmod.'/db/upgrade.php')) {
require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
$newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
$result = $newupgrade_function($currmodule->version, $module);
} else {
$result = true;
}
$currmodule = $DB->get_record('modules', array('name'=>$module->name));
if ($currmodule->version < $module->version) {
// store version if not already there
upgrade_mod_savepoint($result, $module->version, $mod, false);
}
/// Upgrade various components
update_capabilities($component);
events_update_definition($component);
message_update_providers($component);
remove_dir($CFG->dataroot.'/cache', true); // flush cache
$endcallback($component, false);
} else if ($currmodule->version > $module->version) {
throw new downgrade_exception($component, $currmodule->version, $module->version);
}
}
}
/**
* This function finds all available blocks and install them
* into blocks table or do all the upgrade process if newer.
*/
function upgrade_plugins_blocks($startcallback, $endcallback) {
global $CFG, $DB;
require_once($CFG->libdir.'/blocklib.php');
require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
$blocktitles = array(); // we do not want duplicate titles
//Is this a first install
$first_install = null;
$blocks = get_list_of_plugins('blocks');
foreach ($blocks as $blockname) {
if (is_null($first_install)) {
$first_install = ($DB->count_records('block') == 0);
}
/// Check submodules of this module if necessary
$submoduleupgrade = $module->name.'_upgrade_submodules';
if (function_exists($submoduleupgrade)) {
$submoduleupgrade();
if ($blockname == 'NEWBLOCK') { // Someone has unzipped the template, ignore it
continue;
}
/// Run any defaults or final code that is necessary for this module
$fullblock = $CFG->dirroot.'/blocks/'.$blockname;
$component = 'block/'.$blockname;
if ( is_readable($fullmod .'/defaults.php')) {
// Insert default values for any important configuration variables
unset($defaults);
include($fullmod .'/defaults.php'); // include here means execute, not library include
if (!empty($defaults)) {
if (!empty($defaults['_use_config_plugins'])) {
unset($defaults['_use_config_plugins']);
$localcfg = get_config($module->name);
foreach ($defaults as $name => $value) {
if (!isset($localcfg->$name)) {
set_config($name, $value, $module->name);
}
}
} else {
foreach ($defaults as $name => $value) {
if (!isset($CFG->$name)) {
set_config($name, $value);
}
}
}
if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
}
require_once($fullblock.'/block_'.$blockname.'.php');
$classname = 'block_'.$blockname;
if (!class_exists($classname)) {
throw new plugin_defective_exception($component, 'Can not load main class.');
}
$blockobj = new $classname; // This is what we 'll be testing
$blocktitle = $blockobj->get_title();
// OK, it's as we all hoped. For further tests, the object will do them itself.
if (!$blockobj->_self_test()) {
throw new plugin_defective_exception($component, 'Self test failed.');
}
$block = new object(); // This may be used to update the db below
$block->name = $blockname; // The name MUST match the directory
$block->version = $blockobj->get_version();
$block->cron = !empty($blockobj->cron) ? $blockobj->cron : 0;
$block->multiple = $blockobj->instance_allow_multiple() ? 1 : 0;
if (empty($block->version)) {
throw new plugin_defective_exception($component, 'Missing block version.');
}
$currblock = $DB->get_record('block', array('name'=>$block->name));
if (empty($currblock->version)) { // block not installed yet, so install it
// If it allows multiples, start with it enabled
$conflictblock = array_search($blocktitle, $blocktitles);
if ($conflictblock !== false) {
// Duplicate block titles are not allowed, they confuse people
// AND PHP's associative arrays ;)
throw new plugin_defective_exception($component, get_string('blocknameconflict', '', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
}
$startcallback($component, true);
if (file_exists($fullblock.'/db/install.xml')) {
$DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
}
$block->id = $DB->insert_record('block', $block);
if (file_exists($fullblock.'/db/install.php')) {
require_once($fullblock.'/db/install.php');
$post_install_function = 'xmldb_block_'.$blockname.'_install';;
$post_install_function();
}
$blocktitles[$block->name] = $blocktitle;
// Install various components
update_capabilities($component);
events_update_definition($component);
message_update_providers($component);
$endcallback($component, true);
} else if ($currblock->version < $block->version) {
$startcallback($component, false);
if (is_readable($fullblock.'/db/upgrade.php')) {
require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
$newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
$result = $newupgrade_function($currblock->version, $block);
} else {
$result = true;
}
$currblock = $DB->get_record('block', array('name'=>$block->name));
if ($currblock->version < $block->version) {
// store version if not already there
upgrade_block_savepoint($result, $block->version, $block->name, false);
}
if ($currblock->cron != $block->cron) {
// update cron flag if needed
$currblock->cron = $block->cron;
$DB->update_record('block', $currblock);
}
// Upgrade various componebts
events_update_definition($component);
update_capabilities($component);
message_update_providers($component);
$endcallback($component, false);
} else if ($currblock->version > $block->version) {
throw new downgrade_exception($component, $currblock->version, $block->version);
}
}
// Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
if ($first_install) {
require_once($CFG->dirroot.'/'.$CFG->admin.'/pagelib.php');
//Iterate over each course - there should be only site course here now
if ($courses = $DB->get_records('course')) {
foreach ($courses as $course) {
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
blocks_repopulate_page($page);
}
}
page_map_class(PAGE_ADMIN, 'page_admin');
$page = page_create_object(PAGE_ADMIN, 0); // there must be some id number
blocks_repopulate_page($page);
}
}
/**
* This function checks to see whether local database customisations are up-to-date
* by comparing $CFG->local_version to the variable $local_version defined in
* local/version.php. If not, it looks for a function called 'xmldb_local_upgrade'
* in a file called 'local/db/upgrade.php', and if it's there calls it with the
* appropiate $oldversion parameter. Then it updates $CFG->local_version.
*
* @uses $CFG
*/
function upgrade_local_db($startcallback, $endcallback) {
global $CFG, $DB;
// if we don't have code version, just return false
if (!file_exists($CFG->dirroot.'/local/version.php')) {
return;
}
$local_version = null;
require($CFG->dirroot.'/local/version.php'); // Get code versions
if (empty($CFG->local_version)) { // install
$startcallback('local', true);
if (file_exists($CFG->dirroot.'/local/db/install.php')) {
require_once($CFG->dirroot.'/local/db/install.php');
xmldb_local_install();
}
set_config('local_version', $local_version);
/// Install various components
events_update_definition('local');
update_capabilities('local');
message_update_providers('local');
$endcallback('local', true);
} else if ($local_version > $CFG->local_version) { // upgrade!
$startcallback('local', false);
if (file_exists($CFG->dirroot.'/local/db/upgrade.php')) {
require_once($CFG->dirroot.'/local/db/upgrade.php');
xmldb_local_upgrade($CFG->local_version);
}
set_config('local_version', $local_version);
/// Upgrade various components
events_update_definition('local');
update_capabilities('local');
message_update_providers('local');
$endcallback('local', false);
} else if ($local_version < $CFG->local_version) {
throw new downgrade_exception('local', $CFG->local_version, $local_version);
}
}
@ -511,8 +643,33 @@ function upgrade_activity_modules() {
/// upgrade logging functions
////////////////////////////////////////////////
function upgrade_handle_exception($exception, $plugin=null) {
//TODO
function upgrade_handle_exception($ex, $plugin=null) {
global $CFG;
if ($ex instanceof moodle_exception) {
$details = get_string($ex->errorcode, $ex->module, $ex->a)."<br />debugging:".$ex->debuginfo;
} else {
$details = get_string('generalexceptionmessage', 'error', $ex->getMessage());
}
$info = "Exception: ".get_class($ex);
$backtrace = $ex->getTrace();
$place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex));
array_unshift($backtrace, $place);
/// first log upgrade error
upgrade_log(UPGRADE_LOG_ERROR, $plugin, $info, $details, $backtrace);
// always turn on debugging - admins need to know what is going on
$CFG->debug = DEBUG_DEVELOPER;
// now print the exception info as usually
if ($ex instanceof moodle_exception) {
_print_normal_error($ex->errorcode, $ex->module, $ex->a, $ex->link, $backtrace, $ex->debuginfo);
} else {
_print_normal_error('generalexceptionmessage', 'error', $ex->getMessage(), '', $backtrace);
}
die; // not reached
}
/**
@ -528,34 +685,43 @@ function upgrade_handle_exception($exception, $plugin=null) {
function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
global $DB, $USER, $CFG;
static $plugins = null;
if (!$plugins) {
$plugins = get_plugin_types();
}
$plugin = ($plugin==='moodle') ? null : $plugin;
$backtrace = print_backtrace($backtrace, true);
$version = null;
//first try to find out current version number
if (is_null($plugin)) {
if (empty($plugin) or $plugin === 'moodle') {
//main
$version = $CFG->version;
} else if (strpos('mod/', $plugin) === 0) {
} else if ($plugin === 'local') {
//customisation
$version = $CFG->local_version;
} else if (strpos($plugin, 'mod/') === 0) {
try {
$modname = substr($plugin, strlen('mod/'));
$version = $DB->get_field('modules', 'version', array('name'=>$modname));
$version = $version === false ? null : $version;
$version = ($version === false) ? null : $version;
} catch (Exception $ignored) {
}
} else if (strpos('blocks/', $plugin) === 0) {
} else if (strpos($plugin, 'block/') === 0) {
try {
$blockname = substr($plugin, strlen('blocks/'));
$blockname = substr($plugin, strlen('block/'));
if ($block = $DB->get_record('block', array('name'=>$blockname))) {
$version = $block->version;
}
} catch (Exception $ignored) {
}
} else {
$pluginversion = get_config(str_replace('/', '_', $plugin), 'version');
if (!empty($pluginversion)) {
$version = $pluginversion;
}
}
$log = new object();
@ -567,11 +733,10 @@ function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
$log->backtrace = $backtrace;
$log->userid = $USER->id;
$log->timemodified = time();
try {
$DB->insert_record('upgrade_log', $log);
} catch (Exception $ignored) {
// possible during install or upgrade
// possible during install or 2.0 upgrade
}
}
@ -658,6 +823,57 @@ function print_upgrade_separator() {
}
}
/**
* Default start upgrade callback
* @param string $plugin
* @param bool $installation true if installation, false menas upgrade
*/
function print_upgrade_part_start($plugin, $installation) {
if (empty($plugin) or $plugin == 'moodle') {
upgrade_started($installation); // does not store upgrade running flag yet
print_heading(get_string('coresystem'));
} else {
upgrade_started();
print_heading($plugin);
}
if ($installation) {
if (empty($plugin) or $plugin == 'moodle') {
// no need to log - log table not yet there ;-)
} else {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
}
} else {
if (empty($plugin) or $plugin == 'moodle') {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
} else {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
}
}
}
/**
* Default end upgrade callback
* @param string $plugin
* @param bool $installation true if installation, false menas upgrade
*/
function print_upgrade_part_end($plugin, $installation) {
upgrade_started();
if ($installation) {
if (empty($plugin) or $plugin == 'moodle') {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
} else {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
}
} else {
if (empty($plugin) or $plugin == 'moodle') {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
} else {
upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
}
}
notify(get_string('success'), 'notifysuccess');
print_upgrade_separator();
}
function upgrade_get_javascript() {
global $CFG;

View File

@ -1023,7 +1023,7 @@ function popup_form($baseurl, $options, $formid, $selected='', $nothing='choose'
'.location=document.getElementById(\''.$formid.
'\').jump.options[document.getElementById(\''.
$formid.'\').jump.selectedIndex].value;"';
}
}
$output .= '<div style="white-space:nowrap">'.$selectlabel.$button.'<select id="'.$formid.'_jump" name="jump"'.$javascript.'>'."\n";
@ -1229,7 +1229,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
global $CFG, $COURSE, $DB;
static $croncache = array();
$hashstr = '';
if ($text === '') {
@ -1285,7 +1285,7 @@ function format_text($text, $format=FORMAT_MOODLE, $options=NULL, $courseid=NULL
// $hashstr .= time();
$time = time() - $CFG->cachetext;
$md5key = md5($hashstr);
$md5key = md5($hashstr);
if (CLI_SCRIPT) {
if (isset($croncache[$md5key])) {
return $croncache[$md5key];
@ -6954,6 +6954,14 @@ function debugging($message='', $level=DEBUG_NORMAL, $backtrace=null) {
function print_backtrace($callers, $return=false) {
global $CFG;
if (empty($callers)) {
if ($return) {
return '';
} else {
return;
}
}
$from = '<ul style="text-align: left">';
foreach ($callers as $caller) {
if (!isset($caller['line'])) {

View File

@ -2882,17 +2882,6 @@ function assignment_types() {
return $types;
}
/**
* Executes upgrade scripts for assignment types when necessary
*/
function assignment_upgrade_submodules() {
global $CFG;
/// Install/upgrade assignment types (it uses, simply, the standard plugin architecture)
upgrade_plugins('assignment_type', 'mod/assignment/type', "$CFG->wwwroot/$CFG->admin/index.php");
}
function assignment_print_overview($courses, &$htmlarray) {
global $USER, $CFG, $DB;