3 namespace dokuwiki\Extension
;
5 use dokuwiki\ErrorHandler
;
8 * Class to encapsulate access to dokuwiki plugins
10 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author Christopher Smith <chris@jalakai.co.uk>
13 class PluginController
15 /** @var array the types of plugins DokuWiki supports */
16 public const PLUGIN_TYPES
= ['auth', 'admin', 'syntax', 'action', 'renderer', 'helper', 'remote', 'cli'];
18 protected $listByType = [];
19 /** @var array all installed plugins and their enabled state [plugin=>enabled] */
20 protected $masterList = [];
21 protected $pluginCascade = ['default' => [], 'local' => [], 'protected' => []];
22 protected $lastLocalConfigFile = '';
25 * Populates the master list of plugins
27 public function __construct()
30 $this->populateMasterList();
34 * Returns a list of available plugins of given type
36 * @param $type string, plugin_type name;
37 * the type of plugin to return,
38 * use empty string for all types
40 * false to only return enabled plugins,
41 * true to return both enabled and disabled plugins
44 * - plugin names when $type = ''
45 * - or plugin component names when a $type is given
47 * @author Andreas Gohr <andi@splitbrain.org>
49 public function getList($type = '', $all = false)
52 // request the complete list
54 return $all ?
array_keys($this->masterList
) : array_keys(array_filter($this->masterList
));
57 if (!isset($this->listByType
[$type]['enabled'])) {
58 $this->listByType
[$type]['enabled'] = $this->getListByType($type, true);
60 if ($all && !isset($this->listByType
[$type]['disabled'])) {
61 $this->listByType
[$type]['disabled'] = $this->getListByType($type, false);
65 ?
array_merge($this->listByType
[$type]['enabled'], $this->listByType
[$type]['disabled'])
66 : $this->listByType
[$type]['enabled'];
70 * Loads the given plugin and creates an object of it
72 * @param $type string type of plugin to load
73 * @param $name string name of the plugin to load
74 * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
75 * @param $disabled bool true to load even disabled plugins
76 * @return PluginInterface|null the plugin object or null on failure
77 * @author Andreas Gohr <andi@splitbrain.org>
80 public function load($type, $name, $new = false, $disabled = false)
83 //we keep all loaded plugins available in global scope for reuse
86 [$plugin, ] = $this->splitName($name);
89 if (!$disabled && !$this->isEnabled($plugin)) {
93 $class = $type . '_plugin_' . $name;
96 //plugin already loaded?
97 if (!empty($DOKU_PLUGINS[$type][$name])) {
98 if ($new ||
!$DOKU_PLUGINS[$type][$name]->isSingleton()) {
100 return class_exists($class, true) ?
new $class : null;
103 return $DOKU_PLUGINS[$type][$name];
106 //construct class and instantiate
107 if (!class_exists($class, true)) {
108 # the plugin might be in the wrong directory
109 $inf = confToHash(DOKU_PLUGIN
. "$plugin/plugin.info.txt");
110 if ($inf['base'] && $inf['base'] != $plugin) {
113 "Plugin installed incorrectly. Rename plugin directory '%s' to '%s'.",
120 } elseif (preg_match('/^' . DOKU_PLUGIN_NAME_REGEX
. '$/', $plugin) !== 1) {
122 'Plugin name \'%s\' is not a valid plugin name, only the characters a-z and 0-9 are allowed. ' .
123 'Maybe the plugin has been installed in the wrong directory?', hsc($plugin)
128 $DOKU_PLUGINS[$type][$name] = new $class;
130 } catch (\Throwable
$e) {
131 ErrorHandler
::showExceptionMsg($e, sprintf('Failed to load plugin %s', $plugin));
135 return $DOKU_PLUGINS[$type][$name];
139 * Whether plugin is disabled
141 * @param string $plugin name of plugin
142 * @return bool true disabled, false enabled
143 * @deprecated in favor of the more sensible isEnabled where the return value matches the enabled state
145 public function isDisabled($plugin)
147 dbg_deprecated('isEnabled()');
148 return !$this->isEnabled($plugin);
152 * Check whether plugin is disabled
154 * @param string $plugin name of plugin
155 * @return bool true enabled, false disabled
157 public function isEnabled($plugin)
159 return !empty($this->masterList
[$plugin]);
165 * @param string $plugin name of plugin
166 * @return bool true saving succeed, false saving failed
168 public function disable($plugin)
170 if (array_key_exists($plugin, $this->pluginCascade
['protected'])) return false;
171 $this->masterList
[$plugin] = 0;
172 return $this->saveList();
178 * @param string $plugin name of plugin
179 * @return bool true saving succeed, false saving failed
181 public function enable($plugin)
183 if (array_key_exists($plugin, $this->pluginCascade
['protected'])) return false;
184 $this->masterList
[$plugin] = 1;
185 return $this->saveList();
189 * Returns cascade of the config files
191 * @return array with arrays of plugin configs
193 public function getCascade()
195 return $this->pluginCascade
;
199 * Read all installed plugins and their current enabled state
201 protected function populateMasterList()
203 if ($dh = @opendir
(DOKU_PLUGIN
)) {
205 while (false !== ($plugin = readdir($dh))) {
206 if ($plugin[0] === '.') continue; // skip hidden entries
207 if (is_file(DOKU_PLUGIN
. $plugin)) continue; // skip files, we're only interested in directories
209 if (array_key_exists($plugin, $this->masterList
) && $this->masterList
[$plugin] == 0) {
210 $all_plugins[$plugin] = 0;
212 } elseif (array_key_exists($plugin, $this->masterList
) && $this->masterList
[$plugin] == 1) {
213 $all_plugins[$plugin] = 1;
215 $all_plugins[$plugin] = 1;
218 $this->masterList
= $all_plugins;
219 if (!file_exists($this->lastLocalConfigFile
)) {
220 $this->saveList(true);
226 * Includes the plugin config $files
227 * and returns the entries of the $plugins array set in these files
229 * @param array $files list of files to include, latter overrides previous
230 * @return array with entries of the $plugins arrays of the included files
232 protected function checkRequire($files)
235 foreach ($files as $file) {
236 if (file_exists($file)) {
244 * Save the current list of plugins
246 * @param bool $forceSave ;
247 * false to save only when config changed
248 * true to always save
249 * @return bool true saving succeed, false saving failed
251 protected function saveList($forceSave = false)
255 if (empty($this->masterList
)) return false;
257 // Rebuild list of local settings
258 $local_plugins = $this->rebuildLocal();
259 if ($local_plugins != $this->pluginCascade
['local'] ||
$forceSave) {
260 $file = $this->lastLocalConfigFile
;
261 $out = "<?php\n/*\n * Local plugin enable/disable settings\n" .
262 " * Auto-generated through plugin/extension manager\n *\n" .
263 " * NOTE: Plugins will not be added to this file unless there " .
264 "is a need to override a default setting. Plugins are\n" .
265 " * enabled by default.\n */\n";
266 foreach ($local_plugins as $plugin => $value) {
267 $out .= "\$plugins['$plugin'] = $value;\n";
269 // backup current file (remove any existing backup)
270 if (file_exists($file)) {
271 $backup = $file . '.bak';
272 if (file_exists($backup)) @unlink
($backup);
273 if (!@copy
($file, $backup)) return false;
274 if ($conf['fperm']) chmod($backup, $conf['fperm']);
276 //check if can open for writing, else restore
277 return io_saveFile($file, $out);
283 * Rebuild the set of local plugins
285 * @return array array of plugins to be saved in end($config_cascade['plugins']['local'])
287 protected function rebuildLocal()
289 //assign to local variable to avoid overwriting
290 $backup = $this->masterList
;
291 //Can't do anything about protected one so rule them out completely
292 $local_default = array_diff_key($backup, $this->pluginCascade
['protected']);
293 //Diff between local+default and default
294 //gives us the ones we need to check and save
295 $diffed_ones = array_diff_key($local_default, $this->pluginCascade
['default']);
296 //The ones which we are sure of (list of 0s not in default)
297 $sure_plugins = array_filter($diffed_ones, [$this, 'negate']);
298 //the ones in need of diff
299 $conflicts = array_diff_key($local_default, $diffed_ones);
301 return array_merge($sure_plugins, array_diff_assoc($conflicts, $this->pluginCascade
['default']));
305 * Build the list of plugins and cascade
308 protected function loadConfig()
310 global $config_cascade;
311 foreach (['default', 'protected'] as $type) {
312 if (array_key_exists($type, $config_cascade['plugins'])) {
313 $this->pluginCascade
[$type] = $this->checkRequire($config_cascade['plugins'][$type]);
316 $local = $config_cascade['plugins']['local'];
317 $this->lastLocalConfigFile
= array_pop($local);
318 $this->pluginCascade
['local'] = $this->checkRequire([$this->lastLocalConfigFile
]);
319 $this->pluginCascade
['default'] = array_merge(
320 $this->pluginCascade
['default'],
321 $this->checkRequire($local)
323 $this->masterList
= array_merge(
324 $this->pluginCascade
['default'],
325 $this->pluginCascade
['local'],
326 $this->pluginCascade
['protected']
331 * Returns a list of available plugin components of given type
333 * @param string $type plugin_type name; the type of plugin to return,
334 * @param bool $enabled true to return enabled plugins,
335 * false to return disabled plugins
336 * @return array of plugin components of requested type
338 protected function getListByType($type, $enabled)
340 $master_list = $enabled
341 ?
array_keys(array_filter($this->masterList
))
342 : array_keys(array_filter($this->masterList
, [$this, 'negate']));
345 foreach ($master_list as $plugin) {
347 if (file_exists(DOKU_PLUGIN
. "$plugin/$type.php")) {
348 $plugins[] = $plugin;
352 $typedir = DOKU_PLUGIN
. "$plugin/$type/";
353 if (is_dir($typedir)) {
354 if ($dp = opendir($typedir)) {
355 while (false !== ($component = readdir($dp))) {
356 if (strpos($component, '.') === 0 ||
strtolower(substr($component, -4)) !== '.php') continue;
357 if (is_file($typedir . $component)) {
358 $plugins[] = $plugin . '_' . substr($component, 0, -4);
371 * Split name in a plugin name and a component name
373 * @param string $name
376 * - and component name when available, otherwise empty string
378 protected function splitName($name)
380 if (!isset($this->masterList
[$name])) {
381 return sexplode('_', $name, 2, '');
388 * Returns inverse boolean value of the input
390 * @param mixed $input
391 * @return bool inversed boolean value of input
393 protected function negate($input)
395 return !(bool)$input;