Skip to content

Commit

Permalink
Merge pull request #16984 from mauriciofauth/get-plugins
Browse files Browse the repository at this point in the history
Refactor `PhpMyAdmin\Plugins::getPlugins` method
  • Loading branch information
MauricioFauth committed Jun 29, 2021
2 parents b68d858 + 92dc790 commit ef81c79
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 137 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -58,6 +58,7 @@
"symfony/expression-language": "^5.2.3",
"symfony/polyfill-ctype": "^1.17.0",
"symfony/polyfill-mbstring": "^1.17.0",
"symfony/polyfill-php80": "^1.16",
"twig/twig": "^3.0.1",
"webmozart/assert": "^1.10",
"williamdes/mariadb-mysql-kbs": "^1.2"
Expand Down
105 changes: 38 additions & 67 deletions libraries/classes/Plugins.php
Expand Up @@ -4,10 +4,12 @@

namespace PhpMyAdmin;

use FilesystemIterator;
use PhpMyAdmin\Html\MySQLDocumentation;
use PhpMyAdmin\Plugins\AuthenticationPlugin;
use PhpMyAdmin\Plugins\ExportPlugin;
use PhpMyAdmin\Plugins\ImportPlugin;
use PhpMyAdmin\Plugins\Plugin;
use PhpMyAdmin\Plugins\SchemaPlugin;
use PhpMyAdmin\Properties\Options\Groups\OptionsPropertySubgroup;
use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem;
Expand All @@ -22,6 +24,8 @@
use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
use PhpMyAdmin\Properties\Plugins\PluginPropertyItem;
use PhpMyAdmin\Properties\Plugins\SchemaPluginProperties;
use SplFileInfo;
use Throwable;

use function __;
use function array_pop;
Expand All @@ -30,19 +34,16 @@
use function explode;
use function get_class;
use function htmlspecialchars;
use function is_file;
use function mb_strlen;
use function mb_strpos;
use function mb_strtolower;
use function mb_strtoupper;
use function mb_substr;
use function method_exists;
use function opendir;
use function preg_match;
use function preg_match_all;
use function readdir;
use function sprintf;
use function str_replace;
use function str_starts_with;
use function strcasecmp;
use function strcmp;
use function strtolower;
Expand Down Expand Up @@ -83,10 +84,11 @@ public static function getPlugin(string $type, string $format, $param = null): ?
*/
public static function getExport(string $type, bool $singleTable): array
{
return self::getPlugins('export', 'libraries/classes/Plugins/Export/', [
'export_type' => $type,
'single_table' => $singleTable,
]);
global $plugin_param;

$plugin_param = ['export_type' => $type, 'single_table' => $singleTable];

return self::getPlugins('Export');
}

/**
Expand All @@ -96,97 +98,66 @@ public static function getExport(string $type, bool $singleTable): array
*/
public static function getImport(string $type): array
{
return self::getPlugins('import', 'libraries/classes/Plugins/Import/', $type);
global $plugin_param;

$plugin_param = $type;

return self::getPlugins('Import');
}

/**
* @return SchemaPlugin[]
*/
public static function getSchema(): array
{
return self::getPlugins('schema', 'libraries/classes/Plugins/Schema/', null);
return self::getPlugins('Schema');
}

