Skip to content

Commit dbc6a60

Browse files
committed
Make character set support robust
Signed-off-by: st0rmsetter <317109199+st0rmsetter@users.noreply.github.com>
1 parent b596fb3 commit dbc6a60

2 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎src/Plugins/Import/ImportLdi.php‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class ImportLdi extends AbstractImportCsv
4343
'iso-8859-8' => 'hebrew',
4444
'iso-8859-9' => 'latin5',
4545
'iso-8859-13' => 'latin7',
46-
'utf-8' => 'utf8mb4',
4746
'koi8-r' => 'koi8r',
4847
'windows-1250' => 'cp1250',
4948
'windows-1251' => 'cp1251',
@@ -163,12 +162,20 @@ public function doImport(File|null $importHandle = null): array
163162

164163
$sql .= ' INTO TABLE ' . Util::backquote(Current::$table);
165164

165+
// UTF-8 charset imports don't get routed here since it is presumed ImportSettings::$charsetConversion is falsy
166166
if (ImportSettings::$charsetConversion) {
167167
$charset = $this->getDBCharset(ImportSettings::$charsetOfFile);
168168

169-
if ($charset !== null) {
170-
$sql .= ' CHARACTER SET ' . $charset;
169+
if ($charset === null) {
170+
Current::$message = Message::error(
171+
__('The selected character set is not supported by the database system.'),
172+
);
173+
Import::$hasError = true;
174+
175+
return [];
171176
}
177+
178+
$sql .= ' CHARACTER SET ' . $charset;
172179
}
173180

174181
if ($this->terminated !== '') {
@@ -248,6 +255,10 @@ private function setLdiLocalOptionConfig(): void
248255
$this->config->settings['Import']['ldi_local_option'] = true;
249256
}
250257

258+
/**
259+
* Returns the database charset name for a file charset, or null if
260+
* the charset is not supported by LOAD DATA.
261+
*/
251262
private function getDBCharset(string $charset): string|null
252263
{
253264
if ($charset === 'utf-16' && $this->dbi->isMariaDB()) {

‎tests/unit/Plugins/Import/ImportLdiTest.php‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ public static function charsetMappingProvider(): array
232232
'ISO-8859-8' => ['iso-8859-8', 'hebrew'],
233233
'ISO-8859-9' => ['iso-8859-9', 'latin5'],
234234
'ISO-8859-13' => ['iso-8859-13', 'latin7'],
235-
'UTF-8' => ['utf-8', 'utf8mb4'],
236235
'KOI8-R' => ['koi8-r', 'koi8r'],
237236
'Windows-1250' => ['windows-1250', 'cp1250'],
238237
'Windows-1251' => ['windows-1251', 'cp1251'],
@@ -241,8 +240,12 @@ public static function charsetMappingProvider(): array
241240
'Windows-1257' => ['windows-1257', 'cp1257'],
242241
'TIS-620' => ['tis-620', 'tis620'],
243242
'Shift_JIS' => ['SHIFT_JIS', 'sjis'],
243+
'SJIS' => ['SJIS', 'sjis'],
244+
'SJIS-win' => ['SJIS-win', 'cp932'],
244245
'Big5' => ['big5', 'big5'],
245246
'GB2312' => ['gb2312', 'gb2312'],
247+
'euc-jp' => ['euc-jp', 'ujis'],
248+
'ks_c_5601-1987' => ['ks_c_5601-1987', 'euckr'],
246249
];
247250
}
248251

@@ -275,15 +278,21 @@ public function testDoImportWithUtf16OnMySql(): void
275278
$dbi = $this->createMock(DatabaseInterface::class);
276279
$dbi->method('quoteString')
277280
->willReturnCallback(static fn (string $string): string => "'" . $string . "'");
278-
279281
$dbi->method('isMariaDB')->willReturn(false);
280282

281283
$importHandle = new File(ImportSettings::$importFile);
282284
$importHandle->open();
283285

284-
$this->getImportLdi($dbi)->doImport($importHandle);
286+
$importLdi = $this->getImportLdi($dbi);
287+
$result = $importLdi->doImport($importHandle);
285288

286-
self::assertStringNotContainsString('CHARACTER SET utf16', Current::$sqlQuery);
289+
self::assertSame([], $result);
290+
self::assertTrue(Import::$hasError);
291+
self::assertInstanceOf(Message::class, Current::$message);
292+
self::assertStringContainsString(
293+
__('The selected character set is not supported by the database system.'),
294+
Current::$message->__toString(),
295+
);
287296
}
288297

289298
public function testDoImportWithUnsupportedCharsetDoesNotAddCharacterSet(): void

0 commit comments

Comments
 (0)