Skip to content

Commit 505d89c

Browse files
Merge pull request #19099 from kamil-tekiela/Refactoring-import-15
Refactoring import 15
2 parents fa4db41 + cfa9bea commit 505d89c

9 files changed

Lines changed: 29 additions & 109 deletions

File tree

phpstan-baseline.neon

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8275,9 +8275,14 @@ parameters:
82758275
count: 5
82768276
path: src/Html/Generator.php
82778277

8278+
-
8279+
message: "#^Cannot cast mixed to string\\.$#"
8280+
count: 2
8281+
path: src/Html/Generator.php
8282+
82788283
-
82798284
message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#"
8280-
count: 16
8285+
count: 14
82818286
path: src/Html/Generator.php
82828287

82838288
-
@@ -8310,46 +8315,16 @@ parameters:
83108315
count: 2
83118316
path: src/Html/Generator.php
83128317

8313-
-
8314-
message: "#^Parameter \\#1 \\$sqlQuery of static method PhpMyAdmin\\\\Html\\\\Generator\\:\\:formatSql\\(\\) expects string, mixed given\\.$#"
8315-
count: 1
8316-
path: src/Html/Generator.php
8317-
8318-
-
8319-
message: "#^Parameter \\#1 \\$string of function addslashes expects string, mixed given\\.$#"
8320-
count: 1
8321-
path: src/Html/Generator.php
8322-
83238318
-
83248319
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, \\(int\\|string\\) given\\.$#"
83258320
count: 1
83268321
path: src/Html/Generator.php
83278322

8328-
-
8329-
message: "#^Parameter \\#1 \\$string of function htmlspecialchars expects string, mixed given\\.$#"
8330-
count: 1
8331-
path: src/Html/Generator.php
8332-
8333-
-
8334-
message: "#^Parameter \\#1 \\$string of function mb_strlen expects string, mixed given\\.$#"
8335-
count: 1
8336-
path: src/Html/Generator.php
8337-
8338-
-
8339-
message: "#^Parameter \\#1 \\$string of function mb_substr expects string, mixed given\\.$#"
8340-
count: 2
8341-
path: src/Html/Generator.php
8342-
83438323
-
83448324
message: "#^Parameter \\#1 \\$string of function trim expects string, mixed given\\.$#"
83458325
count: 1
83468326
path: src/Html/Generator.php
83478327

8348-
-
8349-
message: "#^Parameter \\#2 \\$subject of function preg_match expects string, mixed given\\.$#"
8350-
count: 3
8351-
path: src/Html/Generator.php
8352-
83538328
-
83548329
message: "#^Parameter \\#2 \\.\\.\\.\\$values of function sprintf expects bool\\|float\\|int\\|string\\|null, mixed given\\.$#"
83558330
count: 1
@@ -8540,11 +8515,6 @@ parameters:
85408515
count: 1
85418516
path: src/Import/Import.php
85428517

8543-
-
8544-
message: "#^Only booleans are allowed in an if condition, string\\|false given\\.$#"
8545-
count: 1
8546-
path: src/Import/Import.php
8547-
85488518
-
85498519
message: "#^Parameter \\#1 \\$precision of static method PhpMyAdmin\\\\Import\\\\DecimalSize\\:\\:fromPrecisionAndScale\\(\\) expects int, int\\|PhpMyAdmin\\\\Import\\\\DecimalSize given\\.$#"
85508520
count: 1

