public function loadTableInsert($Tablename, $Path, $skipHeader = true, $chunk = 100)
{
$result = false;
// Get the column count of the table.
$St = Gdn::structure();
$St->get(self::TABLE_PREFIX . $Tablename);
$ColumnCount = count($St->columns());
$St->reset();
ini_set('auto_detect_line_endings', true);
$fp = fopen($Path, 'rb');
// Figure out the current position.
$fPosition = val('CurrentLoadPosition', $this->Data, 0);
if ($fPosition == 0 && $skipHeader) {
// Skip the header row.
$Row = self::FGetCSV2($fp);
}
if ($fPosition == 0) {
$Px = Gdn::database()->DatabasePrefix . self::TABLE_PREFIX;
Gdn::database()->query("truncate table {$Px}{$Tablename}");
} else {
fseek($fp, $fPosition);
}
$PDO = Gdn::database()->connection();
$PxTablename = Gdn::database()->DatabasePrefix . self::TABLE_PREFIX . $Tablename;
$Inserts = '';
$Count = 0;
while ($Row = self::FGetCSV2($fp)) {
++$Count;
$result = true;
$Row = array_map('trim', $Row);
// Quote the values in the row.
$Row = array_map(array($PDO, 'quote'), $Row);
// Add any extra columns to the row.
while (count($Row) < $ColumnCount) {
$Row[] = 'null';
}
// Add the insert values to the sql.
if (strlen($Inserts) > 0) {
$Inserts .= ',';
}
$Inserts .= '(' . implode(',', $Row) . ')';
if ($Count >= $chunk) {
// Insert in chunks.
$Sql = "insert {$PxTablename} values {$Inserts}";
$this->Database->query($Sql);
$Count = 0;
$Inserts = '';
// Check for a timeout.
if ($this->Timer->elapsedTime() > $this->MaxStepTime) {
// The step's taken too long. Save the file position.
$Pos = ftell($fp);
$this->Data['CurrentLoadPosition'] = $Pos;
$Filesize = filesize($Path);
if ($Filesize > 0) {
$PercentComplete = $Pos / filesize($Path);
$this->Data['CurrentStepMessage'] = $Tablename . ' (' . round($PercentComplete * 100.0) . '%)';
}
fclose($fp);
return 0;
}
}
}
fclose($fp);
if (strlen($Inserts) > 0) {
$Sql = "insert {$PxTablename} values {$Inserts}";
$this->query($Sql);
}
return $result;
}