Skip to content

Commit 3acf78d

Browse files
Merge pull request #18994 from kamil-tekiela/Refactor-import-2
Refactor name generation of file imports
2 parents 58752f9 + 7ebe995 commit 3acf78d

8 files changed

Lines changed: 93 additions & 204 deletions

File tree

phpstan-baseline.neon

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11512,7 +11512,7 @@ parameters:
1151211512

1151311513
-
1151411514
message: "#^Cannot cast mixed to string\\.$#"
11515-
count: 2
11515+
count: 1
1151611516
path: src/Plugins/Import/ImportOds.php
1151711517

1151811518
-
@@ -11600,46 +11600,16 @@ parameters:
1160011600
count: 1
1160111601
path: src/Plugins/Import/ImportSql.php
1160211602

11603-
-
11604-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
11605-
count: 1
11606-
path: src/Plugins/Import/ImportXml.php
11607-
11608-
-
11609-
message: "#^Cannot access property \\$database on mixed\\.$#"
11610-
count: 1
11611-
path: src/Plugins/Import/ImportXml.php
11612-
1161311603
-
1161411604
message: "#^Cannot cast mixed to string\\.$#"
11615-
count: 10
11605+
count: 6
1161611606
path: src/Plugins/Import/ImportXml.php
1161711607

1161811608
-
1161911609
message: "#^Method PhpMyAdmin\\\\Plugins\\\\Import\\\\ImportXml\\:\\:doImport\\(\\) should return array\\<string\\> but returns mixed\\.$#"
1162011610
count: 1
1162111611
path: src/Plugins/Import/ImportXml.php
1162211612

11623-
-
11624-
message: "#^Only booleans are allowed in &&, \\(SimpleXMLElement\\|null\\) given on the left side\\.$#"
11625-
count: 1
11626-
path: src/Plugins/Import/ImportXml.php
11627-
11628-
-
11629-
message: "#^Only booleans are allowed in &&, int given on the right side\\.$#"
11630-
count: 1
11631-
path: src/Plugins/Import/ImportXml.php
11632-
11633-
-
11634-
message: "#^Variable property access on \\(SimpleXMLElement\\|null\\)\\.$#"
11635-
count: 1
11636-
path: src/Plugins/Import/ImportXml.php
11637-
11638-
-
11639-
message: "#^Variable property access on mixed\\.$#"
11640-
count: 1
11641-
path: src/Plugins/Import/ImportXml.php
11642-
1164311613
-
1164411614
message: "#^Method PhpMyAdmin\\\\Plugins\\\\Import\\\\ShapeFileImport\\:\\:readSHP\\(\\) never returns false so it can be removed from the return type\\.$#"
1164511615
count: 1

psalm-baseline.xml

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6391,8 +6391,10 @@
63916391
<code>DatabaseInterface::getInstance()</code>
63926392
<code>DatabaseInterface::getInstance()</code>
63936393
<code>DatabaseInterface::getInstance()</code>
6394+
<code>DatabaseInterface::getInstance()</code>
63946395
</DeprecatedMethod>
63956396
<DocblockTypeContradiction>
6397+
<code>$nameArray === false</code>
63966398
<code><![CDATA[empty($parser->statements[0])]]></code>
63976399
</DocblockTypeContradiction>
63986400
<MixedArgument>
@@ -8658,12 +8660,7 @@
86588660
<DeprecatedMethod>
86598661
<code>DatabaseInterface::getInstance()</code>
86608662
<code>DatabaseInterface::getInstance()</code>
8661-
<code>DatabaseInterface::getInstance()</code>
86628663
</DeprecatedMethod>
8663-
<DocblockTypeContradiction>
8664-
<code>$nameArray === false</code>
8665-
<code>$nameArray === false</code>
8666-
</DocblockTypeContradiction>
86678664
<InvalidArgument>
86688665
<code><![CDATA[$GLOBALS['csv_columns']]]></code>
86698666
<code>$colNames</code>
@@ -8683,10 +8680,6 @@
86838680
<MixedArgument>
86848681
<code><![CDATA[$GLOBALS['csv_new_line']]]></code>
86858682
</MixedArgument>
8686-
<MixedArgumentTypeCoercion>
8687-
<code>$result</code>
8688-
<code>$result</code>
8689-
</MixedArgumentTypeCoercion>
86908683
<MixedAssignment>
86918684
<code><![CDATA[$GLOBALS['csv_new_line']]]></code>
86928685
<code><![CDATA[$GLOBALS['errorUrl']]]></code>
@@ -8767,9 +8760,6 @@
87678760
</PossiblyUnusedReturnValue>
87688761
</file>
87698762
<file src="src/Plugins/Import/ImportMediawiki.php">
8770-
<DeprecatedMethod>
8771-
<code>DatabaseInterface::getInstance()</code>
8772-
</DeprecatedMethod>
87738763
<InvalidNullableReturnType>
87748764
<code>string</code>
87758765
</InvalidNullableReturnType>
@@ -8825,7 +8815,6 @@
88258815
</PossiblyNullArgument>
88268816
<PossiblyNullArrayAccess>
88278817
<code><![CDATA[$tblAttr['name']]]></code>
8828-
<code><![CDATA[$tblAttr['name']]]></code>
88298818
</PossiblyNullArrayAccess>
88308819
<PossiblyNullPropertyFetch>
88318820
<code><![CDATA[$xml->children('office', true)->body]]></code>
@@ -8842,7 +8831,6 @@
88428831
<file src="src/Plugins/Import/ImportShp.php">
88438832
<DeprecatedMethod>
88448833
<code>Config::getInstance()</code>
8845-
<code>DatabaseInterface::getInstance()</code>
88468834
</DeprecatedMethod>
88478835
<InvalidArrayOffset>
88488836
<code><![CDATA[$GLOBALS['buffer']]]></code>
@@ -8914,42 +8902,30 @@
89148902
<file src="src/Plugins/Import/ImportXml.php">
89158903
<MixedArrayAccess>
89168904
<code><![CDATA[$attrs['name']]]></code>
8917-
<code><![CDATA[$rowAttr['name']]]></code>
8918-
<code><![CDATA[$rowAttr['name']]]></code>
8919-
<code><![CDATA[$tblAttr['name']]]></code>
8920-
<code><![CDATA[$tblAttr['name']]]></code>
8921-
<code><![CDATA[$tblAttr['name']]]></code>
89228905
</MixedArrayAccess>
89238906
<MixedAssignment>
89248907
<code>$attrs</code>
8925-
<code>$rowAttr</code>
8926-
<code>$tblAttr</code>
8927-
<code>$v2</code>
89288908
<code>$val2</code>
89298909
<code>$val3</code>
89308910
</MixedAssignment>
89318911
<MixedMethodCall>
89328912
<code>attributes</code>
8933-
<code>attributes</code>
8934-
<code>attributes</code>
89358913
</MixedMethodCall>
89368914
<PossiblyNullArrayAccess>
89378915
<code><![CDATA[$dbAttr['charset']]]></code>
89388916
<code><![CDATA[$dbAttr['collation']]]></code>
89398917
<code><![CDATA[$dbAttr['name']]]></code>
89408918
<code><![CDATA[$dbAttr['name']]]></code>
89418919
</PossiblyNullArrayAccess>
8920+
<PossiblyNullPropertyFetch>
8921+
<code><![CDATA[$xml->children($namespaces['pma'] ?? null)->structure_schemas]]></code>
8922+
</PossiblyNullPropertyFetch>
89428923
<PossiblyNullReference>
89438924
<code>attributes</code>
8944-
<code>children</code>
8945-
<code>count</code>
89468925
</PossiblyNullReference>
89478926
<PossiblyUnusedReturnValue>
89488927
<code>string[]</code>
89498928
</PossiblyUnusedReturnValue>
8950-
<RiskyTruthyFalsyComparison>
8951-
<code>$xml</code>
8952-
</RiskyTruthyFalsyComparison>
89538929
</file>
89548930
<file src="src/Plugins/Import/ShapeFileImport.php">
89558931
<InvalidArrayOffset>

src/Import/Import.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use function function_exists;
3232
use function htmlspecialchars;
3333
use function implode;
34+
use function in_array;
3435
use function is_numeric;
3536
use function max;
3637
use function mb_chr;
@@ -41,8 +42,11 @@
4142
use function mb_strtoupper;
4243
use function mb_substr;
4344
use function mb_substr_count;
45+
use function preg_grep;
4446
use function preg_match;
47+
use function preg_quote;
4548
use function preg_replace;
49+
use function rtrim;
4650
use function sprintf;
4751
use function str_contains;
4852
use function str_starts_with;
@@ -1385,4 +1389,34 @@ public static function getLocalFiles(array $importList): false|string
13851389
$active,
13861390
);
13871391
}
1392+
1393+
public function getNextAvailableTableName(string $databaseName, string $proposedTableName): string
1394+
{
1395+
if ($proposedTableName === '') {
1396+
$proposedTableName = 'TABLE';
1397+
}
1398+
1399+
$importFileName = rtrim($proposedTableName);
1400+
$importFileName = (string) preg_replace('/[^\x{0001}-\x{FFFF}]/u', '_', $importFileName);
1401+
1402+
if ($databaseName !== '') {
1403+
$existingTables = DatabaseInterface::getInstance()->getTables($databaseName);
1404+
1405+
// check to see if {filename} as table exist
1406+
// if no use filename as table name
1407+
if (! in_array($importFileName, $existingTables, true)) {
1408+
return $importFileName;
1409+
}
1410+
1411+
// check if {filename}_ as table exist
1412+
$nameArray = preg_grep('/^' . preg_quote($importFileName, '/') . '_/isU', $existingTables);
1413+
if ($nameArray === false) {
1414+
return $importFileName;
1415+
}
1416+
1417+
return $importFileName . '_' . (count($nameArray) + 1);
1418+
}
1419+
1420+
return $importFileName;
1421+
}
13881422
}

src/Plugins/Import/ImportCsv.php

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@
2626
use function __;
2727
use function array_pad;
2828
use function array_shift;
29-
use function basename;
3029
use function count;
3130
use function mb_strlen;
32-
use function mb_strtolower;
3331
use function mb_substr;
34-
use function preg_grep;
35-
use function preg_replace;
32+
use function pathinfo;
3633
use function preg_split;
3734
use function rtrim;
3835
use function str_contains;
3936
use function strlen;
4037
use function strtr;
4138
use function trim;
4239

40+
use const PATHINFO_FILENAME;
41+
4342
/**
4443
* Handles the import for the CSV format
4544
*/
@@ -689,41 +688,15 @@ private function buildErrorsForParams(
689688

690689
private function getTableNameFromImport(string $databaseName): string
691690
{
692-
$importFileName = basename(ImportSettings::$importFileName, '.csv');
693-
$importFileName = mb_strtolower($importFileName);
694-
$importFileName = (string) preg_replace('/[^a-zA-Z0-9_]/', '_', $importFileName);
695-
696691
// get new table name, if user didn't provide one, set the default name
697692
if (isset($_REQUEST['csv_new_tbl_name']) && (string) $_REQUEST['csv_new_tbl_name'] !== '') {
698693
return $_REQUEST['csv_new_tbl_name'];
699694
}
700695

701-
if ($databaseName !== '') {
702-
$result = DatabaseInterface::getInstance()->fetchResult('SHOW TABLES');
703-
704-
// logic to get table name from filename
705-
// if no table then use filename as table name
706-
if ($result === []) {
707-
return $importFileName;
708-
}
709-
710-
// check to see if {filename} as table exist
711-
$nameArray = preg_grep('/' . $importFileName . '/isU', $result);
712-
// if no use filename as table name
713-
if ($nameArray === false || $nameArray === []) {
714-
return $importFileName;
715-
}
716-
717-
// check if {filename}_ as table exist
718-
$nameArray = preg_grep('/' . $importFileName . '_/isU', $result);
719-
if ($nameArray === false) {
720-
return $importFileName;
721-
}
722-
723-
return $importFileName . '_' . (count($nameArray) + 1);
724-
}
725-
726-
return $importFileName;
696+
return $this->import->getNextAvailableTableName(
697+
$databaseName,
698+
pathinfo(ImportSettings::$importFileName, PATHINFO_FILENAME),
699+
);
727700
}
728701

729702
/**

src/Plugins/Import/ImportMediawiki.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace PhpMyAdmin\Plugins\Import;
99

1010
use PhpMyAdmin\Current;
11-
use PhpMyAdmin\DatabaseInterface;
1211
use PhpMyAdmin\File;
1312
use PhpMyAdmin\Import\ImportSettings;
1413
use PhpMyAdmin\Import\ImportTable;
@@ -21,13 +20,16 @@
2120
use function explode;
2221
use function mb_strlen;
2322
use function mb_substr;
23+
use function pathinfo;
2424
use function preg_match;
2525
use function str_contains;
2626
use function str_replace;
2727
use function str_starts_with;
2828
use function strlen;
2929
use function trim;
3030

31+
use const PATHINFO_FILENAME;
32+
3133
/**
3234
* Handles the import for the MediaWiki format
3335
*/
@@ -288,7 +290,12 @@ private function importDataOneTable(ImportTable $table, array &$sqlStatements):
288290
{
289291
if ($this->analyze) {
290292
// Set the table name
291-
$this->setTableName($table->tableName);
293+
if ($table->tableName === '') {
294+
$table->tableName = $this->import->getNextAvailableTableName(
295+
Current::$database,
296+
pathinfo(ImportSettings::$importFileName, PATHINFO_FILENAME),
297+
);
298+
}
292299

293300
// Set generic names for table headers if they don't exist
294301
if ($table->columns === []) {
@@ -311,22 +318,6 @@ private function importDataOneTable(ImportTable $table, array &$sqlStatements):
311318
$this->import->runQuery('', $sqlStatements);
312319
}
313320

314-
/**
315-
* Sets the table name
316-
*
317-
* @param string $tableName reference to the name of the table
318-
*/
319-
private function setTableName(string &$tableName): void
320-
{
321-
if ($tableName !== '') {
322-
return;
323-
}
324-
325-
$result = DatabaseInterface::getInstance()->fetchResult('SHOW TABLES');
326-
// todo check if the name below already exists
327-
$tableName = 'TABLE ' . (count($result) + 1);
328-
}
329-
330321
/**
331322
* Set generic names for table headers, if they don't exist
332323
*

0 commit comments

Comments
 (0)