r53821 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53820‎ | r53821 | r53822 >
Date:18:24, 27 July 2009
Author:yaron
Status:deferred
Tags:
Comment:
Added dropdown for file encoding; set UTF-8 encoding to happen only for UTF-16 files
Modified paths:
  • /trunk/extensions/DataTransfer/specials/DT_ImportCSV.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DataTransfer/specials/DT_ImportCSV.php
@@ -73,19 +73,25 @@
7474 if ($wgRequest->getCheck('import_file')) {
7575 $text = "<p>" . wfMsg('dt_import_importing') . "</p>\n";
7676 $source = ImportStreamSource::newFromUpload( "csv_file" );
 77+ $encoding = $wgRequest->getVal('encoding');
7778 $pages = array();
78 - $error_msg = self::getCSVData($source->mHandle, $pages);
 79+ $error_msg = self::getCSVData($source->mHandle, $encoding, $pages);
7980 if (! is_null($error_msg))
8081 $text .= $error_msg;
8182 else
8283 $text .= self::modifyPages($pages);
8384 } else {
8485 $select_file_label = wfMsg('dt_import_selectfile', 'CSV');
 86+ $encoding_type_label = wfMsg('dt_import_encodingtype');
8587 $import_button = wfMsg('import-interwiki-submit');
8688 $text =<<<END
8789 <p>$select_file_label</p>
8890 <form enctype="multipart/form-data" action="" method="post">
8991 <p><input type="file" name="csv_file" size="25" /></p>
 92+ <p>$encoding_type_label: <select name="encoding">
 93+ <option selected value="utf8">UTF-8</option>
 94+ <option value="utf16">UTF-16</option>
 95+ </select>
9096 <p><input type="Submit" name="import_file" value="$import_button"></p>
9197 </form>
9298
@@ -96,14 +102,18 @@
97103 }
98104
99105
100 - static function getCSVData($csv_file, &$pages) {
 106+ static function getCSVData($csv_file, $encoding, &$pages) {
 107+ if (is_null($csv_file))
 108+ return wfMsg('emptyfile');
101109 $table = array();
102110 while ($line = fgetcsv($csv_file)) {
103 - // fix values in case the file wasn't UTF-8 encoded -
 111+ // fix values if the file wasn't UTF-8 encoded -
104112 // hopefully the UTF-8 value will work across all
105113 // database encodings
106 - $encoded_line = array_map('utf8_encode', $line);
107 - array_push($table, $encoded_line);
 114+ if ($encoding == 'utf16') {
 115+ $line = array_map('utf8_encode', $line);
 116+ }
 117+ array_push($table, $line);
108118 }
109119 fclose($csv_file);
110120 // check header line to make sure every term is in the

Status & tagging log