From 75397c913dcdfd34679d6223ccd3568655bbc63b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 3 Aug 2020 23:56:20 -0300 Subject: [PATCH] Refactor Display\Export class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Uses template inheritance for the export templates. - Renames Display\Export to Export\Options. Signed-off-by: MaurĂ­cio Meneghini Fauth --- .../Controllers/Database/ExportController.php | 39 ++-- .../Controllers/ExportTemplateController.php | 2 +- .../Controllers/Server/ExportController.php | 40 +++- .../Controllers/Table/ExportController.php | 36 +++- .../Export.php => Export/Options.php} | 77 ++----- libraries/services.php | 8 +- libraries/services_controllers.php | 4 +- templates/database/export/index.twig | 65 +++++- templates/database/export/multi_values.twig | 41 ---- templates/display/export/select_options.twig | 19 -- .../export/display.twig => export.twig} | 49 ++--- .../export/template_options.twig | 0 templates/server/export/index.twig | 35 +++- templates/table/export/index.twig | 11 +- .../ExportTemplateControllerTest.php | 2 +- test/classes/Display/ExportTest.php | 197 ------------------ test/classes/Export/OptionsTest.php | 150 +++++++++++++ 17 files changed, 374 insertions(+), 401 deletions(-) rename libraries/classes/{Display/Export.php => Export/Options.php} (79%) delete mode 100644 templates/database/export/multi_values.twig delete mode 100644 templates/display/export/select_options.twig rename templates/{display/export/display.twig => export.twig} (89%) rename templates/{display => }/export/template_options.twig (100%) delete mode 100644 test/classes/Display/ExportTest.php create mode 100644 test/classes/Export/OptionsTest.php diff --git a/libraries/classes/Controllers/Database/ExportController.php b/libraries/classes/Controllers/Database/ExportController.php index 5db1a60cbd..b711ba2e16 100644 --- a/libraries/classes/Controllers/Database/ExportController.php +++ b/libraries/classes/Controllers/Database/ExportController.php @@ -7,13 +7,16 @@ namespace PhpMyAdmin\Controllers\Database; use PhpMyAdmin\Common; use PhpMyAdmin\Config\PageSettings; use PhpMyAdmin\DatabaseInterface; -use PhpMyAdmin\Display\Export as DisplayExport; use PhpMyAdmin\Export; +use PhpMyAdmin\Export\Options; use PhpMyAdmin\Message; +use PhpMyAdmin\Plugins; +use PhpMyAdmin\Plugins\ExportPlugin; use PhpMyAdmin\Response; use PhpMyAdmin\Template; use PhpMyAdmin\Url; use PhpMyAdmin\Util; +use function array_merge; use function is_array; final class ExportController extends AbstractController @@ -121,11 +124,6 @@ final class ExportController extends AbstractController ]; } - $multi_values = $this->template->render('database/export/multi_values', [ - 'structure_or_data_forced' => $_POST['structure_or_data_forced'] ?? 0, - 'tables' => $tablesForMultiValues, - ]); - if (! isset($sql_query)) { $sql_query = ''; } @@ -143,21 +141,38 @@ final class ExportController extends AbstractController $export_type = 'database'; } - $displayExport = new DisplayExport(); - $display = $displayExport->getDisplay( + $GLOBALS['single_table'] = $_POST['single_table'] ?? $_GET['single_table'] ?? null; + + /** @var ExportPlugin[] $exportList */ + $exportList = Plugins::getPlugins('export', 'libraries/classes/Plugins/Export/', [ + 'export_type' => $export_type, + 'single_table' => isset($GLOBALS['single_table']), + ]); + + if (empty($exportList)) { + $this->response->addHTML(Message::error( + __('Could not load export plugins, please check your installation!') + )->getDisplay()); + + return; + } + + $displayExport = new Options(); + $options = $displayExport->getOptions( $export_type, $db, $table, $sql_query, $num_tables, $unlim_num_rows, - $multi_values + $exportList ); - $this->render('database/export/index', [ + $this->render('database/export/index', array_merge($options, [ 'page_settings_error_html' => $pageSettingsErrorHtml, 'page_settings_html' => $pageSettingsHtml, - 'display' => $display, - ]); + 'structure_or_data_forced' => $_POST['structure_or_data_forced'] ?? 0, + 'tables' => $tablesForMultiValues, + ])); } } diff --git a/libraries/classes/Controllers/ExportTemplateController.php b/libraries/classes/Controllers/ExportTemplateController.php index e7b3766a65..05168748f3 100644 --- a/libraries/classes/Controllers/ExportTemplateController.php +++ b/libraries/classes/Controllers/ExportTemplateController.php @@ -72,7 +72,7 @@ final class ExportTemplateController extends AbstractController $this->response->setRequestStatus(true); $this->response->addJSON( 'data', - $this->template->render('display/export/template_options', [ + $this->template->render('export/template_options', [ 'templates' => is_array($templates) ? $templates : [], 'selected_template' => $_POST['template_id'] ?? null, ]) diff --git a/libraries/classes/Controllers/Server/ExportController.php b/libraries/classes/Controllers/Server/ExportController.php index 0006b3839e..96e4cb0d5d 100644 --- a/libraries/classes/Controllers/Server/ExportController.php +++ b/libraries/classes/Controllers/Server/ExportController.php @@ -8,22 +8,26 @@ use PhpMyAdmin\Common; use PhpMyAdmin\Config\PageSettings; use PhpMyAdmin\Controllers\AbstractController; use PhpMyAdmin\DatabaseInterface; -use PhpMyAdmin\Display\Export; +use PhpMyAdmin\Export\Options; +use PhpMyAdmin\Message; +use PhpMyAdmin\Plugins; +use PhpMyAdmin\Plugins\ExportPlugin; use PhpMyAdmin\Response; use PhpMyAdmin\Template; +use function array_merge; final class ExportController extends AbstractController { - /** @var Export */ + /** @var Options */ private $export; /** * @param Response $response A Response instance. * @param DatabaseInterface $dbi A DatabaseInterface instance. * @param Template $template A Template instance. - * @param Export $export A Export instance. + * @param Options $export A Export instance. */ - public function __construct($response, $dbi, Template $template, Export $export) + public function __construct($response, $dbi, Template $template, Options $export) { parent::__construct($response, $dbi, $template); $this->export = $export; @@ -43,7 +47,7 @@ final class ExportController extends AbstractController $this->addScriptFiles(['export.js']); $select_item = $tmp_select ?? ''; - $multi_values = $this->export->getHtmlForSelectOptions($select_item); + $databases = $this->export->getDatabasesForSelectOptions($select_item); if (! isset($sql_query)) { $sql_query = ''; @@ -55,20 +59,36 @@ final class ExportController extends AbstractController $unlim_num_rows = 0; } - $display = $this->export->getDisplay( + $GLOBALS['single_table'] = $_POST['single_table'] ?? $_GET['single_table'] ?? null; + + /** @var ExportPlugin[] $exportList */ + $exportList = Plugins::getPlugins('export', 'libraries/classes/Plugins/Export/', [ + 'export_type' => 'server', + 'single_table' => isset($GLOBALS['single_table']), + ]); + + if (empty($exportList)) { + $this->response->addHTML(Message::error( + __('Could not load export plugins, please check your installation!') + )->getDisplay()); + + return; + } + + $options = $this->export->getOptions( 'server', $db, $table, $sql_query, $num_tables, $unlim_num_rows, - $multi_values + $exportList ); - $this->render('server/export/index', [ + $this->render('server/export/index', array_merge($options, [ 'page_settings_error_html' => $pageSettingsErrorHtml, 'page_settings_html' => $pageSettingsHtml, - 'display' => $display, - ]); + 'databases' => $databases, + ])); } } diff --git a/libraries/classes/Controllers/Table/ExportController.php b/libraries/classes/Controllers/Table/ExportController.php index 16f3d51e10..0c63072e71 100644 --- a/libraries/classes/Controllers/Table/ExportController.php +++ b/libraries/classes/Controllers/Table/ExportController.php @@ -7,21 +7,24 @@ namespace PhpMyAdmin\Controllers\Table; use PhpMyAdmin\Common; use PhpMyAdmin\Config\PageSettings; use PhpMyAdmin\DatabaseInterface; -use PhpMyAdmin\Display\Export; +use PhpMyAdmin\Export\Options; use PhpMyAdmin\Html\Generator; use PhpMyAdmin\Message; +use PhpMyAdmin\Plugins; +use PhpMyAdmin\Plugins\ExportPlugin; use PhpMyAdmin\Response; use PhpMyAdmin\SqlParser\Parser; use PhpMyAdmin\SqlParser\Statements\SelectStatement; use PhpMyAdmin\SqlParser\Utils\Query; use PhpMyAdmin\Template; use PhpMyAdmin\Url; +use function array_merge; use function implode; use function is_array; class ExportController extends AbstractController { - /** @var Export */ + /** @var Options */ private $export; /** @@ -30,7 +33,7 @@ class ExportController extends AbstractController * @param Template $template A Template instance. * @param string $db Database name. * @param string $table Table name. - * @param Export $export An Export instance. + * @param Options $export An Export instance. */ public function __construct( $response, @@ -38,7 +41,7 @@ class ExportController extends AbstractController Template $template, $db, $table, - Export $export + Options $export ) { parent::__construct($response, $dbi, $template, $db, $table); $this->export = $export; @@ -112,22 +115,37 @@ class ExportController extends AbstractController $unlim_num_rows = 0; } - $display = $this->export->getDisplay( + $GLOBALS['single_table'] = $_POST['single_table'] ?? $_GET['single_table'] ?? null; + + /** @var ExportPlugin[] $exportList */ + $exportList = Plugins::getPlugins('export', 'libraries/classes/Plugins/Export/', [ + 'export_type' => 'table', + 'single_table' => isset($GLOBALS['single_table']), + ]); + + if (empty($exportList)) { + $this->response->addHTML(Message::error( + __('Could not load export plugins, please check your installation!') + )->getDisplay()); + + return; + } + + $options = $this->export->getOptions( 'table', $db, $table, $sql_query, $num_tables, $unlim_num_rows, - '' + $exportList ); - $this->render('table/export/index', [ + $this->render('table/export/index', array_merge($options, [ 'page_settings_error_html' => $pageSettingsErrorHtml, 'page_settings_html' => $pageSettingsHtml, 'message' => $message, - 'display' => $display, - ]); + ])); } public function rows(): void diff --git a/libraries/classes/Display/Export.php b/libraries/classes/Export/Options.php similarity index 79% rename from libraries/classes/Display/Export.php rename to libraries/classes/Export/Options.php index 688d54d79a..112f362267 100644 --- a/libraries/classes/Display/Export.php +++ b/libraries/classes/Export/Options.php @@ -1,22 +1,16 @@ relation = new Relation($GLOBALS['dbi']); - $this->template = new Template(); $this->templateModel = new TemplateModel($GLOBALS['dbi']); } @@ -65,9 +52,9 @@ class Export * * @param string $tmpSelect Tmp selected method of export * - * @return string + * @return array */ - public function getHtmlForSelectOptions($tmpSelect = '') + public function getDatabasesForSelectOptions($tmpSelect = '') { // Check if the selected databases are defined in $_POST // (from clicking Back button on /export page) @@ -102,62 +89,33 @@ class Export ]; } - return $this->template->render('display/export/select_options', ['databases' => $databases]); + return $databases; } /** - * Gets HTML to display export dialogs + * @param string $exportType export type: server|database|table + * @param string $db selected DB + * @param string $table selected table + * @param string $sqlQuery SQL query + * @param int|string $numTables number of tables + * @param int|string $unlimNumRows unlimited number of rows + * @param ExportPlugin[] $exportList * - * @param string $exportType export type: server|database|table - * @param string $db selected DB - * @param string $table selected table - * @param string $sqlQuery SQL query - * @param int|string $numTables number of tables - * @param int|string $unlimNumRows unlimited number of rows - * @param string $multiValues selector options - * - * @return string + * @return array */ - public function getDisplay( + public function getOptions( $exportType, $db, $table, $sqlQuery, $numTables, $unlimNumRows, - $multiValues + array $exportList ) { global $cfg; $cfgRelation = $this->relation->getRelationsParam(); - if (isset($_POST['single_table'])) { - $GLOBALS['single_table'] = $_POST['single_table']; - } - - // Export a single table - if (isset($_GET['single_table'])) { - $GLOBALS['single_table'] = $_GET['single_table']; - } - - /* Scan for plugins */ - /** @var ExportPlugin[] $exportList */ - $exportList = Plugins::getPlugins( - 'export', - 'libraries/classes/Plugins/Export/', - [ - 'export_type' => $exportType, - 'single_table' => isset($GLOBALS['single_table']), - ] - ); - - /* Fail if we didn't find any plugin */ - if (empty($exportList)) { - return Message::error( - __('Could not load export plugins, please check your installation!') - )->getDisplay(); - } - $templates = []; if ($cfgRelation['exporttemplateswork']) { @@ -212,7 +170,7 @@ class Export $hiddenInputs['sql_query'] = $sqlQuery; } - return $this->template->render('display/export/display', [ + return [ 'export_type' => $exportType, 'db' => $db, 'table' => $table, @@ -225,7 +183,6 @@ class Export 'hidden_inputs' => $hiddenInputs, 'export_method' => $_POST['quick_or_custom'] ?? $cfg['Export']['method'] ?? '', 'dropdown' => $dropdown, - 'multi_values' => $multiValues, 'options' => Plugins::getOptions('Export', $exportList), 'can_convert_kanji' => Encoding::canConvertKanji(), 'exec_time_limit' => $cfg['ExecTimeLimit'], @@ -252,7 +209,7 @@ class Export 'has_gzip' => $cfg['GZipDump'] && function_exists('gzencode'), 'selected_compression' => $selectedCompression, 'filename_template' => $filenameTemplate, - ]); + ]; } private function getFileNameTemplate(string $exportType, ?string $filename = null): string diff --git a/libraries/services.php b/libraries/services.php index 27c7492a18..a639403ca8 100644 --- a/libraries/services.php +++ b/libraries/services.php @@ -63,10 +63,6 @@ return [ 'relation' => '@relation', ], ], - 'display_export' => - [ - 'class' => PhpMyAdmin\Display\Export::class, - ], 'error_handler' => [ 'class' => PhpMyAdmin\ErrorHandler::class, @@ -97,6 +93,10 @@ return [ 'arguments' => ['@dbi'], ], + 'export_options' => + [ + 'class' => PhpMyAdmin\Export\Options::class, + ], 'export_template_model' => [ 'class' => PhpMyAdmin\Export\TemplateModel::class, diff --git a/libraries/services_controllers.php b/libraries/services_controllers.php index 854f42460e..474275d387 100644 --- a/libraries/services_controllers.php +++ b/libraries/services_controllers.php @@ -558,7 +558,7 @@ return [ 'response' => '@response', 'dbi' => '@dbi', 'template' => '@template', - 'export' => '@display_export', + 'export' => '@export_options', ], ], PhpMyAdmin\Controllers\Server\ImportController::class => @@ -795,7 +795,7 @@ return [ 'template' => '@template', 'db' => '%db%', 'table' => '%table%', - 'export' => '@display_export', + 'export' => '@export_options', ], ], PhpMyAdmin\Controllers\Table\FindReplaceController::class => diff --git a/templates/database/export/index.twig b/templates/database/export/index.twig index 941c55bbd3..43762e0431 100644 --- a/templates/database/export/index.twig +++ b/templates/database/export/index.twig @@ -1,4 +1,63 @@ -{{ page_settings_error_html|raw }} -{{ page_settings_html|raw }} +{% extends 'export.twig' %} -{{ display|raw }} +{% block title %} + {% if export_type == 'raw' %} + {% trans %}Exporting a raw query{% notes %}A query that the user has written freely{% endtrans %} + {% else %} + {{ 'Exporting tables from "%s" database'|trans|format(db) }} + {% endif %} +{% endblock %} + +{% block selection_options %} + {% if export_type != 'raw' %} +
+

{% trans 'Tables:' %}

+ +
+ + + + + + + + + + + + + + + + + + + + {% for each_table in tables %} + + + + + + + {% endfor %} + +
{% trans 'Tables' %}{% trans 'Structure' %}{% trans 'Data' %}
{% trans 'Select all' %} + + + +
+ + {{ each_table.name }} + + + +
+
+
+ {% endif %} +{% endblock %} + +{% set filename_hint %} + {% trans '@SERVER@ will become the server name and @DATABASE@ will become the database name.' %} +{% endset %} diff --git a/templates/database/export/multi_values.twig b/templates/database/export/multi_values.twig deleted file mode 100644 index 31cf59c9c3..0000000000 --- a/templates/database/export/multi_values.twig +++ /dev/null @@ -1,41 +0,0 @@ -
- - - - - - - - - - - - - - - - - - - - {% for table in tables %} - - - - - - - {% endfor %} - -
{% trans 'Tables' %}{% trans 'Structure' %}{% trans 'Data' %}
{% trans 'Select all' %} - - - -
- - {{ table.name }} - - - -
-
diff --git a/templates/display/export/select_options.twig b/templates/display/export/select_options.twig deleted file mode 100644 index 4ca00baa3c..0000000000 --- a/templates/display/export/select_options.twig +++ /dev/null @@ -1,19 +0,0 @@ -
-

- - {% trans 'Select all' %} - - / - - {% trans 'Unselect all' %} - -

- - -
diff --git a/templates/display/export/display.twig b/templates/export.twig similarity index 89% rename from templates/display/export/display.twig rename to templates/export.twig index 193f685715..31de124d89 100644 --- a/templates/display/export/display.twig +++ b/templates/export.twig @@ -1,15 +1,12 @@ +{{ page_settings_error_html|raw }} +{{ page_settings_html|raw }} + +{% block message %}{% endblock %} + @@ -30,10 +27,12 @@

{% trans 'Existing templates:' %}

@@ -96,16 +95,7 @@ {{ dropdown|raw }} -
- {% if export_type == 'server' %} -

{% trans 'Databases:' %}

- {% elseif export_type == 'database' %} -

{% trans 'Tables:' %}

- {% endif %} - {% if multi_values is not empty %} - {{ multi_values|raw }} - {% endif %} -
+ {% block selection_options %}{% endblock %} {% if rows is not empty %}
@@ -331,18 +321,7 @@
  • diff --git a/templates/display/export/template_options.twig b/templates/export/template_options.twig similarity index 100% rename from templates/display/export/template_options.twig rename to templates/export/template_options.twig diff --git a/templates/server/export/index.twig b/templates/server/export/index.twig index 941c55bbd3..b5bd4c878d 100644 --- a/templates/server/export/index.twig +++ b/templates/server/export/index.twig @@ -1,4 +1,33 @@ -{{ page_settings_error_html|raw }} -{{ page_settings_html|raw }} +{% extends 'export.twig' %} -{{ display|raw }} +{% block title %}{% trans 'Exporting databases from the current server' %}{% endblock %} + +{% block selection_options %} +
    +

    {% trans 'Databases:' %}

    + +
    +

    + + {% trans 'Select all' %} + + / + + {% trans 'Unselect all' %} + +

    + + +
    +
    +{% endblock %} + +{% set filename_hint %} + {% trans '@SERVER@ will become the server name.' %} +{% endset %} diff --git a/templates/table/export/index.twig b/templates/table/export/index.twig index f118d8a51f..6e625dd8a8 100644 --- a/templates/table/export/index.twig +++ b/templates/table/export/index.twig @@ -1,6 +1,9 @@ -{{ page_settings_error_html|raw }} -{{ page_settings_html|raw }} +{% extends 'export.twig' %} -{{ message|raw }} +{% block message %}{{ message|raw }}{% endblock %} -{{ display|raw }} +{% block title %}{{ 'Exporting rows from "%s" table'|trans|format(table) }}{% endblock %} + +{% set filename_hint %} + {% trans '@SERVER@ will become the server name, @DATABASE@ will become the database name and @TABLE@ will become the table name.' %} +{% endset %} diff --git a/test/classes/Controllers/ExportTemplateControllerTest.php b/test/classes/Controllers/ExportTemplateControllerTest.php index fb3c44d1aa..9af63454dc 100644 --- a/test/classes/Controllers/ExportTemplateControllerTest.php +++ b/test/classes/Controllers/ExportTemplateControllerTest.php @@ -85,7 +85,7 @@ class ExportTemplateControllerTest extends AbstractTestCase ]), ]; - $options = $this->template->render('display/export/template_options', [ + $options = $this->template->render('export/template_options', [ 'templates' => $templates, 'selected_template' => null, ]); diff --git a/test/classes/Display/ExportTest.php b/test/classes/Display/ExportTest.php deleted file mode 100644 index 781a48e741..0000000000 --- a/test/classes/Display/ExportTest.php +++ /dev/null @@ -1,197 +0,0 @@ -enableBc(); - $GLOBALS['cfg']['Server']['host'] = 'localhost'; - $GLOBALS['cfg']['Server']['user'] = 'pma_user'; - $GLOBALS['server'] = 0; - - $GLOBALS['table'] = 'table'; - $GLOBALS['db'] = 'PMA'; - - //$_SESSION - $_SESSION['relation'][$GLOBALS['server']] = ''; - - $pmaconfig = $this->getMockBuilder(Config::class) - ->disableOriginalConstructor() - ->getMock(); - - $pmaconfig->expects($this->any()) - ->method('getUserValue') - ->will($this->returnValue('user value for test')); - - $GLOBALS['PMA_Config'] = $pmaconfig; - - $this->export = new Export(); - } - - /** - * Test for Export::getHtmlForOptions - */ - public function testGetHtmlForOptions(): void - { - global $cfg; - $cfg['Export']['method'] = 'XML'; - $cfg['SaveDir'] = '/tmp'; - - $export_type = 'server'; - $db = 'PMA'; - $table = 'PMA_test'; - $multi_values_str = 'multi_values_str'; - $num_tables_str = '10'; - $unlim_num_rows_str = 'unlim_num_rows_str'; - //$single_table = "single_table"; - $GLOBALS['dbi']->getCache()->cacheTableContent([$db, $table, 'ENGINE'], 'MERGE'); - - $columns_info = [ - 'test_column1' => ['COLUMN_NAME' => 'test_column1'], - 'test_column2' => ['COLUMN_NAME' => 'test_column2'], - ]; - $dbi = $this->getMockBuilder(DatabaseInterface::class) - ->disableOriginalConstructor() - ->getMock(); - - $dbi->expects($this->any())->method('getColumnsFull') - ->will($this->returnValue($columns_info)); - $dbi->expects($this->any())->method('getCompatibilities') - ->will($this->returnValue([])); - - $GLOBALS['dbi'] = $dbi; - - //Call the test function - $html = $this->export->getDisplay( - $export_type, - $db, - $table, - '', - $num_tables_str, - $unlim_num_rows_str, - $multi_values_str - ); - - //validate 2: Export::getHtmlForOptionsMethod - $this->assertStringContainsString( - $cfg['Export']['method'], - $html - ); - $this->assertStringContainsString( - '
    ', - $html - ); - $this->assertStringContainsString( - __('Export method:'), - $html - ); - $this->assertStringContainsString( - __('Custom - display all possible options'), - $html - ); - - //validate 3: Export::getHtmlForOptionsSelection - $this->assertStringContainsString( - '
    ', - $html - ); - $this->assertStringContainsString( - '

    ' . __('Databases:') . '

    ', - $html - ); - $this->assertStringContainsString( - $multi_values_str, - $html - ); - - //validate 4: Export::getHtmlForOptionsQuickExport - $this->assertStringContainsString( - 'assertStringContainsString( - 'Save on server in the directory ' . $dir . '', - $html - ); - - //validate 5: Export::getHtmlForAliasModalDialog - $this->assertStringContainsString( - '
    ', - $html - ); - $this->assertStringContainsString( - 'Select database', - $html - ); - $this->assertStringContainsString( - 'Select table', - $html - ); - $this->assertStringContainsString( - 'New database name', - $html - ); - $this->assertStringContainsString( - 'New table name', - $html - ); - - $this->assertStringContainsString( - '
    ', - $html - ); - - $this->assertStringContainsString('