/**
* Reads all plugin information from directory $plugins_dir
* Reads all plugin information
*
* @param string $plugin_type the type of the plugin (import, export, etc)
* @param string $plugins_dir directory with plugins
* @param array|string|null $plugin_param parameter to plugin by which they can
* decide whether they can work
* @param string $type the type of the plugin (import, export, etc)
*
* @return array list of plugin instances
*/
private static function getPlugins(string $plugin_type, string $plugins_dir, $plugin_param): array
private static function getPlugins(string $type): array
{
global $skip_import;

$GLOBALS['plugin_param'] = $plugin_param;

$fullFsPathPluginDir = ROOT_PATH . $plugins_dir;

$handle = @opendir($fullFsPathPluginDir);
if (! $handle) {
try {
$files = new FilesystemIterator(ROOT_PATH . 'libraries/classes/Plugins/' . $type);
} catch (Throwable $e) {
return [];
}

$plugin_list = [];

$namespace = 'PhpMyAdmin\\' . str_replace('/', '\\', mb_substr($plugins_dir, 18));
$class_type = mb_strtoupper($plugin_type[0], 'UTF-8')
. mb_strtolower(mb_substr($plugin_type, 1), 'UTF-8');
$plugins = [];

$prefix_class_name = $namespace . $class_type;

while ($file = @readdir($handle)) {
// In some situations, Mac OS creates a new file for each file
// (for example ._csv.php) so the following regexp
// matches a file which does not start with a dot but ends
// with ".php"
if (
! is_file($fullFsPathPluginDir . $file)
|| ! preg_match(
'@^' . $class_type . '([^\.]+)\.php$@i',
$file,
$matches
)
) {
/** @var SplFileInfo $fileInfo */
foreach ($files as $fileInfo) {
if (! $fileInfo->isReadable() || ! $fileInfo->isFile() || $fileInfo->getExtension() !== 'php') {
continue;
}

/** @var bool $skip_import */
$skip_import = false;

include_once $fullFsPathPluginDir . $file;
if (! str_starts_with($fileInfo->getFilename(), $type)) {
continue;
}

if ($skip_import) {
$class = sprintf('PhpMyAdmin\\Plugins\\%s\\%s', $type, $fileInfo->getBasename('.php'));
if (! class_exists($class)) {
continue;
}

$class_name = $prefix_class_name . $matches[1];
$plugin = new $class_name();
if ($plugin->getProperties() === null) {
$plugin = new $class();
if (! ($plugin instanceof Plugin) || ! $plugin->isAvailable()) {
continue;
}

$plugin_list[] = $plugin;
$plugins[] = $plugin;
}

usort(
$plugin_list,
/**
* @param mixed $cmp_name_1
* @param mixed $cmp_name_2
*/
static function ($cmp_name_1, $cmp_name_2) {
return strcasecmp(
$cmp_name_1->getProperties()->getText(),
$cmp_name_2->getProperties()->getText()
);
}
);
usort($plugins, static function (Plugin $plugin1, Plugin $plugin2): int {
return strcasecmp($plugin1->getProperties()->getText(), $plugin2->getProperties()->getText());
});

return $plugin_list;
return $plugins;
}

/**
Expand Down
23 changes: 7 additions & 16 deletions libraries/classes/Plugins/Export/ExportPdf.php
@@ -1,7 +1,4 @@
<?php
/**
* Produce a PDF report (export) from a query
*/

declare(strict_types=1);

Expand All @@ -14,24 +11,13 @@
use PhpMyAdmin\Properties\Options\Items\RadioPropertyItem;
use PhpMyAdmin\Properties\Options\Items\TextPropertyItem;
use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
use TCPDF;

use function __;
use function class_exists;

// phpcs:disable PSR1.Files.SideEffects
/**
* Skip the plugin if TCPDF is not available.
*/
if (! class_exists('TCPDF')) {
$GLOBALS['skip_import'] = true;

return;
}

// phpcs:enable

/**
* Handles the export for the PDF class
* Produce a PDF report (export) from a query
*/
class ExportPdf extends ExportPlugin
{
Expand Down Expand Up @@ -378,4 +364,9 @@ private function setPdf($pdf)
{
$this->pdf = $pdf;
}

public function isAvailable(): bool
{
return class_exists(TCPDF::class);
}
}
23 changes: 9 additions & 14 deletions libraries/classes/Plugins/Export/ExportXml.php
@@ -1,7 +1,4 @@
<?php
/**
* Set of functions used to build XML dumps of tables
*/

declare(strict_types=1);

Expand Down Expand Up @@ -29,18 +26,8 @@

use const PHP_VERSION;

// phpcs:disable PSR1.Files.SideEffects
/* Can't do server export */
if (! isset($GLOBALS['db']) || strlen($GLOBALS['db']) === 0) {
$GLOBALS['skip_import'] = true;

return;
}

// phpcs:enable

/**
* Handles the export for the XML class
* Used to build XML dumps of tables
*/
class ExportXml extends ExportPlugin
{
Expand Down Expand Up @@ -616,4 +603,12 @@ private function setTables(array $tables)
{
$this->tables = $tables;
}

public function isAvailable(): bool
{
global $db;

// Can't do server export.
return isset($db) && strlen($db) > 0;
}
}
10 changes: 8 additions & 2 deletions libraries/classes/Plugins/ExportPlugin.php
Expand Up @@ -9,6 +9,7 @@

use PhpMyAdmin\Export;
use PhpMyAdmin\Properties\Plugins\ExportPluginProperties;
use PhpMyAdmin\Properties\Plugins\PluginPropertyItem;
use PhpMyAdmin\Relation;
use PhpMyAdmin\Transformations;

Expand All @@ -20,7 +21,7 @@
* methods, but those are not declared here, because they are not implemented
* by all export plugins.
*/
abstract class ExportPlugin
abstract class ExportPlugin implements Plugin
{
/**
* PhpMyAdmin\Properties\Plugins\ExportPluginProperties object containing
Expand Down Expand Up @@ -259,7 +260,7 @@ protected function initSpecificVariables()
*
* @return ExportPluginProperties
*/
public function getProperties()
public function getProperties(): PluginPropertyItem
{
return $this->properties;
}
Expand Down Expand Up @@ -411,4 +412,9 @@ public function getRelationString(

return $relation;
}

public function isAvailable(): bool
{
return true;
}
}
27 changes: 13 additions & 14 deletions libraries/classes/Plugins/Import/ImportLdi.php
@@ -1,7 +1,4 @@
<?php
/**
* CSV import plugin for phpMyAdmin using LOAD DATA
*/

declare(strict_types=1);

Expand All @@ -23,24 +20,18 @@

use const PHP_EOL;

// phpcs:disable PSR1.Files.SideEffects
// We need relations enabled and we work only on database
if (! isset($GLOBALS['plugin_param']) || $GLOBALS['plugin_param'] !== 'table') {
$GLOBALS['skip_import'] = true;

return;
}

// phpcs:enable

/**
* Handles the import for the CSV format using load data
* CSV import plugin for phpMyAdmin using LOAD DATA
*/
class ImportLdi extends AbstractImportCsv
{
public function __construct()
{
parent::__construct();
if (! $this->isAvailable()) {
return;
}

$this->setProperties();
}

Expand Down Expand Up @@ -198,4 +189,12 @@ public function doImport(?File $importHandle = null, array &$sql_data = [])
$this->import->runQuery('', '', $sql_data);
$finished = true;
}

public function isAvailable(): bool
{
global $plugin_param;

// We need relations enabled and we work only on database.
return isset($plugin_param) && $plugin_param === 'table';
}
}

0 comments on commit ef81c79

Please sign in to comment.