From dd88475656bc1b89dda95aeec0cece37bb31bfe0 Mon Sep 17 00:00:00 2001 From: amarjitsingh52922 <47297185+amarjitsingh52922@users.noreply.github.com> Date: Tue, 26 Mar 2019 01:19:16 +0530 Subject: [PATCH] Fix for #13424 Importing a CSV created tables named Table ##" (#14935) Fix #13424 Importing a CSV created tables named "Table ##" Signed-off-by: Amarjit Singh --- import.php | 1 + .../classes/Plugins/Import/ImportCsv.php | 33 +++++++++++++++++-- test/classes/Plugins/Import/ImportCsvTest.php | 9 +++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/import.php b/import.php index 6adea5c18d..95122ee1a9 100644 --- a/import.php +++ b/import.php @@ -461,6 +461,7 @@ $read_limit = $memory_limit / 8; // handle filenames if (isset($_FILES['import_file'])) { $import_file = $_FILES['import_file']['tmp_name']; + $import_file_name=$_FILES['import_file']['name']; } if (! empty($local_import_file) && ! empty($cfg['UploadDir'])) { // sanitize $local_import_file as it comes from a POST diff --git a/libraries/classes/Plugins/Import/ImportCsv.php b/libraries/classes/Plugins/Import/ImportCsv.php index 6410edb9c4..32bff3ce59 100644 --- a/libraries/classes/Plugins/Import/ImportCsv.php +++ b/libraries/classes/Plugins/Import/ImportCsv.php @@ -137,11 +137,20 @@ class ImportCsv extends AbstractImportCsv public function doImport(array &$sql_data = []) { global $db, $table, $csv_terminated, $csv_enclosed, $csv_escaped, - $csv_new_line, $csv_columns, $err_url; + $csv_new_line, $csv_columns, $err_url,$import_file_name; // $csv_replace and $csv_ignore should have been here, // but we use directly from $_POST global $error, $timeout_passed, $finished, $message; + //get basename of file without extension, + //lower case it,strip unwanted characters ,trimming to 10characters commented now + $import_file_name=basename($import_file_name, ".csv"); + $import_file_name=mb_strtolower($import_file_name); + $import_file_name = preg_replace("/[^a-zA-Z0-9_]/", "_", $import_file_name); + /*if (mb_strlen($import_file_name)>10) { + $import_file_name=substr($import_file_name, 0, 10); + }*/ + $replacements = [ '\\n' => "\n", '\\t' => "\t", @@ -679,9 +688,27 @@ class ImportCsv extends AbstractImportCsv $tbl_name = $_REQUEST['csv_new_tbl_name']; } elseif (mb_strlen((string) $db)) { $result = $GLOBALS['dbi']->fetchResult('SHOW TABLES'); - $tbl_name = 'TABLE ' . (count($result) + 1); + + //logic to get table name from filename + // if no table then use filename as tablename + if (count($result)==0) { + $tbl_name=$import_file_name; + } else { + // check to see if {filename} as table exist + $name_array=preg_grep("/{$import_file_name}/isU", $result); + // if no use filename as tablename + if (count($name_array)==0) { + $tbl_name=$import_file_name; + } else { + // check if {filename}_ as table exist + $name_array=preg_grep("/{$import_file_name}_/isU", $result); + $tbl_name=$import_file_name . "_" . (count($name_array)+1); + } + } + //$tbl_name = 'TABLE ' . (count($result) + 1); } else { - $tbl_name = 'TBL_NAME'; + //$tbl_name = 'TBL_NAME'; + $tbl_name=$import_file_name; } $tables[] = [ diff --git a/test/classes/Plugins/Import/ImportCsvTest.php b/test/classes/Plugins/Import/ImportCsvTest.php index d100a0a0ab..15a4fe5114 100644 --- a/test/classes/Plugins/Import/ImportCsvTest.php +++ b/test/classes/Plugins/Import/ImportCsvTest.php @@ -60,6 +60,7 @@ class ImportCsvTest extends PmaTestCase $GLOBALS['csv_enclosed'] = '"'; $GLOBALS['csv_escaped'] = '"'; $GLOBALS['csv_new_line'] = 'auto'; + $GLOBALS['import_file_name']=basename($GLOBALS['import_file'], ".csv"); //$_SESSION @@ -124,7 +125,7 @@ class ImportCsvTest extends PmaTestCase $sql_query ); $this->assertStringContainsString( - 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`TBL_NAME`', + 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`' . $GLOBALS['import_file_name'] . '`', $sql_query ); @@ -215,9 +216,13 @@ class ImportCsvTest extends PmaTestCase $sql_query ); - $this->assertStringContainsString( + /*$this->assertStringContainsString( 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`TBL_NAME`', $sql_query + );*/ + $this->assertStringContainsString( + 'CREATE TABLE IF NOT EXISTS `CSV_DB 1`.`' . $GLOBALS['import_file_name'] . '`', + $sql_query ); $this->assertEquals(