moved plugin controller to Extension namespace

This commit is contained in:
Andreas Gohr 2018-06-15 18:23:52 +02:00
parent c630f65948
commit 3a7140a158
14 changed files with 125 additions and 90 deletions

View File

@ -1,5 +1,6 @@
<?php
use dokuwiki\Extension\PluginController;
use dokuwiki\Extension\Event;
use dokuwiki\Extension\EventHandler;
@ -107,7 +108,7 @@ abstract class DokuWikiTest extends PHPUnit_Framework_TestCase {
// reset loaded plugins
global $plugin_controller_class, $plugin_controller;
/** @var Doku_Plugin_Controller $plugin_controller */
/** @var PluginController $plugin_controller */
$plugin_controller = new $plugin_controller_class();
// disable all non-default plugins

View File

@ -148,7 +148,7 @@ class remote_test extends DokuWikiTest {
parent::setUp();
// mock plugin controller to return our test plugins
$pluginManager = $this->createMock('Doku_Plugin_Controller');
$pluginManager = $this->createMock('dokuwiki\Extension\PluginController');
$pluginManager->method('getList')->willReturn(array('testplugin', 'testplugin2'));
$pluginManager->method('load')->willReturnCallback(
function($type, $plugin) {

View File

@ -1,7 +1,7 @@
#!/usr/bin/php
<?php
use dokuwiki\Extension\CLIPlugin;
use dokuwiki\Extension\PluginController;
use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Colors;
use splitbrain\phpcli\Options;
@ -52,7 +52,7 @@ class PluginCLI extends CLI {
* List available plugins
*/
protected function listPlugins() {
/** @var Doku_Plugin_Controller $plugin_controller */
/** @var PluginController $plugin_controller */
global $plugin_controller;
echo "\n";

View File

@ -6,22 +6,21 @@
* @author Christopher Smith <chris@jalakai.co.uk>
*/
// plugin related constants
use dokuwiki\Extension\PluginInterface;
namespace dokuwiki\Extension;
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
class Doku_Plugin_Controller {
class PluginController
{
protected $list_bytype = array();
protected $tmp_plugins = array();
protected $plugin_cascade = array('default'=>array(),'local'=>array(),'protected'=>array());
protected $plugin_cascade = array('default' => array(), 'local' => array(), 'protected' => array());
protected $last_local_config_file = '';
/**
* Populates the master list of plugins
*/
public function __construct() {
public function __construct()
{
$this->loadConfig();
$this->_populateMasterList();
}
@ -42,7 +41,8 @@ class Doku_Plugin_Controller {
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
public function getList($type='',$all=false){
public function getList($type = '', $all = false)
{
// request the complete list
if (!$type) {
@ -50,10 +50,10 @@ class Doku_Plugin_Controller {
}
if (!isset($this->list_bytype[$type]['enabled'])) {
$this->list_bytype[$type]['enabled'] = $this->_getListByType($type,true);
$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);
$this->list_bytype[$type]['disabled'] = $this->_getListByType($type, false);
}
return $all
@ -72,7 +72,8 @@ class Doku_Plugin_Controller {
* @param $disabled bool true to load even disabled plugins
* @return PluginInterface|null the plugin object or null on failure
*/
public function load($type,$name,$new=false,$disabled=false){
public function load($type, $name, $new = false, $disabled = false)
{
//we keep all loaded plugins available in global scope for reuse
global $DOKU_PLUGINS;
@ -80,14 +81,14 @@ class Doku_Plugin_Controller {
list($plugin, /* $component */) = $this->_splitName($name);
// check if disabled
if(!$disabled && $this->isdisabled($plugin)){
if (!$disabled && $this->isdisabled($plugin)) {
return null;
}
$class = $type.'_plugin_'.$name;
$class = $type . '_plugin_' . $name;
//plugin already loaded?
if(!empty($DOKU_PLUGINS[$type][$name])){
if (!empty($DOKU_PLUGINS[$type][$name])) {
if ($new || !$DOKU_PLUGINS[$type][$name]->isSingleton()) {
return class_exists($class, true) ? new $class : null;
} else {
@ -100,8 +101,8 @@ class Doku_Plugin_Controller {
# the plugin might be in the wrong directory
$dir = $this->get_directory($plugin);
$inf = confToHash(DOKU_PLUGIN."$dir/plugin.info.txt");
if($inf['base'] && $inf['base'] != $plugin){
$inf = confToHash(DOKU_PLUGIN . "$dir/plugin.info.txt");
if ($inf['base'] && $inf['base'] != $plugin) {
msg(
sprintf(
"Plugin installed incorrectly. Rename plugin directory '%s' to '%s'.",
@ -111,7 +112,7 @@ class Doku_Plugin_Controller {
)
), -1
);
} elseif (preg_match('/^'.DOKU_PLUGIN_NAME_REGEX.'$/', $plugin) !== 1) {
} elseif (preg_match('/^' . DOKU_PLUGIN_NAME_REGEX . '$/', $plugin) !== 1) {
msg(
sprintf(
"Plugin name '%s' is not a valid plugin name, only the characters a-z and 0-9 are allowed. " .
@ -132,7 +133,8 @@ class Doku_Plugin_Controller {
* @param string $plugin name of plugin
* @return bool true disabled, false enabled
*/
public function isdisabled($plugin) {
public function isdisabled($plugin)
{
return empty($this->tmp_plugins[$plugin]);
}
@ -142,8 +144,9 @@ class Doku_Plugin_Controller {
* @param string $plugin name of plugin
* @return bool true saving succeed, false saving failed
*/
public function disable($plugin) {
if(array_key_exists($plugin,$this->plugin_cascade['protected'])) return false;
public function disable($plugin)
{
if (array_key_exists($plugin, $this->plugin_cascade['protected'])) return false;
$this->tmp_plugins[$plugin] = 0;
return $this->saveList();
}
@ -154,8 +157,9 @@ class Doku_Plugin_Controller {
* @param string $plugin name of plugin
* @return bool true saving succeed, false saving failed
*/
public function enable($plugin) {
if(array_key_exists($plugin,$this->plugin_cascade['protected'])) return false;
public function enable($plugin)
{
if (array_key_exists($plugin, $this->plugin_cascade['protected'])) return false;
$this->tmp_plugins[$plugin] = 1;
return $this->saveList();
}
@ -166,7 +170,8 @@ class Doku_Plugin_Controller {
* @param string $plugin name of plugin
* @return string name of directory
*/
public function get_directory($plugin) {
public function get_directory($plugin)
{
return $plugin;
}
@ -175,23 +180,25 @@ class Doku_Plugin_Controller {
*
* @return array with arrays of plugin configs
*/
public function getCascade() {
public function getCascade()
{
return $this->plugin_cascade;
}
protected function _populateMasterList() {
protected function _populateMasterList()
{
global $conf;
if ($dh = @opendir(DOKU_PLUGIN)) {
$all_plugins = array();
while (false !== ($plugin = readdir($dh))) {
if ($plugin[0] == '.') continue; // skip hidden entries
if (is_file(DOKU_PLUGIN.$plugin)) continue; // skip files, we're only interested in directories
if (is_file(DOKU_PLUGIN . $plugin)) continue; // skip files, we're only interested in directories
if (array_key_exists($plugin,$this->tmp_plugins) && $this->tmp_plugins[$plugin] == 0){
if (array_key_exists($plugin, $this->tmp_plugins) && $this->tmp_plugins[$plugin] == 0) {
$all_plugins[$plugin] = 0;
} elseif ((array_key_exists($plugin,$this->tmp_plugins) && $this->tmp_plugins[$plugin] == 1)) {
} elseif ((array_key_exists($plugin, $this->tmp_plugins) && $this->tmp_plugins[$plugin] == 1)) {
$all_plugins[$plugin] = 1;
} else {
$all_plugins[$plugin] = 1;
@ -211,10 +218,11 @@ class Doku_Plugin_Controller {
* @param array $files list of files to include, latter overrides previous
* @return array with entries of the $plugins arrays of the included files
*/
protected function checkRequire($files) {
protected function checkRequire($files)
{
$plugins = array();
foreach($files as $file) {
if(file_exists($file)) {
foreach ($files as $file) {
if (file_exists($file)) {
include_once($file);
}
}
@ -224,37 +232,38 @@ class Doku_Plugin_Controller {
/**
* Save the current list of plugins
*
* @param bool $forceSave;
* @param bool $forceSave ;
* false to save only when config changed
* true to always save
* @return bool true saving succeed, false saving failed
*/
protected function saveList($forceSave = false) {
protected function saveList($forceSave = false)
{
global $conf;
if (empty($this->tmp_plugins)) return false;
// Rebuild list of local settings
$local_plugins = $this->rebuildLocal();
if($local_plugins != $this->plugin_cascade['local'] || $forceSave) {
if ($local_plugins != $this->plugin_cascade['local'] || $forceSave) {
$file = $this->last_local_config_file;
$out = "<?php\n/*\n * Local plugin enable/disable settings\n".
" * Auto-generated through plugin/extension manager\n *\n".
" * NOTE: Plugins will not be added to this file unless there ".
"is a need to override a default setting. Plugins are\n".
" * enabled by default.\n */\n";
$out = "<?php\n/*\n * Local plugin enable/disable settings\n" .
" * Auto-generated through plugin/extension manager\n *\n" .
" * NOTE: Plugins will not be added to this file unless there " .
"is a need to override a default setting. Plugins are\n" .
" * enabled by default.\n */\n";
foreach ($local_plugins as $plugin => $value) {
$out .= "\$plugins['$plugin'] = $value;\n";
}
// backup current file (remove any existing backup)
if (file_exists($file)) {
$backup = $file.'.bak';
$backup = $file . '.bak';
if (file_exists($backup)) @unlink($backup);
if (!@copy($file,$backup)) return false;
if (!@copy($file, $backup)) return false;
if (!empty($conf['fperm'])) chmod($backup, $conf['fperm']);
}
//check if can open for writing, else restore
return io_saveFile($file,$out);
return io_saveFile($file, $out);
}
return false;
}
@ -264,36 +273,39 @@ class Doku_Plugin_Controller {
*
* @return array array of plugins to be saved in end($config_cascade['plugins']['local'])
*/
protected function rebuildLocal() {
protected function rebuildLocal()
{
//assign to local variable to avoid overwriting
$backup = $this->tmp_plugins;
//Can't do anything about protected one so rule them out completely
$local_default = array_diff_key($backup,$this->plugin_cascade['protected']);
$local_default = array_diff_key($backup, $this->plugin_cascade['protected']);
//Diff between local+default and default
//gives us the ones we need to check and save
$diffed_ones = array_diff_key($local_default,$this->plugin_cascade['default']);
$diffed_ones = array_diff_key($local_default, $this->plugin_cascade['default']);
//The ones which we are sure of (list of 0s not in default)
$sure_plugins = array_filter($diffed_ones,array($this,'negate'));
$sure_plugins = array_filter($diffed_ones, array($this, 'negate'));
//the ones in need of diff
$conflicts = array_diff_key($local_default,$diffed_ones);
$conflicts = array_diff_key($local_default, $diffed_ones);
//The final list
return array_merge($sure_plugins,array_diff_assoc($conflicts,$this->plugin_cascade['default']));
return array_merge($sure_plugins, array_diff_assoc($conflicts, $this->plugin_cascade['default']));
}
/**
* Build the list of plugins and cascade
*
*/
protected function loadConfig() {
protected function loadConfig()
{
global $config_cascade;
foreach(array('default','protected') as $type) {
if(array_key_exists($type,$config_cascade['plugins']))
foreach (array('default', 'protected') as $type) {
if (array_key_exists($type, $config_cascade['plugins'])) {
$this->plugin_cascade[$type] = $this->checkRequire($config_cascade['plugins'][$type]);
}
}
$local = $config_cascade['plugins']['local'];
$this->last_local_config_file = array_pop($local);
$this->plugin_cascade['local'] = $this->checkRequire(array($this->last_local_config_file));
if(is_array($local)) {
if (is_array($local)) {
$this->plugin_cascade['default'] = array_merge(
$this->plugin_cascade['default'],
$this->checkRequire($local)
@ -309,32 +321,33 @@ class Doku_Plugin_Controller {
/**
* Returns a list of available plugin components of given type
*
* @param string $type plugin_type name; the type of plugin to return,
* @param bool $enabled true to return enabled plugins,
* @param string $type plugin_type name; the type of plugin to return,
* @param bool $enabled true to return enabled plugins,
* false to return disabled plugins
* @return array of plugin components of requested type
*/
protected function _getListByType($type, $enabled) {
protected function _getListByType($type, $enabled)
{
$master_list = $enabled
? array_keys(array_filter($this->tmp_plugins))
: array_keys(array_filter($this->tmp_plugins,array($this,'negate')));
: array_keys(array_filter($this->tmp_plugins, array($this, 'negate')));
$plugins = array();
foreach ($master_list as $plugin) {
$basedir = $this->get_directory($plugin);
if (file_exists(DOKU_PLUGIN."$basedir/$type.php")){
if (file_exists(DOKU_PLUGIN . "$basedir/$type.php")) {
$plugins[] = $plugin;
continue;
}
$typedir = DOKU_PLUGIN."$basedir/$type/";
$typedir = DOKU_PLUGIN . "$basedir/$type/";
if (is_dir($typedir)) {
if ($dp = opendir($typedir)) {
while (false !== ($component = readdir($dp))) {
if (substr($component,0,1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
if (is_file($typedir.$component)) {
$plugins[] = $plugin.'_'.substr($component, 0, -4);
if (substr($component, 0, 1) == '.' || strtolower(substr($component, -4)) != ".php") continue;
if (is_file($typedir . $component)) {
$plugins[] = $plugin . '_' . substr($component, 0, -4);
}
}
closedir($dp);
@ -354,12 +367,13 @@ class Doku_Plugin_Controller {
* - plugin name
* - and component name when available, otherwise empty string
*/
protected function _splitName($name) {
protected function _splitName($name)
{
if (array_search($name, array_keys($this->tmp_plugins)) === false) {
return explode('_',$name,2);
return explode('_', $name, 2);
}
return array($name,'');
return array($name, '');
}
/**
@ -368,7 +382,8 @@ class Doku_Plugin_Controller {
* @param mixed $input
* @return bool inversed boolean value of input
*/
protected function negate($input) {
return !(bool) $input;
protected function negate($input)
{
return !(bool)$input;
}
}

View File

@ -12,6 +12,7 @@
// some ACL level defines
use dokuwiki\PassHash;
use dokuwiki\Extension\AuthPlugin;
use dokuwiki\Extension\PluginController;
use dokuwiki\Extension\Event;
define('AUTH_NONE', 0);
@ -42,7 +43,7 @@ function auth_setup() {
global $INPUT;
global $AUTH_ACL;
global $lang;
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
$AUTH_ACL = array();

View File

@ -335,3 +335,16 @@ function trigger_event($name, &$data, $action=null, $canPreventDefault=true) {
dbg_deprecated('\dokuwiki\Extension\Event::createAndTrigger');
return \dokuwiki\Extension\Event::createAndTrigger($name, $data, $action, $canPreventDefault);
}
/**
* @inheritdoc
* @deprecated 2018-06-15
*/
class Doku_Plugin_Controller extends \dokuwiki\Extension\PluginController {
/** @inheritdoc */
public function __construct()
{
dbg_deprecated(\dokuwiki\Extension\PluginController::class);
parent::__construct();
}
}

View File

@ -194,7 +194,7 @@ init_files();
// setup plugin controller class (can be overwritten in preload.php)
$plugin_types = array('auth', 'admin','syntax','action','renderer', 'helper','remote','cli');
global $plugin_controller_class, $plugin_controller;
if (empty($plugin_controller_class)) $plugin_controller_class = Doku_Plugin_Controller::class;
if (empty($plugin_controller_class)) $plugin_controller_class = dokuwiki\Extension\PluginController::class;
// load libraries
require_once(DOKU_INC.'vendor/autoload.php');

View File

@ -67,7 +67,6 @@ function load_autoload($name){
'IXR_Client' => DOKU_INC.'inc/IXR_Library.php',
'IXR_Error' => DOKU_INC.'inc/IXR_Library.php',
'IXR_IntrospectionServer' => DOKU_INC.'inc/IXR_Library.php',
'Doku_Plugin_Controller'=> DOKU_INC.'inc/plugincontroller.class.php',
'SafeFN' => DOKU_INC.'inc/SafeFN.class.php',
'Sitemapper' => DOKU_INC.'inc/Sitemapper.php',
'Mailer' => DOKU_INC.'inc/Mailer.class.php',

View File

@ -10,6 +10,7 @@
use dokuwiki\Cache\CacheInstructions;
use dokuwiki\Cache\CacheRenderer;
use dokuwiki\ChangeLog\PageChangeLog;
use dokuwiki\Extension\PluginController;
use dokuwiki\Extension\Event;
use dokuwiki\Parsing\Parser;
@ -692,7 +693,7 @@ function p_render($mode,$instructions,&$info,$date_at=''){
* @author Christopher Smith <chris@jalakai.co.uk>
*/
function p_get_renderer($mode) {
/** @var Doku_Plugin_Controller $plugin_controller */
/** @var PluginController $plugin_controller */
global $conf, $plugin_controller;
$rname = !empty($conf['renderer_'.$mode]) ? $conf['renderer_'.$mode] : $mode;

View File

@ -8,6 +8,7 @@
// 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/');
@ -27,7 +28,7 @@ if(!defined('DOKU_PLUGIN_NAME_REGEX')) define('DOKU_PLUGIN_NAME_REGEX', '[a-zA-Z
* @return array with plugin names or plugin component names
*/
function plugin_list($type='',$all=false) {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->getList($type,$all);
}
@ -44,7 +45,7 @@ function plugin_list($type='',$all=false) {
* @return PluginInterface|null the plugin object or null on failure
*/
function plugin_load($type,$name,$new=false,$disabled=false) {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->load($type,$name,$new,$disabled);
}
@ -56,7 +57,7 @@ function plugin_load($type,$name,$new=false,$disabled=false) {
* @return bool true disabled, false enabled
*/
function plugin_isdisabled($plugin) {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->isdisabled($plugin);
}
@ -68,7 +69,7 @@ function plugin_isdisabled($plugin) {
* @return bool true saving succeed, false saving failed
*/
function plugin_enable($plugin) {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->enable($plugin);
}
@ -80,7 +81,7 @@ function plugin_enable($plugin) {
* @return bool true saving succeed, false saving failed
*/
function plugin_disable($plugin) {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->disable($plugin);
}
@ -92,7 +93,7 @@ function plugin_disable($plugin) {
* @return string name of directory
*/
function plugin_directory($plugin) {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->get_directory($plugin);
}
@ -103,7 +104,7 @@ function plugin_directory($plugin) {
* @return array with arrays of plugin configs
*/
function plugin_getcascade() {
/** @var $plugin_controller Doku_Plugin_Controller */
/** @var $plugin_controller PluginController */
global $plugin_controller;
return $plugin_controller->getCascade();
}

View File

@ -9,7 +9,7 @@ class SettingAuthtype extends SettingMultichoice {
/** @inheritdoc */
public function initialize($default = null, $local = null, $protected = null) {
/** @var $plugin_controller \Doku_Plugin_Controller */
/** @var $plugin_controller \dokuwiki\Extension\PluginController */
global $plugin_controller;
// retrieve auth types provided by plugins
@ -22,7 +22,7 @@ class SettingAuthtype extends SettingMultichoice {
/** @inheritdoc */
public function update($input) {
/** @var $plugin_controller \Doku_Plugin_Controller */
/** @var $plugin_controller \dokuwiki\Extension\PluginController */
global $plugin_controller;
// is an update possible/requested?

View File

@ -7,6 +7,7 @@
*/
use dokuwiki\HTTP\DokuHTTPClient;
use dokuwiki\Extension\PluginController;
/**
* Class helper_plugin_extension_extension represents a single extension (plugin or template)
@ -144,7 +145,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin
global $conf;
if ($this->id == $conf['authtype']) return true;
/** @var Doku_Plugin_Controller $plugin_controller */
/** @var PluginController $plugin_controller */
global $plugin_controller;
$cascade = $plugin_controller->getCascade();
return (isset($cascade['protected'][$this->id]) && $cascade['protected'][$this->id]);
@ -172,7 +173,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin
return ($conf['template'] == $this->getBase());
}
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
return !$plugin_controller->isdisabled($this->base);
}
@ -360,7 +361,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin
*/
public function getMissingDependencies()
{
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
$dependencies = $this->getDependencies();
$missing_dependencies = array();
@ -714,7 +715,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin
if (!$this->isInstalled()) return $this->getLang('notinstalled');
if ($this->isEnabled()) return $this->getLang('alreadyenabled');
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
if ($plugin_controller->enable($this->base)) {
$this->purgeCache();
@ -733,7 +734,7 @@ class helper_plugin_extension_extension extends DokuWiki_Plugin
{
if ($this->isTemplate()) return $this->getLang('notimplemented');
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
if (!$this->isInstalled()) return $this->getLang('notinstalled');
if (!$this->isEnabled()) return $this->getLang('alreadydisabled');

View File

@ -6,6 +6,8 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/
use dokuwiki\Extension\PluginController;
/**
* Class helper_plugin_extension_list takes care of the overall GUI
*/
@ -33,7 +35,7 @@ class helper_plugin_extension_gui extends DokuWiki_Plugin
*/
public function tabPlugins()
{
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
echo '<div class="panelHeader">';

View File

@ -8,6 +8,7 @@
use dokuwiki\Cache\Cache;
use dokuwiki\HTTP\DokuHTTPClient;
use dokuwiki\Extension\PluginController;
/**
* Class helper_plugin_extension_repository provides access to the extension repository on dokuwiki.org
@ -25,7 +26,7 @@ class helper_plugin_extension_repository extends DokuWiki_Plugin
*/
public function init()
{
/* @var Doku_Plugin_Controller $plugin_controller */
/* @var PluginController $plugin_controller */
global $plugin_controller;
if ($this->hasAccess()) {
$list = $plugin_controller->getList('', true);