diff options
author | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-11-10 22:18:26 +0300 |
---|---|---|
committer | Maurício Meneghini Fauth <mauricio@fauth.dev> | 2022-11-10 22:18:26 +0300 |
commit | 5df50f6a1140b6490710b8acf51a6c6f6669ec55 (patch) | |
tree | 855e99210668adb49ad145c0feb28ceec1aa2bf5 | |
parent | 7b723d63185632d556242f72a4676472ee32d55f (diff) |
Refactor Tracking export download to remove `exit`
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
-rw-r--r-- | libraries/classes/Controllers/Table/TrackingController.php | 9 | ||||
-rw-r--r-- | libraries/classes/Tracking.php | 25 | ||||
-rw-r--r-- | phpstan-baseline.neon | 10 | ||||
-rw-r--r-- | psalm-baseline.xml | 12 | ||||
-rw-r--r-- | test/classes/TrackingTest.php | 25 |
5 files changed, 46 insertions, 35 deletions
diff --git a/libraries/classes/Controllers/Table/TrackingController.php b/libraries/classes/Controllers/Table/TrackingController.php index db5dbdc2ad..2857956803 100644 --- a/libraries/classes/Controllers/Table/TrackingController.php +++ b/libraries/classes/Controllers/Table/TrackingController.php @@ -6,6 +6,7 @@ namespace PhpMyAdmin\Controllers\Table; use DateTimeImmutable; use PhpMyAdmin\Controllers\AbstractController; +use PhpMyAdmin\Core; use PhpMyAdmin\DbTableExists; use PhpMyAdmin\Http\ServerRequest; use PhpMyAdmin\Message; @@ -25,6 +26,7 @@ use function explode; use function htmlspecialchars; use function in_array; use function is_array; +use function mb_strlen; use function sprintf; final class TrackingController extends AbstractController @@ -135,7 +137,12 @@ final class TrackingController extends AbstractController // Export as file download if ($reportExport !== null && $request->getParsedBodyParam('export_type') === 'sqldumpfile') { - $this->tracking->exportAsFileDownload($tableParam, $GLOBALS['entries']); + $downloadInfo = $this->tracking->getDownloadInfoForExport($tableParam, $GLOBALS['entries']); + $this->response->disable(); + Core::downloadHeader($downloadInfo['filename'], 'text/x-sql', mb_strlen($downloadInfo['dump'])); + echo $downloadInfo['dump']; + + return; } $actionMessage = ''; diff --git a/libraries/classes/Tracking.php b/libraries/classes/Tracking.php index 3507cefa9f..1ffc76e6b6 100644 --- a/libraries/classes/Tracking.php +++ b/libraries/classes/Tracking.php @@ -27,7 +27,6 @@ use function mb_strstr; use function preg_replace; use function rtrim; use function sprintf; -use function strlen; use function strtotime; use const SORT_ASC; @@ -847,35 +846,25 @@ class Tracking } /** - * Function to export as entries + * @param array<int, array<string, int|string>> $entries * - * @param array $entries entries + * @return array<string, string> + * @psalm-return array{filename: non-empty-string, dump: non-empty-string} */ - public function exportAsFileDownload(string $table, array $entries): void + public function getDownloadInfoForExport(string $table, array $entries): array { ini_set('url_rewriter.tags', ''); // Replace all multiple whitespaces by a single space - $table = htmlspecialchars(preg_replace('/\s+/', ' ', $table)); - $dump = '# ' . sprintf( - __('Tracking report for table `%s`'), - $table - ) - . "\n" . '# ' . date('Y-m-d H:i:s') . "\n"; + $table = htmlspecialchars((string) preg_replace('/\s+/', ' ', $table)); + $dump = '# ' . sprintf(__('Tracking report for table `%s`'), $table) . "\n" . '# ' . date('Y-m-d H:i:s') . "\n"; foreach ($entries as $entry) { $dump .= $entry['statement']; } $filename = 'log_' . $table . '.sql'; - ResponseRenderer::getInstance()->disable(); - Core::downloadHeader( - $filename, - 'text/x-sql', - strlen($dump) - ); - echo $dump; - exit; + return ['filename' => $filename, 'dump' => $dump]; } /** diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 3b21186df8..c3585ed4c8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -8361,11 +8361,6 @@ parameters: path: libraries/classes/Tracking.php - - message: "#^Method PhpMyAdmin\\\\Tracking\\:\\:exportAsFileDownload\\(\\) has parameter \\$entries with no value type specified in iterable type array\\.$#" - count: 1 - path: libraries/classes/Tracking.php - - - message: "#^Method PhpMyAdmin\\\\Tracking\\:\\:exportAsSqlDump\\(\\) has parameter \\$entries with no value type specified in iterable type array\\.$#" count: 1 path: libraries/classes/Tracking.php @@ -8576,11 +8571,6 @@ parameters: path: libraries/classes/Tracking.php - - message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, string\\|null given\\.$#" - count: 1 - path: libraries/classes/Tracking.php - - - message: "#^Parameter \\#1 \\$var of function count expects array\\|Countable, mixed given\\.$#" count: 1 path: libraries/classes/Tracking.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 6a888b10e0..26c795e106 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -4167,6 +4167,9 @@ <MixedArgument occurrences="1"> <code>$version</code> </MixedArgument> + <MixedArgumentTypeCoercion occurrences="1"> + <code>$GLOBALS['entries']</code> + </MixedArgumentTypeCoercion> <MixedAssignment occurrences="9"> <code>$GLOBALS['entries']</code> <code>$GLOBALS['errorUrl']</code> @@ -14071,7 +14074,7 @@ <code>$value['Name']</code> </MixedArgument> <MixedArgumentTypeCoercion occurrences="1"/> - <MixedArrayAccess occurrences="17"> + <MixedArrayAccess occurrences="16"> <code>$data[$which_log][$delete_id]</code> <code>$entry['date']</code> <code>$entry['date']</code> @@ -14079,7 +14082,6 @@ <code>$entry['statement']</code> <code>$entry['statement']</code> <code>$entry['statement']</code> - <code>$entry['statement']</code> <code>$entry['username']</code> <code>$entry['username']</code> <code>$entry['username']</code> @@ -14095,7 +14097,7 @@ <code>$entry['line_number']</code> <code>$entry['url_params']</code> </MixedArrayAssignment> - <MixedAssignment occurrences="18"> + <MixedAssignment occurrences="17"> <code>$columns</code> <code>$drop_create_statements</code> <code>$entries[]</code> @@ -14103,7 +14105,6 @@ <code>$entry</code> <code>$entry</code> <code>$entry</code> - <code>$entry</code> <code>$ids[$key]</code> <code>$indexes</code> <code>$row</code> @@ -14118,11 +14119,10 @@ <MixedInferredReturnType occurrences="1"> <code>string</code> </MixedInferredReturnType> - <MixedOperand occurrences="5"> + <MixedOperand occurrences="4"> <code>$drop_create_statements</code> <code>$entry['statement']</code> <code>$entry['statement']</code> - <code>$entry['statement']</code> <code>$temp</code> </MixedOperand> <MixedReturnStatement occurrences="1"> diff --git a/test/classes/TrackingTest.php b/test/classes/TrackingTest.php index 3238241344..6a00ba318e 100644 --- a/test/classes/TrackingTest.php +++ b/test/classes/TrackingTest.php @@ -7,6 +7,7 @@ namespace PhpMyAdmin\Tests; use DateTimeImmutable; use PhpMyAdmin\ConfigStorage\Relation; use PhpMyAdmin\ConfigStorage\RelationParameters; +use PhpMyAdmin\DatabaseInterface; use PhpMyAdmin\SqlQueryForm; use PhpMyAdmin\Template; use PhpMyAdmin\Tracking; @@ -14,7 +15,11 @@ use PhpMyAdmin\Url; use function __; use function _pgettext; +use function date; use function htmlspecialchars; +use function ini_get; +use function ini_restore; +use function ini_set; use function sprintf; /** @@ -584,4 +589,24 @@ class TrackingTest extends AbstractTestCase $this->assertEquals('username3', $entries[0]['username']); $this->assertEquals('statement1', $entries[0]['statement']); } + + public function testGetDownloadInfoForExport(): void + { + $tracking = new Tracking( + $this->createStub(SqlQueryForm::class), + $this->createStub(Template::class), + $this->createStub(Relation::class), + $this->createStub(DatabaseInterface::class) + ); + ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,form=,fieldset='); + $entries = [['statement' => 'first statement'], ['statement' => 'second statement']]; + $expectedDump = '# Tracking report for table `test> table`' . "\n" + . '# ' . date('Y-m-d H:i:s') . "\n" + . 'first statementsecond statement'; + $actual = $tracking->getDownloadInfoForExport('test> table', $entries); + $this->assertSame('log_test> table.sql', $actual['filename']); + $this->assertSame($expectedDump, $actual['dump']); + $this->assertSame('', ini_get('url_rewriter.tags')); + ini_restore('url_rewriter.tags'); + } } |