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); ...@@ -31,9 +31,10 @@ define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php'); require(dirname(dirname(__FILE__)) . '/init.php');
require(get_config('libroot') . 'upgrade.php'); require(get_config('libroot') . 'upgrade.php');
require(get_config('docroot') . 'local/install.php');
$name = param_variable('name'); $name = param_variable('name');
$install = ($name == 'firstcoredata' || $name == 'lastcoredata'); $install = ($name == 'firstcoredata' || $name == 'lastcoredata' || $name == 'localpreinst' || $name == 'localpostinst');
if (!$install) { if (!$install) {
$upgrade = check_upgrades($name); $upgrade = check_upgrades($name);
...@@ -47,15 +48,32 @@ $data = array( ...@@ -47,15 +48,32 @@ $data = array(
); );
if ($install) { if ($install) {
if (!get_config('installed')) { if (!get_config('installed')) {
try { if ($name == 'localpreinst' || $name == 'localpostinst') {
$fun = $name;
$data['localdata'] = true;
}
else {
$fun = 'core_install_' . $name . '_defaults'; $fun = 'core_install_' . $name . '_defaults';
$data['coredata'] = true;
}
try {
$fun(); $fun();
} }
catch (SQLException $e) { catch (SQLException $e) {
json_reply('local', array('error' => true, 'key' => $name, 'errormessage' => $e->getMessage())); 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); json_reply(false, $data);
} }
...@@ -64,6 +82,9 @@ if (!empty($upgrade)) { ...@@ -64,6 +82,9 @@ if (!empty($upgrade)) {
if ($name == 'core') { if ($name == 'core') {
$funname = 'upgrade_core'; $funname = 'upgrade_core';
} }
else if ($name == 'local') {
$funname = 'upgrade_local';
}
else { else {
$funname = 'upgrade_plugin'; $funname = 'upgrade_plugin';
} }
......
...@@ -65,12 +65,15 @@ $installsuccessstring = json_encode(get_string('installsuccess', 'admin')); ...@@ -65,12 +65,15 @@ $installsuccessstring = json_encode(get_string('installsuccess', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin')); $successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin')); $failurestring = json_encode(get_string('upgradefailure', 'admin'));
$coresuccess = json_encode(get_string('coredatasuccess', '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 // Check if Mahara is being installed. An extra hook is required to insert core
// data if so. // data if so.
if (!empty($upgrades['core']->install)) { if (!empty($upgrades['core']->install)) {
$upgrades['firstcoredata'] = true; $upgrades['firstcoredata'] = true;
$upgrades['localpreinst'] = true;
$upgrades['lastcoredata'] = true; $upgrades['lastcoredata'] = true;
$upgrades['localpostinst'] = true;
$smarty->assign('install', true); $smarty->assign('install', true);
} }
...@@ -91,6 +94,9 @@ $js = <<< EOJS ...@@ -91,6 +94,9 @@ $js = <<< EOJS
if (data.coredata) { if (data.coredata) {
message = {$coresuccess}; message = {$coresuccess};
} }
else if (data.localdata) {
message = {$localsuccess};
}
else { else {
if (data.install) { if (data.install) {
message = {$installsuccessstring}; message = {$installsuccessstring};
......
...@@ -41,6 +41,7 @@ $string['fromversion'] = 'From version'; ...@@ -41,6 +41,7 @@ $string['fromversion'] = 'From version';
$string['information'] = 'Information'; $string['information'] = 'Information';
$string['installsuccess'] = 'Successfully installed version '; $string['installsuccess'] = 'Successfully installed version ';
$string['toversion'] = 'To version'; $string['toversion'] = 'To version';
$string['localdatasuccess'] = 'Successfully installed local customisations';
$string['notinstalled'] = 'Not installed'; $string['notinstalled'] = 'Not installed';
$string['nothingtoupgrade'] = 'Nothing to upgrade'; $string['nothingtoupgrade'] = 'Nothing to upgrade';
$string['performinginstallsandupgrades'] = 'Performing installs and upgrades...'; $string['performinginstallsandupgrades'] = 'Performing installs and upgrades...';
......
...@@ -99,6 +99,33 @@ function check_upgrades($name=null) { ...@@ -99,6 +99,33 @@ function check_upgrades($name=null) {
return $toupgrade['core']; 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(); $plugins = array();
if (!empty($name)) { if (!empty($name)) {
try { try {
...@@ -280,6 +307,26 @@ function upgrade_core($upgrade) { ...@@ -280,6 +307,26 @@ function upgrade_core($upgrade) {
return true; 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 * Upgrades the plugin to a new version
* *
...@@ -592,11 +639,9 @@ function core_install_lastcoredata_defaults() { ...@@ -592,11 +639,9 @@ function core_install_lastcoredata_defaults() {
set_profile_field($user->id, 'email', $user->email); set_profile_field($user->id, 'email', $user->email);
set_profile_field($user->id, 'firstname', $user->firstname); set_profile_field($user->id, 'firstname', $user->firstname);
set_profile_field($user->id, 'lastname', $user->lastname); set_profile_field($user->id, 'lastname', $user->lastname);
set_config('installed', true);
handle_event('createuser', $user->id); handle_event('createuser', $user->id);
activity_add_admin_defaults(array($user->id)); activity_add_admin_defaults(array($user->id));
db_commit(); db_commit();
$USER->login('admin', 'mahara');
// if we're installing, set up the block categories here and then poll the plugins. // 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 // if we're upgrading this happens somewhere else. This is because of dependency issues around
...@@ -825,6 +870,18 @@ function sort_upgrades($k1, $k2) { ...@@ -825,6 +870,18 @@ function sort_upgrades($k1, $k2) {
else if ($k2 == 'firstcoredata') { else if ($k2 == 'firstcoredata') {
return 1; 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') { else if ($k1 == 'lastcoredata') {
return 1; 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