OpenApi Gen: add toString method for easier testing
[dokuwiki.git] / inc / pluginutils.php
blobcd26d7e8634cf4f9dee42fc6c9289760ecea13ca
1 <?php
3 /**
4 * Utilities for handling plugins
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Andreas Gohr <andi@splitbrain.org>
8 */
10 // plugin related constants
11 use dokuwiki\Extension\AdminPlugin;
12 use dokuwiki\Extension\PluginController;
13 use dokuwiki\Extension\PluginInterface;
15 if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
16 // note that only [a-z0-9]+ is officially supported,
17 // this is only to support plugins that don't follow these conventions, too
18 if (!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
20 /**
21 * Original plugin functions, remain for backwards compatibility
24 /**
25 * Return list of available plugins
27 * @param string $type type of plugins; empty string for all
28 * @param bool $all; true to retrieve all, false to retrieve only enabled plugins
29 * @return array with plugin names or plugin component names
31 function plugin_list($type = '', $all = false)
33 /** @var $plugin_controller PluginController */
34 global $plugin_controller;
35 $plugins = $plugin_controller->getList($type, $all);
36 sort($plugins, SORT_NATURAL | SORT_FLAG_CASE);
37 return $plugins;
40 /**
41 * Returns plugin object
42 * Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
43 * otherwise an already loaded instance.
45 * @param $type string type of plugin to load
46 * @param $name string name of the plugin to load
47 * @param $new bool true to return a new instance of the plugin, false to use an already loaded instance
48 * @param $disabled bool true to load even disabled plugins
49 * @return PluginInterface|null the plugin object or null on failure
51 function plugin_load($type, $name, $new = false, $disabled = false)
53 /** @var $plugin_controller PluginController */
54 global $plugin_controller;
55 return $plugin_controller->load($type, $name, $new, $disabled);
58 /**
59 * Whether plugin is disabled
61 * @param string $plugin name of plugin
62 * @return bool true disabled, false enabled
64 function plugin_isdisabled($plugin)
66 /** @var $plugin_controller PluginController */
67 global $plugin_controller;
68 return !$plugin_controller->isEnabled($plugin);
71 /**
72 * Enable the plugin
74 * @param string $plugin name of plugin
75 * @return bool true saving succeed, false saving failed
77 function plugin_enable($plugin)
79 /** @var $plugin_controller PluginController */
80 global $plugin_controller;
81 return $plugin_controller->enable($plugin);
84 /**
85 * Disable the plugin
87 * @param string $plugin name of plugin
88 * @return bool true saving succeed, false saving failed
90 function plugin_disable($plugin)
92 /** @var $plugin_controller PluginController */
93 global $plugin_controller;
94 return $plugin_controller->disable($plugin);
97 /**
98 * Returns directory name of plugin
100 * @param string $plugin name of plugin
101 * @return string name of directory
102 * @deprecated 2018-07-20
104 function plugin_directory($plugin)
106 dbg_deprecated('$plugin directly');
107 return $plugin;
111 * Returns cascade of the config files
113 * @return array with arrays of plugin configs
115 function plugin_getcascade()
117 /** @var $plugin_controller PluginController */
118 global $plugin_controller;
119 return $plugin_controller->getCascade();
124 * Return the currently operating admin plugin or null
125 * if not on an admin plugin page
127 * @return Doku_Plugin_Admin
129 function plugin_getRequestAdminPlugin()
131 static $admin_plugin = false;
132 global $ACT,$INPUT,$INFO;
134 if ($admin_plugin === false) {
135 if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
136 $pluginlist = plugin_list('admin');
137 if (in_array($page, $pluginlist)) {
138 // attempt to load the plugin
139 /** @var $admin_plugin AdminPlugin */
140 $admin_plugin = plugin_load('admin', $page);
141 // verify
142 if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
143 $admin_plugin = null;
144 $INPUT->remove('page');
145 msg('For admins only', -1);
151 return $admin_plugin;