summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2010-12-24 23:43:10 -0700
committerLuke Shumaker <LukeShu@sbcglobal.net>2010-12-24 23:43:10 -0700
commit37c03d68a82fb978d46d3a9e7c9051983de38b3d (patch)
tree0aa9066fb5f0f68e3c48ec28ba3db5acb329846f
parent26baad63f2e7d5cd0f783aacdb9eb2a65a638656 (diff)
Add a more robust plugin config system.
2010-11-20: Luke Shumaker <lukeshu@sbcglobal.net> Add a more robust (but backward compatible) plugin config system * lib/util.php: add common_config_section($main), as a companion to common_config($main,$sub) * lib/statusnet.php: Functions for other Places: - add public static pluginFiles($name) which returns a list of all possible filenames a plugin with $name could be defined in. - addPlugin(...): use self::pluginFiles(...) instead of a hard-coded list. Actual Functionality: - add public static getPlugins() which returns array_merge( common_config('plugins','default'), common_config_section('plugin-list') ) - use self::getPlugins() instead of common_config('plugins','default') Robustness: - handle plugins that have a type other than "array" or "null" for parameters without bugging out * actions/pluginenable.php: (in order of in the file): - use StatusNet::getPlugins() instead of common_config('plugins','default') - check if a plugin exists, not whether it is loaded (uses newly added StatusNet::pluginFiles(...)) - Also save to 'plugin-list' (the new plugin system), in addition to 'plugins' (the old plugin system)
-rw-r--r--actions/pluginenable.php11
-rw-r--r--actions/pluginsadminpanel.php2
-rw-r--r--lib/statusnet.php53
-rw-r--r--lib/util.php6
4 files changed, 58 insertions, 14 deletions
diff --git a/actions/pluginenable.php b/actions/pluginenable.php
index 2dbb3e395..4662f4368 100644
--- a/actions/pluginenable.php
+++ b/actions/pluginenable.php
@@ -104,8 +104,14 @@ class PluginEnableAction extends Action
}
$this->plugin = $this->arg('plugin');
- $defaultPlugins = common_config('plugins', 'default');
- if (!array_key_exists($this->plugin, $defaultPlugins)) {
+ $files = StatusNet::pluginFiles($this->plugin);
+ $found = false;
+ foreach ($files as $file) {
+ if (file_exists($file)) {
+ $found = true;
+ }
+ }
+ if (!$found) {
$this->clientError(_('No such plugin.'));
return false;
}
@@ -127,6 +133,7 @@ class PluginEnableAction extends Action
{
$key = 'disable-' . $this->plugin;
Config::save('plugins', $key, $this->overrideValue());
+ Config::save('plugin-list', $this->plugin, 1);
// @fixme this is a pretty common pattern and should be refactored down
if ($this->boolean('ajax')) {
diff --git a/actions/pluginsadminpanel.php b/actions/pluginsadminpanel.php
index bc400bd51..c1f4fe253 100644
--- a/actions/pluginsadminpanel.php
+++ b/actions/pluginsadminpanel.php
@@ -95,7 +95,7 @@ class PluginsadminpanelAction extends AdminPanelAction
*/
protected function showDefaultPlugins()
{
- $plugins = array_keys(common_config('plugins', 'default'));
+ $plugins = array_keys(StatusNet::getPlugins());
natsort($plugins);
if ($plugins) {
diff --git a/lib/statusnet.php b/lib/statusnet.php
index 4c2aacd8f..099894455 100644
--- a/lib/statusnet.php
+++ b/lib/statusnet.php
@@ -34,6 +34,25 @@ class StatusNet
protected static $plugins = array();
/**
+ * Return a list of possible filenames that plugin $name can be in.
+ *
+ * @param string $name class name & plugin file/subdir name
+ */
+ public static function pluginFiles($name) {
+ $name = ucfirst($name);
+ $pluginclass = "{$name}Plugin";
+
+ $files = array("local/plugins/{$pluginclass}.php",
+ "local/plugins/{$name}/{$pluginclass}.php",
+ "local/{$pluginclass}.php",
+ "local/{$name}/{$pluginclass}.php",
+ "plugins/{$pluginclass}.php",
+ "plugins/{$name}/{$pluginclass}.php");
+
+ return $files;
+ }
+
+ /**
* Configure and instantiate a plugin into the current configuration.
* Class definitions will be loaded from standard paths if necessary.
* Note that initialization events won't be fired until later.
@@ -50,12 +69,7 @@ class StatusNet
if (!class_exists($pluginclass)) {
- $files = array("local/plugins/{$pluginclass}.php",
- "local/plugins/{$name}/{$pluginclass}.php",
- "local/{$pluginclass}.php",
- "local/{$name}/{$pluginclass}.php",
- "plugins/{$pluginclass}.php",
- "plugins/{$name}/{$pluginclass}.php");
+ $files = self::pluginFiles($name);
foreach ($files as $file) {
$fullpath = INSTALLDIR.'/'.$file;
@@ -168,22 +182,37 @@ class StatusNet
}
return $sites;
}
+ /**
+ * Return a list of plugins that are currently in the system.
+ *
+ * This means default plugins, plugins that are explicitly enabled, or
+ * plugins that are explicitly disabled.
+ */
+
+ public static function getPlugins() {
+ // Default plugins
+ $pluginlist = common_config('plugins', 'default');
+ // Enabled plugins
+ $enabled=common_config_section('plugin-list');
+ if ($enabled) {
+ $pluginlist = array_merge($pluginlist,$enabled);
+ }
+ return $pluginlist;
+ }
/**
* Fire initialization events for all instantiated plugins.
*/
protected static function initPlugins()
{
- // Load default plugins
- foreach (common_config('plugins', 'default') as $name => $params) {
+ // Load plugins
+ foreach (self::getPlugins() as $name => $params) {
$key = 'disable-' . $name;
if (common_config('plugins', $key)) {
continue;
}
- if (is_null($params)) {
- addPlugin($name);
- } else if (is_array($params)) {
+ if (is_array($params)) {
if (count($params) == 0) {
addPlugin($name);
} else {
@@ -196,6 +225,8 @@ class StatusNet
}
}
}
+ } else {
+ addPlugin($name);
}
}
diff --git a/lib/util.php b/lib/util.php
index 3d4adcf4b..379516529 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -1857,6 +1857,12 @@ function common_config($main, $sub)
array_key_exists($sub, $config[$main])) ? $config[$main][$sub] : false;
}
+function common_config_section($main)
+{
+ global $config;
+ return array_key_exists($main, $config) ? $config[$main] : false;
+}
+
/**
* Pull arguments from a GET/POST/REQUEST array with first-level input checks:
* strips "magic quotes" slashes if necessary, and kills invalid UTF-8 strings.