psalm-baseline.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6598,6 +6598,10 @@
65986598
<PossiblyUndefinedArrayOffset>
65996599
<code><![CDATA[$parts[1]]]></code>
66006600
</PossiblyUndefinedArrayOffset>
6601+
<RedundantCast>
6602+
<code><![CDATA[(string) $GLOBALS['display_query']]]></code>
6603+
<code><![CDATA[(string) $GLOBALS['sql_query']]]></code>
6604+
</RedundantCast>
66016605
<RiskyCast>
66026606
<code><![CDATA[$_REQUEST['pos']]]></code>
66036607
<code><![CDATA[$_REQUEST['session_max_rows']]]></code>
@@ -6608,7 +6612,6 @@
66086612
<code><![CDATA[empty($GLOBALS['show_as_php'])]]></code>
66096613
<code><![CDATA[empty($GLOBALS['show_as_php'])]]></code>
66106614
<code><![CDATA[empty($GLOBALS['show_as_php'])]]></code>
6611-
<code><![CDATA[empty($GLOBALS['unparsed_sql'])]]></code>
66126615
<code><![CDATA[empty($server['ssl_ca'])]]></code>
66136616
</RiskyTruthyFalsyComparison>
66146617
</file>
@@ -6727,7 +6730,6 @@
67276730
<code><![CDATA[$GLOBALS['result'] == false]]></code>
67286731
</RedundantCondition>
67296732
<RiskyTruthyFalsyComparison>
6730-
<code><![CDATA[$error]]></code>
67316733
<code><![CDATA[ImportSettings::$runQuery]]></code>
67326734
</RiskyTruthyFalsyComparison>
67336735
</file>

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@
111111
tbl_storage_engine: string,
112112
token_mismatch: bool,
113113
token_provided: bool,
114-
unparsed_sql?: string,
115114
urlParams: array,
116115
username: string,
117116
xml_export_triggers: bool,

src/Controllers/Import/ImportController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,13 @@ public function __invoke(ServerRequest $request): Response|null
679679
return null;
680680
}
681681

682+
if ($request->hasBodyParam('rollback_query')) {
683+
// We rollback because there might be other queries that need to be executed after this,
684+
// e.g. creation of a bookmark.
685+
$this->dbi->query('ROLLBACK');
686+
ImportSettings::$message .= __('[ROLLBACK occurred.]');
687+
}
688+
682689
if ($GLOBALS['result']) {
683690
// Save a Bookmark with more than one queries (if Bookmark label given).
684691
if (! empty($request->getParsedBodyParam('bkm_label')) && ! empty($GLOBALS['import_text'])) {
@@ -708,13 +715,6 @@ public function __invoke(ServerRequest $request): Response|null
708715
include ROOT_PATH . $GLOBALS['goto'];
709716
}
710717

711-
// If there is request for ROLLBACK in the end.
712-
if (! $request->hasBodyParam('rollback_query')) {
713-
return null;
714-
}
715-
716-
$this->dbi->query('ROLLBACK');
717-
718718
return null;
719719
}
720720
}

