From b9ec5071fe682514146699bf42e48ffb84183778 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Tue, 2 Jul 2019 00:40:54 -0300 Subject: [PATCH] Add Server\Plugin value object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Controllers/Server/PluginsController.php | 63 ++--- libraries/classes/Server/Plugin.php | 252 ++++++++++++++++++ libraries/classes/Server/Plugins.php | 74 +++++ services.yml | 4 + services_controllers.yml | 1 + templates/server/plugins/index.twig | 37 +-- .../Server/PluginsControllerTest.php | 18 +- 7 files changed, 389 insertions(+), 60 deletions(-) create mode 100644 libraries/classes/Server/Plugin.php create mode 100644 libraries/classes/Server/Plugins.php diff --git a/libraries/classes/Controllers/Server/PluginsController.php b/libraries/classes/Controllers/Server/PluginsController.php index 8fc2260658..a8b8e8c78f 100644 --- a/libraries/classes/Controllers/Server/PluginsController.php +++ b/libraries/classes/Controllers/Server/PluginsController.php @@ -13,6 +13,7 @@ namespace PhpMyAdmin\Controllers\Server; use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Response; +use PhpMyAdmin\Server\Plugins; use PhpMyAdmin\Template; /** @@ -23,21 +24,20 @@ use PhpMyAdmin\Template; class PluginsController extends AbstractController { /** - * @var array plugin details + * @var Plugins */ private $plugins; /** - * Constructs PluginsController - * * @param Response $response Response object * @param DatabaseInterface $dbi DatabaseInterface object * @param Template $template Template object + * @param Plugins $plugins Plugins object */ - public function __construct($response, $dbi, Template $template) + public function __construct($response, $dbi, Template $template, Plugins $plugins) { parent::__construct($response, $dbi, $template); - $this->setPlugins(); + $this->plugins = $plugins; } /** @@ -54,43 +54,32 @@ class PluginsController extends AbstractController $scripts->addFile('vendor/jquery/jquery.tablesorter.js'); $scripts->addFile('server/plugins.js'); - $pluginsTypeClean = []; - foreach (array_keys($this->plugins) as $pluginType) { - $pluginsTypeClean[$pluginType] = preg_replace( + $plugins = []; + $serverPlugins = $this->plugins->getAll(); + foreach ($serverPlugins as $plugin) { + $plugins[$plugin->getType()][] = [ + 'name' => $plugin->getName(), + 'type' => $plugin->getType(), + 'version' => $plugin->getVersion(), + 'description' => $plugin->getDescription(), + 'author' => $plugin->getAuthor(), + 'license' => $plugin->getLicense(), + 'status' => $plugin->getStatus(), + ]; + } + ksort($plugins); + + $cleanTypes = []; + foreach (array_keys($plugins) as $type) { + $cleanTypes[$type] = preg_replace( '/[^a-z]/', '', - mb_strtolower($pluginType) + mb_strtolower($type) ); } return $this->template->render('server/plugins/index', [ - 'plugins' => $this->plugins, - 'plugins_type_clean' => $pluginsTypeClean, + 'plugins' => $plugins, + 'clean_types' => $cleanTypes, ]); } - - /** - * Sets details about server plugins - * - * @return void - */ - private function setPlugins(): void - { - $sql = "SELECT plugin_name, - plugin_type, - (plugin_status = 'ACTIVE') AS is_active, - plugin_type_version, - plugin_author, - plugin_description, - plugin_license - FROM information_schema.plugins - ORDER BY plugin_type, plugin_name"; - - $res = $this->dbi->query($sql); - $this->plugins = []; - while ($row = $this->dbi->fetchAssoc($res)) { - $this->plugins[$row['plugin_type']][] = $row; - } - $this->dbi->freeResult($res); - ksort($this->plugins); - } } diff --git a/libraries/classes/Server/Plugin.php b/libraries/classes/Server/Plugin.php new file mode 100644 index 0000000000..77ce59d471 --- /dev/null +++ b/libraries/classes/Server/Plugin.php @@ -0,0 +1,252 @@ +name = $name; + $this->version = $version; + $this->status = $status; + $this->type = $type; + $this->typeVersion = $typeVersion; + $this->library = $library; + $this->libraryVersion = $libraryVersion; + $this->author = $author; + $this->description = $description; + $this->license = $license; + $this->loadOption = $loadOption; + $this->maturity = $maturity; + $this->authVersion = $authVersion; + } + + /** + * @param array $state array with the properties + * @return self + */ + public static function fromState(array $state): self + { + return new self( + $state['name'] ?? '', + $state['version'] ?? null, + $state['status'] ?? '', + $state['type'] ?? '', + $state['typeVersion'] ?? null, + $state['library'] ?? null, + $state['libraryVersion'] ?? null, + $state['author'] ?? null, + $state['description'] ?? null, + $state['license'] ?? '', + $state['loadOption'] ?? null, + $state['maturity'] ?? null, + $state['authVersion'] ?? null + ); + } + + /** + * @return string + */ + public function getName(): string + { + return $this->name; + } + + /** + * @return string|null + */ + public function getVersion(): ?string + { + return $this->version; + } + + /** + * @return string + */ + public function getStatus(): string + { + return $this->status; + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @return string|null + */ + public function getTypeVersion(): ?string + { + return $this->typeVersion; + } + + /** + * @return string|null + */ + public function getLibrary(): ?string + { + return $this->library; + } + + /** + * @return string|null + */ + public function getLibraryVersion(): ?string + { + return $this->libraryVersion; + } + + /** + * @return string|null + */ + public function getAuthor(): ?string + { + return $this->author; + } + + /** + * @return string|null + */ + public function getDescription(): ?string + { + return $this->description; + } + + /** + * @return string + */ + public function getLicense(): string + { + return $this->license; + } + + /** + * @return string|null + */ + public function getLoadOption(): ?string + { + return $this->loadOption; + } + + /** + * @return string|null + */ + public function getMaturity(): ?string + { + return $this->maturity; + } + + /** + * @return string|null + */ + public function getAuthVersion(): ?string + { + return $this->authVersion; + } +} diff --git a/libraries/classes/Server/Plugins.php b/libraries/classes/Server/Plugins.php new file mode 100644 index 0000000000..be6856996d --- /dev/null +++ b/libraries/classes/Server/Plugins.php @@ -0,0 +1,74 @@ +dbi = $dbi; + } + + /** + * @return Plugin[] + */ + public function getAll(): array + { + global $cfg; + + $sql = 'SHOW PLUGINS;'; + if (! $cfg['Server']['DisableIS']) { + $sql = 'SELECT * FROM information_schema.PLUGINS ORDER BY PLUGIN_TYPE, PLUGIN_NAME;'; + } + $result = $this->dbi->query($sql); + $plugins = []; + while ($row = $this->dbi->fetchAssoc($result)) { + $plugins[] = $this->mapRowToPlugin($row); + } + $this->dbi->freeResult($result); + + return $plugins; + } + + /** + * @param array $row Row fetched from database + * @return Plugin + */ + private function mapRowToPlugin(array $row): Plugin + { + return Plugin::fromState([ + 'name' => $row['PLUGIN_NAME'] ?? $row['Name'], + 'version' => $row['PLUGIN_VERSION'] ?? null, + 'status' => $row['PLUGIN_STATUS'] ?? $row['Status'], + 'type' => $row['PLUGIN_TYPE'] ?? $row['Type'], + 'typeVersion' => $row['PLUGIN_TYPE_VERSION'] ?? null, + 'library' => $row['PLUGIN_LIBRARY'] ?? $row['Library'] ?? null, + 'libraryVersion' => $row['PLUGIN_LIBRARY_VERSION'] ?? null, + 'author' => $row['PLUGIN_AUTHOR'] ?? null, + 'description' => $row['PLUGIN_DESCRIPTION'] ?? null, + 'license' => $row['PLUGIN_LICENSE'] ?? $row['License'], + 'loadOption' => $row['LOAD_OPTION'] ?? null, + 'maturity' => $row['PLUGIN_MATURITY'] ?? null, + 'authVersion' => $row['PLUGIN_AUTH_VERSION'] ?? null, + ]); + } +} diff --git a/services.yml b/services.yml index 488c99a885..55f6d92340 100644 --- a/services.yml +++ b/services.yml @@ -103,6 +103,10 @@ services: response: factory: 'PhpMyAdmin\Response::getInstance' + server_plugins: + class: 'PhpMyAdmin\Server\Plugins' + arguments: ['@dbi'] + server_privileges: class: 'PhpMyAdmin\Server\Privileges' arguments: ['@template', '@dbi', '@relation', '@relation_cleanup'] diff --git a/services_controllers.yml b/services_controllers.yml index 59390491bf..1bd87c0047 100644 --- a/services_controllers.yml +++ b/services_controllers.yml @@ -128,6 +128,7 @@ services: response: '@response' dbi: '@dbi' template: '@template' + plugins: '@server_plugins' PhpMyAdmin\Controllers\Server\ReplicationController: class: 'PhpMyAdmin\Controllers\Server\ReplicationController' diff --git a/templates/server/plugins/index.twig b/templates/server/plugins/index.twig index d75481d8cf..ec8bbd49d8 100644 --- a/templates/server/plugins/index.twig +++ b/templates/server/plugins/index.twig @@ -5,18 +5,17 @@
- {% for plugin_type, plugin_list in plugins %} + {% for type, list in plugins %}
- +
@@ -28,20 +27,28 @@ - {% for plugin in plugin_list %} + {% for plugin in list %} - - - - + + + + {% endfor %} diff --git a/test/classes/Controllers/Server/PluginsControllerTest.php b/test/classes/Controllers/Server/PluginsControllerTest.php index c40ce3dcd0..099707c4bf 100644 --- a/test/classes/Controllers/Server/PluginsControllerTest.php +++ b/test/classes/Controllers/Server/PluginsControllerTest.php @@ -13,6 +13,7 @@ use PhpMyAdmin\Config; use PhpMyAdmin\Controllers\Server\PluginsController; use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\Response; +use PhpMyAdmin\Server\Plugins; use PhpMyAdmin\Template; use PHPUnit\Framework\TestCase; @@ -51,13 +52,13 @@ class PluginsControllerTest extends TestCase * Prepare plugin list */ $row = [ - 'plugin_name' => 'plugin_name1', - 'plugin_type' => 'plugin_type1', - 'plugin_type_version' => 'plugin_version1', - 'plugin_author' => 'plugin_author1', - 'plugin_license' => 'plugin_license1', - 'plugin_description' => 'plugin_description1', - 'is_active' => true, + 'PLUGIN_NAME' => 'plugin_name1', + 'PLUGIN_TYPE' => 'plugin_type1', + 'PLUGIN_VERSION' => 'plugin_version1', + 'PLUGIN_AUTHOR' => 'plugin_author1', + 'PLUGIN_LICENSE' => 'plugin_license1', + 'PLUGIN_DESCRIPTION' => 'plugin_description1', + 'PLUGIN_STATUS' => 'ACTIVE', ]; $dbi = $this->getMockBuilder(DatabaseInterface::class) @@ -79,7 +80,8 @@ class PluginsControllerTest extends TestCase $controller = new PluginsController( Response::getInstance(), $dbi, - new Template() + new Template(), + new Plugins($dbi) ); $actual = $controller->index();
- {{ plugin_type }} + {{ type }}
- {{ plugin.plugin_name }} - {% if not plugin.is_active %} + {{ plugin.name }} + {% if plugin.status != 'ACTIVE' %} - {% trans 'disabled' %} + {% if plugin.status == 'INACTIVE' %} + {% trans 'inactive' %} + {% elseif plugin.status == 'DISABLED' %} + {% trans 'disabled' %} + {% elseif plugin.status == 'DELETING' %} + {% trans 'deleting' %} + {% elseif plugin.status == 'DELETED' %} + {% trans 'deleted' %} + {% endif %} {% endif %} {{ plugin.plugin_description }}{{ plugin.plugin_type_version }}{{ plugin.plugin_author }}{{ plugin.plugin_license }}{{ plugin.description }}{{ plugin.version }}{{ plugin.author }}{{ plugin.license }}