diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-11-11 20:26:20 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-11-12 18:56:48 +0300 |
commit | 32d06ae22e90202a9926c8f5221c2edd79be2422 (patch) | |
tree | c4017de5fcde8bffb811349c2875a8d3dc955ffa /libraries | |
parent | 8b6140287132565a6d47d87fb62e19b084f336c9 (diff) |
Extract schema export from schema plugins
Moves the response handling to the SchemaExportController.
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
Diffstat (limited to 'libraries')
18 files changed, 105 insertions, 149 deletions
diff --git a/libraries/classes/Controllers/SchemaExportController.php b/libraries/classes/Controllers/SchemaExportController.php index 3a935fe7f2..3c2d2eb566 100644 --- a/libraries/classes/Controllers/SchemaExportController.php +++ b/libraries/classes/Controllers/SchemaExportController.php @@ -4,16 +4,18 @@ declare(strict_types=1); namespace PhpMyAdmin\Controllers; +use PhpMyAdmin\Core; use PhpMyAdmin\Dbal\DatabaseName; +use PhpMyAdmin\Exceptions\ExportException; use PhpMyAdmin\Export; use PhpMyAdmin\Html\MySQLDocumentation; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; use PhpMyAdmin\ResponseRenderer; -use RuntimeException; use function __; use function is_string; +use function mb_strlen; /** * Schema export handler @@ -51,10 +53,20 @@ class SchemaExportController * Include the appropriate Schema Class depending on $exportType, default is PDF. */ try { - $this->export->processExportSchema($db, $exportType); - } catch (RuntimeException $exception) { + $exportInfo = $this->export->getExportSchemaInfo($db, $exportType); + } catch (ExportException $exception) { $this->response->setRequestStatus(false); $this->response->addHTML(Message::error($exception->getMessage())->getDisplay()); + + return; } + + $this->response->disable(); + Core::downloadHeader( + $exportInfo['fileName'], + $exportInfo['mediaType'], + mb_strlen($exportInfo['fileData'], '8bit') + ); + echo $exportInfo['fileData']; } } diff --git a/libraries/classes/Export.php b/libraries/classes/Export.php index ed1418fd3f..69c4c5af23 100644 --- a/libraries/classes/Export.php +++ b/libraries/classes/Export.php @@ -11,9 +11,9 @@ use PhpMyAdmin\Controllers\Database\ExportController as DatabaseExportController use PhpMyAdmin\Controllers\Server\ExportController as ServerExportController; use PhpMyAdmin\Controllers\Table\ExportController as TableExportController; use PhpMyAdmin\Dbal\DatabaseName; +use PhpMyAdmin\Exceptions\ExportException; use PhpMyAdmin\Plugins\ExportPlugin; use PhpMyAdmin\Plugins\SchemaPlugin; -use RuntimeException; use function __; use function array_filter; @@ -1240,9 +1240,13 @@ class Export * get all the export options and verify * call and include the appropriate Schema Class depending on $export_type * - * @param non-empty-string $exportType format of the export + * @param non-empty-string $exportType + * + * @return array{fileName: non-empty-string, mediaType: non-empty-string, fileData: string} + * + * @throws ExportException */ - public function processExportSchema(DatabaseName $db, string $exportType): void + public function getExportSchemaInfo(DatabaseName $db, string $exportType): array { /** * default is PDF, otherwise validate it's only letters a-z @@ -1257,11 +1261,12 @@ class Export // Check schema export type if ($exportPlugin === null) { - throw new RuntimeException(__('Bad type!')); + throw new ExportException(__('Bad type!')); } $this->dbi->selectDb($db); - $exportPlugin->exportSchema($db->getName()); + + return $exportPlugin->getExportInfo($db); } private function getHTMLForRefreshButton(string $exportType): string diff --git a/libraries/classes/Pdf.php b/libraries/classes/Pdf.php index 7b93ebf83f..7ad6626320 100644 --- a/libraries/classes/Pdf.php +++ b/libraries/classes/Pdf.php @@ -13,7 +13,6 @@ use TCPDF_FONTS; use function __; use function count; -use function strlen; use function strtr; /** @@ -137,21 +136,4 @@ class Pdf extends TCPDF )->getDisplay(); exit; } - - /** - * Sends file as a download to user. - * - * @param string $filename file name - */ - public function download($filename): void - { - $pdfData = $this->getPDFData(); - ResponseRenderer::getInstance()->disable(); - Core::downloadHeader( - $filename, - 'application/pdf', - strlen($pdfData) - ); - echo $pdfData; - } } diff --git a/libraries/classes/Plugins/Schema/Dia/Dia.php b/libraries/classes/Plugins/Schema/Dia/Dia.php index db61104fb6..794fc9fd10 100644 --- a/libraries/classes/Plugins/Schema/Dia/Dia.php +++ b/libraries/classes/Plugins/Schema/Dia/Dia.php @@ -7,13 +7,9 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema\Dia; -use PhpMyAdmin\Core; -use PhpMyAdmin\ResponseRenderer; use XMLWriter; -use function ob_end_clean; -use function ob_get_clean; -use function strlen; +use function is_string; /** * This Class inherits the XMLwriter class and @@ -157,26 +153,10 @@ class Dia extends XMLWriter $this->endDocument(); } - /** - * Output Dia Document for download - * - * @see XMLWriter::flush() - * - * @param string $fileName name of the dia document - */ - public function showOutput($fileName): void + public function getOutputData(): string { - if (ob_get_clean()) { - ob_end_clean(); - } + $data = $this->flush(); - $output = $this->flush(); - ResponseRenderer::getInstance()->disable(); - Core::downloadHeader( - $fileName, - 'application/x-dia-diagram', - strlen($output) - ); - print $output; + return is_string($data) ? $data : ''; } } diff --git a/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php b/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php index 6c654253b8..b2a9c5518d 100644 --- a/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php +++ b/libraries/classes/Plugins/Schema/Dia/DiaRelationSchema.php @@ -146,11 +146,11 @@ class DiaRelationSchema extends ExportRelationSchema } /** - * Output Dia Document for download + * @return array{fileName: non-empty-string, fileData: string} */ - public function showOutput(): void + public function getExportInfo(): array { - $this->diagram->showOutput($this->getFileName('.dia')); + return ['fileName' => $this->getFileName('.dia'), 'fileData' => $this->diagram->getOutputData()]; } /** diff --git a/libraries/classes/Plugins/Schema/Eps/Eps.php b/libraries/classes/Plugins/Schema/Eps/Eps.php index 4e62f4b6ad..bf45c26e95 100644 --- a/libraries/classes/Plugins/Schema/Eps/Eps.php +++ b/libraries/classes/Plugins/Schema/Eps/Eps.php @@ -7,11 +7,6 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema\Eps; -use PhpMyAdmin\Core; -use PhpMyAdmin\ResponseRenderer; - -use function strlen; - /** * This Class is EPS Library and * helps in developing structure of EPS Schema Export @@ -234,24 +229,8 @@ class Eps $this->stringCommands .= "showpage \n"; } - /** - * Output EPS Document for download - * - * @param string $fileName name of the eps document - */ - public function showOutput($fileName): void + public function getOutputData(): string { - // if(ob_get_clean()){ - //ob_end_clean(); - //} - $output = $this->stringCommands; - ResponseRenderer::getInstance() - ->disable(); - Core::downloadHeader( - $fileName, - 'image/x-eps', - strlen($output) - ); - print $output; + return $this->stringCommands; } } diff --git a/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php b/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php index 4edddf53a8..0f1750d1dd 100644 --- a/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php +++ b/libraries/classes/Plugins/Schema/Eps/EpsRelationSchema.php @@ -153,11 +153,11 @@ class EpsRelationSchema extends ExportRelationSchema } /** - * Output Eps Document for download + * @return array{fileName: non-empty-string, fileData: string} */ - public function showOutput(): void + public function getExportInfo(): array { - $this->diagram->showOutput($this->getFileName('.eps')); + return ['fileName' => $this->getFileName('.eps'), 'fileData' => $this->diagram->getOutputData()]; } /** diff --git a/libraries/classes/Plugins/Schema/ExportRelationSchema.php b/libraries/classes/Plugins/Schema/ExportRelationSchema.php index 370e2a2e84..38ca2ea899 100644 --- a/libraries/classes/Plugins/Schema/ExportRelationSchema.php +++ b/libraries/classes/Plugins/Schema/ExportRelationSchema.php @@ -237,13 +237,11 @@ class ExportRelationSchema } /** - * Returns the file name + * @param non-empty-string $extension * - * @param string $extension file extension - * - * @return string file name + * @return non-empty-string */ - protected function getFileName($extension): string + protected function getFileName(string $extension): string { $pdfFeature = $this->relation->getRelationParameters()->pdfFeature; diff --git a/libraries/classes/Plugins/Schema/Pdf/Pdf.php b/libraries/classes/Plugins/Schema/Pdf/Pdf.php index cf675e721a..0aac8a0645 100644 --- a/libraries/classes/Plugins/Schema/Pdf/Pdf.php +++ b/libraries/classes/Plugins/Schema/Pdf/Pdf.php @@ -14,6 +14,7 @@ use PhpMyAdmin\Util; use function __; use function count; use function getcwd; +use function is_string; use function max; use function mb_ord; use function str_replace; @@ -428,4 +429,12 @@ class Pdf extends PdfLib { $this->offline = $value; } + + public function getOutputData(): string + { + /** @var mixed $data */ + $data = $this->getPDFData(); + + return is_string($data) ? $data : ''; + } } diff --git a/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php b/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php index d927bb2079..198192eeb5 100644 --- a/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php +++ b/libraries/classes/Plugins/Schema/Pdf/PdfRelationSchema.php @@ -316,11 +316,11 @@ class PdfRelationSchema extends ExportRelationSchema } /** - * Output Pdf Document for download + * @return array{fileName: non-empty-string, fileData: string} */ - public function showOutput(): void + public function getExportInfo(): array { - $this->diagram->download($this->getFileName('.pdf')); + return ['fileName' => $this->getFileName('.pdf'), 'fileData' => $this->diagram->getOutputData()]; } /** diff --git a/libraries/classes/Plugins/Schema/SchemaDia.php b/libraries/classes/Plugins/Schema/SchemaDia.php index fec1eb5d7d..66df011b44 100644 --- a/libraries/classes/Plugins/Schema/SchemaDia.php +++ b/libraries/classes/Plugins/Schema/SchemaDia.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema; +use PhpMyAdmin\Dbal\DatabaseName; use PhpMyAdmin\Plugins\Schema\Dia\DiaRelationSchema; use PhpMyAdmin\Plugins\SchemaPlugin; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; @@ -78,15 +79,17 @@ class SchemaDia extends SchemaPlugin } /** - * Exports the schema into DIA format. - * - * @param string $db database name + * @return array{fileName: non-empty-string, mediaType: non-empty-string, fileData: string} */ - public function exportSchema($db): bool + public function getExportInfo(DatabaseName $db): array { - $export = new DiaRelationSchema($db); - $export->showOutput(); + $export = new DiaRelationSchema($db->getName()); + $exportInfo = $export->getExportInfo(); - return true; + return [ + 'fileName' => $exportInfo['fileName'], + 'mediaType' => 'application/x-dia-diagram', + 'fileData' => $exportInfo['fileData'], + ]; } } diff --git a/libraries/classes/Plugins/Schema/SchemaEps.php b/libraries/classes/Plugins/Schema/SchemaEps.php index 5d9704fc4b..64f27876d1 100644 --- a/libraries/classes/Plugins/Schema/SchemaEps.php +++ b/libraries/classes/Plugins/Schema/SchemaEps.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema; +use PhpMyAdmin\Dbal\DatabaseName; use PhpMyAdmin\Plugins\Schema\Eps\EpsRelationSchema; use PhpMyAdmin\Plugins\SchemaPlugin; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; @@ -79,15 +80,17 @@ class SchemaEps extends SchemaPlugin } /** - * Exports the schema into EPS format. - * - * @param string $db database name + * @return array{fileName: non-empty-string, mediaType: non-empty-string, fileData: string} */ - public function exportSchema($db): bool + public function getExportInfo(DatabaseName $db): array { - $export = new EpsRelationSchema($db); - $export->showOutput(); + $export = new EpsRelationSchema($db->getName()); + $exportInfo = $export->getExportInfo(); - return true; + return [ + 'fileName' => $exportInfo['fileName'], + 'mediaType' => 'image/x-eps', + 'fileData' => $exportInfo['fileData'], + ]; } } diff --git a/libraries/classes/Plugins/Schema/SchemaPdf.php b/libraries/classes/Plugins/Schema/SchemaPdf.php index 3aae787dbb..692a325c6f 100644 --- a/libraries/classes/Plugins/Schema/SchemaPdf.php +++ b/libraries/classes/Plugins/Schema/SchemaPdf.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema; +use PhpMyAdmin\Dbal\DatabaseName; use PhpMyAdmin\Plugins\Schema\Pdf\PdfRelationSchema; use PhpMyAdmin\Plugins\SchemaPlugin; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; @@ -113,16 +114,18 @@ class SchemaPdf extends SchemaPlugin } /** - * Exports the schema into PDF format. - * - * @param string $db database name + * @return array{fileName: non-empty-string, mediaType: non-empty-string, fileData: string} */ - public function exportSchema($db): bool + public function getExportInfo(DatabaseName $db): array { - $export = new PdfRelationSchema($db); - $export->showOutput(); - - return true; + $export = new PdfRelationSchema($db->getName()); + $exportInfo = $export->getExportInfo(); + + return [ + 'fileName' => $exportInfo['fileName'], + 'mediaType' => 'application/pdf', + 'fileData' => $exportInfo['fileData'], + ]; } public static function isAvailable(): bool diff --git a/libraries/classes/Plugins/Schema/SchemaSvg.php b/libraries/classes/Plugins/Schema/SchemaSvg.php index bbb718ae0b..564ca9179a 100644 --- a/libraries/classes/Plugins/Schema/SchemaSvg.php +++ b/libraries/classes/Plugins/Schema/SchemaSvg.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema; +use PhpMyAdmin\Dbal\DatabaseName; use PhpMyAdmin\Plugins\Schema\Svg\SvgRelationSchema; use PhpMyAdmin\Plugins\SchemaPlugin; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; @@ -66,15 +67,17 @@ class SchemaSvg extends SchemaPlugin } /** - * Exports the schema into SVG format. - * - * @param string $db database name + * @return array{fileName: non-empty-string, mediaType: non-empty-string, fileData: string} */ - public function exportSchema($db): bool + public function getExportInfo(DatabaseName $db): array { - $export = new SvgRelationSchema($db); - $export->showOutput(); + $export = new SvgRelationSchema($db->getName()); + $exportInfo = $export->getExportInfo(); - return true; + return [ + 'fileName' => $exportInfo['fileName'], + 'mediaType' => 'image/svg+xml', + 'fileData' => $exportInfo['fileData'], + ]; } } diff --git a/libraries/classes/Plugins/Schema/Svg/Svg.php b/libraries/classes/Plugins/Schema/Svg/Svg.php index a2eb9a8657..f0fa6a3e38 100644 --- a/libraries/classes/Plugins/Schema/Svg/Svg.php +++ b/libraries/classes/Plugins/Schema/Svg/Svg.php @@ -7,14 +7,12 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins\Schema\Svg; -use PhpMyAdmin\Core; -use PhpMyAdmin\ResponseRenderer; use XMLWriter; use function intval; use function is_int; +use function is_string; use function sprintf; -use function strlen; /** * This Class inherits the XMLwriter class and @@ -164,29 +162,11 @@ class Svg extends XMLWriter $this->endDocument(); } - /** - * output RelationStatsSvg Document - * - * svg document prompted to the user for download - * RelationStatsSvg document saved in .svg extension and can be - * easily changeable by using any svg IDE - * - * @see XMLWriter::startElement() - * @see XMLWriter::writeAttribute() - * - * @param string $fileName file name - */ - public function showOutput($fileName): void + public function getOutputData(): string { - //ob_get_clean(); - $output = $this->flush(); - ResponseRenderer::getInstance()->disable(); - Core::downloadHeader( - $fileName, - 'image/svg+xml', - strlen($output) - ); - print $output; + $data = $this->flush(); + + return is_string($data) ? $data : ''; } /** diff --git a/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php b/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php index 1d4b6eee7e..f6a26b3fae 100644 --- a/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php +++ b/libraries/classes/Plugins/Schema/Svg/SvgRelationSchema.php @@ -176,11 +176,11 @@ class SvgRelationSchema extends ExportRelationSchema } /** - * Output RelationStatsSvg Document for download + * @return array{fileName: non-empty-string, fileData: string} */ - public function showOutput(): void + public function getExportInfo(): array { - $this->diagram->showOutput($this->getFileName('.svg')); + return ['fileName' => $this->getFileName('.svg'), 'fileData' => $this->diagram->getOutputData()]; } /** diff --git a/libraries/classes/Plugins/SchemaPlugin.php b/libraries/classes/Plugins/SchemaPlugin.php index e2992e5032..79f17facd2 100644 --- a/libraries/classes/Plugins/SchemaPlugin.php +++ b/libraries/classes/Plugins/SchemaPlugin.php @@ -7,6 +7,7 @@ declare(strict_types=1); namespace PhpMyAdmin\Plugins; +use PhpMyAdmin\Dbal\DatabaseName; use PhpMyAdmin\Properties\Options\Groups\OptionsPropertyMainGroup; use PhpMyAdmin\Properties\Options\Items\BoolPropertyItem; use PhpMyAdmin\Properties\Plugins\PluginPropertyItem; @@ -58,11 +59,9 @@ abstract class SchemaPlugin implements Plugin abstract protected function setProperties(): SchemaPluginProperties; /** - * Exports the schema into the specified format. - * - * @param string $db database name + * @return array{fileName: non-empty-string, mediaType: non-empty-string, fileData: string} */ - abstract public function exportSchema($db): bool; + abstract public function getExportInfo(DatabaseName $db): array; /** * Adds export options common to all plugins. diff --git a/libraries/classes/ResponseRenderer.php b/libraries/classes/ResponseRenderer.php index 4306973cc8..765d3dbbd2 100644 --- a/libraries/classes/ResponseRenderer.php +++ b/libraries/classes/ResponseRenderer.php @@ -69,7 +69,7 @@ class ResponseRenderer * * @var bool */ - private $isDisabled; + protected $isDisabled; /** * Whether there were any errors during the processing of the request * Only used for ajax responses |