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 <amarjitsingh52922@gmail.com>
This commit is contained in:
amarjitsingh52922 2019-03-26 01:19:16 +05:30 committed by Maurício Meneghini Fauth
parent 2829863ee7
commit dd88475656
3 changed files with 38 additions and 5 deletions

View File

@ -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

View File

@ -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[] = [

View File

@ -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(