Commit 0280714c authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Basic install/upgrade system for local db customisations

parent 5434d19d
......@@ -31,9 +31,10 @@ define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
require(get_config('libroot') . 'upgrade.php');
require(get_config('docroot') . 'local/install.php');
$name = param_variable('name');
$install = ($name == 'firstcoredata' || $name == 'lastcoredata');
$install = ($name == 'firstcoredata' || $name == 'lastcoredata' || $name == 'localpreinst' || $name == 'localpostinst');
if (!$install) {
$upgrade = check_upgrades($name);
......@@ -47,15 +48,32 @@ $data = array(
);
if ($install) {
if (!get_config('installed')) {
try {
if ($name == 'localpreinst' || $name == 'localpostinst') {
$fun = $name;
$data['localdata'] = true;
}
else {
$fun = 'core_install_' . $name . '_defaults';
$data['coredata'] = true;
}
try {
$fun();
}
catch (SQLException $e) {
json_reply('local', array('error' => true, 'key' => $name, 'errormessage' => $e->getMessage()));
}
if ($name == 'localpostinst') {
// Update local version
$config = new StdClass;
require(get_config('docroot') . 'local/version.php');
set_config('localversion', $config->version);
set_config('localrelease', $config->release);
// Installation is finished
set_config('installed', true);
$USER->login('admin', 'mahara');
}
}
$data['coredata'] = true;
json_reply(false, $data);
}
......@@ -64,6 +82,9 @@ if (!empty($upgrade)) {
if ($name == 'core') {
$funname = 'upgrade_core';
}
else if ($name == 'local') {
$funname = 'upgrade_local';
}
else {
$funname = 'upgrade_plugin';
}
......
......@@ -65,12 +65,15 @@ $installsuccessstring = json_encode(get_string('installsuccess', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin'));
$coresuccess = json_encode(get_string('coredatasuccess', 'admin'));
$localsuccess = json_encode(get_string('localdatasuccess', 'admin'));
// Check if Mahara is being installed. An extra hook is required to insert core
// data if so.
if (!empty($upgrades['core']->install)) {
$upgrades['firstcoredata'] = true;
$upgrades['localpreinst'] = true;
$upgrades['lastcoredata'] = true;
$upgrades['localpostinst'] = true;
$smarty->assign('install', true);
}
......@@ -91,6 +94,9 @@ $js = <<< EOJS
if (data.coredata) {
message = {$coresuccess};
}
else if (data.localdata) {
message = {$localsuccess};
}
else {
if (data.install) {
message = {$installsuccessstring};
......
......@@ -41,6 +41,7 @@ $string['fromversion'] = 'From version';
$string['information'] = 'Information';
$string['installsuccess'] = 'Successfully installed version ';
$string['toversion'] = 'To version';
$string['localdatasuccess'] = 'Successfully installed local customisations';
$string['notinstalled'] = 'Not installed';
$string['nothingtoupgrade'] = 'Nothing to upgrade';
$string['performinginstallsandupgrades'] = 'Performing installs and upgrades...';
......
......@@ -99,6 +99,33 @@ function check_upgrades($name=null) {
return $toupgrade['core'];
}
if (!$installing && (empty($name) || $name == 'local')) {
$localversion = get_config('localversion');
$localrelease = get_config('localrelease');
if (is_null($localversion)) {
$localversion = 0;
$localrelease = 0;
}
$config = new StdClass;
require(get_config('docroot') . 'local/version.php');
if ($config->version > $localversion) {
$toupgrade['local'] = (object) array(
'upgrade' => true,
'from' => $localversion,
'fromrelease' => $localrelease,
'to' => $config->version,
'torelease' => $config->release,
);
}
if ($name == 'local') {
$toupgrade['local']->disablelogin = $disablelogin;
return $toupgrade['local'];
}
}
$plugins = array();
if (!empty($name)) {
try {
......@@ -280,6 +307,26 @@ function upgrade_core($upgrade) {
return true;
}
/**
* Upgrades local customisations.
*
* @param object $upgrade The version to upgrade to
* @return bool Whether the upgrade succeeded or not
* @throws SQLException If the upgrade failed due to a database error
*/
function upgrade_local($upgrade) {
db_begin();
require_once(get_config('docroot') . 'local/upgrade.php');
xmldb_local_upgrade($upgrade->from);
set_config('localversion', $upgrade->to);
set_config('localrelease', $upgrade->torelease);
db_commit();
return true;
}
/**
* Upgrades the plugin to a new version
*
......@@ -592,11 +639,9 @@ function core_install_lastcoredata_defaults() {
set_profile_field($user->id, 'email', $user->email);
set_profile_field($user->id, 'firstname', $user->firstname);
set_profile_field($user->id, 'lastname', $user->lastname);
set_config('installed', true);
handle_event('createuser', $user->id);
activity_add_admin_defaults(array($user->id));
db_commit();
$USER->login('admin', 'mahara');
// if we're installing, set up the block categories here and then poll the plugins.
// if we're upgrading this happens somewhere else. This is because of dependency issues around
......@@ -825,6 +870,18 @@ function sort_upgrades($k1, $k2) {
else if ($k2 == 'firstcoredata') {
return 1;
}
else if ($k1 == 'localpreinst') {
return -1;
}
else if ($k2 == 'localpreinst') {
return 1;
}
else if ($k1 == 'localpostinst') {
return 1;
}
else if ($k2 == 'localpostinst') {
return -1;
}
else if ($k1 == 'lastcoredata') {
return 1;
}
......
<?php
/**
* Pre- and post-install hooks for local database customisations.
*/
function localpreinst() {
}
function localpostinst() {
}
?>
\ No newline at end of file
<?php
/**
* Upgrades for local customisations.
*/
defined('INTERNAL') || die();
function xmldb_local_upgrade($oldversion=0) {
}
?>
<?php
/**
* Version number for local customisations.
*/
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 0;
$config->release = '0.0.0';
?>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment