Skip to content
Snippets Groups Projects
Commit 119ca0d7 authored by Prasad's avatar Prasad
Browse files

Fixes #829: Removed dependency on LOAD DATA command while importing

parent 92ef784e
No related branches found
No related tags found
1 merge request!311Language italian translation
......@@ -136,12 +136,6 @@ class Vtiger_Import_View extends Vtiger_Index_View {
$fileFormat = strtolower($fileFormat);
}
// Pre-conditional check for CSV import.
if ($fileFormat == 'csv') {
$isLocalInfileEnabled = Vtiger_Util_Helper::checkDbLocalInfileSupport();
if (!$isLocalInfileEnabled) throw new Exception(vtranslate('ERR_LOCAL_INFILE_NOT_ON', 'Import'));
}
$viewer->assign('AVAILABLE_FIELDS', $moduleMeta->getMergableFields());
$viewer->assign('ENTITY_FIELDS', $moduleMeta->getEntityFields());
$viewer->assign('ERROR_MESSAGE', $request->get('error_message'));
......
No preview for this file type
......@@ -68,56 +68,39 @@ class Import_CSVReader_Reader extends Import_FileReader_Reader {
public function read() {
global $default_charset;
$filePath = $this->getFilePath();
// if file encoded type is other than over default database charset we need to convert
if($this->request->get('file_encoding') != $default_charset) {
$data = file_get_contents($filePath);
$result = mb_convert_encoding($data,$default_charset,$this->request->get('file_encoding'));
file_put_contents($filePath, $result);
}
// to add escape slashes
$filePath = addslashes($this->getFilePath());
$fileHandler = $this->getFileHandler();
$status = $this->createTable();
if(!$status) {
return false;
}
$fieldMapping = $this->request->get('field_mapping');
$fieldNames = array();
foreach($fieldMapping as $fieldName => $index) {
$fieldNames[$index] = $fieldName;
}
$this->addRecordsToDB($filePath,$fieldNames);
}
public function addRecordsToDB($filePath,$columnNames) {
$db = PearDatabase::getInstance();
$tableName = Import_Utils_Helper::getDbTableName($this->user);
$delimiter = $this->request->get('delimiter');
$encoding = $this->request->get('file_encoding');
$withEncoding = ' CHARACTER SET ' . ($encoding == 'UTF-8' ? 'UTF8' : 'latin1');
$query = 'LOAD DATA LOCAL INFILE "'.$filePath.'" INTO TABLE '.$tableName. $withEncoding.' FIELDS TERMINATED BY "'.$delimiter.'" OPTIONALLY ENCLOSED BY "\"" LINES TERMINATED BY "\n"';
if($this->hasHeader()){
$query .= " IGNORE 1 LINES ";
}
// to ignore values from file which are not mapped
$keys = array_keys($columnNames);
$maxValue = max($keys);
for($i=0;$i<$maxValue;$i++){
if(!$columnNames[$i]){
$columnNames[$i] = "@ignore";
$delimiter = $this->request->get('delimiter');
$hasHeader = $this->request->get('has_header');
$fileEncoding = $this->request->get('file_encoding');
// NOTE: Retaining row-read and insert as LOAD DATA command is being disabled by default.
$i = -1;
while($data = fgetcsv($fileHandler, 0, $delimiter)) {
$i++;
if($hasHeader && $i == 0) continue;
$mappedData = array();
$allValuesEmpty = true;
foreach($fieldMapping as $fieldName => $index) {
$fieldValue = $data[$index];
$mappedData[$fieldName] = $fieldValue;
if($fileEncoding != $default_charset) {
$mappedData[$fieldName] = $this->convertCharacterEncoding($fieldValue, $fileEncoding, $default_charset);
}
if(!empty($fieldValue)) $allValuesEmpty = false;
}
if($allValuesEmpty) continue;
$fieldNames = array_keys($mappedData);
$fieldValues = array_values($mappedData);
$this->addRecordToDB($fieldNames, $fieldValues);
}
ksort($columnNames);
$query .= '('.implode(',',$columnNames).')';
global $dbconfigoption;
$db->database = null; // we shouldn't use existing connection with client flag = 0
$dbconfigoption['clientFlags'] = 128; // To enable LOAD DATA INFILE... query for database
$db->pquery($query,array());
$this->setNumberOfRecordsRead($tableName,$db);
unset($fileHandler);
}
}
?>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment