[MODULES] List all available plugins and allow enabling them via UI

Yet another revision of the Admin Plugin Management tool
This commit is contained in:
Diogo Cordeiro
2019-08-16 15:06:27 +01:00
parent e1514a151c
commit a05bea0af5
6 changed files with 176 additions and 75 deletions

View File

@@ -32,30 +32,68 @@ class PluginList extends Widget
{
public $plugins = [];
public function __construct($plugins, $out)
/**
* PluginList constructor.
* @param Action $out
* @param array|null $plugins
*/
public function __construct(Action $out, ?array $plugins = null)
{
parent::__construct($out);
$this->plugins = $plugins;
$this->plugins = is_null($plugins) ? $this->grabAllPluginNames() : $plugins;
}
/**
* List of names of all available plugins (distribution and third parties).
* Warning: Plugin not modules, it doesn't include core modules.
*
* @return array
*/
public static function grabAllPluginNames(): array
{
$plugins = [];
$distribution_plugins = glob(INSTALLDIR . '/plugins/*');
foreach ($distribution_plugins as $plugin) {
$plugin_name = ltrim($plugin, INSTALLDIR . '/plugins/');
if ($plugin_name == 'README.md') {
continue;
}
$plugins[] = $plugin_name;
}
unset($distribution_plugins);
$thirdparty_plugins = glob(INSTALLDIR . '/local/plugins/*');
foreach ($thirdparty_plugins as $plugin) {
$plugins[] = ltrim($plugin, INSTALLDIR . '/local/plugins/');
}
unset($thirdparty_plugins);
natsort($plugins);
return $plugins;
}
public function show()
{
if (!$this->plugins) {
$this->out->element('p', null,
// TRANS: Text displayed on plugin admin page when no plugin are enabled.
_m('All plugins have been disabled from the ' .
'site\'s configuration file.'));
}
$this->startList();
$this->showPlugins();
$this->endList();
}
public function startList()
public function startList(): void
{
$this->out->elementStart('table', 'plugin_list');
}
public function endList()
public function endList(): void
{
$this->out->elementEnd('table');
}
public function showPlugins()
public function showPlugins(): void
{
foreach ($this->plugins as $plugin) {
$pli = $this->newListItem($plugin);
@@ -63,12 +101,106 @@ class PluginList extends Widget
}
}
public function newListItem($plugin)
public function newListItem($plugin): PluginListItem
{
return new PluginListItem($plugin, $this->out);
}
/** Local cache for plugin version info */
protected static $versions = false;
/**
* Lazy-load the set of active plugin version info.
* Warning: Plugin not modules, it doesn't include core modules.
* @return array
*/
public static function getPluginVersions(): array
{
if (!is_array(self::$versions)) {
$plugin_versions = [];
Event::handle('PluginVersion', [&$plugin_versions]);
self::$versions = $plugin_versions;
}
return self::$versions;
}
/**
* We need a proper name for comparison, that is, without spaces nor the `(section)`
* Therefore, a plugin named "Diogo Cordeiro (diogo@fc.up.pt)" becomes "DiogoCordeiro"
*
* WARNING: You may have to use strtolower() in your situation
*
* @param string $plugin_name
* @return string Name without spaces nor parentheses section
*/
public static function internalizePluginName(string $plugin_name): string
{
$name_without_spaces = str_replace(' ', '', $plugin_name);
$name_without_spaces_nor_parentheses_section = substr($name_without_spaces, 0, strpos($name_without_spaces . '(', '('));
return $name_without_spaces_nor_parentheses_section;
}
/**
* It calls self::getPluginVersions() and for each it builds an array with the self::internalizePluginName()
*
* @return array
*/
public static function getActivePluginVersions(): array
{
$versions = self::getPluginVersions();
$active_plugins = [];
foreach ($versions as $info) {
$internal_plugin_name = self::internalizePluginName($info['name']);
// This is sensitive case
$key = 'disable-' . $internal_plugin_name;
if (common_config('plugins', $key)) {
continue;
}
$active_plugins[] = $info;
}
return $active_plugins;
}
/**
* Checks if a given plugin was loaded (added in config.php with addPlugin())
*
* @param string $plugin
* @return bool
* @see PluginListItem->metaInfo() Warning: horribly inefficient and may explode!
*/
public static function isPluginLoaded(string $plugin): bool
{
$versions = self::getPluginVersions();
foreach ($versions as $info) {
$internal_plugin_name = self::internalizePluginName($info['name']);
// For a proper comparison, we do it in lower case
if (strtolower($internal_plugin_name) == strtolower($plugin)) {
return true;
}
}
return false;
}
/**
* Checks if a given plugin is active (both loaded and not set as inactive in the database)
*
* @param string $plugin
* @return bool
* @see self::isPluginLoaded() Warning: horribly inefficient and may explode!
*/
public static function isPluginActive(string $plugin): bool
{
$key = 'disable-' . $plugin;
return self::isPluginLoaded($plugin) && !common_config('plugins', $key);
}
}
/**
* Class PluginListItem
*/
class PluginListItem extends Widget
{
/** Current plugin. */
@@ -87,13 +219,13 @@ class PluginListItem extends Widget
{
$meta = $this->metaInfo();
$this->out->elementStart('tr', array('id' => 'plugin-' . $this->plugin));
$this->out->elementStart('tr', ['id' => 'plugin-' . $this->plugin]);
// Name and controls
$this->out->elementStart('td');
$this->out->elementStart('div');
if (!empty($meta['homepage'])) {
$this->out->elementStart('a', array('href' => $meta['homepage']));
$this->out->elementStart('a', ['href' => $meta['homepage']]);
}
$this->out->text($this->plugin);
if (!empty($meta['homepage'])) {
@@ -125,7 +257,7 @@ class PluginListItem extends Widget
if (!empty($meta['rawdescription'])) {
$this->out->raw($meta['rawdescription']);
} elseif (!empty($meta['description'])) {
$this->out->raw($meta['description']);
$this->out->text($meta['description']);
}
$this->out->elementEnd('td');
@@ -140,11 +272,10 @@ class PluginListItem extends Widget
*/
protected function getControlForm()
{
$key = 'disable-' . $this->plugin;
if (common_config('plugins', $key)) {
return new PluginEnableForm($this->out, $this->plugin);
} else {
if (PluginList::isPluginActive($this->plugin)) {
return new PluginDisableForm($this->out, $this->plugin);
} else {
return new PluginEnableForm($this->out, $this->plugin);
}
}
@@ -154,19 +285,19 @@ class PluginListItem extends Widget
* Doesn't work for disabled plugins either.
*
* @fixme pull structured data from plugin source
* ^ Maybe by introducing a ini file in each plugin's directory? But a typical instance doesn't have all that many
* plugins anyway, no need for urgent action
*/
public function metaInfo()
{
$versions = self::getPluginVersions();
$versions = PluginList::getPluginVersions();
$found = false;
foreach ($versions as $info) {
// We need a proper name for comparison, that is, without spaces nor the `(section)`
// Therefore, a plugin named "Diogo Cordeiro (diogo@fc.up.pt)" becomes "DiogoCordeiro"
$name_without_spaces = str_replace(' ', '', $info['name']);
$name_without_spaces_nor_parentheses_section = substr($name_without_spaces, 0, strpos($name_without_spaces.'(', '('));
$internal_plugin_name = PluginList::internalizePluginName($info['name']);
if (strtolower($name_without_spaces_nor_parentheses_section) == strtolower($this->plugin)) {
// For a proper comparison, we do it in lower case
if (strtolower($internal_plugin_name) == strtolower($this->plugin)) {
if ($found) {
$found['rawdescription'] .= "<br />\n" . $info['rawdescription'];
} else {
@@ -180,21 +311,7 @@ class PluginListItem extends Widget
} else {
return ['name' => $this->plugin,
// TRANS: Plugin description for a disabled plugin.
'rawdescription' => _m('plugin-description', '(The plugin description is unavailable when a plugin has been disabled.)')];
'rawdescription' => _m('plugin-description', '(The plugin description is unavailable when a plugin hasn\'t been loaded.)')];
}
}
/**
* Lazy-load the set of active plugin version info
* @return array
*/
protected static function getPluginVersions()
{
if (!is_array(self::$versions)) {
$versions = [];
Event::handle('ModuleVersion', [&$versions]);
self::$versions = $versions;
}
return self::$versions;
}
}