dokuwiki/inc/pluginutils.php

152 lines
4.2 KiB
PHP
Raw Normal View History

<?php
/**
* Utilities for handling plugins
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
*/
// plugin related constants
use dokuwiki\Extension\AdminPlugin;
use dokuwiki\Extension\PluginController;
use dokuwiki\Extension\PluginInterface;
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
// note that only [a-z0-9]+ is officially supported,
// this is only to support plugins that don't follow these conventions, too
if(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z0-9\x7f-\xff]+');
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
2009-01-18 20:01:43 +01:00
/**
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
2009-01-18 20:01:43 +01:00
* Original plugin functions, remain for backwards compatibility
*/
/**
* Return list of available plugins
*
* @param string $type type of plugins; empty string for all
* @param bool $all; true to retrieve all, false to retrieve only enabled plugins
* @return array with plugin names or plugin component names
*/
function plugin_list($type='',$all=false)
2019-10-23 12:47:46 +02:00
{
/** @var $plugin_controller PluginController */
global $plugin_controller;
2019-10-23 12:47:46 +02:00
$plugins = $plugin_controller->getList($type,$all);
sort($plugins, SORT_NATURAL|SORT_FLAG_CASE);
return $plugins;
}
/**
* Returns plugin object
* Returns only new instances of a plugin when $new is true or if plugin is not Singleton,
* otherwise an already loaded instance.
*
* @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
* @param $disabled bool true to load even disabled plugins
* @return PluginInterface|null the plugin object or null on failure
*/
2019-10-23 12:47:46 +02:00
function plugin_load($type,$name,$new=false,$disabled=false)
{
/** @var $plugin_controller PluginController */
global $plugin_controller;
2010-08-29 12:27:41 +02:00
return $plugin_controller->load($type,$name,$new,$disabled);
}
/**
* Whether plugin is disabled
*
* @param string $plugin name of plugin
2014-10-01 17:05:40 +02:00
* @return bool true disabled, false enabled
*/
function plugin_isdisabled($plugin)
2019-10-23 12:47:46 +02:00
{
/** @var $plugin_controller PluginController */
global $plugin_controller;
return !$plugin_controller->isEnabled($plugin);
}
/**
* Enable the plugin
*
* @param string $plugin name of plugin
2014-10-01 17:05:40 +02:00
* @return bool true saving succeed, false saving failed
*/
function plugin_enable($plugin)
2019-10-23 12:47:46 +02:00
{
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->enable($plugin);
}
/**
* Disable the plugin
*
* @param string $plugin name of plugin
2014-10-01 17:05:40 +02:00
* @return bool true saving succeed, false saving failed
*/
function plugin_disable($plugin)
2019-10-23 12:47:46 +02:00
{
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->disable($plugin);
}
/**
* Returns directory name of plugin
*
* @param string $plugin name of plugin
* @return string name of directory
* @deprecated 2018-07-20
*/
function plugin_directory($plugin)
2019-10-23 12:47:46 +02:00
{
dbg_deprecated('$plugin directly');
return $plugin;
}
/**
* Returns cascade of the config files
*
* @return array with arrays of plugin configs
*/
function plugin_getcascade()
2019-10-23 12:47:46 +02:00
{
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->getCascade();
}
/**
* Return the currently operating admin plugin or null
* if not on an admin plugin page
*
* @return Doku_Plugin_Admin
*/
function plugin_getRequestAdminPlugin()
2019-10-23 12:47:46 +02:00
{
static $admin_plugin = false;
global $ACT,$INPUT,$INFO;
if ($admin_plugin === false) {
if (($ACT == 'admin') && ($page = $INPUT->str('page', '', true)) != '') {
$pluginlist = plugin_list('admin');
if (in_array($page, $pluginlist)) {
// attempt to load the plugin
/** @var $admin_plugin AdminPlugin */
$admin_plugin = plugin_load('admin', $page);
// verify
if ($admin_plugin && !$admin_plugin->isAccessibleByCurrentUser()) {
$admin_plugin = null;
$INPUT->remove('page');
msg('For admins only',-1);
}
}
}
}
return $admin_plugin;
}