first commit. dokuwiki.
[h2N7SspZmY.git] / inc / plugincontroller.class.php
blob4400a41870c7ef6f55afdb9ea49d1ea2accf9146
1 <?php
2 /**
3 * Class to encapsulate access to dokuwiki plugins
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Christopher Smith <chris@jalakai.co.uk>
7 */
9 // plugin related constants
10 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12 class Doku_Plugin_Controller {
14 var $list_enabled = array();
15 var $list_disabled = array();
16 var $list_bytype = array();
18 function Doku_Plugin_Controller() {
19 $this->_populateMasterList();
22 /**
23 * Returns a list of available plugins of given type
25 * @param $type string, plugin_type name;
26 * the type of plugin to return,
27 * use empty string for all types
28 * @param $all bool;
29 * false to only return enabled plugins,
30 * true to return both enabled and disabled plugins
32 * @return array of plugin names
34 * @author Andreas Gohr <andi@splitbrain.org>
36 function getList($type='',$all=false){
38 // request the complete list
39 if (!$type) {
40 return $all ? array_merge($this->list_enabled,$this->list_disabled) : $this->list_enabled;
43 if (!isset($this->list_bytype[$type]['enabled'])) {
44 $this->list_bytype[$type]['enabled'] = $this->_getListByType($type,true);
46 if ($all && !isset($this->list_bytype[$type]['disabled'])) {
47 $this->list_bytype[$type]['disabled'] = $this->_getListByType($type,false);
50 return $all ? array_merge($this->list_bytype[$type]['enabled'],$this->list_bytype[$type]['disabled']) : $this->list_bytype[$type]['enabled'];
53 /**
54 * Loads the given plugin and creates an object of it
56 * @author Andreas Gohr <andi@splitbrain.org>
58 * @param $type string type of plugin to load
59 * @param $name string name of the plugin to load
60 * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
61 * @return objectreference the plugin object or null on failure
63 function &load($type,$name,$new=false){
64 //we keep all loaded plugins available in global scope for reuse
65 global $DOKU_PLUGINS;
67 //plugin already loaded?
68 if(!empty($DOKU_PLUGINS[$type][$name])){
69 if ($new && !$DOKU_PLUGINS[$type][$name]->isSingleton()) {
70 $class = $type.'_plugin_'.$name;
71 return class_exists($class) ? new $class : null;
72 } else {
73 return $DOKU_PLUGINS[$type][$name];
77 //try to load the wanted plugin file
78 list($plugin,$component) = $this->_splitName($name);
79 $dir = $this->get_directory($plugin);
80 $file = $component ? "$type/$component.php" : "$type.php";
82 if(!is_file(DOKU_PLUGIN."$dir/$file")){
83 return null;
86 if (!include_once(DOKU_PLUGIN."$dir/$file")) {
87 return null;
90 //construct class and instantiate
91 $class = $type.'_plugin_'.$name;
92 if (!class_exists($class)) return null;
94 $DOKU_PLUGINS[$type][$name] = new $class;
95 return $DOKU_PLUGINS[$type][$name];
98 function isdisabled($plugin) {
99 return (array_search($plugin, $this->list_enabled) === false);
102 function enable($plugin) {
103 if (array_search($plugin, $this->list_disabled) !== false) {
104 return @unlink(DOKU_PLUGIN.$plugin.'/disabled');
106 return false;
109 function disable($plugin) {
110 if (array_search($plugin, $this->list_enabled) !== false) {
111 return @touch(DOKU_PLUGIN.$plugin.'/disabled');
113 return false;
116 function get_directory($plugin) {
117 return $plugin;
120 function _populateMasterList() {
121 if ($dh = opendir(DOKU_PLUGIN)) {
122 while (false !== ($plugin = readdir($dh))) {
123 if ($plugin == '.' || $plugin == '..' || $plugin == 'tmp') continue;
124 if (is_file(DOKU_PLUGIN.$plugin)) continue;
126 if (substr($plugin,-9) == '.disabled') {
127 // the plugin was disabled by rc2009-01-26
128 // disabling mechanism was changed back very soon again
129 // to keep everything simple we just skip the plugin completely
130 }elseif(@file_exists(DOKU_PLUGIN.$plugin.'/disabled')){
131 $this->list_disabled[] = $plugin;
132 } else {
133 $this->list_enabled[] = $plugin;
139 function _getListByType($type, $enabled) {
140 $master_list = $enabled ? $this->list_enabled : $this->list_disabled;
142 $plugins = array();
143 foreach ($master_list as $plugin) {
144 $dir = $this->get_directory($plugin);
146 if (@file_exists(DOKU_PLUGIN."$dir/$type.php")){
147 $plugins[] = $plugin;
148 } else {
149 if ($dp = @opendir(DOKU_PLUGIN."$dir/$type/")) {
150 while (false !== ($component = readdir($dp))) {
151 if (substr($component,0,1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
152 if (is_file(DOKU_PLUGIN."$dir/$type/$component")) {
153 $plugins[] = $plugin.'_'.substr($component, 0, -4);
156 closedir($dp);
161 return $plugins;
164 function _splitName($name) {
165 if (array_search($name, $this->list_enabled + $this->list_disabled) === false) {
166 return explode('_',$name,2);
169 return array($name,'');