src/Html/Generator.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,16 @@ public static function getMessage(
407407

408408
if ($sqlQuery === null) {
409409
if (! empty($GLOBALS['display_query'])) {
410-
$sqlQuery = $GLOBALS['display_query'];
411-
} elseif (! empty($GLOBALS['unparsed_sql'])) {
412-
$sqlQuery = $GLOBALS['unparsed_sql'];
410+
$sqlQuery = (string) $GLOBALS['display_query'];
413411
} elseif (! empty($GLOBALS['sql_query'])) {
414-
$sqlQuery = $GLOBALS['sql_query'];
412+
$sqlQuery = (string) $GLOBALS['sql_query'];
415413
} else {
416414
$sqlQuery = '';
417415
}
418416
}
419417

420418
$config = Config::getInstance();
421-
$renderSql = $config->settings['ShowSQL'] == true && ! empty($sqlQuery) && $sqlQuery !== ';';
419+
$renderSql = $config->settings['ShowSQL'] == true && $sqlQuery !== '' && $sqlQuery !== ';';
422420

423421
if (isset($GLOBALS['using_bookmark_message'])) {
424422
$retval .= $GLOBALS['using_bookmark_message']->getDisplay();
@@ -455,34 +453,19 @@ public static function getMessage(
455453
$retval .= ' ' . $message->getMessage() . "\n";
456454
$retval .= '</div>' . "\n";
457455

458-
$queryTooBig = false;
459-
460-
$queryLength = mb_strlen($sqlQuery);
461-
if ($queryLength > $config->settings['MaxCharactersInDisplayedSQL']) {
462-
// when the query is large (for example an INSERT of binary
463-
// data), the parser chokes; so avoid parsing the query
464-
$queryTooBig = true;
465-
$queryBase = mb_substr($sqlQuery, 0, $config->settings['MaxCharactersInDisplayedSQL']) . '[...]';
466-
} else {
467-
$queryBase = $sqlQuery;
468-
}
469-
470456
// Html format the query to be displayed
471457
// If we want to show some sql code it is easiest to create it here
472458
/* SQL-Parser-Analyzer */
473459

474460
if (! empty($GLOBALS['show_as_php'])) {
475461
$newLine = '\\n"<br>' . "\n" . '&nbsp;&nbsp;&nbsp;&nbsp;. "';
476-
$queryBase = htmlspecialchars(addslashes($queryBase));
462+
$queryBase = htmlspecialchars(addslashes($sqlQuery));
477463
$queryBase = preg_replace('/((\015\012)|(\015)|(\012))/', $newLine, $queryBase);
478464
$queryBase = '<code class="php" dir="ltr"><pre>' . "\n"
479465
. '$sql = "' . $queryBase . '";' . "\n"
480466
. '</pre></code>';
481-
} elseif ($queryTooBig) {
482-
$queryBase = '<code class="sql" dir="ltr"><pre>' . "\n"
483-
. htmlspecialchars($queryBase, ENT_COMPAT) . '</pre></code>';
484467
} else {
485-
$queryBase = self::formatSql($queryBase);
468+
$queryBase = self::formatSql($sqlQuery, true);
486469
}
487470

488471
// Prepares links that may be displayed to edit/explain the query
@@ -507,6 +490,7 @@ public static function getMessage(
507490
// but only explain a SELECT (that has not been explained)
508491
/* SQL-Parser-Analyzer */
509492
$explainLink = '';
493+
$queryTooBig = mb_strlen($sqlQuery) > $config->settings['MaxCharactersInDisplayedSQL'];
510494
$isSelect = preg_match('@^SELECT[[:space:]]+@i', $sqlQuery);
511495
if (! empty($config->settings['SQLQuery']['Explain']) && ! $queryTooBig) {
512496
$explainParams = $urlParams;
@@ -1085,8 +1069,6 @@ public static function getListNavigator(
10851069
* @param bool $truncate truncate the query if it is too long
10861070
*
10871071
* @return string the formatted sql
1088-
*
1089-
* @global array $cfg the configuration array
10901072
*/
10911073
public static function formatSql(string $sqlQuery, bool $truncate = false): string
10921074
{

src/Import/Import.php

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,6 @@ public function runQuery(string $sql, array &$sqlData): void
240240

241241
// Do we have something to push into buffer?
242242
$this->importRunBuffer = $sql !== '' ? $sql . ';' : null;
243-
244-
// In case of ROLLBACK, notify the user.
245-
if (! isset($_POST['rollback_query'])) {
246-
return;
247-
}
248-
249-
ImportSettings::$message .= __('[ROLLBACK occurred.]');
250243
}
251244

252245
/**
@@ -928,20 +921,10 @@ public function buildSql(
928921
ImportSettings::$importNotice = $message;
929922
}
930923

931-
/**
932-
* Handles request for ROLLBACK.
933-
*
934-
* @param string $sqlQuery SQL query(s)
935-
*/
936924
public function handleRollbackRequest(string $sqlQuery): void
937925
{
938926
$sqlDelimiter = $_POST['sql_delimiter'];
939927
$queries = explode($sqlDelimiter, $sqlQuery);
940-
$error = false;
941-
$errorMsg = __(
942-
'Only INSERT, UPDATE, DELETE and REPLACE '
943-
. 'SQL queries containing transactional engine tables can be rolled back.',
944-
);
945928
$dbi = DatabaseInterface::getInstance();
946929
foreach ($queries as $sqlQuery) {
947930
if ($sqlQuery === '') {
@@ -953,17 +936,14 @@ public function handleRollbackRequest(string $sqlQuery): void
953936
continue;
954937
}
955938

956-
$globalError = $dbi->getError();
957-
$error = $globalError !== '' ? $globalError : $errorMsg;
958-
959-
break;
960-
}
939+
$sqlError = $dbi->getError();
940+
$error = $sqlError !== '' ? $sqlError : __(
941+
'Only INSERT, UPDATE, DELETE and REPLACE '
942+
. 'SQL queries containing transactional engine tables can be rolled back.',
943+
);
961944

962-
if ($error) {
963-
unset($_POST['rollback_query']);
964945
$response = ResponseRenderer::getInstance();
965-
$message = Message::rawError($error);
966-
$response->addJSON('message', $message);
946+
$response->addJSON('message', Message::rawError($error));
967947
$response->callExit();
968948
}
969949

src/ParseAnalyze.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class ParseAnalyze
2626
*/
2727
public static function sqlQuery(string $sqlQuery, string $db): array
2828
{
29-
// @todo: move to returned results (also in all the calling chain)
30-
$GLOBALS['unparsed_sql'] = $sqlQuery;
31-
3229
$info = Query::getAll($sqlQuery);
3330

3431
$table = '';

tests/unit/Html/GeneratorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ public function testGetMessage(): void
476476
{
477477
Config::getInstance()->settings['ShowSQL'] = true;
478478
$GLOBALS['display_query'] = null;
479-
$GLOBALS['unparsed_sql'] = null;
480479
$GLOBALS['sql_query'] = 'SELECT 1;';
481480
$usingBookmarkMessage = Message::notice('Bookmark message');
482481
$GLOBALS['using_bookmark_message'] = $usingBookmarkMessage;
@@ -533,7 +532,6 @@ public function testGetMessage2(): void
533532
$config->settings['SQLQuery']['Edit'] = false;
534533
$config->settings['SQLQuery']['Refresh'] = true;
535534
$GLOBALS['display_query'] = 'EXPLAIN SELECT 1;';
536-
$GLOBALS['unparsed_sql'] = null;
537535
$GLOBALS['sql_query'] = null;
538536
DatabaseInterface::$instance = $this->createDatabaseInterface();
539537
Current::$database = 'test_db';

tests/unit/ParseAnalyzeTest.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ public function testSqlQuery(): void
2424
$GLOBALS['lang'] = 'en';
2525
ResponseRenderer::getInstance()->setAjax(false);
2626

27-
$GLOBALS['unparsed_sql'] = '';
28-
2927
$actual = ParseAnalyze::sqlQuery('SELECT * FROM `sakila`.`actor`', 'sakila_test');
3028

31-
/** @psalm-suppress TypeDoesNotContainType */
32-
self::assertSame('SELECT * FROM `sakila`.`actor`', $GLOBALS['unparsed_sql']);
3329
self::assertSame('sakila', $actual[1]);
3430
self::assertSame('actor', $actual[2]);
3531
self::assertTrue($actual[0]->flags->reload);
@@ -44,12 +40,8 @@ public function testSqlQuery2(): void
4440
$GLOBALS['lang'] = 'en';
4541
ResponseRenderer::getInstance()->setAjax(false);
4642

47-
$GLOBALS['unparsed_sql'] = '';
48-
4943
$actual = ParseAnalyze::sqlQuery('SELECT `first_name`, `title` FROM `actor`, `film`', 'sakila');
5044

51-
/** @psalm-suppress TypeDoesNotContainType */
52-
self::assertSame('SELECT `first_name`, `title` FROM `actor`, `film`', $GLOBALS['unparsed_sql']);
5345
self::assertSame('sakila', $actual[1]);
5446
self::assertSame('', $actual[2]);
5547
self::assertFalse($actual[0]->flags->reload);

0 commit comments

Comments
 (0)