Commit bb3ccb41 authored by Penny Leach's avatar Penny Leach Committed by Nigel McNie
Browse files

Blocktype categories now get installed at appropriate places.

Fixed up some of the admin/extensions/plugins.php stuff to do with
artefacts & blocktypes and their interdependencies.
(there's still a todo in there but it's cosmetic).
parent ff99d749
......@@ -34,7 +34,11 @@ define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'plugins');
require('upgrade.php');
// @TODO when artefact plugins get installed, move the not installed blocktypes
// that get installed into the list of installed blocktype plugins
$plugins = array();
$plugins['blocktype'] = array();
foreach (plugin_types() as $plugin) {
// this has to happen first because of broken artefact/blocktype ordering
......@@ -42,12 +46,12 @@ foreach (plugin_types() as $plugin) {
$plugins[$plugin]['installed'] = array();
$plugins[$plugin]['notinstalled'] = array();
}
foreach (plugin_types() as $plugin) {
foreach (array_keys($plugins) as $plugin) {
if ($installed = get_records_array($plugin . '_installed')) {
foreach ($installed as $i) {
$key = $i->name;
if ($plugin == 'blocktype' && !empty($i->artefactplugin)) {
$key = $i->artefactplugin . '/' . $i->name;
if ($plugin == 'blocktype') {
$key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
}
$plugins[$plugin]['installed'][$key] = array();
if ($plugin == 'artefact') {
......@@ -78,6 +82,7 @@ foreach (plugin_types() as $plugin) {
$dirhandle = opendir(get_config('docroot') . $plugin);
while (false !== ($dir = readdir($dirhandle))) {
$installed = false; // reinitialise
if (strpos($dir, '.') === 0) {
continue;
}
......@@ -87,6 +92,8 @@ foreach (plugin_types() as $plugin) {
if (array_key_exists($dir, $plugins[$plugin]['installed'])) {
$installed = true;
}
// if we're already installed keep going
// if we're an artefact plugin, we have to check for blocktypes.
if ($plugin != 'artefact' && empty($installed)) {
continue;
}
......@@ -115,6 +122,9 @@ foreach (plugin_types() as $plugin) {
}
if (!array_key_exists($dir . '/' . $btdir, $plugins['blocktype']['installed'])) {
try {
if (!array_key_exists($dir, $plugins['artefact']['installed'])) {
throw new InstallationException(get_string('blocktypeprovidedbyartefactnotinstallable', 'error', $dir));
}
validate_plugin('blocktype', $dir . '/' . $btdir,
get_config('docroot') . 'artefact/' . $dir . '/blocktype/' . $btdir);
$plugins['blocktype']['notinstalled'][$dir . '/' . $btdir] = array();
......
......@@ -66,6 +66,7 @@ $string['blocktypelibmissing'] = 'Missing lib.php for block %s in artefact plugi
$string['blocktypemissingconfigform'] = 'Block type %s must implement config_form';
$string['blocktypemissingconfigsave'] = 'Block type %s must implement config_save';
$string['versionphpmissing'] = 'Plugin %s %s is missing version.php!';
$string['blocktypeprovidedbyartefactnotinstallable'] = 'This will be installed as part of the installation of artefact plugin %s';
// if you change these next two , be sure to change them in libroot/errors.php
// as they are duplicated there, in the case that get_string was not available.
......
......@@ -809,6 +809,7 @@ function safe_require($plugintype, $pluginname, $filename='lib.php', $function='
function plugin_types() {
static $pluginstocheck;
if (empty($pluginstocheck)) {
// ORDER MATTERS! artefact has to be first!
$pluginstocheck = array('artefact', 'auth', 'notification', 'search', 'blocktype');
}
return $pluginstocheck;
......@@ -837,6 +838,9 @@ function call_static_method($class, $method) {
function generate_class_name() {
$args = func_get_args();
if (count($args) == 2 && $args[0] == 'blocktype') {
return 'PluginBlockType' . ucfirst(blocktype_namespaced_to_single($args[1]));
}
return 'Plugin' . implode('', array_map('ucfirst', $args));
}
......@@ -844,10 +848,21 @@ function generate_artefact_class_name($type) {
return 'ArtefactType' . ucfirst($type);
}
function generate_blocktype_class_name($type) {
return 'BlockType' . ucfirst($type);
function blocktype_namespaced_to_single($blocktype) {
if (strpos($blocktype, '/') === false) { // system blocktype
return $blocktype;
}
return substr($blocktype, strpos($blocktype, '/') + 1 );
}
function blocktype_single_to_namespaced($blocktype, $artefact='') {
if (empty($artefact)) {
return $blocktype;
}
return $artefact . '/' . $blocktype;
}
/**
* Fires an event which can be handled by different parts of the system
*/
......
......@@ -128,7 +128,7 @@ function check_upgrades($name=null) {
}
if ($plugin == 'artefact') { // go check it for blocks as well
$btlocation = get_config('docroot') . $plugin . '/blocktype';
$btlocation = get_config('docroot') . $plugin . '/' . $dir . '/blocktype';
if (!is_dir($btlocation)) {
continue;
}
......@@ -137,7 +137,7 @@ function check_upgrades($name=null) {
if (strpos($btdir, '.') === 0) {
continue;
}
if (!is_dir(get_config('docroot') . $plugin . '/blocktype/' . $btdir)) {
if (!is_dir(get_config('docroot') . $plugin . '/' . $dir . '/blocktype/' . $btdir)) {
continue;
}
$plugins[] = array('blocktype', $dir . '/' . $btdir);
......@@ -424,10 +424,17 @@ function upgrade_plugin($upgrade) {
delete_records_select('artefact_installed_type', $select,
array_merge(array($pluginname),$types));
}
//@todo install blocks here too.
// install a blocktype category for this plugin
if (get_config('installed') && !record_exists('blocktype_category', 'name', $pluginname)) {
insert_record('blocktype_category', (object)array('name' => $pluginname));
}
}
// install blocktype categories.
if ($plugintype == 'blocktype' && get_config('installed')) {
install_blocktype_categories_for_plugin($pluginname);
}
$prevversion = (empty($upgrade->install)) ? $upgrade->from : 0;
call_static_method($pcname, 'postinst', $prevversion);
......@@ -435,6 +442,17 @@ function upgrade_plugin($upgrade) {
$status = false;
}
$db->CompleteTrans();
// we have to do this after committing the current transaction because we call ourselves recursively...
if ($plugintype == 'artefact' && get_config('installed')) {
// only install associated blocktype plugins if we're not in the process of installing
if ($blocktypes = call_static_method($pcname, 'get_block_types')) {
foreach ($blocktypes as $bt) {
$upgrade = check_upgrades('blocktype.' . $pluginname . '/' . $bt);
upgrade_plugin($upgrade);
}
}
}
return $status;
......@@ -543,6 +561,13 @@ function core_install_lastcoredata_defaults() {
set_profile_field($user->id, 'lastname', $user->lastname);
set_config('installed', true);
db_commit();
// 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
// the order of installation stuff.
install_blocktype_categories();
}
function core_install_firstcoredata_defaults() {
......@@ -730,6 +755,62 @@ function sort_upgrades($k1, $k2) {
else if ($k2 == 'lastcoredata') {
return -1;
}
return 0;
// else obey the order plugin types returns (strip off plugintype. from the start)
$weight1 = array_search(substr($k1, 0, strpos($k1, '.')), plugin_types());
$weight2 = array_search(substr($k2, 0, strpos($k2, '.')), plugin_types());
return ($weight1 > $weight2);
}
/** core blocktype categories the system exports
* (eg not tied to artefact plugins)
*/
function get_core_blocktype_categories() {
return array();
}
function install_blocktype_categories_for_plugin($blocktype) {
safe_require('blocktype', $blocktype);
$blocktype = blocktype_namespaced_to_single($blocktype);
db_begin();
delete_records('blocktype_installed_category', 'blocktype', $blocktype);
if ($cats = call_static_method(generate_class_name('blocktype', $blocktype), 'get_categories')) {
foreach ($cats as $cat) {
insert_record('blocktype_installed_category', (object)array(
'blocktype' => $blocktype,
'category' => $cat
));
}
}
db_commit();
}
function install_blocktype_categories() {
db_begin();
if ($artefacts = plugins_installed('artefact')) {
$artefacts = array_map(create_function('$a', 'return $a->name;'), $artefacts);
}
else {
$artefacts = array();
}
$categories = array_merge(get_core_blocktype_categories(), $artefacts);
$installedcategories = get_column('blocktype_category', 'name');
if ($toinstall = array_diff($categories, $installedcategories)) {
foreach ($toinstall as $i) {
insert_record('blocktype_category', (object)array('name' => $i));
}
}
db_commit();
// poll all the installed blocktype plugins and ask them what categories they export
if ($blocktypes = plugins_installed('blocktype')) {
foreach ($blocktypes as $bt) {
install_blocktype_categories_for_plugin(blocktype_single_to_namespaced($bt->name, $bt->artefactplugin));
}
}
}
?>
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