summaryrefslogtreecommitdiff
path: root/inc/pluginutils.php
diff options
context:
space:
mode:
authorChris Smith <chris.eureka@jalakai.co.uk>2009-01-18 20:01:43 +0100
committerChris Smith <chris.eureka@jalakai.co.uk>2009-01-18 20:01:43 +0100
commit10e43949456b8da1c4514f0eb674c306139df05b (patch)
tree695afffd75a7b6e13bf81f5d342043c71e266a18 /inc/pluginutils.php
parentca2b464bb4f7cab9b83cd6e2508c6079e3f948cc (diff)
downloadrpg-10e43949456b8da1c4514f0eb674c306139df05b.tar.gz
rpg-10e43949456b8da1c4514f0eb674c306139df05b.tar.bz2
Major rework of pluginutils
This patch completely reworks pluginutils to: - reduce the number of file accesses to enumerate and load plugins - change the way disabled plugins are recorded. a disabled plugin will now have ".disabled" added to its directory name (this halves the number of file accesses required to enumerate installed plugins) - place the guts of pluginutils code inside a class, Doku_Plugin_Controller, the existing access routines are preserved and no changes are required. - add two globals, $plugin_controller_class & $plugin_controller this allows preload.php to define its own plugin controller class - update config plugin to support new plugin structure config plugin now issues a PLUGIN_CONFIG_PLUGINLIST event before it finalizes the list of plugins it will be working with. Handlers of this event can remove plugins from the list. - update plugin manager plugin to support new plugin structure plugin manager now issues a PLUGIN_PLUGINMANAGER_PLUGINLIST event similarly to config plugin. - plugin manager updated to redirect after changes to plugins and to use msg() Finally, this patch contains a one-shot action plugin which will automatically convert a plugins directory from the old style disabled file to the new style. Note for darcs users, the new disabled format will mean a couple of old oneshot plugins, importoldchangelog and importoldindex, will have their directory names changed, which could lead to darcs wanting to record the change. darcs-hash:20090118190143-f07c6-d2e79af546a49a4af5817dd0c5cc27066e67c4d0.gz
Diffstat (limited to 'inc/pluginutils.php')
-rw-r--r--inc/pluginutils.php221
1 files changed, 151 insertions, 70 deletions
diff --git a/inc/pluginutils.php b/inc/pluginutils.php
index 100f781f2..61a2939d3 100644
--- a/inc/pluginutils.php
+++ b/inc/pluginutils.php
@@ -8,93 +8,174 @@
// plugin related constants
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
+
$plugin_types = array('admin','syntax','action','renderer', 'helper');
+global $plugin_controller_class, $plugin_controller;
+if (empty($plugin_controller_class)) $plugin_controller_class = 'Doku_Plugin_Controller';
+
+$plugin_controller = new $plugin_controller_class();
+
/**
- * Returns a list of available plugins of given type
- *
- * @param $type string, plugin_type name;
- * the type of plugin to return,
- * use empty string for all types
- * @param $all bool;
- * false to only return enabled plugins,
- * true to return both enabled and disabled plugins
- *
- * @return array of plugin names
- *
- * @author Andreas Gohr <andi@splitbrain.org>
+ * Original plugin functions, remain for backwards compatibility
*/
-function plugin_list($type='',$all=false){
- $plugins = array();
- if ($dh = opendir(DOKU_PLUGIN)) {
- while (false !== ($plugin = readdir($dh))) {
- if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
- if (is_file(DOKU_PLUGIN.$plugin)) continue;
-
- // if required, skip disabled plugins
- if (!$all && plugin_isdisabled($plugin)) continue;
-
- if ($type=='' || @file_exists(DOKU_PLUGIN."$plugin/$type.php")){
- $plugins[] = $plugin;
+function plugin_list($type='',$all=false) { global $plugin_controller; return $plugin_controller->getList($type,$all); }
+function &plugin_load($type,$name,$new=false) { global $plugin_controller; return $plugin_controller->load($type,$name,$new); }
+function plugin_isdisabled($plugin) { global $plugin_controller; return $plugin_controller->isdisabled($plugin); }
+function plugin_enable($plugin) { global $plugin_controller; return $plugin_controller->enable($plugin); }
+function plugin_disable($plugin) { global $plugin_controller; return $plugin_controller->disable($plugin); }
+function plugin_directory($plugin) { global $plugin_controller; return $plugin_controller->get_directory($plugin); }
+
+class Doku_Plugin_Controller {
+
+ var $list_enabled = array();
+ var $list_disabled = array();
+ var $list_bytype = array();
+
+ function Doku_Plugin_Controller() {
+ $this->_populateMasterList();
+ }
+
+ /**
+ * Returns a list of available plugins of given type
+ *
+ * @param $type string, plugin_type name;
+ * the type of plugin to return,
+ * use empty string for all types
+ * @param $all bool;
+ * false to only return enabled plugins,
+ * true to return both enabled and disabled plugins
+ *
+ * @return array of plugin names
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+ function getList($type='',$all=false){
+
+ // request the complete list
+ if (!$type) {
+ return $all ? array_merge($this->list_enabled,$this->list_disabled) : $this->list_enabled;
+ }
+
+ if (!isset($this->list_bytype[$type]['enabled'])) {
+ $this->list_bytype[$type]['enabled'] = $this->_getListByType($type,true);
+ }
+ if ($all && !isset($this->list_bytype[$type]['disabled'])) {
+ $this->list_bytype[$type]['disabled'] = $this->_getListByType($type,false);
+ }
+
+ return $all ? array_merge($this->list_bytype[$type]['enabled'],$this->list_bytype[$type]['disabled']) : $this->list_bytype[$type]['enabled'];
+ }
+
+ /**
+ * Loads the given plugin and creates an object of it
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ *
+ * @param $type string type of plugin to load
+ * @param $name string name of the plugin to load
+ * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
+ * @return objectreference the plugin object or null on failure
+ */
+ function &load($type,$name,$new=false){
+ //we keep all loaded plugins available in global scope for reuse
+ global $DOKU_PLUGINS;
+
+ //plugin already loaded?
+ if(!empty($DOKU_PLUGINS[$type][$name])){
+ if ($new) {
+ $class = $type.'_plugin_'.$name;
+ return class_exists($class) ? new $class : null;
+ } else {
+ return $DOKU_PLUGINS[$type][$name];
+ }
+ }
+
+ //try to load the wanted plugin file
+ list($plugin,$component) = $this->_splitName($name);
+ $dir = !$this->isdisabled($plugin) ? $plugin : $plugin.'.disabled';
+ $file = $component ? "$type/$component.php" : "$type.php";
+
+ if (!include_once(DOKU_PLUGIN."$dir/$file")) {
+ return null;
+ }
+
+ //construct class and instantiate
+ $class = $type.'_plugin_'.$name;
+ if (!class_exists($class)) return null;
+
+ $DOKU_PLUGINS[$type][$name] = new $class;
+ return $DOKU_PLUGINS[$type][$name];
+ }
+
+ function isdisabled($plugin) {
+ return (array_search($plugin, $this->list_enabled) === false);
+ }
+
+ function enable($plugin) {
+ if (array_search($plugin, $this->list_disabled) !== false) {
+ return @rename(DOKU_PLUGIN.$plugin.'.disabled',DOKU_PLUGIN.$plugin);
+ }
+ return false;
+ }
+
+ function disable($plugin) {
+ if (array_search($plugin, $this->list_enabled) !== false) {
+ return @rename(DOKU_PLUGIN.$plugin,DOKU_PLUGIN.$plugin.'.disabled');
+ }
+ return false;
+ }
+
+ function get_directory($plugin) {
+ return $this->isdisabled($plugin) ? $plugin.'.disabled' : $plugin;
+ }
+
+ function _populateMasterList() {
+ if ($dh = opendir(DOKU_PLUGIN)) {
+ while (false !== ($plugin = readdir($dh))) {
+ if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
+ if (is_file(DOKU_PLUGIN.$plugin)) continue;
+
+ if (substr($plugin,-9) == '.disabled') {
+ $this->list_disabled[] = substr($plugin,0,-9);
+ } else {
+ $this->list_enabled[] = $plugin;
+ }
+ }
+ }
+ }
+
+ function _getListByType($type, $enabled) {
+ $master_list = $enabled ? $this->list_enabled : $this->list_disabled;
+
+ $plugins = array();
+ foreach ($master_list as $plugin) {
+ $dir = $enabled ? $plugin : $plugin.'.disabled';
+
+ if (@file_exists(DOKU_PLUGIN."$dir/$type.php")){
+ $plugins[] = $plugin;
} else {
- if ($dp = @opendir(DOKU_PLUGIN."$plugin/$type/")) {
+ if ($dp = @opendir(DOKU_PLUGIN."$dir/$type/")) {
while (false !== ($component = readdir($dp))) {
if (substr($component,0,1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
- if (is_file(DOKU_PLUGIN."$plugin/$type/$component")) {
- $plugins[] = $plugin.'_'.substr($component, 0, -4);
+ if (is_file(DOKU_PLUGIN."$dir/$type/$component")) {
+ $plugins[] = $plugin.'_'.substr($component, 0, -4);
}
}
closedir($dp);
}
}
}
- closedir($dh);
- }
- return $plugins;
-}
-/**
- * Loads the given plugin and creates an object of it
- *
- * @author Andreas Gohr <andi@splitbrain.org>
- *
- * @param $type string type of plugin to load
- * @param $name string name of the plugin to load
- * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
- * @return objectreference the plugin object or null on failure
- */
-function &plugin_load($type,$name,$new=false){
- //we keep all loaded plugins available in global scope for reuse
- global $DOKU_PLUGINS;
-
- //plugin already loaded?
- if(!empty($DOKU_PLUGINS[$type][$name])){
- if ($new) {
- $class = $type.'_plugin_'.$name;
- return class_exists($class) ? new $class : null;
- } else {
- return $DOKU_PLUGINS[$type][$name];
- }
+ return $plugins;
}
- //try to load the wanted plugin file
- if (@file_exists(DOKU_PLUGIN."$name/$type.php")){
- include_once(DOKU_PLUGIN."$name/$type.php");
- }else{
- list($plugin, $component) = preg_split("/_/",$name, 2);
- if (!$component || !include_once(DOKU_PLUGIN."$plugin/$type/$component.php")) {
- return null;
+ function _splitName($name) {
+ if (array_search($name, $this->list_enabled + $this->list_disabled) === false) {
+ return explode('_',$name,2);
}
- }
- //construct class and instantiate
- $class = $type.'_plugin_'.$name;
- if (!class_exists($class)) return null;
+ return array($name,'');
+ }
- $DOKU_PLUGINS[$type][$name] = new $class;
- return $DOKU_PLUGINS[$type][$name];
}
-
-function plugin_isdisabled($name) { return @file_exists(DOKU_PLUGIN.$name.'/disabled'); }
-function plugin_enable($name) { return @unlink(DOKU_PLUGIN.$name.'/disabled'); }
-function plugin_disable($name) { return @touch(DOKU_PLUGIN.$name.'/disabled'); }